public Attachment_BE VALIDATE_XML(ref Attachment_BE attach, string metodoPago, SharedSettings.Settings settings) { try { //Establecemos conexion con SAP RfcConfigParameters rfc = SAP_Connection.GetParameters(settings); RfcDestination rfcDest = null; rfcDest = RfcDestinationManager.GetDestination(rfc); //Creamos repositorio para la función RfcRepository repo = rfcDest.Repository; IRfcFunction validate_mail = repo.CreateFunction("Z_MAIL_VALIDATE_DATA"); validate_mail.SetValue("P_RFC_VENDOR", attach.RFC_VEND); validate_mail.SetValue("P_RFC_COMPANY", attach.RFC_COMP); validate_mail.SetValue("P_MET_PAGO", metodoPago); //Ejecutamos la consulta validate_mail.Invoke(rfcDest); attach.RFC_COMPANY = validate_mail.GetString("ERFC_COMPANY"); attach.RFC_VENDOR = validate_mail.GetString("ERFC_VENDOR"); attach.LIFNR = validate_mail.GetString("ELIFNR"); attach.BUKRS = validate_mail.GetString("EBUKRS"); //attach.ECALLECOMP = validate_mail.GetString("ECALLECOMP"); //attach.E_N_EXTCOMP = validate_mail.GetString("E_N_EXTCOMP"); //attach.ECOLCOMP = validate_mail.GetString("ECOLCOMP"); //attach.EMUNI_COMP = validate_mail.GetString("EMUNI_COMP"); //attach.ECPCOMP = validate_mail.GetString("ECPCOMP"); //attach.EPAISCOMP = validate_mail.GetString("EPAISCOMP"); //attach.ECALLEVEND = validate_mail.GetString("ECALLEVEND"); //attach.E_N_EXTVEND = validate_mail.GetString("E_N_EXTVEND"); //attach.ECOLVEND = validate_mail.GetString("ECOLVEND"); //attach.EMUNI_VEND = validate_mail.GetString("EMUNI_VEND"); //attach.ECPVEND = validate_mail.GetString("ECPVEND"); //attach.EPAISVEND = validate_mail.GetString("EPAISVEND"); //attach.EEDO_COMP = validate_mail.GetString("EEDO_COMP"); //attach.EEDO_VEND = validate_mail.GetString("EEDO_VEND"); attach.Desc_Error = attach.Desc_Error + validate_mail.GetString("E_MET_PAGO"); return(attach); } catch (RfcCommunicationException e) { throw e; } catch (RfcLogonException e) { // user could not logon... throw e; } catch (RfcAbapRuntimeException e) { // serious problem on ABAP system side... throw e; } catch (RfcAbapBaseException e) { // The function module returned an ABAP exception, an ABAP message // or an ABAP class-based exception... throw e; } catch (Exception e) { throw e; } }
private static void Procesar(FileInfo[] fileInfosxml, FileInfo[] fileInfospdf, string clase) { XmlDocument xmlobj = new XmlDocument(); TimbreFiscalDigital timbre = new TimbreFiscalDigital(); Comprobante comprobante = new Comprobante(); Comprobante2 comprobante2 = new Comprobante2(); Nomina nomina = new Nomina(); XmlSerializer serializer; Pagos pagos = new Pagos(); DataLayer dataLayer = new DataLayer(); Attachment_BE attach = new Attachment_BE(); List <Attachment_BE> attachments = new List <Attachment_BE>(); List <Relacionados> relacionados = new List <Relacionados>(); string xml = ""; bool errorxml = false; MostrarMensajeConsola("Se encontraron " + fileInfosxml.Length + "XML y " + fileInfospdf.Length + " PDFs"); for (int i = 0; i < fileInfosxml.Length; i++) { MostrarLinea("*"); MostrarMensajeConsola((i + 1) + " de " + fileInfosxml.Length); MostrarMensajeConsola(fileInfosxml[i].Name); xmlobj.Load(new MemoryStream(cargaArch(fileInfosxml[i].Name, fileInfosxml, false))); attach.XML = xml = xmlobj.InnerXml; if (xml.Contains("Version=\"3.3\"")) { serializer = new XmlSerializer(typeof(Comprobante)); comprobante = (Comprobante)serializer.Deserialize(new StringReader(xml)); serializer = new XmlSerializer(typeof(TimbreFiscalDigital)); timbre = (TimbreFiscalDigital)serializer.Deserialize( new StringReader(comprobante.Complemento[0].Any.Where(x => x.LocalName == "TimbreFiscalDigital").Select(x => x.OuterXml).ToArray()[0])); attach.Desc_Error = EstatusCFDI(comprobante.Emisor.Rfc, comprobante.Receptor.Rfc, comprobante.Total.ToString(), timbre.UUID); attach.WRBTR = decimal.Round(comprobante.Total, 2); if (clase == "P") { attach.EXT = "XML"; if (xml.Contains("pago10:Pagos")) { serializer = new XmlSerializer(typeof(Pagos)); pagos = (Pagos)serializer.Deserialize( new StringReader(comprobante.Complemento[0].Any.Where(x => x.LocalName == "Pagos").Select(x => x.OuterXml).ToArray()[0])); attach.WRBTR = decimal.Round(pagos.Pago.Sum(x => x.Monto), 2); } } else if (clase == "C") { attach.EXT = "CTE"; if (xml.Contains("nomina12:Nomina")) { serializer = new XmlSerializer(typeof(Nomina)); nomina = (Nomina)serializer.Deserialize( new StringReader(comprobante.Complemento[0].Any.Where(x => x.LocalName == "Nomina").Select(x => x.OuterXml).ToArray()[0])); attach.EXT = "NOM"; } } if (comprobante.Impuestos != null) { if (comprobante.Impuestos.TotalImpuestosRetenidos != 0) { attach.RETENCION = "X"; } } attach.RFC_VENDOR = "X"; attach.RFC_COMPANY = "X"; attach.WAERS = comprobante.Moneda; attach.FechaDocumento = comprobante.Fecha; attach.SerFolio = comprobante.Serie + " " + comprobante.Folio; attach.BLDAT = FechaFormatoSAP(comprobante.Fecha); attach.FILE = fileInfosxml[i].Name; attach.RFC_VEND = comprobante.Emisor.Rfc; attach.RFC_COMP = comprobante.Receptor.Rfc; attach.XBLNR = comprobante.Serie + comprobante.Folio; attach.UUID_XML = timbre.UUID.ToUpper(); if (clase == "P") { try { dataLayer.VALIDATE_XML(ref attach, comprobante.MetodoPago, settings); } catch (Exception e) { errorxml = true; //MostrarMensajeConsola(e.Message, false); } } relacionados.Clear(); if (pagos.Pago != null) { for (int j = 0; j < pagos.Pago.Length; j++) { if (pagos.Pago[j].DoctoRelacionado != null) { foreach (var relacionado in pagos.Pago[j].DoctoRelacionado) { if (relacionados.Exists(x => x.UUID == relacionado.IdDocumento.ToUpper()) == false) { relacionados.Add(new Relacionados( attach.BUKRS, timbre.UUID.ToUpper(), relacionado.IdDocumento.ToUpper(), fileInfosxml[i].Name, relacionado.ImpPagado, relacionado.Folio, relacionado.Serie, relacionado.MonedaDR.ToString(), relacionado.MetodoDePagoDR.ToString(), "" )); } } } else { errorxml = true; MostrarMensajeConsola("Coprobante de pago tiene estructura incorrecta"); break; } } } pagos = new Pagos(); } else if (xml.Contains("Version=\"3.2\"")) { serializer = new XmlSerializer(typeof(Comprobante2)); comprobante2 = (Comprobante2)serializer.Deserialize(new StringReader(xml)); serializer = new XmlSerializer(typeof(TimbreFiscalDigital)); timbre = (TimbreFiscalDigital)serializer.Deserialize(new StringReader(comprobante.Complemento[0].ToString())); attach.Desc_Error = EstatusCFDI(comprobante2.Emisor.rfc, comprobante2.Receptor.rfc, comprobante2.total.ToString(), timbre.UUID); if (comprobante2.Impuestos.totalImpuestosRetenidos != 0) { attach.RETENCION = "X"; } attach.EXT = "XML"; attach.RFC_VENDOR = comprobante2.Emisor.rfc; attach.RFC_COMPANY = comprobante2.Receptor.rfc; attach.WRBTR = decimal.Round(comprobante2.total, 2); attach.WAERS = comprobante2.Moneda; attach.FechaDocumento = comprobante2.fecha; attach.SerFolio = comprobante2.serie + " " + comprobante2.folio; attach.BLDAT = FechaFormatoSAP(comprobante2.fecha); attach.FILE = fileInfosxml[i].Name; attach.RFC_VEND = comprobante2.Emisor.rfc; attach.RFC_COMP = comprobante2.Receptor.rfc; attach.XBLNR = comprobante2.serie + comprobante2.folio; attach.UUID_XML = timbre.UUID.ToUpper(); if (clase == "P") { try { dataLayer.VALIDATE_XML(ref attach, comprobante2.metodoDePago, settings); } catch (Exception e) { errorxml = true; //MostrarMensajeConsola(e.Message, false); } } } if (attach.Desc_Error.Contains("Vigente")) { attach.SAT = "X"; attach.RES_PDF = cargaArch(fileInfosxml[i].Name, fileInfospdf, true); } else { attach.XML = ""; } if (attach.RFC_COMPANY == "" && attach.RFC_VENDOR == "") { attach.Desc_Error = attach.Desc_Error + "<@>" + "El RFC de la empresa y proveedor son incorrectos"; MostrarMensajeConsola("El RFC de la empresa y proveedor son incorrectos"); } else { if (attach.RFC_COMPANY == "") { attach.Desc_Error = attach.Desc_Error + "<@>" + "El RFC de la empresa es incorrecto"; MostrarMensajeConsola("El RFC de la empresa es incorrecto"); } if (attach.RFC_VENDOR == "") { attach.Desc_Error = attach.Desc_Error + "<@>" + "El RFC del proveedor es incorrecto"; } } if (attach.XML != "") { MAIL_DATA_BE mail_data = new MAIL_DATA_BE(); attachments.Add(attach); string r = ""; if (errorxml == false) { try { r = dataLayer.SAVE_MAIL_DATA(mail_data, attachments, relacionados, settings); } catch (Exception e) { errorxml = true; //MostrarMensajeConsola(e.Message, false); } } if (errorxml == false) { MostrarMensajeConsola("Información guardada en SAP"); Mover(fileInfosxml[i], attach.EXT, true); if (fileInfospdf.Length > 0) { FileInfo info = fileInfospdf.Where(x => x.Name.Substring(0, x.Name.Length - 4) == (fileInfosxml[i].Name.Substring(0, (fileInfosxml[i].Name.Length - 4)))).SingleOrDefault(); if (info != null) { Mover(info, attach.EXT, true); } } } else { MostrarMensajeConsola(r); Mover(fileInfosxml[i], attach.EXT, false); if (fileInfospdf.Length > 0) { FileInfo info = fileInfospdf.Where(x => x.Name.Substring(0, x.Name.Length - 4) == (fileInfosxml[i].Name.Substring(0, (fileInfosxml[i].Name.Length - 4)))).SingleOrDefault(); if (info != null) { Mover(info, attach.EXT, false); } } } attachments.Clear(); } else { Mover(fileInfosxml[i], attach.EXT, false); if (fileInfospdf.Length > 0) { FileInfo info = fileInfospdf.Where(x => x.Name.Substring(0, x.Name.Length - 4) == (fileInfosxml[i].Name.Substring(0, (fileInfosxml[i].Name.Length - 4)))).SingleOrDefault(); if (info != null) { Mover(info, attach.EXT, false); } } } attach = new Attachment_BE(); MostrarLinea("*"); errorxml = false; } }