Exemplo n.º 1
0
        //Genera Referncias
        public RestResponse GenerarReferenciaREST(string SOURCE_REQ, int ID_SEQ_NUM, string DOCUMENT_ID, int DAYS_DURATION, decimal AMOUNT)
        {
            RestResponse data = new RestResponse();

            if (!string.IsNullOrEmpty(DOCUMENT_ID))
            {
                string   UAG_BNK_ID_REF = Generales.CalculateReference(ID_SEQ_NUM, DOCUMENT_ID.ToInteger(), DateTime.Now.AddDays(DAYS_DURATION), AMOUNT);
                DateTime END_DATE       = DateTime.Now.AddDays(DAYS_DURATION);

                PRDUAGPKEntities   context = new PRDUAGPKEntities();
                PS_UAG_REF_BNK_TBL log     = new PS_UAG_REF_BNK_TBL
                {
                    GUID           = Guid.NewGuid().ToString(),
                    AMOUNT         = AMOUNT,
                    SOURCE_REQ     = SOURCE_REQ,
                    ID_SEQ_NUM     = ID_SEQ_NUM,
                    DOCUMENT_ID    = DOCUMENT_ID.ToString(),
                    DAYS_DURATION  = DAYS_DURATION,
                    UAG_BNK_ID_REF = UAG_BNK_ID_REF,
                    END_DATE       = END_DATE,
                    CREATE_DATE    = DateTime.Now
                };
                context.PS_UAG_REF_BNK_TBL.Add(log);
                context.SaveChanges();

                data = new RestResponse()
                {
                    response       = true,
                    message        = "Referencia generada correctamente",
                    ID_SEQ_NUM     = ID_SEQ_NUM,
                    DOCUMENT_ID    = DOCUMENT_ID,
                    DAYS_DURATION  = DAYS_DURATION,
                    AMOUNT         = AMOUNT,
                    END_DATE       = string.Format("{0:yyyy-MM-dd}", END_DATE),
                    UAG_BNK_ID_REF = UAG_BNK_ID_REF
                };
            }
            else
            {
                data = new RestResponse()
                {
                    response = false,
                    message  = "El parametro DOCUMENT_ID no tiene un formato correcto"
                };
            }
            return(data);
        }
