Beispiel #1
0
        private DTEDefTypeDocumentoReferencia GetRef(Detalle detalle)
        {
            try
            {
                DTEDefTypeDocumento             obj  = (DTEDefTypeDocumento)detalle.DTEDef.Item;
                DTEDefTypeDocumentoReferencia[] refr = obj.Referencia;
                if (refr != null)
                {
                    DTEDefTypeDocumentoReferencia r = refr.FirstOrDefault(x => x.TpoDocRef.ToUpper() == "SEN");
                    if (r != null)
                    {
                        return(r);
                    }
                }
            }
            catch (System.Exception)
            {
                return(null);

                throw;
            }

            return(null);
        }
Beispiel #2
0
 public GetReferenceCen(Detalle detalle)
 {
     DocumentoReferencia = GetRef(detalle);
 }
        public static async Task <ResultBillingWindow> GetBillingWindowByNaturalKeyAsync(DTEDefTypeDocumentoReferencia referencia)
        {
            TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
            string   r1 = referencia.RazonRef.Substring(0, referencia.RazonRef.IndexOf(']') + 1).TrimStart();
            string   r2 = referencia.RazonRef.Substring(0, referencia.RazonRef.IndexOf(']', referencia.RazonRef.IndexOf(']') + 1) + 1);

            r2 = r2.Substring(r2.IndexOf(']') + 1);

            // Controlling lower & upper
            string rznRef = ti.ToTitleCase(r2.ToLower());

            try
            {
                using (CustomWebClient wc = new CustomWebClient())
                {
                    Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/billing-windows/?natural_key={r1 + rznRef}");
                    wc.Headers[HttpRequestHeader.ContentType] = "application/json";
                    string res = await wc.DownloadStringTaskAsync(uri);

                    if (res != null)
                    {
                        BillingWindow b = JsonConvert.DeserializeObject <BillingWindow>(res, new JsonSerializerSettings {
                            NullValueHandling = NullValueHandling.Ignore
                        });
                        if (b.Count > 0)
                        {
                            return(b.Results[0]);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(null);
        }
        private void ValidateCen(Detalle detalle, bool isCreditor)
        {
            try
            {
                if (detalle.Folio > 0 && detalle.IsParticipant) // Facturada
                {
                    Flag = LetterFlag.Green;                    // DEFAULT.
                    if (detalle.DTEDef != null)
                    {
                        DTEDefTypeDocumento dte = (DTEDefTypeDocumento)detalle.DTEDef.Item;
                        if (dte.Encabezado.IdDoc.FmaPago != DTEDefTypeDocumentoEncabezadoIdDocFmaPago.Crédito) // VALIDE FORMA PAGO.
                        {
                            Flag    = LetterFlag.Yellow;
                            FmaPago = true;
                        }
                        if (dte.Referencia != null)
                        {
                            DTEDefTypeDocumentoReferencia referencia = dte.Referencia.FirstOrDefault(x => x.TpoDocRef.ToUpper() == "SEN");
                            if (referencia != null && detalle.Instruction != null)
                            {
                                if (string.IsNullOrEmpty(referencia.NroLinRef))
                                {
                                    NroLinRef = true;
                                }
                                if (Compare(referencia.FolioRef, detalle.Instruction.PaymentMatrix.ReferenceCode, true) == -1) // DE01724A17C14S0015
                                {
                                    Flag     = LetterFlag.Red;
                                    FolioRef = true;
                                }
                                if (Compare(referencia.RazonRef, detalle.Instruction.PaymentMatrix.NaturalKey, true) == -1) // SEN_[RBPA][Ene18-Dic18][R][V02]
                                {
                                    Flag     = LetterFlag.Red;
                                    RazonRef = true;
                                }
                                if (string.IsNullOrEmpty(referencia.FchRef.ToString()))
                                {
                                    FchRef = true;
                                }
                            }
                            else
                            {
                                // NO REF CEN.
                                Flag      = LetterFlag.Red;
                                FolioRef  = true;
                                RazonRef  = true;
                                TpoDocRef = true;
                            }
                        }
                        else
                        {
                            // NO REFS.
                            Flag      = LetterFlag.Red;
                            FolioRef  = true;
                            RazonRef  = true;
                            TpoDocRef = true;
                        }

                        // Valide Instruction (Only Debtor)
                        if (detalle.Instruction == null && isCreditor == false)
                        {
                            Flag      = LetterFlag.Red;
                            FolioRef  = true;
                            RazonRef  = true;
                            TpoDocRef = true;
                        }
                        else
                        {
                            // Valide Amount
                            if (Convert.ToUInt32(dte.Encabezado.Totales.MntNeto) != detalle.Instruction.Amount)
                            {
                                Flag = LetterFlag.Blue;
                            }
                        }
                        // Valide excluide
                        // Enel Distribución Chile S.A. & Chilquinta Energía S.A && Cge S.A
                        // 96800570 / 96813520 / 76411321
                        if ((detalle.RutReceptor == "96800570" || detalle.RutReceptor == "96813520" || detalle.RutReceptor == "76411321") && (isCreditor == false))
                        {
                            Flag = LetterFlag.Clear;
                        }
                    }
                    else
                    {   // No Xml
                        Flag = LetterFlag.Red;
                    }
                }
                else
                {
                    // No facturada & No participant
                    Flag = LetterFlag.Clear;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        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());
        }