Пример #1
0
 /// <summary>
 /// Deserializes workflow markup into an TNfeProc object
 /// </summary>
 /// <param name="xml">string workflow markup to deserialize</param>
 /// <param name="obj">Output TNfeProc object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool Deserialize(string xml, out TNfeProc obj, out System.Exception exception)
 {
     exception = null;
     obj = default(TNfeProc);
     try
     {
         obj = Deserialize(xml);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
Пример #2
0
        public void Generate(DownloadedHtmlData downloadedData, string directory)
        {
            var nota = new TNfeProc();

            UpdateDadosNfe(downloadedData.ChaveAcessso,
                            nota,
                            downloadedData.DadosNfe,
                            downloadedData.DadosEmitente,
                            downloadedData.InformacoesAdicionais);

            UpdateDadosEmitente(nota, downloadedData.DadosEmitente);
            UpdateDadosDestinatario(nota, downloadedData.DadosDestinatario);
            UpdateProdutos(nota, downloadedData.Products);
            UpdateTotais(nota, downloadedData.Totais);
            UpdateTransporte(nota, downloadedData.DadosTransporte);
            UpdateAdicionais(nota, downloadedData.InformacoesAdicionais);

            var xml = Util.Serialize(nota);
            xml = xml.Replace("protNFe versao=\"2.00\"", "protNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\"");
            SaveXml(xml, directory);
        }
Пример #3
0
        private void UpdateDadosDestinatario(TNfeProc nota, IList<PostResultItem> dadosDestinatario)
        {
            nota.NFe.infNFe.dest = new TNFeInfNFeDest
            {
                ItemElementName = ITCTypeCNPJCPF.CNPJ,
                Item = GetValue(dadosDestinatario, "CNPJ")
                    .Replace(".", string.Empty)
                    .Replace("/", string.Empty)
                    .Replace("-", string.Empty).Trim(),
                xNome = GetValue(dadosDestinatario, "Nome / Razão Social"),
                IE = GetValue(dadosDestinatario, "Inscrição Estadual")
            };

            var ender = GetValue(dadosDestinatario, "Endereço");
            var enderParts = ender.Split(new[] { "," }, StringSplitOptions.None);

            var nro = enderParts.Length >= 1 ? Regex.Match(enderParts[1], @"\d+").Value : string.Empty;
            if (string.IsNullOrWhiteSpace(nro))
                nro = Regex.Match(ender, @"\d+").Value;
            var xLgr = enderParts.Length > 0 ? enderParts[0] : string.Empty;
            nota.NFe.infNFe.dest.enderDest = new TEndereco();
            if (!string.IsNullOrWhiteSpace(xLgr))
                nota.NFe.infNFe.dest.enderDest.xLgr = xLgr;
            if (!string.IsNullOrWhiteSpace(nro))
                nota.NFe.infNFe.dest.enderDest.nro = nro;
            var xCpl = enderParts.Length >= 1 ? enderParts[1].Trim() : string.Empty;
            if (!string.IsNullOrWhiteSpace(xCpl) && xCpl != nota.NFe.infNFe.dest.enderDest.nro && xCpl.Length > 3)
                nota.NFe.infNFe.dest.enderDest.xCpl = string.IsNullOrWhiteSpace(nota.NFe.infNFe.dest.enderDest.nro) ? xCpl.Trim() : xCpl.Replace(nota.NFe.infNFe.dest.enderDest.nro, string.Empty).Trim();
            var bairro = GetValue(dadosDestinatario, "Bairro / Distrito");
            if (!string.IsNullOrWhiteSpace(bairro))
                nota.NFe.infNFe.dest.enderDest.xBairro = bairro;
            nota.NFe.infNFe.dest.enderDest.cMun = GetValue(dadosDestinatario, "Município").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
            nota.NFe.infNFe.dest.enderDest.xMun = GetValue(dadosDestinatario, "Município").Split(new[] { "-" }, StringSplitOptions.None)[1].Trim(); ;
            nota.NFe.infNFe.dest.enderDest.UF = GetTUF(GetValue(dadosDestinatario, "UF"));
            nota.NFe.infNFe.dest.enderDest.CEP = GetValue(dadosDestinatario, "CEP").Replace("-", string.Empty);
            nota.NFe.infNFe.dest.enderDest.cPais = "1058";
            nota.NFe.infNFe.dest.enderDest.xPais = "BRASIL";
            var fone = GetValue(dadosDestinatario, "Telefone");
            if (!string.IsNullOrWhiteSpace(fone))
                nota.NFe.infNFe.dest.enderDest.fone = fone
                    .Replace("(", string.Empty)
                    .Replace(")", string.Empty)
                    .Replace("-", string.Empty);
        }
Пример #4
0
 private void UpdateAdicionais(TNfeProc nota, IList<PostResultItem> adicionais)
 {
     var infAdFisco = GetValue(adicionais, "Descrição").Trim();
     if (!string.IsNullOrWhiteSpace(infAdFisco))
     {
         nota.NFe.infNFe.infAdic = new TNFeInfNFeInfAdic();
         nota.NFe.infNFe.infAdic.infAdFisco = infAdFisco;
     }
 }
Пример #5
0
 private void UpdateTransporte(TNfeProc nota, IList<PostResultItem> itensTransporte)
 {
     nota.NFe.infNFe.transp = new TNFeInfNFeTransp();
     var modFrete = GetValue(itensTransporte, "Modalidade do Frete").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
     switch (modFrete)
     {
         case "0":
             nota.NFe.infNFe.transp.modFrete = TNFeInfNFeTranspModFrete.Item0;
             break;
         case "1":
             nota.NFe.infNFe.transp.modFrete = TNFeInfNFeTranspModFrete.Item1;
             break;
         case "2":
             nota.NFe.infNFe.transp.modFrete = TNFeInfNFeTranspModFrete.Item2;
             break;
         case "9":
             nota.NFe.infNFe.transp.modFrete = TNFeInfNFeTranspModFrete.Item9;
             break;
     }
     nota.NFe.infNFe.transp.transporta = new TNFeInfNFeTranspTransporta();
     var transCnpj = GetValue(itensTransporte, "CNPJ").Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty);
     if (!string.IsNullOrWhiteSpace(transCnpj))
     {
         nota.NFe.infNFe.transp.transporta.ItemElementName = ITCTypeCNPJCPF.CNPJ;
         nota.NFe.infNFe.transp.transporta.Item = transCnpj;
     }
     var transIe = GetValue(itensTransporte, "Inscrição Estadual");
     if (!string.IsNullOrWhiteSpace(transIe))
         nota.NFe.infNFe.transp.transporta.IE = transIe;
     var transXnome = GetValue(itensTransporte, "Razão Social / Nome");
     if (!string.IsNullOrWhiteSpace(transXnome))
         nota.NFe.infNFe.transp.transporta.xNome = transXnome;
     var transEnder = GetValue(itensTransporte, "Endereço Completo");
     if (!string.IsNullOrWhiteSpace(transEnder))
         nota.NFe.infNFe.transp.transporta.xEnder = transEnder;
     var transMun = GetValue(itensTransporte, "Município");
     if (!string.IsNullOrWhiteSpace(transMun))
         nota.NFe.infNFe.transp.transporta.xMun = transMun;
     nota.NFe.infNFe.transp.transporta.UF = GetTUF(GetValue(itensTransporte, "UF"));
     var pesoB = GetValue(itensTransporte, "Peso Bruto");
     nota.NFe.infNFe.transp.vol = new[]
     {
         new TNFeInfNFeTranspVol
         {
             qVol = GetValue(itensTransporte, "Quantidade"),
         }
     };
     if (!string.IsNullOrWhiteSpace(pesoB))
         nota.NFe.infNFe.transp.vol[0].pesoB = double.Parse(pesoB).ToString("0.000", enUs);
     var marca = GetValue(itensTransporte, "Marca dos Volumes");
     if (!string.IsNullOrWhiteSpace(marca))
         nota.NFe.infNFe.transp.vol[0].marca = marca;
 }
Пример #6
0
 public static bool Deserialize(string xml, out TNfeProc obj)
 {
     System.Exception exception = null;
     return Deserialize(xml, out obj, out exception);
 }
Пример #7
0
 public static bool LoadFromFile(string fileName, out TNfeProc obj, out System.Exception exception)
 {
     return LoadFromFile(fileName, Encoding.UTF8, out obj, out exception);
 }
Пример #8
0
        private void UpdateProdutos(TNfeProc nota, IList<Produto> produtos)
        {
            var itensNfe = new List<TNFeInfNFeDet>();
            foreach (var produto in produtos)
            {
                var det = new TNFeInfNFeDet
                {
                    nItem = produto.Num,
                    prod = new TNFeInfNFeDetProd
                    {
                        cProd = produto.CodigoProduto,
                        cEAN = produto.CodigoEANComercial,
                        xProd = produto.Descricao,
                        NCM = produto.CodigoNCM,
                        CFOP = GetProdCfop(produto.CFOP),
                        uCom = produto.UnidadeComercial,
                        qCom = double.Parse(produto.QuantidadeComercial).ToString("0.00", enUs),
                        vUnCom = double.Parse(produto.ValorUnitarioComercializacao).ToString("0.0000", enUs),
                        vProd = double.Parse(produto.Valor).ToString("0.00", enUs),
                        cEANTrib = produto.CodigoEANTributavel,
                        uTrib = produto.UnidadeTributavel,
                        qTrib = double.Parse(produto.QuantidadeTributavel).ToString("0.00", enUs),
                        vUnTrib = double.Parse(produto.ValorUnitarioTributacao).ToString("0.0000", enUs),
                        indTot = int.Parse(produto.IndicadorComposicaoValorTotalNFe.Split(new[] { "-" }, StringSplitOptions.None)[0]) == 1 ? TNFeInfNFeDetProdIndTot.Item1 : TNFeInfNFeDetProdIndTot.Item0
                    }
                };

                if (!string.IsNullOrWhiteSpace(produto.nDI))
                {
                    det.prod.DI = new[]
                    {
                        new TNFeInfNFeDetProdDI
                        {
                            nDI = produto.nDI,
                            dDI = DateTime.Parse(produto.dDI).ToString("yyyy-MM-dd"),
                            xLocDesemb = produto.xLocDesemb,
                            UFDesemb = GetUFEmi(produto.UFDesemb),
                            dDesemb = DateTime.Parse(produto.dDesemb).ToString("yyyy-MM-dd"),
                            cExportador = produto.cExportador,
                            adi = new[]
                            {
                                new TNFeInfNFeDetProdDIAdi
                                {
                                    nAdicao = produto.nAdicao,
                                    nSeqAdic = produto.nSeqAdic,
                                    cFabricante = produto.cFabricante
                                }
                            }
                        }
                    };
                }

                det.imposto = new TNFeInfNFeDetImposto();

                var tiposImposto = new List<object>();

                //############################ ICMS ############################
                if (!string.IsNullOrWhiteSpace(produto.TributacaoICMS))
                {

                    var tributacaoIcms = produto.TributacaoICMS.Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
                    var icms = new TNFeInfNFeDetImpostoICMS();

                    object detIcms;
                    switch (tributacaoIcms)
                    {
                        case "00":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS00();
                            var icms00 = (TNFeInfNFeDetImpostoICMSICMS00)detIcms;
                            if (!string.IsNullOrWhiteSpace(produto.modBC))
                            {
                                var modBC = produto.modBC.Split(new[] { '-' })[0].Trim();
                                switch (modBC)
                                {
                                    case "0":
                                        icms00.modBC = TNFeInfNFeDetImpostoICMSICMS00ModBC.Item0;
                                        break;
                                    case "1":
                                        icms00.modBC = TNFeInfNFeDetImpostoICMSICMS00ModBC.Item1;
                                        break;
                                    case "2":
                                        icms00.modBC = TNFeInfNFeDetImpostoICMSICMS00ModBC.Item2;
                                        break;
                                    case "3":
                                        icms00.modBC = TNFeInfNFeDetImpostoICMSICMS00ModBC.Item3;
                                        break;
                                }
                            }
                            icms00.vBC = double.Parse(produto.vBC).ToString("0.00", enUs);
                            icms00.pICMS = double.Parse(produto.pICMS).ToString("0.00", enUs);
                            icms00.vICMS = double.Parse(produto.vICMS).ToString("0.00", enUs);
                            break;
                        case "10":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS10();
                            break;
                        case "20":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS20();
                            break;
                        case "30":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS30();
                            break;
                        case "40":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS40();
                            //((TNFeInfNFeDetImpostoICMSICMS40)detIcms).orig =  Torig. produto.OrigemMercadoria;
                            break;
                        case "51":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS51();
                            break;
                        case "60":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS60();
                            break;
                        case "70":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS70();
                            break;
                        case "90":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS90();
                            break;
                        case "Part":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSPart();
                            break;
                        case "101":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN101();
                            break;
                        case "102":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN102();
                            break;
                        case "201":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN201();
                            break;
                        case "202":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN202();
                            break;
                        case "500":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN500();
                            break;
                        case "900":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSSN900();
                            break;
                        case "ST":
                            detIcms = new TNFeInfNFeDetImpostoICMSICMSST();
                            break;
                        default:
                            detIcms = new TNFeInfNFeDetImpostoICMSICMS40();
                            break;
                    }
                    icms.Item = detIcms;
                    tiposImposto.Add(icms);
                    det.imposto.Items = tiposImposto.ToArray();
                }
                //############################ PIS ############################
                if (!string.IsNullOrWhiteSpace(produto.PIS_CST))
                {
                    var pis = new TNFeInfNFeDetImpostoPIS();
                    var pisCST = new TNFeInfNFeDetImpostoPISPISNT();
                    var pCst = produto.PIS_CST.Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
                    switch (pCst)
                    {
                        case "04":
                            pisCST.CST = TNFeInfNFeDetImpostoPISPISNTCST.Item04;
                            break;
                        case "06":
                            pisCST.CST = TNFeInfNFeDetImpostoPISPISNTCST.Item06;
                            break;
                        case "07":
                            pisCST.CST = TNFeInfNFeDetImpostoPISPISNTCST.Item07;
                            break;
                        case "08":
                            pisCST.CST = TNFeInfNFeDetImpostoPISPISNTCST.Item08;
                            break;
                        case "09":
                            pisCST.CST = TNFeInfNFeDetImpostoPISPISNTCST.Item09;
                            break;
                        default:
                            break;
                    }
                    pis.Item = pisCST;
                    det.imposto.PIS = pis;
                }
                //############################ IPI ############################
                if (!string.IsNullOrWhiteSpace(produto.IPI_CST))
                {
                    var ipi = new TNFeInfNFeDetImpostoIPI();
                    ipi.cEnq = produto.cEnq.Trim();
                    var ipiTrib = new TNFeInfNFeDetImpostoIPIIPITrib();
                    switch (produto.IPI_CST.Split(new[] { '-' })[0].Trim())
                    {
                        case "00":
                            ipiTrib.CST = TNFeInfNFeDetImpostoIPIIPITribCST.Item00;
                            break;
                        case "49":
                            ipiTrib.CST = TNFeInfNFeDetImpostoIPIIPITribCST.Item49;
                            break;
                        case "50":
                            ipiTrib.CST = TNFeInfNFeDetImpostoIPIIPITribCST.Item50;
                            break;
                        case "99":
                            ipiTrib.CST = TNFeInfNFeDetImpostoIPIIPITribCST.Item99;
                            break;
                    }
                    ipiTrib.vIPI = double.Parse(produto.IPI_vIpi).ToString("0.00", enUs);

                    var items = new List<ItemsChoiceType>
                    {
                        ItemsChoiceType.vBC,
                        ItemsChoiceType.pIPI
                    };
                    ipiTrib.ItemsElementName = items.ToArray();
                    ipiTrib.Items = new[]
                    {
                        produto.IPI_vBC.Replace(",","."),
                        string.Format("{0:0.00}", double.Parse(produto.IPI_pIpi)).Replace(",","."),
                    };
                    ipi.Item = ipiTrib;

                    tiposImposto.Add(ipi);
                    det.imposto.Items = tiposImposto.ToArray();
                }
                //############################ COFINS ############################
                if (!string.IsNullOrWhiteSpace(produto.COFINS_CST))
                {
                    det.imposto.COFINS = new TNFeInfNFeDetImpostoCOFINS();
                    var confinsNT = new TNFeInfNFeDetImpostoCOFINSCOFINSNT();
                    var cofinsCST = produto.COFINS_CST.Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
                    switch (cofinsCST)
                    {
                        case "04":
                            confinsNT.CST = TNFeInfNFeDetImpostoCOFINSCOFINSNTCST.Item04;
                            break;
                        case "06":
                            confinsNT.CST = TNFeInfNFeDetImpostoCOFINSCOFINSNTCST.Item06;
                            break;
                        case "07":
                            confinsNT.CST = TNFeInfNFeDetImpostoCOFINSCOFINSNTCST.Item07;
                            break;
                        case "08":
                            confinsNT.CST = TNFeInfNFeDetImpostoCOFINSCOFINSNTCST.Item08;
                            break;
                        case "09":
                            confinsNT.CST = TNFeInfNFeDetImpostoCOFINSCOFINSNTCST.Item09;
                            break;
                    }
                    det.imposto.COFINS.Item = confinsNT;
                    itensNfe.Add(det);
                }
            }
            nota.NFe.infNFe.det = itensNfe.ToArray();
        }
Пример #9
0
 public virtual void SaveToFile(string fileName, TNfeProc nfe)
 {
     SaveToFile(fileName, nfe, Encoding.UTF8);
 }
Пример #10
0
 public virtual bool SaveToFile(string fileName, TNfeProc nfe, out System.Exception exception)
 {
     return SaveToFile(fileName, nfe, Encoding.UTF8, out exception);
 }
Пример #11
0
 /// <summary>
 /// Serializes current TNfeProc object into file
 /// </summary>
 /// <param name="fileName">full path of outupt xml file</param>
 /// <param name="exception">output Exception value if failed</param>
 /// <returns>true if can serialize and save into file; otherwise, false</returns>
 public virtual bool SaveToFile(string fileName, TNfeProc nfe, System.Text.Encoding encoding, out System.Exception exception)
 {
     exception = null;
     try
     {
         SaveToFile(fileName, nfe, encoding);
         return true;
     }
     catch (System.Exception e)
     {
         exception = e;
         return false;
     }
 }
Пример #12
0
 public static string Serialize(TNfeProc nfe)
 {
     return Serialize(nfe, Encoding.UTF8);
 }
Пример #13
0
 /// <summary>
 /// Serializes current TNfeProc object into an XML document
 /// </summary>
 /// <returns>string XML value</returns>
 public static string Serialize(TNfeProc nfe, System.Text.Encoding encoding)
 {
     System.IO.StreamReader streamReader = null;
     System.IO.MemoryStream memoryStream = null;
     try
     {
         memoryStream = new System.IO.MemoryStream();
         System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
         xmlWriterSettings.Encoding = encoding;
         System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
         Serializer.Serialize(xmlWriter, nfe);
         memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
         streamReader = new System.IO.StreamReader(memoryStream);
         return streamReader.ReadToEnd();
     }
     finally
     {
         if ((streamReader != null))
         {
             streamReader.Dispose();
         }
         if ((memoryStream != null))
         {
             memoryStream.Dispose();
         }
     }
 }
Пример #14
0
 public static bool LoadFromFile(string fileName, out TNfeProc obj)
 {
     System.Exception exception = null;
     return LoadFromFile(fileName, out obj, out exception);
 }
Пример #15
0
        private void UpdateDadosEmitente(TNfeProc nota, IList<PostResultItem> dadosEmitente)
        {
            nota.NFe.infNFe.emit = new TNFeInfNFeEmit();
            nota.NFe.infNFe.emit.ItemElementName = ITCTypeCNPJCPF.CNPJ;
            nota.NFe.infNFe.emit.Item = GetValue(dadosEmitente, "CNPJ")
                .Replace(".", string.Empty)
                .Replace("/", string.Empty)
                .Replace("-", string.Empty).Trim();
            nota.NFe.infNFe.emit.xNome = GetValue(dadosEmitente, "Nome / Razão Social");
            var fant = GetValue(dadosEmitente, "Nome Fantasia");
            if (!string.IsNullOrWhiteSpace(fant))
                nota.NFe.infNFe.emit.xFant = fant;
            var ie = GetValue(dadosEmitente, "Inscrição Estadual");
            if (!string.IsNullOrWhiteSpace(ie))
                nota.NFe.infNFe.emit.IE = ie;

            var iest = GetValue(dadosEmitente, "Inscrição Estadual do Substituto Tributário");
            if (!string.IsNullOrWhiteSpace(iest))
                nota.NFe.infNFe.emit.IEST = iest;

            var im = GetValue(dadosEmitente, "Inscrição Municipal");
            if (!string.IsNullOrWhiteSpace(im))
                nota.NFe.infNFe.emit.IM = im;

            var cnae = GetValue(dadosEmitente, "CNAE Fiscal");
            if (!string.IsNullOrWhiteSpace(cnae))
                nota.NFe.infNFe.emit.CNAE = cnae;

            var crt = GetValue(dadosEmitente, "Código de Regime Tributário").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
            if (!string.IsNullOrWhiteSpace(crt))
                nota.NFe.infNFe.emit.CRT = GetCrtEmit(int.Parse(crt));

            var ender = GetValue(dadosEmitente, "Endereço");
            var enderParts = ender.Split(new[] { "," }, StringSplitOptions.None);

            var nro = enderParts.Length >= 1 ? Regex.Match(enderParts[1], @"\d+").Value : string.Empty;
            if (string.IsNullOrWhiteSpace(nro))
                nro = Regex.Match(ender, @"\d+").Value;

            var xLgr = enderParts.Length > 0 ? enderParts[0] : string.Empty;
            nota.NFe.infNFe.emit.enderEmit = new TEnderEmi();
            if (!string.IsNullOrWhiteSpace(xLgr))
                nota.NFe.infNFe.emit.enderEmit.xLgr = xLgr;
            if (!string.IsNullOrWhiteSpace(nro))
                nota.NFe.infNFe.emit.enderEmit.nro = nro;
            var xCpl = enderParts.Length >= 1 ? enderParts[1].Trim() : string.Empty;
            if (!string.IsNullOrWhiteSpace(xCpl) && xCpl != nota.NFe.infNFe.emit.enderEmit.nro && xCpl.Length > 3)
                nota.NFe.infNFe.emit.enderEmit.xCpl = string.IsNullOrWhiteSpace(nota.NFe.infNFe.emit.enderEmit.nro) ? xCpl.Trim() : xCpl.Replace(nota.NFe.infNFe.emit.enderEmit.nro, string.Empty).Trim();
            var bairro = GetValue(dadosEmitente, "Bairro / Distrito");
            if (!string.IsNullOrWhiteSpace(bairro))
                nota.NFe.infNFe.emit.enderEmit.xBairro = bairro;
            nota.NFe.infNFe.emit.enderEmit.cMun = GetValue(dadosEmitente, "Município").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();
            nota.NFe.infNFe.emit.enderEmit.xMun = GetValue(dadosEmitente, "Município").Split(new[] { "-" }, StringSplitOptions.None)[1].Trim(); ;
            nota.NFe.infNFe.emit.enderEmit.UF = GetUFEmi(GetValue(dadosEmitente, "UF"));
            nota.NFe.infNFe.emit.enderEmit.CEP = GetValue(dadosEmitente, "CEP").Replace("-", string.Empty);
            nota.NFe.infNFe.emit.enderEmit.cPais = TEnderEmiCPais.Item1058;
            nota.NFe.infNFe.emit.enderEmit.xPais = TEnderEmiXPais.BRASIL;
            nota.NFe.infNFe.emit.enderEmit.cPaisSpecified = true;
            nota.NFe.infNFe.emit.enderEmit.xPaisSpecified = true;
            var fone = GetValue(dadosEmitente, "Telefone");
            if (!string.IsNullOrWhiteSpace(GetValue(dadosEmitente, "Telefone")))
                nota.NFe.infNFe.emit.enderEmit.fone = fone
                    .Replace("(", string.Empty)
                    .Replace(")", string.Empty)
                    .Replace("-", string.Empty);
        }
Пример #16
0
        private void UpdateDadosNfe(string chave,
                                    TNfeProc nota,
                                    IList<PostResultItem> dadosNfe,
                                    IList<PostResultItem> dadosEmitente,
                                    IList<PostResultItem> dadosAdicionais)
        {
            var chaveNfe = chave.Replace("-", string.Empty).Replace(".", string.Empty).Replace("/", string.Empty);

            var versaoNfe = "2.00";
            var cNF = chaveNfe.Substring(35, 8);
            var cDV = chave.Split(new[] { "-" }, StringSplitOptions.None)[8];
            var indPag = int.Parse(GetValue(dadosNfe, "Forma de Pagamento").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim());
            var municipioEmitente = GetValue(dadosEmitente, "Município").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim();

            nota.NFe = new TNFe();
            nota.versao = versaoNfe;

            nota.NFe.infNFe = new TNFeInfNFe();
            nota.NFe.infNFe.versao = versaoNfe;
            nota.NFe.infNFe.Id = "NFe" + chaveNfe;

            var dataEmissaoText = GetValue(dadosNfe, "Data de Emissão");
            var dataEmissao = DateTime.Parse(dataEmissaoText);

            var dataHoraSaidaText = GetValue(dadosNfe, "Data Saída/Entrada");
            var dataHoraSaida = string.IsNullOrWhiteSpace(dataHoraSaidaText) ? (DateTime?)null : DateTime.Parse(dataHoraSaidaText.Replace("às", string.Empty).Replace("\r\n", string.Empty));

            nota.NFe.infNFe.ide = new TNFeInfNFeIde();
            nota.NFe.infNFe.ide.cUF = GetUF(dadosNfe.Where(a => a.AttributeName == "UF").FirstOrDefault().AttributeValue);
            nota.NFe.infNFe.ide.cNF = cNF;
            nota.NFe.infNFe.ide.natOp = GetValue(dadosNfe, "Natureza da Operação");
            nota.NFe.infNFe.ide.indPag = GetIndPag(indPag);
            nota.NFe.infNFe.ide.mod = TMod.Item55;
            nota.NFe.infNFe.ide.serie = GetValue(dadosNfe, "Série");
            nota.NFe.infNFe.ide.nNF = GetValue(dadosNfe, "Número");
            nota.NFe.infNFe.ide.dEmi = dataEmissao.ToString("yyyy-MM-dd");
            if (dataHoraSaida != null)
                nota.NFe.infNFe.ide.dSaiEnt = dataHoraSaida.Value.ToString("yyyy-MM-dd");
            //nota.NFe.infNFe.ide.hSaiEnt = dataHoraSaida == null ? string.Empty : dataHoraSaida.Value.ToString("hh:mm:ss");
            nota.NFe.infNFe.ide.tpNF = GetTpNF(GetValue(dadosNfe, "Tipo da Operação"));
            nota.NFe.infNFe.ide.cMunFG = municipioEmitente;
            nota.NFe.infNFe.ide.tpImp = GetTpImp(GetValue(dadosAdicionais, "Formato de Impressão DANFE").Contains("retrato") ? 1 : 2);
            nota.NFe.infNFe.ide.tpEmis = GetTpEmis(int.Parse(GetValue(dadosNfe, "Tipo de Emissão").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim()));
            nota.NFe.infNFe.ide.cDV = cDV;
            nota.NFe.infNFe.ide.tpAmb = GetTpAmb(dadosNfe.Where(d => d.Legend.Contains("produção")).Any() ? 1 : 2);
            nota.NFe.infNFe.ide.finNFe = GetFinNFe(int.Parse(GetValue(dadosNfe, "Finalidade").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim()));
            nota.NFe.infNFe.ide.procEmi = GetProcEmi(int.Parse(GetValue(dadosNfe, "Processo").Split(new[] { "-" }, StringSplitOptions.None)[0].Trim()));
            nota.NFe.infNFe.ide.verProc = GetValue(dadosNfe, "Versão do Processo").Trim();

            nota.NFe.Signature = new SignatureType();
            nota.NFe.Signature.SignedInfo = new SignedInfoType();
            nota.NFe.Signature.SignedInfo.CanonicalizationMethod = new SignedInfoTypeCanonicalizationMethod();
            nota.NFe.Signature.SignedInfo.SignatureMethod = new SignedInfoTypeSignatureMethod();
            nota.NFe.Signature.SignedInfo.Reference = new ReferenceType();
            var transforms = new List<TransformType>();
            transforms.Add(new TransformType { Algorithm = TTransformURI.httpwwww3org200009xmldsigenvelopedsignature });
            transforms.Add(new TransformType { Algorithm = TTransformURI.httpwwww3orgTR2001RECxmlc14n20010315 });
            nota.NFe.Signature.SignedInfo.Reference.Transforms = transforms.ToArray();
            nota.NFe.Signature.SignedInfo.Reference.URI = string.Format("#{0}", nota.NFe.infNFe.Id);
            nota.NFe.Signature.SignedInfo.Reference.DigestMethod = new ReferenceTypeDigestMethod();
            nota.NFe.Signature.SignedInfo.Reference.DigestValue = Convert.FromBase64String(GetValue(dadosNfe, "Digest Value da NF-e"));
            nota.NFe.Signature.SignatureValue = new SignatureValueType();
            nota.NFe.Signature.SignatureValue.Value = Convert.FromBase64String("7jypESxiNaQCsktIU9bl+fdyFQs0sng/t2vVMhP9M/6jsXW33s3i2mMqrzVDoo19f9vR86F2exnaNQB4byMMQSZ/71yJ9RKkmS9CO6OBTxQ8lBXIzg90k3nmYijxl7hA6VvMwyFGZC1rgGI0P0ciLgX3Ej4Y84WZpO/JHrvc09A82H8Sm4UIewmPRqfJ+y6PTBr3inSLSIe/DwpVFR00Z23yq7vy3O4+iQIisHlVFbeJ3a5Hct6oKdCuKozmFL11M4PtDH85j1E4RubPO5QJ8lHJk/EmtUU4CkJ7U1FqyGivRBcRMy4p04F16rZ2yo7ng1of/fI2Y6ZcIdzy1fCSWQ==");
            nota.NFe.Signature.KeyInfo = new KeyInfoType();
            nota.NFe.Signature.KeyInfo.X509Data = new X509DataType();
            nota.NFe.Signature.KeyInfo.X509Data.X509Certificate = Convert.FromBase64String("MIIIWjCCBkKgAwIBAgIQRFokGgIPJKQZKn93udtj7TANBgkqhkiG9w0BAQsFADB4MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE2MDQGA1UECxMtU2VjcmV0YXJpYSBkYSBSZWNlaXRhIEZlZGVyYWwgZG8gQnJhc2lsIC0gUkZCMRwwGgYDVQQDExNBQyBDZXJ0aXNpZ24gUkZCIEc0MB4XDTEyMDgxNzAwMDAwMFoXDTE1MDgxNjIzNTk1OVowgf8xCzAJBgNVBAYTAkJSMRMwEQYDVQQKFApJQ1AtQnJhc2lsMQswCQYDVQQIEwJTUDEUMBIGA1UEBxQLU0FOVE8gQU5EUkUxNjA0BgNVBAsULVNlY3JldGFyaWEgZGEgUmVjZWl0YSBGZWRlcmFsIGRvIEJyYXNpbCAtIFJGQjEWMBQGA1UECxQNUkZCIGUtQ05QSiBBMzEhMB8GA1UECxQYQXV0ZW50aWNhZG8gcG9yIEFSIEFjaXNhMUUwQwYDVQQDEzxURUMgUEFOIElORFVTVFJJQSBFIENPTUVSQ0lPIERFIFBMQVNUSUNPIExUREE6NTkzNTc3NDkwMDAxMzMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD36llPg+ZJo+JLldBV5NzD/Xfr/bRX4EfEBrwra7DoLoR+/gP3HX8qLW02jWz/zv8ngOtONLZ+K213cU6+xwcijA/IKjBNbgecs+HUr15LHSSfIHMaoEggwHVAmCFazXEONp3vS5x39wEy3nx51kDp59ii/rWLlAakHFxIPBUbw//ghxcpuzwMEUY+DJHfxLJ6X5P0bjXQA3960PuK1hLseRnfULGwcq57VDIsf6U4do1JkC44QvhHPRHq2gQF3u6WEe7sWJ1Lms4dwhnAiPRCumLarOiR/HpeuknItiIyq7F2nDkZp5/xrbWJtYUxsDByV4SwpEvwOmAmemRyX76JAgMBAAGjggNWMIIDUjCBuwYDVR0RBIGzMIGwoD0GBWBMAQMEoDQEMjI4MDQxOTUyNjczMzk2NjI4MDQwMDAwMDAwMDAwMDAwMDAwMDA1NDc3ODg3NlNTUFNQoCMGBWBMAQMCoBoEGE1BVElMREUgVkFSR0FTIFJPRFJJR1VFU6AZBgVgTAEDA6AQBA41OTM1Nzc0OTAwMDEzM6AXBgVgTAEDB6AOBAwwMDAwMDAwMDAwMDCBFmFkbS5tYXJhQHRlY3Bhbi5jb20uYnIwCQYDVR0TBAIwADAfBgNVHSMEGDAWgBQukerWbeWyWYLcOIUpdjQWVjzQPjAOBgNVHQ8BAf8EBAMCBeAwfwYDVR0gBHgwdjB0BgZgTAECAwYwajBoBggrBgEFBQcCARZcaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9kcGMvQUNfQ2VydGlzaWduX1JGQi9EUENfQUNfQ2VydGlzaWduX1JGQi5wZGYwggEWBgNVHR8EggENMIIBCTBXoFWgU4ZRaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9sY3IvQUNDZXJ0aXNpZ25SRkJHNC9MYXRlc3RDUkwuY3JsMFagVKBShlBodHRwOi8vaWNwLWJyYXNpbC5vdXRyYWxjci5jb20uYnIvcmVwb3NpdG9yaW8vbGNyL0FDQ2VydGlzaWduUkZCRzQvTGF0ZXN0Q1JMLmNybDBWoFSgUoZQaHR0cDovL3JlcG9zaXRvcmlvLmljcGJyYXNpbC5nb3YuYnIvbGNyL0NlcnRpc2lnbi9BQ0NlcnRpc2lnblJGQkc0L0xhdGVzdENSTC5jcmwwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMIGbBggrBgEFBQcBAQSBjjCBizBfBggrBgEFBQcwAoZTaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9jZXJ0aWZpY2Fkb3MvQUNfQ2VydGlzaWduX1JGQl9HNC5wN2MwKAYIKwYBBQUHMAGGHGh0dHA6Ly9vY3NwLmNlcnRpc2lnbi5jb20uYnIwDQYJKoZIhvcNAQELBQADggIBAMOL0qvHUhyXEBkrI+EoDGcr/+W1sYHji5htxIQKyO/l/b4Pv0VPUinEOeE+DPsZKd5k75XpKlGnNJtYb5vrCyTDAOC7kyWEwd4yhOiuhB2l3tcxIcGIu524rcOrNawsRPKOOnMOsY+pGGWvKFmVaXG6YoPlcEhRaoplh35VJW5sZcsaZzh5L2iT+o80jBeJBYws1uE+ZAMEQEAaIXOLr7TpkR1nZkeQHIreeR475RifNZUa8vMb9WzvTMtLLAKnlAn0sqSScOlDhxA8hTkmlIXr+saPdsiFimrkgqq30UjArbJY0qqVxYo9lQHBQaVctH2sI+X0bdAwH3J+TG0uWv42c0WoXQ0zQ5gce3BoVBLiVS1j251yjDPaizRiu2A3EnD8NSnVopppOqWSvkUaImdQ6WVXCNGeUt0oMMezwplrr+srsW8YA9LVCpkh13oaQw8XrXclrTKIM50qbHcEkWTGf6nOARmwPSU3E+ho5gee1NbZNdsHDuyArKhSTFaC7pce9L0pReY2XqSwr0VjEYHUCdprc1P/PwL4zzyVTbt7XxDLJhXHQHb9tem6uLi6/IpJkhHi2Dut4XY/nYlo261IizCRJG7VSGxqJ2WdzSM6EwW51E1rMdYnryEEUDeELajauXs3o9qAIGSeGFBwKMQr8M93e35ozcgCRL+BoGWj");

            nota.protNFe = new TProtNFe();
            nota.protNFe.versao = "2.00";
            nota.protNFe.infProt = new TProtNFeInfProt();
            nota.protNFe.infProt.tpAmb = TAmb.Producao;
            nota.protNFe.infProt.chNFe = chaveNfe;
            nota.protNFe.infProt.dhRecbto = DateTime.Now;
            nota.protNFe.infProt.nProt = GetValue(dadosNfe, "Protocolo");
            nota.protNFe.infProt.digVal = Convert.FromBase64String(GetValue(dadosNfe, "Digest Value da NF-e"));
            nota.protNFe.infProt.cStat = "100";
            nota.protNFe.infProt.xMotivo = "Autorizado o uso da NF-e";
            nota.protNFe.infProt.verAplic = "SP_NFE_PL_006q";
        }
Пример #17
0
 public virtual void SaveToFile(string fileName, TNfeProc nfe, System.Text.Encoding encoding)
 {
     System.IO.StreamWriter streamWriter = null;
     try
     {
         string xmlString = Serialize(nfe, encoding);
         streamWriter = new System.IO.StreamWriter(fileName, false, Encoding.UTF8);
         streamWriter.WriteLine(xmlString);
         streamWriter.Close();
     }
     finally
     {
         if ((streamWriter != null))
         {
             streamWriter.Dispose();
         }
     }
 }
Пример #18
0
 private void UpdateTotais(TNfeProc nota, IList<PostResultItem> totais)
 {
     nota.NFe.infNFe.total = new TNFeInfNFeTotal();
     nota.NFe.infNFe.total.ICMSTot = new TNFeInfNFeTotalICMSTot();
     nota.NFe.infNFe.total.ICMSTot.vBC = double.Parse(GetValue(totais, "Base de Cálculo ICMS")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vICMS = double.Parse(GetValue(totais, "Valor do ICMS")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vBCST = double.Parse(GetValue(totais, "Base de Cálculo ICMS ST")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vST = double.Parse(GetValue(totais, "Valor ICMS Substituição")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vProd = double.Parse(GetValue(totais, "Valor Total dos Produtos")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vFrete = double.Parse(GetValue(totais, "Valor do Frete")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vSeg = double.Parse(GetValue(totais, "Valor do Seguro")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vDesc = double.Parse(GetValue(totais, "Valor Total dos Descontos")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vII = double.Parse(GetValue(totais, "Valor Total do II")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vIPI = double.Parse(GetValue(totais, "Valor Total do IPI")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vPIS = double.Parse(GetValue(totais, "Valor do PIS")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vCOFINS = double.Parse(GetValue(totais, "Valor da COFINS")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vOutro = double.Parse(GetValue(totais, "Outras Despesas Acessórias")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ICMSTot.vNF = double.Parse(GetValue(totais, "Valor Total da NFe")).ToString("0.00", enUs);
     nota.NFe.infNFe.total.ISSQNtot = new TNFeInfNFeTotalISSQNtot();
 }
Пример #19
0
 /// <summary>
 /// Deserializes xml markup from file into an TNfeProc object
 /// </summary>
 /// <param name="fileName">string xml file to load and deserialize</param>
 /// <param name="obj">Output TNfeProc object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool LoadFromFile(string fileName, System.Text.Encoding encoding, out TNfeProc obj, out System.Exception exception)
 {
     exception = null;
     obj = default(TNfeProc);
     try
     {
         obj = LoadFromFile(fileName, encoding);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }