public Cana() { qTotMes_Tipo = TpcnTipoCampo.tcDec10; qTotAnt_Tipo = TpcnTipoCampo.tcDec10; qTotGer_Tipo = TpcnTipoCampo.tcDec10; fordia = new List<fordia>(); deduc = new List<deduc>(); }
public Cana() { qTotMes_Tipo = TpcnTipoCampo.tcDec10; qTotAnt_Tipo = TpcnTipoCampo.tcDec10; qTotGer_Tipo = TpcnTipoCampo.tcDec10; fordia = new List <fordia>(); deduc = new List <deduc>(); }
public Prod() { vUnCom_Tipo = TpcnTipoCampo.tcDec10; vUnTrib_Tipo = TpcnTipoCampo.tcDec10; indTot = TpcnIndicadorTotal.itSomaTotalNFe; DI = new List<DI>(); med = new List<Med>(); arma = new List<Arma>(); detExport = new List<detExport>(); }
public Prod() { vUnCom_Tipo = TpcnTipoCampo.tcDec10; vUnTrib_Tipo = TpcnTipoCampo.tcDec10; indTot = TpcnIndicadorTotal.itSomaTotalNFe; DI = new List <DI>(); med = new List <Med>(); arma = new List <Arma>(); detExport = new List <detExport>(); }
private object LerCampo(TpcnTipoCampo Tipo, TpcnResources tag, ObOp optional, int minLength, int maxLength, bool trim) { int nDecimais = 0; string ConteudoTag = ""; try { ConteudoTag = RetornarConteudoTag(tag.ToString()); if (ConteudoTag != "") if (ConteudoTag.StartsWith(prefix)) ConteudoTag = ""; int len = ConteudoTag.Length; if (len == 0 && optional == ObOp.Opcional) { } else { switch (Tipo) { case TpcnTipoCampo.tcHor: maxLength = minLength = 8; //hh:mm:ss break; case TpcnTipoCampo.tcDatYYYY_MM_DD: maxLength = minLength = 10; //yyyy-MM-dd break; case TpcnTipoCampo.tcDatYYYYMMDD: maxLength = minLength = 8; //yyyyMMdd break; case TpcnTipoCampo.tcDatHor: maxLength = minLength = 19; //aaaa-mm-dd hh:mm:ss break; /* case TpcnTipoCampo.tcDec2: nDecimais = 2; break; case TpcnTipoCampo.tcDec3: nDecimais = 3; break; case TpcnTipoCampo.tcDec4: nDecimais = 4; break; case TpcnTipoCampo.tcDec10: nDecimais = 10; break;*/ default: if (Tipo >= TpcnTipoCampo.tcDec2 && Tipo <= TpcnTipoCampo.tcDec10) nDecimais = (int)Tipo; break; } if (len == 0 && minLength > 0) { this.cMensagemErro += "Layout: " + this.layout.Replace(prefix, "") + Environment.NewLine; this.cMensagemErro += string.Format("Segmento [{0}]: tag <{1}> deve ser informada.\r\n" + "\tLinha: {2}: Conteudo do segmento: {3}", this.FSegmento, tag.ToString(), this.LinhaLida, this.Registro.Substring(1)) + Environment.NewLine; } else { switch (Tipo) { case TpcnTipoCampo.tcDec2: case TpcnTipoCampo.tcDec3: case TpcnTipoCampo.tcDec4: case TpcnTipoCampo.tcDec5: case TpcnTipoCampo.tcDec6: case TpcnTipoCampo.tcDec7: case TpcnTipoCampo.tcDec8: case TpcnTipoCampo.tcDec9: case TpcnTipoCampo.tcDec10: //quando numerico do tipo double não consiste o tamanho minimo nem maximo break; default: if ((len > maxLength || len < minLength) && (maxLength + minLength > 0)) { this.cMensagemErro += "Layout: " + this.layout.Replace(prefix, "") + Environment.NewLine; this.cMensagemErro += string.Format("Segmento [{0}]: tag <{1}> deve ter seu tamanho entre {2} e {3}. Conteudo: {4}" + "\r\n\tLinha: {5}: Conteudo do segmento: {6}", this.FSegmento, tag.ToString(), minLength, maxLength, ConteudoTag, this.LinhaLida, this.Registro.Substring(1)) + Environment.NewLine; } break; } } } if (optional == ObOp.Obrigatorio || (optional == ObOp.Opcional && len != 0)) { switch (Tipo) { case TpcnTipoCampo.tcDec2: case TpcnTipoCampo.tcDec3: case TpcnTipoCampo.tcDec4: case TpcnTipoCampo.tcDec5: case TpcnTipoCampo.tcDec6: case TpcnTipoCampo.tcDec7: case TpcnTipoCampo.tcDec8: case TpcnTipoCampo.tcDec9: case TpcnTipoCampo.tcDec10: { int pos = ConteudoTag.IndexOf(".") + 1; int ndec = ConteudoTag.Substring(pos).Length; string xdec = ConteudoTag.Substring(pos); // // ajusta o numero de casas decimais while (ndec > nDecimais) { if (xdec.Substring(ndec - 1, 1) == "0") --ndec; else break; } if (ndec > nDecimais) { this.cMensagemErro += "Layout: " + this.layout.Replace(prefix, "") + Environment.NewLine; this.cMensagemErro += string.Format("Segmento [{0}]: tag <{1}> número de casas decimais deve ser de {2} e existe(m) {3}" + "\r\n\tLinha: {4}: Conteudo do segmento: {5}", this.FSegmento, tag.ToString(), nDecimais, ndec, this.LinhaLida, this.Registro.Substring(1)) + Environment.NewLine; } #region -- atribui o numero de casas decimais que serão gravadas if (ndec < (int)TpcnTipoCampo.tcDec2 || ndec > (int)TpcnTipoCampo.tcDec10) ndec = (int)TpcnTipoCampo.tcDec2; TpcnTipoCampo tipo = (TpcnTipoCampo)ndec; if (tag == TpcnResources.vUnCom) { NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = tipo; /* switch (ndec) { case 2: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec4; break; case 5: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec5; break; case 6: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec6; break; case 10: NFe.det[NFe.det.Count - 1].Prod.vUnCom_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } if (tag == TpcnResources.vUnTrib) { NFe.det[NFe.det.Count - 1].Prod.vUnTrib_Tipo = tipo; /* switch (ndec) { case 2: NFe.det[NFe.det.Count - 1].Prod.vUnTrib_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.det[NFe.det.Count - 1].Prod.vUnTrib_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.det[NFe.det.Count - 1].Prod.vUnTrib_Tipo = TpcnTipoCampo.tcDec4; break; case 10: NFe.det[NFe.det.Count - 1].Prod.vUnTrib_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } if (tag == TpcnResources.qTotMes) { NFe.cana.qTotMes_Tipo = tipo; /* switch (ndec) { case 2: NFe.cana.qTotMes_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.cana.qTotMes_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.cana.qTotMes_Tipo = TpcnTipoCampo.tcDec4; break; case 10: NFe.cana.qTotMes_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } if (tag == TpcnResources.qTotAnt) { NFe.cana.qTotAnt_Tipo = tipo; /* switch (ndec) { case 2: NFe.cana.qTotAnt_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.cana.qTotAnt_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.cana.qTotAnt_Tipo = TpcnTipoCampo.tcDec4; break; case 10: NFe.cana.qTotAnt_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } if (tag == TpcnResources.qTotGer) { NFe.cana.qTotGer_Tipo = tipo; /* switch (ndec) { case 2: NFe.cana.qTotGer_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.cana.qTotGer_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.cana.qTotGer_Tipo = TpcnTipoCampo.tcDec4; break; case 10: NFe.cana.qTotGer_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } if (tag == TpcnResources.qtde) { NFe.cana.fordia[NFe.cana.fordia.Count - 1].qtde_Tipo = tipo; /* switch (ndec) { case 2: NFe.cana.fordia[NFe.cana.fordia.Count - 1].qtde_Tipo = TpcnTipoCampo.tcDec2; break; case 3: NFe.cana.fordia[NFe.cana.fordia.Count - 1].qtde_Tipo = TpcnTipoCampo.tcDec3; break; case 4: NFe.cana.fordia[NFe.cana.fordia.Count - 1].qtde_Tipo = TpcnTipoCampo.tcDec4; break; case 10: NFe.cana.fordia[NFe.cana.fordia.Count - 1].qtde_Tipo = TpcnTipoCampo.tcDec10; break; }*/ } #endregion } break; } } switch (Tipo) { case TpcnTipoCampo.tcDatYYYYMMDD: return this.getDate2(Tipo, ConteudoTag); case TpcnTipoCampo.tcDatYYYY_MM_DD: case TpcnTipoCampo.tcDatHor: return this.getDateTime(Tipo, ConteudoTag); case TpcnTipoCampo.tcHor: return this.getTime(ConteudoTag); case TpcnTipoCampo.tcDec2: case TpcnTipoCampo.tcDec3: case TpcnTipoCampo.tcDec4: case TpcnTipoCampo.tcDec5: case TpcnTipoCampo.tcDec6: case TpcnTipoCampo.tcDec7: case TpcnTipoCampo.tcDec8: case TpcnTipoCampo.tcDec9: case TpcnTipoCampo.tcDec10: return Convert.ToDouble("0" + ConteudoTag.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)); case TpcnTipoCampo.tcInt: return Convert.ToInt32("0" + SomenteNumeros(ConteudoTag)); default: return (trim ? ConteudoTag.Trim() : ConteudoTag); } } catch (Exception ex) { this.cMensagemErro += "Layout: " + this.layout.Replace(prefix, "") + Environment.NewLine; this.cMensagemErro += string.Format("Segmento [{0}]: tag <{1}> Conteudo: {2}\r\n" + "\tLinha: {3}: Conteudo do segmento: {4}\r\n\tMensagem de erro: {5}", this.FSegmento, tag.ToString(), ConteudoTag, this.LinhaLida, this.Registro.Substring(1), ex.Message) + Environment.NewLine; switch (Tipo) { case TpcnTipoCampo.tcHor: case TpcnTipoCampo.tcDatYYYY_MM_DD: case TpcnTipoCampo.tcDatYYYYMMDD: case TpcnTipoCampo.tcDatHor: return DateTime.MinValue; case TpcnTipoCampo.tcDec2: case TpcnTipoCampo.tcDec3: case TpcnTipoCampo.tcDec4: case TpcnTipoCampo.tcDec5: case TpcnTipoCampo.tcDec6: case TpcnTipoCampo.tcDec7: case TpcnTipoCampo.tcDec8: case TpcnTipoCampo.tcDec9: case TpcnTipoCampo.tcDec10: return 0.0; case TpcnTipoCampo.tcInt: return 0; default: return ""; } } }
private double LerDouble(TpcnTipoCampo Tipo, TpcnResources tag, ObOp optional, int minLength, int maxLength) { return (double)this.LerCampo(Tipo, tag, optional, minLength, maxLength, true); }
/// <summary> /// getDateTime2 /// </summary> public DateTime getDate2(TpcnTipoCampo Tipo, string value) { if (string.IsNullOrEmpty(value)) return DateTime.MinValue; if (value.Contains("-")) return this.getDateTime(Tipo, value); try { int _ano = Convert.ToInt16(value.Substring(0, 4)); int _mes = Convert.ToInt16(value.Substring(4, 2)); int _dia = Convert.ToInt16(value.Substring(6, 2)); return new DateTime(_ano, _mes, _dia); } catch { throw new Exception("Data inválida do conteudo [" + value + "]"); } }
/// <summary> /// getDateTime /// </summary> public DateTime getDateTime(TpcnTipoCampo Tipo, string value) { if (string.IsNullOrEmpty(value)) return DateTime.MinValue; try { int _ano = Convert.ToInt16(value.Substring(0, 4)); int _mes = Convert.ToInt16(value.Substring(5, 2)); int _dia = Convert.ToInt16(value.Substring(8, 2)); if (Tipo == TpcnTipoCampo.tcDatHor && value.Contains(":")) { int _hora = Convert.ToInt16(value.Substring(11, 2)); int _min = Convert.ToInt16(value.Substring(14, 2)); int _seg = Convert.ToInt16(value.Substring(17, 2)); return new DateTime(_ano, _mes, _dia, _hora, _min, _seg); } return new DateTime(_ano, _mes, _dia); } catch { throw new Exception("Data inválida do conteudo [" + value + "]"); } }
/// <summary> /// GerarXml /// </summary> /// <param name="NFe"></param> public void GerarXml(NFe NFe, string folderDestino) { doc = new XmlDocument(); XmlDeclaration node = doc.CreateXmlDeclaration("1.0", "UTF-8", ""); doc.InsertBefore(node, doc.DocumentElement); /// /// NFe /// XmlNode xmlInf = doc.CreateElement("NFe"); //if (NFe.ide.cUF != 29) //Bahia //{ // XmlAttribute xmlVersion = doc.CreateAttribute("xmlns:xsi"); // xmlVersion.Value = "http://www.w3.org/2001/XMLSchema-instance"; // xmlInf.Attributes.Append(xmlVersion); //} XmlAttribute xmlVersion1 = doc.CreateAttribute("xmlns"); xmlVersion1.Value = NFeStrConstants.NAME_SPACE_NFE;// !string.IsNullOrEmpty(Propriedade.nsURI_nfe) ? Propriedade.nsURI_nfe : "http://www.portalfiscal.inf.br/nfe"; xmlInf.Attributes.Append(xmlVersion1); doc.AppendChild(xmlInf); string cChave = NFe.ide.cUF.ToString() + NFe.ide.dEmi.Year.ToString("0000").Substring(2) + NFe.ide.dEmi.Month.ToString("00"); //data AAMM if (NFe.infNFe.Versao >= 3) cChave = NFe.ide.cUF.ToString() + NFe.ide.dhEmi.Substring(2, 2) + NFe.ide.dhEmi.Substring(5, 2); //data AAMM Int64 iTmp = Convert.ToInt64("0" + NFe.emit.CNPJ + NFe.emit.CPF); cChave += iTmp.ToString("00000000000000"); cChave += Convert.ToInt32(NFe.ide.mod).ToString("00"); if (NFe.ide.cNF == 0) { /// /// gera codigo aleatorio /// NFe.ide.cNF = this.GerarCodigoNumerico(NFe.ide.nNF); } string ccChave = cChave + NFe.ide.serie.ToString("000") + NFe.ide.nNF.ToString("000000000") + ((int)NFe.ide.tpEmis).ToString("0") + NFe.ide.cNF.ToString("00000000"); if (NFe.ide.cDV == 0) { /// /// calcula digito verificador /// NFe.ide.cDV = this.GerarDigito(ccChave); } else { int ccDV = this.GerarDigito(ccChave); if (NFe.ide.cDV != ccDV) throw new Exception(string.Format("Digito verificador informado, [{0}] é diferente do calculado, [{1}]", NFe.ide.cDV, ccDV)); } cChave += NFe.ide.serie.ToString("000") + NFe.ide.nNF.ToString("000000000") + ((int)NFe.ide.tpEmis).ToString("0") + NFe.ide.cNF.ToString("00000000") + NFe.ide.cDV.ToString("0"); NFe.infNFe.ID = cChave; /// /// infNFe /// if (NFe.infNFe.Versao == 0) NFe.infNFe.Versao = Convert.ToDecimal(versoes.VersaoXMLNFe.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)); nDecimaisPerc = (double)NFe.infNFe.Versao >= 3.10 ? TpcnTipoCampo.tcDec4 : TpcnTipoCampo.tcDec2; XmlElement infNfe = doc.CreateElement("infNFe"); XmlAttribute infNfeAttr1 = doc.CreateAttribute(TpcnResources.versao.ToString()); infNfeAttr1.Value = NFe.infNFe.Versao.ToString("0.00").Replace(",", "."); infNfe.Attributes.Append(infNfeAttr1); XmlAttribute infNfeAttrId = doc.CreateAttribute(TpcnResources.Id.ToString()); infNfeAttrId.Value = "NFe" + NFe.infNFe.ID; infNfe.Attributes.Append(infNfeAttrId); xmlInf.AppendChild(infNfe); this.convertToOem = true; infNfe.AppendChild(GerarIde(NFe)); infNfe.AppendChild(GerarEmit(NFe)); GerarAvulsa(NFe, infNfe); XmlNode nodeDest = GerarDest(NFe); if (nodeDest != null && nodeDest.HasChildNodes) ///danasa 6/2014 infNfe.AppendChild(nodeDest); ///somente grava o nó "dest" se exibir GerarRetirada(NFe, infNfe); GerarEntrega(NFe, infNfe); GerarautXML(NFe, infNfe); GerarDet(NFe, infNfe); GerarTotal(NFe, infNfe); GerarTransp(NFe.Transp, infNfe); GerarCobr(NFe.Cobr, infNfe); if (NFe.infNFe.Versao >= 3 && NFe.ide.mod == TpcnMod.modNFCe) GerarPag(NFe, infNfe); GerarInfAdic(NFe.InfAdic, infNfe); GerarExporta(NFe, NFe.exporta, infNfe); GerarCompra(NFe.compra, infNfe); GerarCana(NFe.cana, infNfe); this.cFileName = NFe.infNFe.ID + Propriedade.ExtEnvio.Nfe; if (!string.IsNullOrEmpty(folderDestino)) { if (folderDestino.Substring(folderDestino.Length - 1, 1) == @"\") folderDestino = folderDestino.Substring(0, folderDestino.Length - 1); if (!Directory.Exists(Path.Combine(folderDestino, "Convertidos"))) { /// /// cria uma pasta temporária para armazenar o XML convertido /// System.IO.Directory.CreateDirectory(Path.Combine(folderDestino, "Convertidos")); } string strRetorno = this.cFileName; strRetorno = Path.Combine(folderDestino, "Convertidos"); this.cFileName = Path.Combine(strRetorno, this.cFileName); /// /// salva o XML strRetorno = this.cFileName; doc.Save(@strRetorno); } /// /// retorna o conteudo do XML da nfe XMLString = doc.OuterXml; }
private void wCampo(object obj, TpcnTipoCampo Tipo, string TAG, ObOp Obrigatorio, int nAlign) { TAG = TAG.Trim(); if (obj == null) return; if (Tipo == TpcnTipoCampo.tcDatYYYY_MM_DD || Tipo == TpcnTipoCampo.tcDatHor) if (((DateTime)obj).Year == 1) if (Obrigatorio == ObOp.Opcional) return; if (Obrigatorio == ObOp.Opcional) { if (Tipo == TpcnTipoCampo.tcInt) if ((int)obj == 0) return; if (Tipo >= TpcnTipoCampo.tcDec2 && Tipo <= TpcnTipoCampo.tcDec10)// || Tipo == TpcnTipoCampo.tcDec3 || Tipo == TpcnTipoCampo.tcDec4 || Tipo == TpcnTipoCampo.tcDec10) if ((double)obj == 0) return; if (Tipo == TpcnTipoCampo.tcHor) if ((DateTime)obj == DateTime.MinValue) return; if (obj.ToString().Trim() == "") return; } XmlElement valueEl1 = doc.CreateElement(TAG); switch (Tipo) { case TpcnTipoCampo.tcDec2: case TpcnTipoCampo.tcDec3: case TpcnTipoCampo.tcDec4: case TpcnTipoCampo.tcDec5: case TpcnTipoCampo.tcDec6: case TpcnTipoCampo.tcDec7: case TpcnTipoCampo.tcDec8: case TpcnTipoCampo.tcDec9: case TpcnTipoCampo.tcDec10: if (((double)obj) > 0 || Obrigatorio == ObOp.Obrigatorio) { valueEl1.InnerText = ((double)obj).ToString("0." + ("").PadLeft((int)Tipo, '0')).Replace(",", "."); } break; case TpcnTipoCampo.tcDatHor: if (((DateTime)obj).Year > 1) valueEl1.InnerText = ((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ss"); break; case TpcnTipoCampo.tcHor: if (Obrigatorio == ObOp.Opcional && ((DateTime)obj) == DateTime.MinValue) return; valueEl1.InnerText = ((DateTime)obj).ToString("HH:mm:ss"); break; case TpcnTipoCampo.tcDatYYYY_MM_DD: if (((DateTime)obj).Year > 1) valueEl1.InnerText = ((DateTime)obj).ToString("yyyy-MM-dd"); break; case TpcnTipoCampo.tcDatYYYYMMDD: if (((DateTime)obj).Year > 1) valueEl1.InnerText = ((DateTime)obj).ToString("yyyyMMdd"); break; default: if (nAlign > 0) { valueEl1.InnerText = obj.ToString().PadLeft(nAlign, '0'); } else if (Tipo == TpcnTipoCampo.tcInt) { if (((int)obj) != 0 || Obrigatorio == ObOp.Obrigatorio) valueEl1.InnerText = ((int)obj).ToString(); } else if (obj.ToString().Trim() != "") { if (TAG == TpcnResources.xProd.ToString() && !this.convertToOem) valueEl1.InnerText = obj.ToString().TrimStart().TrimEnd(); else valueEl1.InnerText = ConvertToOEM(obj.ToString().TrimStart().TrimEnd()); } break; } nodeCurrent.AppendChild(valueEl1); }
/// <summary> /// wCampo /// </summary> /// <param name="obj"></param> /// <param name="Tipo"></param> /// <param name="TAG"></param> /// <param name="Obrigatorio"></param> /// <param name="nAlign"></param> private void wCampo(object obj, TpcnTipoCampo Tipo, TpcnResources TAG, ObOp Obrigatorio, int nAlign) { wCampo(obj, Tipo, TAG.ToString(), Obrigatorio, nAlign); }
/// <summary> /// wCampo /// </summary> /// <param name="obj"></param> /// <param name="Tipo"></param> /// <param name="TAG"></param> private void wCampo(object obj, TpcnTipoCampo Tipo, TpcnResources TAG) { wCampo(obj, Tipo, TAG.ToString(), ObOp.Obrigatorio, 0); }