public void Gravar(TB_FAT_NOTA nota, int?formaPagamento, ref int posicaoTransacao) { try { Conexao.Iniciar(ref posicaoTransacao); var itens = nota.TB_FAT_NOTA_ITEMs; var itensNovos = new List <TB_FAT_NOTA_ITEM>(); nota = new TB_FAT_NOTA { ID_NOTA = nota.ID_NOTA, ID_EMPRESA = nota.ID_EMPRESA, ID_NOTA_REFERENCIA = nota.ID_NOTA_REFERENCIA, ID_CONFIGURACAO_FISCAL = nota.ID_CONFIGURACAO_FISCAL, ID_CONFIGURACAO_FINANCEIRO = nota.ID_CONFIGURACAO_FINANCEIRO, ID_USUARIO = nota.ID_USUARIO, ID_CLIFOR = nota.ID_CLIFOR, ID_TRANSPORTADORA = nota.ID_TRANSPORTADORA, ID_VEICULO = nota.ID_VEICULO, ID_MOEDA = nota.ID_MOEDA, TP_MOVIMENTO = nota.TP_MOVIMENTO, TP_NOTA = nota.TP_NOTA, DT_ENTRADASAIDA = nota.DT_ENTRADASAIDA, DT_EMISSAO = Conexao.DataHora }; if (!nota.ID_EMPRESA.TemValor()) { throw new SYSException(Mensagens.Necessario("empresa da nota")); } if (!nota.ID_CLIFOR.TemValor()) { throw new SYSException(Mensagens.Necessario("clifor da nota")); } nota.TP_MOVIMENTO.Validar(true, 1); if (!(nota.TP_MOVIMENTO == "E" | nota.TP_MOVIMENTO == "S")) { throw new SYSException(Mensagens.Necessario("o tipo do movimento da nota")); } if (nota.TP_MOVIMENTO == "E" && !nota.ID_NOTA_REFERENCIA.TemValor()) { throw new SYSException(Mensagens.Necessario("o identificador da nota de referência")); } if (nota.TP_MOVIMENTO == "E" && !nota.DT_ENTRADASAIDA.DataValida()) { throw new SYSException(Mensagens.Necessario("a data de entrada da nota")); } if (!nota.ID_CONFIGURACAO_FISCAL.TemValor()) { throw new SYSException(Mensagens.Necessario("o identificador da configuração fiscal")); } if (!nota.ID_CONFIGURACAO_FINANCEIRO.TemValor()) { throw new SYSException(Mensagens.Necessario("o identificador da configuração financeiro")); } if (!nota.ID_USUARIO.TemValor()) { nota.ID_USUARIO = Parametros.ID_Usuario; } if (!nota.ID_MOEDA.TemValor()) { nota.ID_MOEDA = Parametros.ID_MoedaPadrao; } if (itens == null || !itens.Count.TemValor()) { throw new SYSException(Mensagens.Necessario("os itens da nota")); } nota.ID_NOTA = (Conexao.BancoDados.TB_FAT_NOTAs.Where(a => a.ID_EMPRESA == nota.ID_EMPRESA).Any() ? Conexao.BancoDados.TB_FAT_NOTAs.Where(a => a.ID_EMPRESA == nota.ID_EMPRESA).Max(a => a.ID_NOTA) : 0) + 1; Conexao.BancoDados.TB_FAT_NOTAs.InsertOnSubmit(nota); Conexao.Enviar(); // Itens da nota foreach (var item in itens) { var novoItem = new TB_FAT_NOTA_ITEM { ID_NOTA = nota.ID_NOTA, ID_EMPRESA = nota.ID_EMPRESA, ID_ITEM = item.ID_ITEM, ID_PRODUTO = item.ID_PRODUTO, QT = item.QT, VL_UNITARIO = item.VL_UNITARIO, VL_FRETE = item.VL_FRETE, VL_SEGURO = item.VL_SEGURO, VL_DESCONTO = item.VL_DESCONTO, VL_ACRESCIMO = item.VL_ACRESCIMO, VL_SUBTOTAL = (item.VL_UNITARIO.Padrao() * item.QT.Padrao()) + item.VL_FRETE.Padrao() + item.VL_SEGURO.Padrao() + item.VL_ACRESCIMO.Padrao() - item.VL_DESCONTO.Padrao(), ST_COMPOE = item.ST_COMPOE, DS_OBSERVACAO = item.DS_OBSERVACAO, DS_OBSERVACAOFISCAL = item.DS_OBSERVACAOFISCAL }; if (!novoItem.ID_PRODUTO.TemValor()) { throw new SYSException(Mensagens.Necessario("o produto do item")); } novoItem.VL_DESCONTO.Validar(); novoItem.QT.Validar(); novoItem.VL_UNITARIO.Validar(); if (!novoItem.VL_UNITARIO.TemValor() && !novoItem.QT.TemValor()) { throw new SYSException(Mensagens.Necessario("valor e/ou quantidade do item")); } novoItem.ID_ITEM = (Conexao.BancoDados.TB_FAT_NOTA_ITEMs.Where(a => a.ID_EMPRESA == nota.ID_EMPRESA && a.ID_NOTA == a.ID_NOTA).Any() ? Conexao.BancoDados.TB_FAT_NOTA_ITEMs.Where(a => a.ID_EMPRESA == nota.ID_EMPRESA && a.ID_NOTA == a.ID_NOTA).Max(a => a.ID_ITEM) : 0) + 1; var novoItemTributos = novoItem.TB_FAT_NOTA_ITEM_TRIBUTOs; Conexao.BancoDados.TB_FAT_NOTA_ITEMs.InsertOnSubmit(novoItem); Conexao.Enviar(); itensNovos.Add(novoItem); // Remove o item do estoque new QEstoque().Gravar(new TB_EST_ESTOQUE { DT = Conexao.DataHora, ID_EMPRESA = nota.ID_EMPRESA, ID_PRODUTO = novoItem.ID_PRODUTO.Padrao(), QT = item.QT.Padrao() * -1 // Saída }, ref posicaoTransacao); // tributos do item if (novoItemTributos == null || !novoItemTributos.Count.TemValor()) { var tributos = Conexao.BancoDados.TB_EST_PRODUTO_TRIBUTOs.Where(a => a.ID_PRODUTO == novoItem.ID_PRODUTO).Select(a => a.TB_FIS_TRIBUTO).ToList(); if (tributos == null || !tributos.Count.TemValor()) { tributos = Conexao.BancoDados.TB_EST_GRUPO_TRIBUTOs.Where(a => a.ID_GRUPO == novoItem.TB_EST_PRODUTO.ID_GRUPO).Select(a => a.TB_FIS_TRIBUTO).ToList(); } if (tributos != null && tributos.Count.TemValor()) { novoItemTributos = tributos.Select(a => new TB_FAT_NOTA_ITEM_TRIBUTO { ID_EMPRESA = nota.ID_EMPRESA, ID_NOTA = nota.ID_NOTA, ID_ITEM = item.ID_ITEM, ID_TRIBUTO = a.ID_TRIBUTO }).ToEntitySet(); } } if (novoItemTributos == null || !novoItemTributos.Count.TemValor()) { throw new SYSException(Mensagens.Necessario("os tributos do item")); } foreach (var tributo in novoItemTributos) { tributo.ID_EMPRESA = nota.ID_EMPRESA; tributo.ID_NOTA = nota.ID_NOTA; tributo.ID_ITEM = novoItem.ID_ITEM; if (!tributo.ID_TRIBUTO.TemValor()) { throw new SYSException(Mensagens.Necessario("o tributo do item")); } tributo.VL_BASECALCULO = novoItem.VL_SUBTOTAL.Validar(); if (nota.TB_CON_EMPRESA.TB_REL_CLIFOR.TB_REL_CLIFOR_X_ENDERECOs == null || nota.TB_CON_EMPRESA.TB_REL_CLIFOR.TB_REL_CLIFOR_X_ENDERECOs.Count == 0) { throw new SYSException(Mensagens.Necessario("os endereços da empresa")); } var enderecoEmpresa = nota.TB_CON_EMPRESA.TB_REL_CLIFOR.TB_REL_CLIFOR_X_ENDERECOs.FirstOrDefault().TB_REL_ENDERECO; var enderecoCliente = nota.TB_REL_CLIFOR.TB_REL_CLIFOR_X_ENDERECOs.FirstOrDefault().TB_REL_ENDERECO; //var produto = new Cadastros.Estoque.QProduto().Buscar(item.ID_PRODUTO.Padrao()).FirstOrDefault(); //tributo.PC_ALIQUOTA = enderecoEmpresa.ID_CIDADE == enderecoCliente.ID_CIDADE ? produto.PC_ALIQUOTA_MUNICIPAL.Padrao() : // (enderecoEmpresa.ID_UF == enderecoCliente.ID_UF ? produto.PC_ALIQUOTA_ESTADUAL.Padrao() : // ((enderecoEmpresa.ID_PAIS == enderecoCliente.ID_PAIS ? produto.PC_ALIQUOTA_FEDERAL.Padrao() : produto.PC_ALIQUOTA_INTERNACIONAL.Padrao()))); //tributo.VL = (tributo.VL_BASECALC.Padrao() / 100) * tributo.PC_ALIQUOTA.Padrao(); Conexao.BancoDados.TB_FAT_NOTA_ITEM_TRIBUTOs.InsertOnSubmit(tributo); Conexao.Enviar(); } } Conexao.Enviar(); var configuracaoFinanceiro = Conexao.BancoDados.TB_FIN_CONFIGURACAOs.FirstOrDefault(a => a.ID_CONFIGURACAO_FINANCEIRO == nota.ID_CONFIGURACAO_FINANCEIRO); var duplicata = new TB_FIN_DUPLICATA { ID_EMPRESA = nota.ID_EMPRESA, ID_CLIFOR = nota.ID_CLIFOR.Padrao(), ID_MOEDA = nota.ID_MOEDA.Padrao(), ID_CENTROCUSTO = configuracaoFinanceiro.ID_CENTROCUSTO, ID_CONDICAOPAGAMENTO = configuracaoFinanceiro.ID_CONDICAOPAGAMENTO, ID_DOCUMENTO = nota.ID_NOTA.ToString().Validar(true, 16), VL = itensNovos.Sum(a => a.VL_SUBTOTAL.Padrao()), QT_PARCELAS = configuracaoFinanceiro.QT_PARCELAS, DT_EMISSAO = Conexao.DataHora, TP = nota.TP_MOVIMENTO == "E" ? "P" : "R" }; new QDuplicata().Gravar(duplicata, itensNovos.Sum(a => a.VL_DESCONTO.Padrao()), 0m, formaPagamento, ref posicaoTransacao); Conexao.BancoDados.TB_FIN_DUPLICATA_X_NOTAs.InsertOnSubmit(new TB_FIN_DUPLICATA_X_NOTA { ID_DUPLICATA = duplicata.ID_DUPLICATA, ID_NOTA = nota.ID_NOTA, ID_EMPRESA = nota.ID_EMPRESA }); var documento = new TB_FIS_DOCUMENTO { ID_EMPRESA = nota.ID_EMPRESA }; new QDocumento().Gravar(documento, ref posicaoTransacao); Conexao.BancoDados.TB_FAT_NOTA_X_DOCUMENTOs.InsertOnSubmit(new TB_FAT_NOTA_X_DOCUMENTO { ID_NOTA = nota.ID_NOTA, ID_EMPRESA = nota.ID_EMPRESA, ID_DOCUMENTO = documento.ID_DOCUMENTO }); Conexao.Enviar(); ID_NOTA = nota.ID_NOTA; Conexao.BancoDados.TB_FAT_NOTA_X_CPFs.InsertOnSubmit(new TB_FAT_NOTA_X_CPF { ID_EMPRESA = nota.ID_EMPRESA, ID_NOTA = nota.ID_NOTA, CPF = new QClifor().Buscar(nota.ID_CLIFOR ?? 0).ToList()[0].CPF }); Conexao.Enviar(); Conexao.Finalizar(ref posicaoTransacao); } catch (Exception excessao) { Conexao.Voltar(ref posicaoTransacao); throw excessao; } }
public void Gravar(MPedido pedido, bool gerarNotaFiscal, ref int posicaoTransacao, List <MPedidoItem> transferencia = null) { try { Conexao.Iniciar(ref posicaoTransacao); if (transferencia != null) { for (int i = 0; i < transferencia.Count; i++) { DeletarItemParaTransferir(transferencia[i], ref posicaoTransacao); } } var existente = Conexao.BancoDados.TB_COM_PEDIDOs.FirstOrDefault(a => a.ID_PEDIDO == pedido.ID_PEDIDO); #region Inserção if (existente == null) { pedido.ID_PEDIDO = (Conexao.BancoDados.TB_COM_PEDIDOs.Any() ? Conexao.BancoDados.TB_COM_PEDIDOs.Max(a => a.ID_PEDIDO) : 0) + 1; var vPedido = new TB_COM_PEDIDO(); vPedido.ID_PEDIDO = pedido.ID_PEDIDO; vPedido.ID_PEDIDO_ORIGEM = pedido.ID_PEDIDO_ORIGEM == 0 ? null : pedido.ID_PEDIDO_ORIGEM; vPedido.ID_EMPRESA = pedido.ID_EMPRESA; vPedido.ID_USUARIO = pedido.ID_USUARIO; vPedido.ID_CLIFOR = pedido.ID_CLIFOR; vPedido.ST_PEDIDO = pedido.ST_PEDIDO; vPedido.ST_ATIVO = pedido.ST_ATIVO; vPedido.DT_CADASTRO = Conexao.DataHora; vPedido.TP_MOVIMENTO = pedido.TP_MOVIMENTO; Conexao.BancoDados.TB_COM_PEDIDOs.InsertOnSubmit(vPedido); Conexao.Enviar(); } #endregion #region Atualização else { existente.ID_EMPRESA = pedido.ID_EMPRESA; existente.ID_CLIFOR = pedido.ID_CLIFOR; existente.ST_ATIVO = pedido.ST_ATIVO; existente.ID_PEDIDO_ORIGEM = pedido.ID_PEDIDO_ORIGEM == 0 ? null : pedido.ID_PEDIDO_ORIGEM; existente.ST_PEDIDO = pedido.ST_PEDIDO; existente.TP_MOVIMENTO = pedido.TP_MOVIMENTO; Conexao.Enviar(); } ID_PEDIDO = pedido.ID_PEDIDO; for (int i = 0; i < pedido.Itens.Count; i++) { GravaPedidoItem(pedido, pedido.Itens[i]); } if (gerarNotaFiscal) { //GRAVA NOTA FISCAL var nota = new TB_FAT_NOTA(); nota.ID_CLIFOR = pedido.ID_CLIFOR; nota.ID_EMPRESA = pedido.ID_EMPRESA; nota.ID_USUARIO = pedido.ID_USUARIO; nota.TP_MOVIMENTO = pedido.TP_MOVIMENTO; nota.DT_ENTRADASAIDA = pedido.DT_CADASTRO; nota.ID_NOTA_REFERENCIA = pedido.ID_PEDIDO; // Revisar nota.ID_CONFIGURACAO_FISCAL = 1; nota.ID_CONFIGURACAO_FINANCEIRO = 1; for (int i = 0; i < pedido.Itens.Count; i++) { nota.TB_FAT_NOTA_ITEMs.Add(new TB_FAT_NOTA_ITEM { ID_PRODUTO = pedido.Itens[i].ID_PRODUTO, QT = pedido.Itens[i].QUANTIDADE, VL_UNITARIO = pedido.Itens[i].VL_UNITARIO, VL_DESCONTO = pedido.Itens[i].VL_DESCONTO, ID_EMPRESA = pedido.ID_EMPRESA }); } var referencia = new QNota(); referencia.Gravar(nota, pedido.CONDICAO_PAGAMENTO, ref posicaoTransacao); ID_NOTA = referencia.ID_NOTA; } #endregion Conexao.Enviar(); Conexao.Finalizar(ref posicaoTransacao); } catch (Exception excessao) { Conexao.Voltar(ref posicaoTransacao); throw excessao; } }