Exemplo n.º 2
0
        // Procesar Invoice XML
        public SfJsonInvoiceXML ProcesarInvoiceXMLREST(string strcarpeta, string strsource)
        {
            //produccion
            PRNFACTEntities contextoCFDI = new PRNFACTEntities();
            string          source       = strsource;

            SfJsonInvoiceXML retorno = new SfJsonInvoiceXML();

            retorno.PreExistentes = 0;
            retorno.Procesados    = 0;
            retorno.CfdiError     = new List <CfdiError>();
            CfdiError operacionError = new CfdiError();
            bool      ExisteError    = true;
            string    uuid;
            decimal   total;

            string strerror    = string.Empty;
            string strLocalUrl = ConfigurationManager.AppSettings["LocalUrl"];
            string strSolucionFactibleUsuario  = ConfigurationManager.AppSettings["SolucionFactibleUsuario"];
            string strSolucionFactiblePassword = ConfigurationManager.AppSettings["SolucionFactiblePassword"];

            PRNFACTEntities  contextUAG  = new PRNFACTEntities();
            PRDUAGPKEntities contextFSCM = new PRDUAGPKEntities();
            string           ip          = Dns.GetHostName();


            string[] filenames = new string[] { };

            string host = HttpContext.Current.Request.Url.Host.ToLower();

            try //Path
            {
                if (host == "localhost")
                {
                    /*Local Host*/
                    string LocalPath = System.Environment.CurrentDirectory;
                    filenames = Directory.GetFiles(strLocalUrl + strcarpeta + "/");
                }
                else
                {
                    filenames = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/CFDI/" + strcarpeta + "/"));
                }
            }
            catch (IOException ex)
            {
                strerror = ex.GetType().Name + " Message " + ex.Message;
            }
            if (filenames.Any())
            {
                foreach (string filename in filenames)
                {
                    List <string> mensajesRel  = new List <string>();
                    List <string> mensajesForm = new List <string>();
                    //HCR por cada uno de los XML que se lean de la Carpeta sumo un Complemento
                    retorno.Invoice++;

                    //Flag si existe un Error por cada uno de los archivos
                    ExisteError = false;

                    try
                    {
                        byte[] archivoXML = File.ReadAllBytes(filename);
                        //mensajesForm = Generales.ValidateForm(archivoXML);
                        DateTime fechatemp = DateTime.Today;
                        //DateTime fecha1 = new DateTime(fechatemp.Year, fechatemp.Month, 1);
                        DateTime fecha1 = new DateTime(fechatemp.Year - 1, 1, 1);
                        DateTime fecha2;

                        if (fechatemp.Month + 1 < 13)
                        {
                            fecha2 = new DateTime(fechatemp.Year, fechatemp.Month + 1, 1).AddDays(-1);
                        }
                        else
                        {
                            fecha2 = new DateTime(fechatemp.Year + 1, 1, 1).AddDays(-1);
                        }



                        /* DateTime openFromDate = fecha1;
                        *  DateTime openToDate = fecha2;
                        *  int OYF = 2019;
                        *  int OYT = 2020;
                        *  string UagfRFC = "UAG7806127I8";*/


                        /*Get Config from PS*/
                        string   BUSSINES_UNIT = "UAG01";
                        DateTime openFromDate  = (DateTime)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.OPEN_FROM_DATE).FirstOrDefault().Value.AbsoluteStart();
                        DateTime openToDate    = (DateTime)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.OPEN_TO_DATE).FirstOrDefault().Value.AbsoluteEnd();
                        int      OYF           = (int)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.OPEN_YEAR_FROM).FirstOrDefault();
                        int      OYT           = (int)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.OPEN_YEAR_TO).FirstOrDefault();
                        string   UagfRFC       = (string)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.UAG_CFDI_RECEPTRFC).FirstOrDefault();
                        string   userid        = (string)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.FO_USERID).FirstOrDefault();
                        string   pw            = (string)contextFSCM.PS_UAG_CFDI_CNFG_V.Where(l => l.BUSINESS_UNIT == BUSSINES_UNIT).Select(l => l.FO_PASSWORD).FirstOrDefault();


                        Invoice validation = new Invoice(archivoXML, openFromDate, openToDate, OYF, OYT, UagfRFC);
                        mensajes = validation.getValidationMessages();
                        if (mensajes.Count == 0 || mensajes[0].Equals("No se permiten facturas fuera del mes en curso."))
                        {
                            if (!Generales.ValidateExistenceXML(archivoXML))
                            {
                                //Procesa CDFI
                                RecibeCFDPortType ws      = new RecibeCFDPortTypeClient("RecibeCFDHttpsSoap12Endpoint");
                                recibeCFDRequest  request = new recibeCFDRequest();
                                request.usuario  = strSolucionFactibleUsuario;
                                request.password = strSolucionFactiblePassword;
                                request.cfd      = archivoXML;
                                recibeCFDResponse response = ws.recibeCFD(request);

                                if ([email protected] == "OK" ||
                                    ([email protected] == "ERROR" &&
                                     (Generales.ValidateMessage([email protected])
                                     )
                                    )
                                    )
                                {
                                    SaveFromXML custom     = new SaveFromXML(archivoXML, source);
                                    string      impuestos  = Generales.getImpuestos(archivoXML, custom.UUID);
                                    string      descuentos = Generales.getDescuentos(archivoXML, custom.UUID);
                                    string      tipoCambio = Generales.getTipoCambio(archivoXML, custom.UUID);
                                    string      subTotal   = Generales.getSubtotal(archivoXML, custom.UUID);
                                    if (!impuestos.Equals(""))
                                    {
                                        Generales.DeleteUUID(custom.UUID);
                                        mensajes.Add(impuestos);
                                    }
                                    else if (!descuentos.Equals(""))
                                    {
                                        Generales.DeleteUUID(custom.UUID);
                                        mensajes.Add(descuentos);
                                    }
                                    else if (!tipoCambio.Equals(""))
                                    {
                                        Generales.DeleteUUID(custom.UUID);
                                        mensajes.Add(tipoCambio);
                                    }
                                    else if (!subTotal.Equals(""))
                                    {
                                        Generales.DeleteUUID(custom.UUID);
                                        mensajes.Add(subTotal);
                                    }
                                    else
                                    {
                                        uuid  = custom.UUID;
                                        total = custom.TOTAL;

                                        UAG_LOG log = new UAG_LOG();
                                        log.UAG_LOG_USER  = UagfRFC;
                                        log.UAG_LOG_DESCR = "Se cargo una factura con UUID:" + custom.UUID;
                                        log.UAG_LOG_ACT   = "Agregar";
                                        log.UAG_LOG_URL   = HttpContext.Current.Request.Url.AbsolutePath.Replace(".aspx", string.Empty);
                                        log.UAG_LOG_FEC   = DateTime.Now;
                                        log.UAG_LOG_IP    = host;
                                        contextoCFDI.UAG_LOG.Add(log);
                                        contextoCFDI.SaveChanges();


                                        UAG_UUID_LOG log2 = new UAG_UUID_LOG();
                                        log2.SOURCE          = source;
                                        log2.UAG_CFDI_UUID   = custom.UUID;
                                        log2.UAG_LOG_FEC     = DateTime.Now;
                                        log2.UAG_LOG_ACCCION = "A";
                                        log2.UAG_LOG_DESCR   = "Save XML";
                                        log2.UAG_LOG_IP      = host;
                                        contextoCFDI.UAG_UUID_LOG.Add(log2);
                                        contextoCFDI.SaveChanges();

                                        mensajes.Add("Factura cargada correctamente");
                                    }
                                }
                            }
                        }
                        else
                        {
                            string errores = "";
                            foreach (string mensaje in mensajes)
                            {
                                errores += mensaje + "<br>";
                            }

                            operacionError.FileName = System.IO.Path.GetFileName(filename);
                            operacionError.Detalle  = errores;
                            retorno.CfdiError.Add(operacionError);
                            ExisteError = true;
                        }
                    }
                    catch (System.Exception)
                    {
                        //retorno.Errores++;
                        //retorno.DetallesErrores.Add(aux);
                        //response.Error = true;
                    }
                    if (ExisteError)
                    {
                        retorno.ConErrores++;
                    }
                }
            }
            else
            {
                // No se Encontraron Archivos en la Carpeta
            }
            return(retorno);
        }