Exemplo n.º 1
0
        }//fin MenuEvent

        //Para PEru EasyDot
        public new void EnviarFE_PE_ED(String DocEntry, Boolean RunningUnderSQLServer, String SeriePE, String FolioNum, String TipoDocElec, String sObjType, String lRUC)
        {
            String      URL;
            String      URL_PDF;
            String      ProcedimientoE;
            String      ProcedimientoD;
            XmlDocument oXml = null;
            String      userED;
            String      passED;
            TFunctions  Reg = new TFunctions();

            SAPbobsCOM.Company Cmpny = SBO_f.Cmpny;
            Reg.SBO_f = SBO_f;
            String    Status;
            String    sMessage = "";
            Int32     lRetCode;
            String    DocDate = "";
            XDocument miXML   = null;
            XElement  xNodo   = null;
            String    ExternalFolio;

            SAPbobsCOM.Recordset ors = ((SAPbobsCOM.Recordset)Cmpny.GetBusinessObject(BoObjectTypes.BoRecordset));
            String        sXML;
            TDLLparaXMLPE Dll = new TDLLparaXMLPE();

            Dll.SBO_f = SBO_f;
            String MostrarXML = "N";

            try
            {
                if (RunningUnderSQLServer)
                {
                    s = @"SELECT U_URLEasyDot 'URL', ISNULL(U_UserED,'') 'User', ISNULL(U_PwdED,'') 'Pass', ISNULL(U_MostrarXML,'N') 'MostrarXML' FROM [@VID_FEPARAM]";
                }
                else
                {
                    s = @"SELECT ""U_URLEasyDot"" ""URL"", IFNULL(""U_UserED"",'') ""User"", IFNULL(""U_PwdED"",'') ""Pass"", IFNULL(""U_MostrarXML"",'N') ""MostrarXML"" FROM ""@VID_FEPARAM"" ";
                }

                ors.DoQuery(s);
                if (ors.RecordCount == 0)
                {
                    SBO_f.SBOApp.StatusBar.SetText("No se ha ingresado URL", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error);
                }
                else if (((System.String)ors.Fields.Item("URL").Value).Trim() == "")
                {
                    SBO_f.SBOApp.StatusBar.SetText("No se ha ingresado URL", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error);
                }
                else if (((System.String)ors.Fields.Item("User").Value).Trim() == "")
                {
                    throw new Exception("No se encuentra usuario en Parametros");
                }
                else if (((System.String)ors.Fields.Item("Pass").Value).Trim() == "")
                {
                    throw new Exception("No se encuentra password en Parametros");
                }
                else
                {
                    userED     = Reg.DesEncriptar((System.String)(ors.Fields.Item("User").Value).ToString().Trim());
                    passED     = Reg.DesEncriptar((System.String)(ors.Fields.Item("Pass").Value).ToString().Trim());
                    MostrarXML = ((System.String)ors.Fields.Item("MostrarXML").Value).Trim();

                    URL     = ((System.String)ors.Fields.Item("URL").Value).Trim() + "/SendRetencion.ashx";
                    URL_PDF = ((System.String)ors.Fields.Item("URL").Value).Trim() + "/SendPdf.ashx";
                    //validar que exista procedimentos para tipo documento
                    if (RunningUnderSQLServer)
                    {
                        s = "select ISNULL(U_ProcNomE,'') 'ProcNomE', ISNULL(U_ProcNomD,'') 'ProcNomD' FROM [@VID_FEPROCED] where ISNULL(U_Habili,'N') = 'Y' and U_TipoDocPE = '{0}'";
                    }
                    else
                    {
                        s = @"select IFNULL(""U_ProcNomE"",'') ""ProcNomE"", IFNULL(""U_ProcNomD"",'') ""ProcNomD"" FROM ""@VID_FEPROCED"" where IFNULL(""U_Habili"",'N') = 'Y' and ""U_TipoDocPE"" = '{0}'";
                    }

                    s = String.Format(s, TipoDocElec);
                    ors.DoQuery(s);
                    if (ors.RecordCount == 0)
                    {
                        throw new Exception("No se encuentra procedimientos para Documento electronico " + TipoDocElec);
                    }
                    else
                    {
                        ProcedimientoE = ((System.String)ors.Fields.Item("ProcNomE").Value).Trim();
                        ProcedimientoD = ((System.String)ors.Fields.Item("ProcNomD").Value).Trim();

                        if (ProcedimientoE == "")
                        {
                            throw new Exception("No se encuentra procedimiento de encabezado para Documento electronico " + TipoDocElec);
                        }
                        else if (ProcedimientoD == "")
                        {
                            throw new Exception("No se encuentra procedimiento de detalle para Documento electronico " + TipoDocElec);
                        }

                        if (RunningUnderSQLServer)
                        {
                            s = @"exec {0} {1}, '{2}', '{3}'";//Encabezado
                        }
                        else
                        {
                            s = @"CALL {0} ({1}, '{2}', '{3}')";
                        }
                        s = String.Format(s, ProcedimientoE, DocEntry, TipoDocElec, sObjType);
                        ors.DoQuery(s);

                        if (ors.RecordCount == 0)
                        {
                            throw new Exception("No se encuentra datos para Documento electronico " + TipoDocElec);
                        }
                        else
                        {
                            var bImpresion = false;
                            ExternalFolio = ((System.String)ors.Fields.Item("IdDocumento").Value).Trim();
                            miXML         = new XDocument(
                                new XDeclaration("1.0", "utf-8", "yes"),
                                new XElement("DocumentoElectronico"));

                            sXML = Dll.GenerarXMLStringPayment(ref ors, TipoDocElec, ref miXML, "E");
                            if (sXML == "")
                            {
                                throw new Exception("Problema para generar xml Documento electronico " + TipoDocElec);
                            }

                            //DETALLE
                            if (RunningUnderSQLServer)
                            {
                                s = @"exec {0} {1}, '{2}', '{3}'";
                            }
                            else
                            {
                                s = @"CALL {0} ({1}, '{2}', '{3}')";
                            }
                            s = String.Format(s, ProcedimientoD, DocEntry, TipoDocElec, sObjType);
                            ors.DoQuery(s);

                            if (ors.RecordCount == 0)
                            {
                                throw new Exception("No se encuentra datos de detalle para Documento electronico " + TipoDocElec);
                            }
                            else
                            {
                                sXML = Dll.GenerarXMLStringPayment(ref ors, TipoDocElec, ref miXML, "D");
                                if (sXML == "")
                                {
                                    throw new Exception("Problema para generar xml Documento electronico " + TipoDocElec);
                                }

                                oXml = new XmlDocument();
                                using (var xmlReader = miXML.CreateReader())
                                {
                                    oXml.Load(xmlReader);
                                }

                                //Agrega el PDF al xml
                                XmlNode node;
                                if (oXml.SelectSingleNode("//CamposExtras") == null)
                                {
                                    node = oXml.CreateNode(XmlNodeType.Element, "CamposExtras", null);
                                }
                                else
                                {
                                    node = oXml.SelectSingleNode("//CamposExtras");
                                }

                                if (MostrarXML == "Y")
                                {
                                    SBO_f.oLog.OutLog(oXml.InnerXml);
                                }
                                //ENVIO AL PORTAL
                                s = Reg.UpLoadDocumentByUrl2(oXml, null, RunningUnderSQLServer, URL, userED, passED, TipoDocElec + "_" + ExternalFolio);

                                oXml.LoadXml(s);
                                //var Configuracion = oXml.GetElementsByTagName("Error");
                                var lista     = ((XmlElement)oXml.GetElementsByTagName("Error")[0]).GetElementsByTagName("ErrorText");
                                var ErrorText = lista[0].InnerText;
                                if (ErrorText.Length > 250)
                                {
                                    ErrorText = ErrorText.Substring(0, 250);
                                }
                                lista = ((XmlElement)oXml.GetElementsByTagName("Error")[0]).GetElementsByTagName("ErrorCode");
                                var ErrorCode = lista[0].InnerText;
                                lista = ((XmlElement)oXml.GetElementsByTagName("Error")[0]).GetElementsByTagName("PDF417");
                                var PDF417 = lista[0].InnerText;
                                lista = ((XmlElement)oXml.GetElementsByTagName("Error")[0]).GetElementsByTagName("IdDocument");
                                var IdDocument = lista[0].InnerText;

                                if (ErrorCode != "0")
                                {
                                    SBO_f.SBOApp.StatusBar.SetText("Error envio documento electrónico (1)", SAPbouiCOM.BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error);
                                    SBO_f.oLog.OutLog("Error en envio documento electronico al portal (1) Codigo Error Portal: " + ErrorCode + " Mensaje Portal: " + ErrorText);
                                    //sObjType = "13";
                                    if (ErrorCode == "-103")
                                    {
                                        Status = "RR";
                                    }
                                    else
                                    {
                                        Status = "EE";
                                    }
                                    sMessage = ErrorText;
                                    var emsg = sMessage;
                                    if (sMessage == "")
                                    {
                                        sMessage = "Error envio documento electronico a EasyDot";
                                    }

                                    var oPayments1 = (SAPbobsCOM.Documents)(Cmpny.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oVendorPayments));
                                    if (oPayments1.GetByKey(Convert.ToInt32(DocEntry)))
                                    {
                                        DocDate = SBO_f.DateToStr(oPayments1.DocDate);
                                        if (emsg == "")
                                        {
                                            oPayments1.UserFields.Fields.Item("U_EstadoFE").Value = "N";
                                        }
                                        else if (Status == "EE")
                                        {
                                            oPayments1.UserFields.Fields.Item("U_EstadoFE").Value = "P";
                                        }
                                        else if (Status == "RR")
                                        {
                                            oPayments1.UserFields.Fields.Item("U_EstadoFE").Value = "A";
                                        }
                                        else
                                        {
                                            oPayments1.UserFields.Fields.Item("U_EstadoFE").Value = "P";
                                        }

                                        lRetCode = oPayments1.Update();
                                        if (lRetCode != 0)
                                        {
                                            s = SBO_f.Cmpny.GetLastErrorDescription();
                                            SBO_f.SBOApp.StatusBar.SetText("Error actualizar documento - " + s, BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                                            sMessage = "Error actualizar documento - " + s;
                                            SBO_f.oLog.OutLog(sMessage);
                                        }
                                    }
                                }
                                else
                                {
                                    if (PDF417 == "")
                                    {
                                        SBO_f.oLog.OutLog("No se ha recibido PDF417 -> " + ExternalFolio);
                                        SBO_f.SBOApp.StatusBar.SetText("No se ha recibido PDF417", SAPbouiCOM.BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error);
                                    }
                                    Status = "RR";
                                    //sObjType = "13";
                                    sMessage = "Enviado satisfactoriamente a EasyDot y Aceptado";
                                    SBO_f.SBOApp.StatusBar.SetText("Se ha enviado satisfactoriamente el documento electronico", SAPbouiCOM.BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
                                    var oPayments = ((SAPbobsCOM.Payments)Cmpny.GetBusinessObject(BoObjectTypes.oVendorPayments));
                                    if (oPayments.GetByKey(Convert.ToInt32(DocEntry)))
                                    {
                                        DocDate = SBO_f.DateToStr(oPayments.DocDate);
                                        //oPayments.Printed = PrintStatusEnum.psYes;
                                        oPayments.UserFields.Fields.Item("U_EstadoFE").Value = "A";
                                        oPayments.UserFields.Fields.Item("U_PDF417").Value   = PDF417.Trim();
                                        lRetCode = oPayments.Update();
                                        if (lRetCode != 0)
                                        {
                                            s = SBO_f.Cmpny.GetLastErrorDescription();
                                            SBO_f.SBOApp.StatusBar.SetText("Error actualizar documento - " + s, BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                                            sMessage = "Error actualizar documento - " + s;
                                            SBO_f.oLog.OutLog(sMessage);
                                        }
                                        else
                                        {
                                            bImpresion = true;
                                        }
                                    }
                                    else
                                    {
                                        sMessage   = "No se ha encontrado documento al actualizar Impresion";
                                        bImpresion = false;
                                    }
                                }
                                oXml = null;

                                if (RunningUnderSQLServer)
                                {
                                    s = "SELECT DocEntry, U_Status, U_Id FROM [@VID_FELOG] WITH (NOLOCK) WHERE U_DocEntry = {0} AND U_ObjType = '{1}'";
                                }
                                else
                                {
                                    s = @"SELECT ""DocEntry"", ""U_Status"", ""U_Id"" FROM ""@VID_FELOG"" WHERE ""U_DocEntry"" = {0} AND ""U_ObjType"" = '{1}' ";
                                }
                                s = String.Format(s, DocEntry, sObjType);
                                ors.DoQuery(s);
                                if (ors.RecordCount == 0)
                                {
                                    Reg.FELOGAdd(Int32.Parse(DocEntry), sObjType, "--", SeriePE, Int32.Parse(FolioNum), Status, sMessage, TipoDocElec, SBO_f.SBOApp.Company.UserName, "", IdDocument, ErrorText, DocDate, ExternalFolio);
                                }
                                else
                                {
                                    if ((System.String)(ors.Fields.Item("U_Status").Value) != "RR")
                                    {
                                        SBO_f.SBOApp.StatusBar.SetText("Documento se ha enviado a EasyDot", SAPbouiCOM.BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
                                        Reg.FELOGUptM((System.Int32)(ors.Fields.Item("DocEntry").Value), Int32.Parse(DocEntry), sObjType, "--", SeriePE, Int32.Parse(FolioNum), Status, sMessage, TipoDocElec, SBO_f.SBOApp.Company.UserName, "", (IdDocument == "0" ? ((System.String)ors.Fields.Item("U_Id").Value).Trim() : IdDocument), ErrorText, DocDate, ExternalFolio);
                                    }
                                    else
                                    {
                                        SBO_f.SBOApp.StatusBar.SetText("Documento ya se ha enviado anteriormente a EasyDot y se encuentra en Sunat", SAPbouiCOM.BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
                                    }
                                }

                                if ((bImpresion) && (PDF417 != ""))
                                {
                                    //obtiene string de pdf
                                    var sPDF = Reg.PDFenString(TipoDocElec, DocEntry, sObjType, SeriePE, FolioNum, RunningUnderSQLServer);

                                    /*var sjson = @"<root><DocType>{0}</DocType><DocNum>{1}</DocNum><RUC>{2}</RUC><PDF>{3}</PDF></root>";
                                     * sjson = String.Format(sjson, TipoDocElec, ExternalFolio, RUC, sPDF);
                                     * XmlDocument xm = new XmlDocument();
                                     * xm.LoadXml(sjson);
                                     * String json = JsonConvert.SerializeXmlNode(xm);*/
                                    var sjson = @"""DocType"":""{0}"",""DocNum"":""{1}"",""RUC"":""{2}"",""PDF"":""{3}""";
                                    sjson = String.Format(sjson, TipoDocElec, ExternalFolio, lRUC, sPDF);
                                    sjson = "{" + sjson + "}";
                                    s     = Reg.UpLoadDocumentByUrl(null, sjson, RunningUnderSQLServer, URL_PDF, userED, passED, TipoDocElec + "_" + ExternalFolio);


                                    SBO_f.SBOApp.StatusBar.SetText("PDF enviado al portal", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception x)
            {
                SBO_f.SBOApp.StatusBar.SetText("EnviarFE_PE_ED: " + x.Message + " ** Trace: " + x.StackTrace, BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error);
                SBO_f.oLog.OutLog("EnviarFE_PE_ED: " + x.Message + " ** Trace: " + x.StackTrace);
            }
            finally
            {
                SBO_f._ReleaseCOMObject(ors);
                SBO_f._ReleaseCOMObject(oXml);
                SBO_f._ReleaseCOMObject(miXML);
                SBO_f._ReleaseCOMObject(xNodo);
            }
        }