예제 #1
0
        public async Task <List <Detalle> > GetCreditor(List <ResultInstruction> list)
        {
            ServiceEvento            dataEvento = new ServiceEvento(TokenSii);
            DteInfoRef               infoLastF  = null;
            List <Detalle>           detalles   = new List <Detalle>();
            Dictionary <string, int> dic        = Properties.Settings.Default.DicReem;
            int   c = 0;
            float porcent;
            List <Task <List <Detalle> > > tareas = new List <Task <List <Detalle> > >();

            tareas = list.Select(async instruction =>
            {
                try
                {
                    // GET PARTICIPANT DEBTOR
                    instruction.ParticipantDebtor = await Participant.GetParticipantByIdAsync(instruction.Debtor);
                    //REEMPLAZOS
                    if (dic.ContainsKey(instruction.ParticipantDebtor.Id.ToString()))
                    {
                        instruction.ParticipantNew = await Participant.GetParticipantByIdAsync(dic[instruction.ParticipantDebtor.Id.ToString()]);
                    }
                    // ROOT CLASS.
                    Detalle detalle = new Detalle(instruction.ParticipantDebtor.Rut, instruction.ParticipantDebtor.VerificationCode, instruction.ParticipantDebtor.BusinessName, instruction.Amount, instruction, true);
                    // GET INFO OF INVOICES.
                    List <DteInfoRef> dteInfos    = await DteInfoRef.GetInfoRefAsync(instruction, Conn, "F");
                    List <DteInfoRef> dteInfoRefs = new List <DteInfoRef>();
                    if (dteInfos != null)
                    {
                        foreach (DteInfoRef item in dteInfos)
                        {
                            if (string.Compare(item.Glosa, instruction.PaymentMatrix.NaturalKey, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                dteInfoRefs.Add(item);
                            }
                        }
                        // ATTACH FILES.
                        detalle.DteInfoRefs = dteInfoRefs;
                        // ATTACH PRINCIPAL DOC.
                        if (detalle.DteInfoRefs.Count >= 1)
                        {
                            infoLastF = detalle.DteInfoRefs.First(); // SHOW THE LAST DOC.
                            if (dteInfoRefs.First().DteFiles != null)
                            {
                                switch (detalle.DteInfoRefs.First().DteFiles.Count)
                                {
                                case 1:
                                    if (infoLastF.DteFiles[0].TipoXML == null)
                                    {
                                        detalle.DTEDef  = HSerialize.DTE_To_Object(infoLastF.DteFiles[0].Archivo);
                                        detalle.DTEFile = infoLastF.DteFiles[0].Archivo;
                                    }
                                    break;

                                default:
                                    {
                                        detalle.DTEDef  = HSerialize.DTE_To_Object(infoLastF.DteFiles.FirstOrDefault(x => x.TipoXML == "D").Archivo);
                                        detalle.DTEFile = infoLastF.DteFiles.FirstOrDefault(x => x.TipoXML == "D").Archivo;
                                        break;
                                    }
                                }
                            }
                            detalle.DteInfoRefLast = infoLastF;
                            detalle.NroInt         = infoLastF.NroInt;
                            detalle.FechaEmision   = infoLastF.Fecha.ToString();
                            detalle.Folio          = infoLastF.Folio;
                            detalle.MntNeto        = infoLastF.NetoAfecto;
                            detalle.MntIva         = infoLastF.IVA;
                            detalle.MntTotal       = infoLastF.Total;
                            // GET INFO FROM SII
                            if (infoLastF.EnviadoSII == 1 && infoLastF.AceptadoCliente == 0) // 1 Enviado / 0 No enviado
                            {
                                detalle.FechaRecepcion = infoLastF.FechaEnvioSII.ToString("dd-MM-yyyy");
                                // EVENTS FROM SII
                                DataEvento evento = await dataEvento.GetStatusDteAsync("Creditor", TokenSii, "33", detalle, UserParticipant);
                                if (evento != null)
                                {
                                    detalle.DataEvento    = evento;
                                    detalle.StatusDetalle = GetStatus(detalle);
                                }
                            }
                            if (detalle.StatusDetalle == StatusDetalle.Pending && infoLastF.AceptadoCliente == 1)
                            {
                                detalle.FechaRecepcion = infoLastF.FechaEnvioSII.ToString("dd-MM-yyyy");
                                detalle.StatusDetalle  = StatusDetalle.Accepted;
                            }
                            else if (detalle.StatusDetalle == StatusDetalle.Accepted && infoLastF.AceptadoCliente == 0)
                            {
                                DteFiles.UpdateFiles(Conn, detalle); // UPDATE DTE_DocCab SET infoLastF.EnviadoCliente = 1
                            }
                        }
                        // SEND DTE TO CEN.
                        if (detalle.StatusDetalle == StatusDetalle.Accepted && detalle.Instruction != null && detalle.Instruction.StatusBilled == Instruction.StatusBilled.NoFacturado)
                        {
                            ResultDte resultDte = await Dte.SendDteCreditorAsync(detalle, TokenCen, detalle.DTEFile);
                            if (resultDte != null)
                            {
                                detalle.Instruction.Dte          = resultDte;
                                detalle.Instruction.StatusBilled = Instruction.StatusBilled.Facturado;
                            }
                        }
                        detalle.ValidatorFlag = new HFlagValidator(detalle, true);
                    }
                    else
                    {
                        detalle.ValidatorFlag = new HFlagValidator()
                        {
                            Flag = LetterFlag.Clear
                        };
                    }
                    detalles.Add(detalle);
                    c++;
                    porcent = (float)(100 * c) / list.Count;
                    await ReportProgress(porcent, $"Processing 'Pay Instructions' {instruction.Id}-{instruction.PaymentMatrix.PublishDate.ToString("dd-MM-yyyy")}, wait please.  ({c}/{list.Count})");
                    return(detalles);
                }
                catch (Exception)
                {
                    return(null);

                    throw;
                }
            }).ToList();
            await Task.WhenAll(tareas);

            return(detalles.OrderBy(x => x.Instruction.Id).ToList());
        }
예제 #2
0
        public async Task <List <Detalle> > GetDebtor(List <Detalle> detalles, string p)
        {
            int            c                      = 0;
            ServiceEvento  dataEvento             = new ServiceEvento(TokenSii);
            List <Detalle> detallesFinal          = new List <Detalle>();
            List <Task <List <Detalle> > > tareas = new List <Task <List <Detalle> > >();

            tareas = detalles.Select(async item =>
            {
                DTEDefType xmlObjeto = null;
                string nameFile      = null;
                // GET XML FILE

                nameFile = p + $"\\{UserParticipant.Rut}-{UserParticipant.VerificationCode}\\{item.RutReceptor}-{item.DvReceptor}__{item.Tipo}__{item.Folio}.xml";

                if (File.Exists(nameFile))
                {
                    xmlObjeto = HSerialize.DTE_To_Object(nameFile);
                }
                // GET PARTICPANT INFO FROM CEN
                ResultParticipant participant = await Participant.GetParticipantByRutAsync(item.RutReceptor.ToString());
                if (participant != null && participant.Id > 0)
                {
                    item.IsParticipant     = true;
                    item.ParticipantMising = participant;
                }
                if (xmlObjeto != null)
                {
                    item.DTEDef = xmlObjeto;
                    if (item.IsParticipant)
                    {
                        // GET REFERENCE SEN.
                        DTEDefTypeDocumentoReferencia r = null;
                        GetReferenceCen doc             = new GetReferenceCen(item);
                        if (doc != null)
                        {
                            r = doc.DocumentoReferencia;
                        }
                        if (r != null && r.RazonRef != null)
                        {
                            // GET WINDOW.
                            ResultBillingWindow window = await BillingWindow.GetBillingWindowByNaturalKeyAsync(r);
                            // GET MATRIX.
                            if (window != null && window.Id > 0)
                            {
                                List <ResultPaymentMatrix> matrices = await PaymentMatrix.GetPaymentMatrixByBillingWindowIdAsync(window);
                                if (matrices != null && matrices.Count > 0)
                                {
                                    ResultPaymentMatrix matrix = matrices.FirstOrDefault(x => x.NaturalKey.Equals(r.RazonRef.Trim(), StringComparison.OrdinalIgnoreCase));
                                    if (matrix != null)
                                    {
                                        ResultInstruction instruction = await Instruction.GetInstructionDebtorAsync(matrix, participant, UserParticipant);
                                        if (instruction != null && instruction.Id > 0)
                                        {
                                            item.Instruction = instruction;
                                            item.Instruction.ParticipantCreditor = participant;
                                            item.Instruction.ParticipantDebtor   = UserParticipant;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // FLAGS IF EXISTS XML FILE
                item.ValidatorFlag = new HFlagValidator(item, false);
                // EVENTS FROM SII
                item.DataEvento = await dataEvento.GetStatusDteAsync2("Debtor", TokenSii, "33", item, UserParticipant);
                // STATUS DOC
                if (item.DataEvento != null)
                {
                    item.StatusDetalle = GetStatus(item);
                }
                // INSERT IN CEN
                if (item.StatusDetalle == StatusDetalle.Accepted && item.Instruction != null)
                {
                    // 1 No Facturado y cuando hay más de 1 dte informado 2 Facturado 3 Facturado
                    // con retraso Existe el DTE?
                    ResultDte doc = await Dte.GetDteAsync(item, false);
                    if (doc == null)
                    {
                        // Enviar el DTE
                        ResultDte resultDte = await Dte.SendDteDebtorAsync(item, TokenCen);
                        if (resultDte != null && resultDte.Folio > 0)
                        {
                            item.Instruction.Dte = resultDte;
                        }
                    }
                    else
                    {
                        item.Instruction.Dte = doc;
                    }
                }
                detallesFinal.Add(item);
                c++;
                float porcent = (float)(100 * c) / detalles.Count;
                await ReportProgress(porcent, $"Processing 'Pay Instructions' {item.Folio}, wait please.  ({c}/{detalles.Count})");
                return(detalles);
            }).ToList();
            await Task.WhenAll(tareas);

            return(detalles.OrderBy(x => x.FechaRecepcion).ToList());
        }