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()); }