public Invoice() { _id = -1; _provider = new Enterprise(); _client = new Enterprise(); _providerInvoiceId = String.Empty; _paymentAmount = 0; _invoiceDate = DateTime.MinValue; _receptionDate = DateTime.Now; _annotations = String.Empty; _binaryFile = null; _fileExtension = InvoiceFileExtension.INVALID; _currency = Currency.MXN; _currencySymbol = "MXN"; _exchangeRate = 1; _originalPaymentAmount = 0; _uid = null; _paymentMethods = new List <PaymentMethod>(); }
public Invoice(byte[] fileBytes, DateTime Open_From_Date, DateTime Open_To_Date, int _openYearFrom, int _openYearTo, string _UAGRFC) { string emisorNombre = ""; string emisorRFC = ""; string receptorNombre = ""; string receptorRFC = ""; string comprobanteTotal = ""; string comprobanteFolio = ""; string comprobantemetodoPago = ""; string comprobanteFecha = ""; _openFromDate = Open_From_Date; _openToDate = Open_To_Date; _id = -1; _receptionDate = DateTime.Now; _annotations = String.Empty; _binaryFile = fileBytes; _fileSize = _binaryFile.Length; _fileExtension = InvoiceFileExtension.INVALID; _paymentMethods = new List <PaymentMethod>(); UAGRFC = _UAGRFC; OpenYearFrom = _openYearFrom; OpenYearTo = _openYearTo; System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(_binaryFile); if (memoryStream.CanSeek) { byte[] bof = new byte[4]; memoryStream.Read(bof, 0, 4); /* * EF BB BF = utf-8 * FF FE = ucs-2le, ucs-4le, and ucs-16le * FE FF = utf-16 and ucs-2 * 00 00 FE FF = ucs-4 */ if (((bof[0] == 0xEF) && (bof[1] == 0xBB) && (bof[2] == 0xBF)) || ((bof[0] == 0xFF) && (bof[1] == 0xFE)) || ((bof[0] == 0xFE) && (bof[1] == 0xFF)) || ((bof[0] == 0x0) && (bof[1] == 0x0) && (bof[2] == 0xFE) && (bof[3] == 0xFF))) { if ((bof[0] == 0xEF) && (bof[1] == 0xBB) && (bof[2] == 0xBF)) { _encoding = "UTF-8"; } else { _encoding = "Unicode"; } } else { if ((bof[0] == 0x3C) && (bof[1] == 0x3F) && (bof[2] == 0x78) && (bof[3] == 0x6D)) { _encoding = "UTF-8"; _fileExtension = InvoiceFileExtension.XML; } else { _encoding = "ASCII"; if ((bof[0] == 0x25) && (bof[1] == 0x50) && (bof[2] == 0x44) && (bof[3] == 0x46)) { _fileExtension = InvoiceFileExtension.PDF; _currency = Currency.MXN; _exchangeRate = 1; } } } memoryStream.Seek(0, System.IO.SeekOrigin.Begin); if (!String.IsNullOrEmpty(_encoding) && (_fileExtension != InvoiceFileExtension.PDF)) { _cfdi = new System.Xml.XmlDocument(); try { System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(memoryStream); _cfdi.Load(reader); } catch (Exception) { memoryStream.Seek(0, System.IO.SeekOrigin.Begin); _encoding = "ISO-8859-1"; System.IO.StreamReader streamReader = new System.IO.StreamReader(memoryStream, System.Text.Encoding.GetEncoding(_encoding)); try { _cfdi.Load(streamReader); } catch (Exception) { _fileExtension = InvoiceFileExtension.INVALID; return; } } _fileExtension = InvoiceFileExtension.XML; try { System.Xml.XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(cfdi.NameTable); nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3"); nsmgr.AddNamespace("tfd", "http://www.sat.gob.mx/TimbreFiscalDigital"); System.Xml.XmlNode xmlVoucher = cfdi.SelectSingleNode("cfdi:Comprobante", nsmgr); System.Xml.XmlNode xmlClient = cfdi.SelectSingleNode("cfdi:Comprobante/cfdi:Receptor", nsmgr); System.Xml.XmlNode xmlProvider = cfdi.SelectSingleNode("cfdi:Comprobante/cfdi:Emisor", nsmgr); System.Xml.XmlNode xmlTimbreFiscal = cfdi.SelectSingleNode("cfdi:Comprobante/cfdi:Complemento/tfd:TimbreFiscalDigital", nsmgr); if (xmlVoucher.Attributes["version"] == null) { //version 3.3 if (xmlVoucher.Attributes["Version"] != null) { emisorNombre = "Nombre"; emisorRFC = "Rfc"; receptorNombre = "Nombre"; receptorRFC = "Rfc"; comprobanteTotal = "Total"; comprobanteFolio = "Folio"; comprobantemetodoPago = "MetodoPago"; comprobanteFecha = "Fecha"; } } else { //version 3.2 or last emisorNombre = "nombre"; emisorRFC = "rfc"; receptorNombre = "nombre"; receptorRFC = "rfc"; comprobanteTotal = "total"; comprobanteFolio = "folio"; comprobantemetodoPago = "metodoDePago"; comprobanteFecha = "fecha"; } provider = new Enterprise(xmlProvider.Attributes[emisorNombre] == null ? xmlProvider.Attributes[emisorRFC].Value.Trim() : xmlProvider.Attributes[emisorNombre].Value, xmlProvider.Attributes[emisorRFC].Value.Trim()); _providerNameSpecified = xmlProvider.Attributes[emisorNombre] != null; client = new Enterprise(xmlClient.Attributes[receptorNombre] == null ? xmlClient.Attributes[receptorRFC].Value.Trim() : xmlClient.Attributes[receptorNombre].Value, xmlClient.Attributes[receptorRFC].Value.Trim()); _originalPaymentAmount = xmlVoucher.Attributes[comprobanteTotal] == null ? 0 : Double.Parse(xmlVoucher.Attributes[comprobanteTotal].Value); providerInvoiceId = xmlVoucher.Attributes[comprobanteFolio] == null ? "" : xmlVoucher.Attributes[comprobanteFolio].Value; if (xmlClient.Attributes[receptorRFC].Value == UAGRFC) { client.id = 1; } _currency = xmlVoucher.Attributes["Moneda"] == null ? Currency.MXN : ((xmlVoucher.Attributes["Moneda"].Value.ToUpper().Replace(".", "").Trim().Contains("MX") || (xmlVoucher.Attributes["Moneda"].Value.ToUpper().Replace(".", "").Trim().Equals("MN") || xmlVoucher.Attributes["Moneda"].Value.ToUpper().Replace("É", "E").Trim().Contains("MEX")) ? Currency.MXN : Currency.OTHER)); if (_currency == Currency.MXN) { _exchangeRate = 1; } else { if (xmlVoucher.Attributes["TipoCambio"] == null) { _exchangeRate = 0; } else { Double.TryParse(xmlVoucher.Attributes["TipoCambio"].Value, NumberStyles.Currency, CultureInfo.CreateSpecificCulture("es-MX"), out _exchangeRate); if ((_exchangeRate == 0) && (xmlVoucher.Attributes["TipoCambio"].Value.ToUpper().Contains("PESOS"))) { _exchangeRate = 1; } } } _currencySymbol = xmlVoucher.Attributes["Moneda"] == null ? "MXN" : xmlVoucher.Attributes["Moneda"].Value; paymentAmount = _originalPaymentAmount * _exchangeRate; invoiceDate = System.Xml.XmlConvert.ToDateTime(xmlVoucher.Attributes[comprobanteFecha].Value, System.Xml.XmlDateTimeSerializationMode.Utc); uid = Guid.Parse(xmlTimbreFiscal.Attributes["UUID"].Value.ToUpper()); //version 3.2 or last string[] tmpPaymentMethods = xmlVoucher.Attributes[comprobantemetodoPago] == null ? new string[0] : xmlVoucher.Attributes[comprobantemetodoPago].Value.Split(','); foreach (string tmpPaymentMethod in tmpPaymentMethods) { paymentMethods.Add(new PaymentMethod(tmpPaymentMethod)); } } catch (Exception) { _fileExtension = InvoiceFileExtension.INVALID; } } } }