예제 #1
0
        /// <summary>
        /// Crea una nueva interfaz con los registros fallidos pero aprobados
        /// </summary>
        /// <param name="newRecords">Lista de nuevos registros a agregar</param>
        /// <returns></returns>
        public bool WriteApproved(List <OracleRecord> newRecords)
        {
            var estado = false;

            try
            {
                LogUtilities.ShowMessage("Procesando " + newRecords.Count + " archivos aprobados", 1);
                var reporte = new OutputInterface();
                reporte.AddRecords(newRecords);
                var outputFile = DateTime.Now.ToString("dd-MM-yyyyThh-mm-ss") + "_output.csv";
                reporte.Save(outputFile);
                estado = true;
            }
            catch (Exception ex)
            {
                LogUtilities.ShowMessage(ex.ToString(), 1, false);
            }
            return(estado);
        }
예제 #2
0
        private bool Scenarios23(List <List <OracleRecord> > recordsGroup, int scenario)
        {
            var     valid = true;
            decimal invoiceAmountGroup;
            decimal amountGroupCc = 0;
            var     recordsCc     = new List <OracleRecord>();
            var     regex         = new Regex(@"[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}");

            foreach (var recordGroup in recordsGroup)
            {
                var adjacentRecords = recordGroup.GroupAdjacent(record => record.Cc).Select(grp => grp.ToList()).ToList();
                if (scenario == 3 || scenario == 4)
                {
                    adjacentRecords = FillBlanksScenario3(adjacentRecords);
                }
                for (var i = 0; i < adjacentRecords.Count; i++)
                {
                    var group      = adjacentRecords[i];
                    var invoiceNum = group.First().InvoiceNum;
                    var uuids      = string.Join(",", group.Select(x => x.UuidCfdi));
                    try
                    {
                        #region Valida que el UUID exista en el portal si es que no tiene permisos, que el MontoTotal corresponda con el del portal, que el RFC corresponda con el del portal y que el UUID tenga una estructura válida

                        foreach (var record in group)
                        {
                            if (_isExtranjero)
                            {
                                continue;
                            }
                            else if (record.NoInvDetail2.Equals("Y"))
                            {
                                continue;
                            }
                            else if (scenario == 2 && !regex.IsMatch(record.UuidCfdi.ToUpper()))
                            {
                                continue;
                            }
                            else if (!_dbRecords.ContainsKey(record.UuidCfdi.ToUpper()))
                            {
                                if (_isPermisoUuid && !string.IsNullOrEmpty(record.UuidCfdi))
                                {
                                    continue;
                                }
                                throw new Exception(record.UuidCfdi + "||" + "El UUID no existe en el portal");
                            }
                            decimal dMontoRecord;
                            decimal dMontoXml;
                            //var montoRecord = Regex.Replace(record.MontoTotal, @"[^(\d|,|\.)]", "");
                            var montoRecord = record.MontoTotal;
                            var montoXml    = _dbRecords[record.UuidCfdi.ToUpper()].ElementAt(1);
                            var rfcXml      = _dbRecords[record.UuidCfdi.ToUpper()].ElementAt(0);
                            decimal.TryParse(montoRecord, out dMontoRecord);
                            decimal.TryParse(montoXml, out dMontoXml);
                            if (dMontoRecord != dMontoXml && Math.Abs(dMontoRecord - dMontoXml) > Convert.ToDecimal(0.99))
                            {
                                throw new Exception(record.UuidCfdi + "||" + "El monto del registro de Oracle en el xls (" + montoRecord + ") no coincide con el monto del xml asociado (" + montoXml + ")");
                            }
                            if (scenario != 3 && scenario != 4 && !record.SupplierNum.Equals(rfcXml))
                            {
                                throw new Exception(record.UuidCfdi + "||" + "El RFC en el registro de Oracle en el xls (" + record.SupplierNum + ") no coincide con el RFC del xml asociado (" + rfcXml + ")");
                            }
                        }

                        #endregion

                        if (scenario == 3 || scenario == 4)
                        {
                            #region Valida que los InvoiceAmount de los registros del InvoiceNum sean iguales

                            decimal.TryParse(group.First().InvoiceAmount, out invoiceAmountGroup);
                            var valor = group.Sum(x => decimal.Parse(x.InvoiceAmount));
                            //  var equals = group.All(x => decimal.Parse(x.InvoiceAmount) == invoiceAmountGroup);

                            var equals = group.Sum(x => decimal.Parse(x.InvoiceAmount)) == invoiceAmountGroup;

                            if (!equals)
                            {
                                throw new Exception("El InvoiceAmount de los registros del InvoiceNum \"" + invoiceNum + "\" no son iguales");
                            }

                            #endregion

                            #region Valida que la sumatoria de los MontoTotal sea igual al InvoiceAmount

                            var sumGroup = group.Sum(x =>
                            {
                                decimal monto;
                                decimal.TryParse(x.MontoTotal, out monto);
                                return(monto);
                            });
                            //      if (sumGroup != invoiceAmountGroup && Math.Abs(sumGroup - invoiceAmountGroup) > Convert.ToDecimal(0.9))
                            if (sumGroup != invoiceAmountGroup && (sumGroup - invoiceAmountGroup) > Convert.ToDecimal(0.9))
                            {
                                throw new Exception("La sumatoria del MontoTotal de los registros del InvoiceNum \"" + invoiceNum + "\" (" + sumGroup + ") no coincide  con el InvoiceAmount (" + invoiceAmountGroup + ")");
                            }

                            #endregion

                            #region Valida que la sumatoria de los Amount sea igual al Invoice

                            if (scenario == 4)
                            {
                                amountGroupCc += sumGroup = group.Sum(x =>
                                {
                                    decimal monto;
                                    decimal.TryParse(x.Amount, out monto);
                                    return(monto);
                                });
                                recordsCc.AddRange(group.ToList());
                                var cont = i + 1;
                                if ((i + 1) == adjacentRecords.Count)
                                {
                                    if (amountGroupCc != invoiceAmountGroup && Math.Abs(amountGroupCc - invoiceAmountGroup) > Convert.ToDecimal(0.9))
                                    {
                                        throw new Exception("La sumatoria del Amount de los registros del InvoiceNum \"" + invoiceNum + "\" (" + sumGroup + ") no coincide  con el InvoiceAmount (" + invoiceAmountGroup + ")");
                                    }
                                }
                            }

                            #endregion
                        }

                        if (scenario == 4)
                        {
                            #region Agrega todos los Records agrupados por el CC
                            if ((i + 1) == adjacentRecords.Count)
                            {
                                _reporte.AddRecords(recordsCc);
                                LogUtilities.ShowMessage("[OK=RowNumbers:" + recordsCc.First().RowNumber + "-" + recordsCc.Last().RowNumber + ", UUIDs:" + uuids + "]", 1);
                            }
                            #endregion
                        }
                        else
                        {
                            #region Agrega todos los Records del grupo
                            _reporte.AddRecords(group.ToList());
                            LogUtilities.ShowMessage("[OK=RowNumbers:" + group.First().RowNumber + "-" + group.Last().RowNumber + ", UUIDs:" + uuids + "]", 1);
                            #endregion
                        }
                    }
                    catch (Exception ex)
                    {
                        foreach (var record in group)
                        {
                            var mensaje = ex.Message;
                            try
                            {
                                var split = ex.Message.Split(new char[] { '|', '|' }, StringSplitOptions.RemoveEmptyEntries);
                                var uuid  = split.FirstOrDefault();
                                var msg   = split.LastOrDefault();
                                if (record.UuidCfdi.Equals(uuid))
                                {
                                    mensaje = msg;
                                }
                                else
                                {
                                    mensaje = "Error heredado del UUID " + uuid;
                                }
                            }
                            catch { }
                            var pair = new KeyValuePair <OracleRecord, string>(record, mensaje);
                            _failRecords.Add(pair);
                        }
                        valid = false;
                        LogUtilities.ShowMessage("[FAIL=RowNumbers:" + group.First().RowNumber + "-" + group.Last().RowNumber + ", UUIDs:" + uuids + "]: " + ex.Message, 3);
                    }
                    finally
                    {
                        LogUtilities.ShowNewLine();
                    }
                }
            }
            return(valid);
        }