public PedidoConsolidado2(string arquivo, string filtro_pedidos, bool por_item, bool por_pedido, bool por_cliente, bool por_fabrica, bool mostrar_valores, bool mostrar_subcodigo, string observacao_fabrica, List <string> pedidosDestacarIpi) { #region seleciona pedidos ArrayList pedidos = new ArrayList(); string sql = "select " + "p.COD_FORNECEDOR,p.DAT_ORCAMENTO,p.COD_ORCAMENTO,p.COD_PEDIDO,p.NRO_PEDIDO,p.OBSERVACAO,p.VLR_PEDIDO," + "o.COD_CLIENTE,o.COD_CARACTERISTICA,o.COD_VENDEDOR,o.VLR_DESCONTO,c.NOM_PARCEIRO, " + "f.DES_FORMULA,f.DES_FORMULA_PEDIDO, " + "(select 1 from PEDIDOS p2 where p2.COD_FORNECEDOR=p.COD_FORNECEDOR and p2.DAT_ORCAMENTO=p.DAT_ORCAMENTO and p2.COD_ORCAMENTO=p.COD_ORCAMENTO and p2.COD_PEDIDO=2), " + "p.VLR_INICIAL " + "from PEDIDOS p " + "inner join ORCAMENTOS o on o.COD_FORNECEDOR=p.COD_FORNECEDOR and o.DAT_ORCAMENTO=p.DAT_ORCAMENTO and o.COD_ORCAMENTO=p.COD_ORCAMENTO " + "inner join PARCEIROS c on c.COD_PARCEIRO=o.COD_CLIENTE " + "inner join CARACTERISTICAS f on f.COD_FORNECEDOR=o.COD_FORNECEDOR and f.COD_CARACTERISTICA=o.COD_CARACTERISTICA " + "where " + filtro_pedidos + " " + "order by p.COD_FORNECEDOR,p.DAT_ORCAMENTO,p.COD_ORCAMENTO,p.COD_PEDIDO"; //StreamWriter sw = new StreamWriter(new FileStream("c:\\softplace\\softplace.log", FileMode.Create)); //sw.WriteLine(sql); //sw.Close(); FbCommand cmd = new FbCommand(sql, Globais.bd); FbDataReader reader = cmd.ExecuteReader(CommandBehavior.Default); if (!por_fabrica) { por_cliente = true; } string ultimo_cliente = ""; while (reader.Read()) { DadosPedido dados = new DadosPedido(); dados.COD_FORNECEDOR = reader.GetString(0).Trim(); dados.DAT_ORCAMENTO = reader.GetDateTime(1); dados.COD_ORCAMENTO = reader.GetInt16(2); dados.COD_PEDIDO = reader.GetInt16(3); dados.NRO_PEDIDO = !reader.IsDBNull(4) ? reader.GetInt16(4) : (short)0; //MessageBox.Show("Pedido:" + dados.NRO_PEDIDO.ToString()); dados.OBSERVACAO = !reader.IsDBNull(5) ? reader.GetString(5).Trim() : ""; dados.VLR_PEDIDO = !reader.IsDBNull(6) ? reader.GetFloat(6) : 0f; dados.VLR_INICIAL = !reader.IsDBNull(15) ? reader.GetFloat(15) : 0f; dados.COD_CLIENTE = !reader.IsDBNull(7) ? reader.GetString(7).Trim() : ""; dados.COD_CARACTERISTICA = !reader.IsDBNull(8) ? reader.GetString(8).Trim() : ""; dados.COD_VENDEDOR = !reader.IsDBNull(9) ? reader.GetString(9).Trim() : ""; dados.VLR_DESCONTO = !reader.IsDBNull(10) ? reader.GetFloat(10) : 0f; dados.NOM_PARCEIRO = !reader.IsDBNull(11) ? reader.GetString(11).Trim() : ""; dados.DES_FORMULA = !reader.IsDBNull(12) ? reader.GetString(12).Trim() : ""; dados.DES_FORMULA_PEDIDO = !reader.IsDBNull(13) ? reader.GetString(13).Trim() : ""; if (dados.DES_FORMULA_PEDIDO.Length == 0) { dados.DES_FORMULA_PEDIDO = dados.DES_FORMULA; } if (!dados.COD_CLIENTE.Equals(ultimo_cliente)) { if (!ultimo_cliente.Equals("")) { por_cliente = false; } ultimo_cliente = dados.COD_CLIENTE; } if (dados.COD_PEDIDO == 1) { if (!reader.IsDBNull(14)) { dados.VLR_DESCONTO = 0; } dados.CarregaItens(); } else { DadosItem item = new DadosItem(); item.QTD_ITEM = 1; item.VLR_PRECO = dados.VLR_PEDIDO; item.COD_PRODUTO = "Serviço"; item.SUB_CODIGO = "-"; item.DES_PRODUTO = dados.OBSERVACAO; dados.itens = new ArrayList(); dados.itens.Add(item); } pedidos.Add(dados); } reader.Close(); if (pedidos.Count == 0) { return; } #endregion #region ordena os itens SortedList pedidos_ord = new SortedList(); string chave = ""; // # fabrica pedido item // 0 0 0 0 // 1 0 0 1 // 2 0 1 0 // 3 0 1 1 // 4 1 0 0 // 5 1 0 1 // 6 1 1 0 // 7 1 1 1 string zera_desconto_fornecedor = ""; DateTime zera_desconto_data = DateTime.Now; short zera_desconto_orcamento = 0; foreach (DadosPedido pedido in pedidos) { int i = 0; if (pedido.itens != null) { foreach (DadosItem item in pedido.itens) { i++; #region seta chave if (por_fabrica) // 4 5 6 7 { if (por_pedido) // 6 7 { if (por_item) // 7 { // fabrica + pedido + item chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.COD_PRODUTO + item.SUB_CODIGO + item.COD_ESPECIFICOS + item.SEQ_ITEM; } else // 6 { // fabrica + pedido chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } } else // 4 5 { if (por_item) // 5 { // fabrica + item chave = pedido.COD_FORNECEDOR + item.COD_PRODUTO + item.SUB_CODIGO + item.COD_ESPECIFICOS + pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } else // 4 { // fabrica = fabrica + pedido chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } } } else // 0 1 2 3 { if (por_pedido) // 2 3 { if (por_item) // 3 { // pedido + item = fabrica + pedido + item chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.COD_PRODUTO + item.SUB_CODIGO + item.COD_ESPECIFICOS + item.SEQ_ITEM; } else // 2 { // pedido = fabrica + pedido chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } } else // 0 1 { if (por_item) // 1 { // por item chave = item.COD_PRODUTO + item.SUB_CODIGO + item.COD_ESPECIFICOS + pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } else // 0 { // nada = pedido chave = pedido.COD_FORNECEDOR + pedido.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + pedido.COD_ORCAMENTO.ToString("00000") + pedido.COD_PEDIDO.ToString("0") + item.SEQ_ITEM; } } } #endregion item.COD_FORNECEDOR = pedido.COD_FORNECEDOR; item.DAT_ORCAMENTO = pedido.DAT_ORCAMENTO; item.COD_ORCAMENTO = pedido.COD_ORCAMENTO; item.COD_PEDIDO = pedido.COD_PEDIDO; item.NRO_PEDIDO = pedido.NRO_PEDIDO; item.OBSERVACAO = pedido.OBSERVACAO; item.VLR_PEDIDO = pedido.VLR_PEDIDO; item.COD_CLIENTE = pedido.COD_CLIENTE; item.COD_CARACTERISTICA = pedido.COD_CARACTERISTICA; item.COD_VENDEDOR = pedido.COD_VENDEDOR; // o desconto será feito no primeiro item do pedido // alterado em 19/06/11 // o consolidado estava ficando errado quando o valor do pedido era alterado if (pedido.COD_FORNECEDOR.Equals(zera_desconto_fornecedor) && (pedido.DAT_ORCAMENTO == zera_desconto_data) && (pedido.COD_ORCAMENTO == zera_desconto_orcamento)) { // o desconto já foi feito no outro pedido desse orçamento pedido.VLR_DESCONTO = 0; pedido.VLR_INICIAL = pedido.VLR_PEDIDO; // evita duplicação do desconto nos próximos itens } bool deuDesconto = false; if (pedido.VLR_PEDIDO < pedido.VLR_INICIAL) { item.VLR_DESCONTO = pedido.VLR_INICIAL - pedido.VLR_PEDIDO; } else if (pedido.VLR_PEDIDO > pedido.VLR_INICIAL) { item.VLR_DESCONTO = pedido.VLR_PEDIDO - pedido.VLR_INICIAL; } else { item.VLR_DESCONTO = pedido.VLR_DESCONTO; deuDesconto = true; } if ((item.VLR_DESCONTO > -0.2) && (item.VLR_DESCONTO < 0.2)) { item.VLR_DESCONTO = 0; deuDesconto = false; } pedido.VLR_DESCONTO = 0; pedido.VLR_INICIAL = pedido.VLR_PEDIDO; // evita duplicação do desconto nos próximos itens if (deuDesconto) { zera_desconto_fornecedor = pedido.COD_FORNECEDOR; zera_desconto_data = pedido.DAT_ORCAMENTO; zera_desconto_orcamento = pedido.COD_ORCAMENTO; } item.NOM_PARCEIRO = pedido.NOM_PARCEIRO; item.DES_FORMULA = pedido.DES_FORMULA; item.DES_FORMULA_PEDIDO = pedido.DES_FORMULA_PEDIDO; pedidos_ord.Add(chave, item); //MessageBox.Show(chave); } } } #endregion FileStream fs; try { fs = new FileStream(arquivo, FileMode.Create); doc = new Document(PageSize.LETTER.Rotate()); writer = PdfWriter.GetInstance(doc, fs); doc.Open(); CabecalhoRelatorio(); Tabela table; //= new Tabela(mostrar_valores ? 24 : 12); string ultimo_fornecedor = ""; string ultimo_pedido = ""; DadosItem item0 = (DadosItem)pedidos_ord.GetByIndex(0); CabecalhoCliente(item0.COD_FORNECEDOR, item0.DAT_ORCAMENTO, item0.COD_ORCAMENTO, !por_cliente); if (por_fabrica) { CabecalhoFornecedor(item0.COD_FORNECEDOR); } if (por_pedido) { CabecalhoPedido(item0.COD_FORNECEDOR, item0.DAT_ORCAMENTO, item0.COD_ORCAMENTO, item0.NRO_PEDIDO, item0.COD_VENDEDOR, item0.OBSERVACAO); } int itens_tabela = 0; //doc.Add(table); table = new Tabela(mostrar_valores ? 24 : 12); CabecalhoTabela(table, mostrar_valores); float total = 0; float desconto = 0; for (int i = 0; i < pedidos_ord.Count; i++) { DadosItem item = (DadosItem)pedidos_ord.GetByIndex(i); if (item.QTD_ITEM == 0) // item agrupado { continue; } string chave_fornecedor = item.COD_FORNECEDOR; string chave_pedido = item.DAT_ORCAMENTO.ToString("yyyy/MM/dd") + item.COD_ORCAMENTO.ToString("00000"); string chaveDestacar = item.COD_FORNECEDOR.Trim() + ":" + item.DAT_ORCAMENTO.ToString("M/d/yyyy") + ":" + item.COD_ORCAMENTO.ToString() + ":" + item.COD_PEDIDO.ToString(); bool destacar = false; foreach (string aux in pedidosDestacarIpi) { if (aux.Equals(chaveDestacar)) { destacar = true; break; } } if (!destacar) { if (item.PER_IPI > 0) { item.VLR_PRECO += (item.PER_IPI * item.VLR_PRECO / 100); } item.PER_IPI = 0; } #region agrupa por item if (por_item) { string chave_item = item.COD_PRODUTO + item.SUB_CODIGO + item.COD_ESPECIFICOS + item.VLR_PRECO.ToString(); for (int j = i + 1; j < pedidos_ord.Count; j++) { DadosItem proximo = (DadosItem)pedidos_ord.GetByIndex(j); string chave_proximo = proximo.COD_PRODUTO + proximo.SUB_CODIGO + proximo.COD_ESPECIFICOS + proximo.VLR_PRECO.ToString(); if (chave_item.Equals(chave_proximo)) { item.QTD_ITEM += proximo.QTD_ITEM; proximo.QTD_ITEM = 0; } } } #endregion bool quebrou_pedido = false; if (ultimo_fornecedor != chave_fornecedor) { if (por_fabrica) { if (itens_tabela > 0) { if (mostrar_valores) { Totais(table, total, desconto, por_fabrica); } total = 0; desconto = 0; doc.Add(table); RodapeFornecedor(observacao_fabrica); doc.NewPage(); table = new Tabela(mostrar_valores ? 24 : 12); CabecalhoRelatorio(); CabecalhoFornecedor(item.COD_FORNECEDOR); if (por_pedido) { if (ultimo_pedido != chave_pedido) { quebrou_pedido = true; CabecalhoPedido(item.COD_FORNECEDOR, item.DAT_ORCAMENTO, item.COD_ORCAMENTO, item.NRO_PEDIDO, item.COD_VENDEDOR, item.OBSERVACAO); ultimo_pedido = chave_pedido; } } itens_tabela = 0; CabecalhoTabela(table, mostrar_valores); } } ultimo_fornecedor = chave_fornecedor; } if (!quebrou_pedido && por_pedido && (ultimo_pedido != chave_pedido)) { if (itens_tabela > 0) { if (mostrar_valores) { Totais(table, total, desconto, por_fabrica); } total = 0; desconto = 0; doc.Add(table); table = new Tabela(mostrar_valores ? 24 : 12); CabecalhoPedido(item.COD_FORNECEDOR, item.DAT_ORCAMENTO, item.COD_ORCAMENTO, item.NRO_PEDIDO, item.COD_VENDEDOR, item.OBSERVACAO); itens_tabela = 0; CabecalhoTabela(table, mostrar_valores); } ultimo_pedido = chave_pedido; } desconto += item.VLR_DESCONTO; if (mostrar_subcodigo) { table.AddCell(CelulaGrid(item.COD_PRODUTO + "-" + item.SUB_CODIGO, mostrar_valores ? 4 : 2, false, false)); } else { table.AddCell(CelulaGrid(item.COD_PRODUTO, mostrar_valores ? 4 : 2, false, false)); } table.AddCell(CelulaGrid(item.DES_PRODUTO, mostrar_valores ? 6 : 5, false, false)); table.AddCell(CelulaGrid(item.COD_ESPECIFICOS, mostrar_valores ? 4 : 3, false, false)); table.AddCell(CelulaGrid(item.QTD_ITEM.ToString(), 2, false, false)); float per_frete = cCaracteristicas.Frete(item.COD_FORNECEDOR, item.COD_CARACTERISTICA); if (mostrar_valores) { float ipi = item.PER_IPI; float preco = item.VLR_PRECO; float semipi = item.VLR_PRECO; if (item.COD_PEDIDO == 1) { if (!item.IDT_ESPECIAL.Equals("S")) { Globais.CalculaFormula(ref preco, item.DES_FORMULA_PEDIDO, ipi, per_frete, 0); Globais.CalculaFormula(ref semipi, item.DES_FORMULA_PEDIDO, 0, per_frete, 0); } else { // desfaz formula Globais.DesfazFormula(ref preco, item.DES_FORMULA, ipi, per_frete, 0); Globais.DesfazFormula(ref semipi, item.DES_FORMULA, ipi, per_frete, 0); // refaz parte do pedido Globais.CalculaFormula(ref preco, item.DES_FORMULA_PEDIDO, ipi, per_frete, 0); Globais.CalculaFormula(ref semipi, item.DES_FORMULA_PEDIDO, 0, per_frete, 0); } } else { ipi = 0; } total += preco * item.QTD_ITEM; table.AddCell(CelulaGrid(semipi.ToString("###,###,##0.00"), 2, false, true)); table.AddCell(CelulaGrid((semipi * item.QTD_ITEM).ToString("###,###,##0.00"), 2, false, true)); table.AddCell(CelulaGrid(ipi.ToString("#0.00") + "%", 2, false, true)); table.AddCell(CelulaGrid((preco * item.QTD_ITEM).ToString("###,###,##0.00"), 2, false, true)); } itens_tabela++; } if (itens_tabela > 0) { if (mostrar_valores) { Totais(table, total, desconto, por_fabrica); } total = 0; desconto = 0; doc.Add(table); if (por_fabrica) { RodapeFornecedor(observacao_fabrica); } } doc.Close(); fs.Close(); } catch (Exception e) { Log.Grava(Globais.sUsuario, "erro:" + e.Message); MessageBox.Show("Erro na geração do relatório\r\n" + e.Message); return; } }
public float DiferencaFormulas(string fornecedor, string caracteristica, DateTime data, short orcamento, ref string servico) { string formula = "", formula_pedido = ""; float total_orcamento = 0, total_pedido = 0; FbCommand cmd = new FbCommand("select DES_FORMULA,DES_FORMULA_PEDIDO,TXT_SERVICO " + "from CARACTERISTICAS " + "where COD_FORNECEDOR='" + fornecedor + "' and " + " COD_CARACTERISTICA='" + caracteristica + "'", Globais.bd); FbDataReader reader = cmd.ExecuteReader(CommandBehavior.Default); if (reader.Read()) { formula = !reader.IsDBNull(0) ? reader.GetString(0).Trim() : ""; formula_pedido = !reader.IsDBNull(1) ? reader.GetString(1).Trim() : ""; servico = !reader.IsDBNull(2) ? reader.GetString(2).Trim() : ""; } reader.Close(); if (formula_pedido.Length == 0) { return(0f); } if (formula_pedido.CompareTo(formula) == 0) { return(0f); } string sql = "select " + " a.cod_produto," + " a.sub_codigo," + " a.qtd_item," + " a.vlr_preco," + " a.vlr_preco_tabela, " + " a.idt_especial," + " b.per_ipi " + "from itens a, produtos b " + "where a.cod_fornecedor='" + fornecedor + "' and " + " a.dat_orcamento='" + data.ToString("M/d/yyyy") + "' and " + " a.cod_orcamento=" + orcamento + " and " + " a.cod_fornecedor=b.cod_parceiro and " + " a.cod_produto=b.cod_produto and " + " a.sub_codigo=b.sub_codigo " + "order by a.cod_area,a.seq_item"; cmd = new FbCommand(sql, Globais.bd); reader = cmd.ExecuteReader(CommandBehavior.Default); string codigo; string sub_codigo; short qtd; float preco_unitario; float preco_tabela; bool especial; float ipi; //float fator; //float preco; while (reader.Read()) { float valor_orcamento = 0, valor_pedido = 0; codigo = reader.GetString(0).Trim(); sub_codigo = reader.GetString(1).Trim(); qtd = reader.GetInt16(2); preco_unitario = reader.GetFloat(3) * qtd; preco_tabela = reader.GetFloat(4) * qtd; especial = reader.GetString(5).Trim().CompareTo("S") == 0;; ipi = reader.GetFloat(6); if (especial) { // desfaz formula preco_tabela = preco_unitario; Globais.DesfazFormula(ref preco_tabela, formula, ipi, 0, 0); /* * for (int i=formula.Trim().Length-4; i>=0; i-=4) * { * if (formula[i] == 'x') * { * fator = (Globais.StrToFloat(formula.Substring(i+1, 3)) - 1) * 100; * } * if (formula.Substring(i, 4).CompareTo("+IPI") == 0) * { * fator = ipi; * } * else * { * fator = Globais.StrToFloat(formula.Substring(i, 4)); * } * preco_tabela = (preco_tabela * 100) / (100 + fator); * } */ } //if (!especial) //{ valor_orcamento = preco_tabela; valor_pedido = preco_tabela; float per_frete = cCaracteristicas.Frete(fornecedor, caracteristica); Globais.CalculaFormula(ref valor_orcamento, formula, ipi, per_frete, 0); /* * for (int i=0; i<formula.Trim().Length; i+=4) * { * if (formula[i] == 'x') * { * fator = Globais.StrToFloat(formula.Substring(i+1, 3)); * valor_orcamento *= fator; * continue; * } * if (formula.Substring(i, 4).CompareTo("+IPI") == 0) * { * fator = ipi; * valor_orcamento += (valor_orcamento * fator / (float)100); * } * else * { * fator = Globais.StrToFloat(formula.Substring(i, 4)); * valor_orcamento += (valor_orcamento * fator / (float)100); * } * } */ Globais.CalculaFormula(ref valor_pedido, formula_pedido, ipi, per_frete, 0); /* * for (int i=0; i<formula_pedido.Trim().Length; i+=4) * { * if (formula_pedido[i] == 'x') * { * fator = Globais.StrToFloat(formula_pedido.Substring(i+1, 3)); * valor_pedido *= fator; * continue; * } * if (formula_pedido.Substring(i, 4).CompareTo("+IPI") == 0) * { * fator = ipi; * valor_pedido += (valor_pedido * fator / (float)100); * } * else * { * fator = Globais.StrToFloat(formula_pedido.Substring(i, 4)); * valor_pedido += (valor_pedido * fator / (float)100); * } * } */ /* * } * else * { * preco = preco_unitario; * valor_orcamento += preco; * valor_pedido += preco; * } */ total_orcamento += valor_orcamento; total_pedido += valor_pedido; } reader.Close(); return(total_orcamento - total_pedido); }