示例#1
0
 /// <summary>Función para Inicializar las Comprobaciones.
 /// <para>Inicializa las Comprobaciones del archivo Json de Plantillas. Referencia: <see cref="InicializarComprobaciones(oComprobacion[])"/> se agrega la referencia ExcelAddIn.Logic para invocarla.</para>
 /// <seealso cref="InicializarComprobaciones(oComprobacion[])"/>
 /// </summary>
 public string InicializarComprobaciones(oComprobacion[] _Comprobaciones)
 {
     oPlantilla[] _Templates = Assembler.LoadJson <oPlantilla[]>($"{Access.Configuration.Path}\\jsons\\Plantillas.json");
     foreach (oPlantilla _Template in _Templates)
     {
         FileInfo _Excel = new FileInfo($"{Access.Configuration.Path}\\templates\\{_Template.Nombre}");
         using (ExcelPackage _package = new ExcelPackage(_Excel)) {
             foreach (oComprobacion _Comprobacion in _Comprobaciones.Where(o => o.IdTipoPlantilla == _Template.IdTipoPlantilla).ToArray())
             {
                 _Comprobacion.setCeldas();
                 ExcelWorksheet _workSheet = _package.Workbook.Worksheets[_Comprobacion.Destino.Anexo];
                 int            _maxValue  = _workSheet.Dimension.Rows + 1;
                 int            _maxRow    = (_workSheet.Dimension.Rows / 2) + (_workSheet.Dimension.Rows % 2);
                 for (int i = 1; i <= _maxRow; i++)
                 {
                     _Comprobacion.Destino.Fila = (_workSheet.Cells[i, 1].Text == _Comprobacion.Destino.Indice) ? i : _Comprobacion.Destino.Fila;
                     _Comprobacion.Destino.Fila = (_workSheet.Cells[(_maxValue - i), 1].Text == _Comprobacion.Destino.Indice) ? _maxValue - i : _Comprobacion.Destino.Fila;
                     if (_Comprobacion.Destino.Fila > -1)
                     {
                         oCelda[] _Celdas  = _Comprobacion.Celdas.Where(o => o.Indice == _Comprobacion.Destino.Indice && o.Anexo == _Comprobacion.Destino.Anexo).ToArray();
                         oCelda[] _cCeldas = _Comprobacion.CeldasCondicion.Where(o => o.Indice == _Comprobacion.Destino.Indice && o.Anexo == _Comprobacion.Destino.Anexo).ToArray();
                         _Comprobacion.Destino.setCeldaExcel(_workSheet.Cells[_Comprobacion.Destino.Fila, _Comprobacion.Destino.Columna], "");
                         foreach (oCelda _Celda in _Celdas)
                         {
                             _Celda.Fila = _Comprobacion.Destino.Fila;
                             _Celda.setCeldaExcel(_workSheet.Cells[_Celda.Fila, _Celda.Columna], _Comprobacion.Destino.Anexo);
                         }
                         foreach (oCelda _Celda in _cCeldas)
                         {
                             _Celda.Fila = _Comprobacion.Destino.Fila;
                             _Celda.setCeldaExcel(_workSheet.Cells[_Celda.Fila, _Celda.Columna], _Comprobacion.Destino.Anexo);
                         }
                         oCelda[] _Faltantes = _Comprobacion.Celdas.Where(o => o.Fila == -1).ToArray();
                         foreach (oCelda _Faltante in _Faltantes)
                         {
                             oCelda _Result = _Comprobaciones.Where(o => o.Destino != null && o.Destino.Indice == _Faltante.Indice && o.Destino.Anexo == _Faltante.Anexo.ToUpper()).Select(o => o.Destino).FirstOrDefault();
                             if (_Result != null)
                             {
                                 _Faltante.Fila = _Result.Fila;
                                 _Faltante.setCeldaExcel(_workSheet.Cells[_Faltante.Fila, _Faltante.Columna], _Comprobacion.Destino.Anexo);
                             }
                             if (_Result == null)
                             {
                                 ExcelWorksheet _ws = _package.Workbook.Worksheets[_Faltante.Anexo];
                                 int            _mv = _ws.Dimension.Rows + 1;
                                 int            _mr = (_ws.Dimension.Rows / 2) + (_ws.Dimension.Rows % 2);
                                 for (int j = 1; j <= _mr; j++)
                                 {
                                     _Faltante.Fila = (_ws.Cells[j, 1].Text == _Faltante.Indice) ? j : _Faltante.Fila;
                                     _Faltante.Fila = (_ws.Cells[(_mv - j), 1].Text == _Faltante.Indice) ? _mv - j : _Faltante.Fila;
                                     if (_Faltante.Fila > -1)
                                     {
                                         _Faltante.setCeldaExcel(_ws.Cells[_Faltante.Fila, _Faltante.Columna], _Comprobacion.Destino.Anexo);
                                         break;
                                     }
                                 }
                             }
                         }
                         oCelda[] _cFaltantes = _Comprobacion.CeldasCondicion.Where(o => o.Fila == -1).ToArray();
                         foreach (oCelda _Faltante in _cFaltantes)
                         {
                             oCelda _Result = _Comprobaciones.Where(o => o.Destino != null && o.Destino.Indice == _Faltante.Indice && o.Destino.Anexo == _Faltante.Anexo.ToUpper()).Select(o => o.Destino).FirstOrDefault();
                             if (_Result != null)
                             {
                                 _Faltante.Fila = _Result.Fila;
                                 _Faltante.setCeldaExcel(_workSheet.Cells[_Faltante.Fila, _Faltante.Columna], _Comprobacion.Destino.Anexo);
                             }
                             if (_Result == null)
                             {
                                 ExcelWorksheet _ws = _package.Workbook.Worksheets[_Faltante.Anexo];
                                 int            _mv = _ws.Dimension.Rows + 1;
                                 int            _mr = (_ws.Dimension.Rows / 2) + (_ws.Dimension.Rows % 2);
                                 for (int j = 1; j <= _mr; j++)
                                 {
                                     _Faltante.Fila = (_ws.Cells[j, 1].Text == _Faltante.Indice) ? j : _Faltante.Fila;
                                     _Faltante.Fila = (_ws.Cells[(_mv - j), 1].Text == _Faltante.Indice) ? _mv - j : _Faltante.Fila;
                                     if (_Faltante.Fila > -1)
                                     {
                                         _Faltante.setCeldaExcel(_ws.Cells[_Faltante.Fila, _Faltante.Columna], _Comprobacion.Destino.Anexo);
                                         break;
                                     }
                                 }
                             }
                         }
                         break;
                     }
                 }
                 _Comprobacion.setFormulaExcel();
             }
         }
     }
     return(JsonConvert.SerializeObject(_Comprobaciones));
 }
示例#2
0
        public void btnAceptar_Click(object sender, EventArgs e)
        {
            Globals.ThisAddIn._result.Clear();
            Globals.ThisAddIn._CrucesSinDiferencia.Clear();
            Globals.ThisAddIn._CrucesQueNoAplican.Clear();
            string _Path = ExcelAddIn.Access.Configuration.Path;

            oValidaCruces[] _ValidaCruces = Assembler.LoadJson <oValidaCruces[]>($"{_Path}\\jsons\\ValidacionCruces.json");

            Generales.Proteccion(false);

            try
            {
                if (!ValidaCruces(_ValidaCruces))
                {
                    this.Hide();
                    return;
                }

                this.pgbCruces.Visible   = true;
                lblTitle.Text            = "Comienzo verificación, por favor espere!! ";
                this.btnAceptar.Visible  = false;
                this.btnCancelar.Visible = false;
                int progress = 0;
                progress       += 10;
                pgbCruces.Value = progress;
                oTipoPlantilla[] _TemplateTypes = Assembler.LoadJson <oTipoPlantilla[]>($"{_Path}\\jsons\\TiposPlantillas.json");
                oCruce[]         _Cruces        = Assembler.LoadJson <oCruce[]>($"{_Path}\\jsons\\Cruces.json");

                _TotalValidaciones = _Cruces.Count();

                //List<oCruce> _result = new List<oCruce>();
                FileInfo _Excel = new FileInfo(Globals.ThisAddIn.Application.ActiveWorkbook.FullName);

                progress       += 10;
                pgbCruces.Value = progress;
                //FileInfo _Excel = new FileInfo($"{_Path}\\jsons\\SIPRED-EstadosFinancierosGeneral.xlsm");
                oTipoPlantilla _TemplateType = null;

                using (ExcelPackage _package = new ExcelPackage(_Excel))
                {
                    foreach (oTipoPlantilla _TT in _TemplateTypes)
                    {
                        if (_package.Workbook.Worksheets.Where(o => o.Name == _TT.Clave).FirstOrDefault() != null)
                        {
                            _TemplateType = _TT;
                        }
                    }
                    progress       += 10;
                    pgbCruces.Value = progress;
                    if (_TemplateType != null)
                    {
                        //INTEROOP//
                        Workbook  wb = Globals.ThisAddIn.Application.ActiveWorkbook;
                        Worksheet xlSht = null;
                        Range     currentCell = null;
                        Range     currentFind = null; Range oRng; Range vlrange;
                        string[]  Formula;
                        string    indice;
                        string    DetalleFr;

                        try
                        {
                            foreach (oCruce _Cruce in _Cruces.Where(o => o.IdTipoPlantilla == _TemplateType.IdTipoPlantilla))
                            {
                                _Cruce.setCeldas();
                                List <oCelda> CeldaNws = new List <oCelda>();

                                foreach (oCelda _Celda in _Cruce.CeldasFormula)
                                {
                                    ExcelWorksheet _workSheet = _package.Workbook.Worksheets[_Celda.Anexo];
                                    lblTitle.Text = "Verificando " + _Celda.Anexo + " " + _Celda.Original.ToString();
                                    if (_workSheet != null)
                                    {
                                        xlSht = (Worksheet)wb.Worksheets.get_Item(_Celda.Anexo);
                                        int _maxValue = xlSht.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell).Row;
                                        currentCell = (Range)xlSht.get_Range("A1", "A" + (_maxValue).ToString());
                                        currentFind = currentCell.Find(_Celda.Indice, Type.Missing, XlFindLookIn.xlValues, XlLookAt.xlPart,
                                                                       XlSearchOrder.xlByRows, XlSearchDirection.xlNext, false,
                                                                       Type.Missing, Type.Missing);

                                        if (currentFind != null)
                                        {
                                            _Celda.Fila = currentFind.Row;
                                            _Celda.setFullAddressCeldaExcel(_workSheet.Cells[_Celda.Fila, _Celda.Columna]);
                                            _Celda.Concepto = _workSheet.Cells[_Celda.Fila, 2].Text;
                                            currentCell     = (Range)xlSht.Cells[_Celda.Fila, _Celda.Columna];

                                            if (currentCell.get_Value(Type.Missing) != null)
                                            {
                                                _Celda.Valor = currentCell.get_Value(Type.Missing).ToString();
                                            }
                                            else
                                            {
                                                _Celda.Valor = "0";
                                            }
                                        }

                                        int j = 0;
                                        if (_Cruce.Formula.Contains(":") && _Cruce.Formula.Contains("SUM"))
                                        {
                                            if (_Cruce.Formula.Contains("="))
                                            {
                                                Formula = _Cruce.Formula.Split('=');
                                                for (j = 0; j < Formula.Count(); j++)
                                                {
                                                    if (Formula[j].Contains(":"))
                                                    {
                                                        DetalleFr = Formula[j];
                                                        break;
                                                    }
                                                }
                                            }

                                            if (_Cruce.CeldasFormula[j].Anexo == _Cruce.CeldasFormula[j + 1].Anexo)
                                            {
                                                ExcelWorksheet _workSheetAnx = _package.Workbook.Worksheets[_Cruce.CeldasFormula[j].Anexo];

                                                if (CeldaNws.Count() == 0)
                                                {
                                                    CeldaNws = _Cruce.CeldasFormula.ToList();
                                                }

                                                if ((_Cruce.CeldasFormula[j].Fila != _Cruce.CeldasFormula[j + 1].Fila) && (_Cruce.CeldasFormula[j].Columna == _Cruce.CeldasFormula[j + 1].Columna))
                                                {
                                                    for (int r = _Cruce.CeldasFormula[j].Fila; r < _Cruce.CeldasFormula[j + 1].Fila; r++)
                                                    {
                                                        oRng = (Range)xlSht.Cells[r, 1];
                                                        if (oRng.get_Value(Type.Missing) != null)
                                                        {
                                                            indice = oRng.get_Value(Type.Missing).ToString();

                                                            if (_Cruce.CeldasFormula[j].Indice != indice && _Cruce.CeldasFormula[j + 1].Indice != indice)
                                                            {
                                                                oCelda CeldaNw = new oCelda();
                                                                CeldaNw.Fila     = oRng.Row;
                                                                CeldaNw.Indice   = indice;
                                                                CeldaNw.Columna  = _Celda.Columna;
                                                                CeldaNw.Anexo    = _Cruce.CeldasFormula[j].Anexo;
                                                                CeldaNw.Original = "";
                                                                CeldaNw.Grupo    = j;
                                                                CeldaNw.Concepto = _workSheetAnx.Cells[CeldaNw.Fila, 2].Text;
                                                                vlrange          = (Range)xlSht.Cells[r, CeldaNw.Columna];

                                                                if (vlrange.get_Value(Type.Missing) != null)
                                                                {
                                                                    CeldaNw.Valor = vlrange.get_Value(Type.Missing).ToString();
                                                                }
                                                                else
                                                                {
                                                                    CeldaNw.Valor = "0";
                                                                }

                                                                CeldaNw.setFullAddressCeldaExcel(_workSheetAnx.Cells[CeldaNw.Fila, CeldaNw.Columna]);
                                                                if (!CeldaNws.Contains(CeldaNw))
                                                                {
                                                                    CeldaNws.Add(CeldaNw);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if ((_Cruce.CeldasFormula[j].Fila == _Cruce.CeldasFormula[j + 1].Fila && _Cruce.CeldasFormula[j].Fila != -1) && (_Cruce.CeldasFormula[j].Columna != _Cruce.CeldasFormula[j + 1].Columna))
                                                    {
                                                        Worksheet xlShtAnx = (Worksheet)wb.Worksheets.get_Item(_Cruce.CeldasFormula[j].Anexo);
                                                        CeldaNws = new List <oCelda>();
                                                        if (CeldaNws.Count() == 0)
                                                        {
                                                            CeldaNws = _Cruce.CeldasFormula.ToList();
                                                        }

                                                        for (int r = _Cruce.CeldasFormula[j].Columna; r < _Cruce.CeldasFormula[j + 1].Columna; r++)
                                                        {
                                                            if (_Cruce.CeldasFormula[j].Columna != r && _Cruce.CeldasFormula[j + 1].Columna != r)
                                                            {
                                                                oCelda CeldaNw = new oCelda();
                                                                CeldaNw.Fila     = _Cruce.CeldasFormula[j].Fila;
                                                                CeldaNw.Indice   = _Cruce.CeldasFormula[j].Indice;
                                                                CeldaNw.Columna  = r;
                                                                CeldaNw.Anexo    = _Cruce.CeldasFormula[j].Anexo;
                                                                CeldaNw.Original = "";
                                                                CeldaNw.Grupo    = j;
                                                                CeldaNw.Concepto = _Cruce.CeldasFormula[j].Concepto;
                                                                vlrange          = (Range)xlShtAnx.Cells[_Cruce.CeldasFormula[j].Fila, r];

                                                                if (vlrange.get_Value(Type.Missing) != null)
                                                                {
                                                                    CeldaNw.Valor = vlrange.get_Value(Type.Missing).ToString();
                                                                }
                                                                else
                                                                {
                                                                    CeldaNw.Valor = "0";
                                                                }

                                                                CeldaNw.setFullAddressCeldaExcel(_workSheetAnx.Cells[CeldaNw.Fila, CeldaNw.Columna]);
                                                                if (!CeldaNws.Contains(CeldaNw))
                                                                {
                                                                    CeldaNws.Add(CeldaNw);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                pgbCruces.Value = progress;
                                foreach (oCeldaCondicion _Celda in _Cruce.CeldasCondicion)
                                {
                                    ExcelWorksheet _workSheet = _package.Workbook.Worksheets[_Celda.Anexo];
                                    if (_workSheet != null)
                                    {
                                        xlSht = (Worksheet)wb.Worksheets.get_Item(_Celda.Anexo);
                                        int _maxValue = xlSht.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell).Row;
                                        currentCell = (Range)xlSht.get_Range("A1", "A" + (_maxValue).ToString());


                                        currentFind = currentCell.Find(_Celda.Indice, Type.Missing, XlFindLookIn.xlValues, XlLookAt.xlPart,
                                                                       XlSearchOrder.xlByRows, XlSearchDirection.xlNext, false,
                                                                       Type.Missing, Type.Missing);

                                        if (currentFind != null)
                                        {
                                            _Celda.Fila = currentFind.Row;
                                            _Celda.setFullAddressCeldaExcel(_workSheet.Cells[_Celda.Fila, _Celda.Columna]);
                                            _Celda.Concepto = _workSheet.Cells[_Celda.Fila, 2].Text;
                                        }
                                    }
                                }
                                //catch
                                _Cruce.setFormulaExcel();

                                if (CeldaNws.Count() > 0)
                                {
                                    _Cruce.CeldasFormula = CeldaNws.OrderBy(x => x.Indice).ToArray();
                                }

                                xlSht = (Worksheet)wb.Worksheets.get_Item("SIPRED");
                                Range    Test_Range    = (Range)xlSht.get_Range("A1");
                                string   ValorAnterior = Test_Range.get_Value(Type.Missing);
                                string[] formula;
                                Test_Range.Formula      = "=" + _Cruce.FormulaExcel;
                                _Cruce.ResultadoFormula = Test_Range.get_Value(Type.Missing).ToString();
                                xlSht.Cells[1, 1]       = ValorAnterior;// restauro

                                if (_Cruce.FormulaExcel.Contains("="))
                                {
                                    formula       = _Cruce.FormulaExcel.Split('=');
                                    Test_Range    = (Range)xlSht.get_Range("A3");
                                    ValorAnterior = Test_Range.get_Value(Type.Missing);
                                    if (chksigno.Checked)
                                    {
                                        Test_Range.Formula = "=ABS(" + formula[0] + ")-ABS(" + formula[1] + ")";
                                    }
                                    else
                                    {
                                        Test_Range.Formula = "=(" + formula[0] + " - " + formula[1] + ")";
                                    }
                                    if (!Globals.ThisAddIn.Application.WorksheetFunction.IsErr(Test_Range))
                                    {
                                        _Cruce.Diferencia = Test_Range.get_Value(Type.Missing).ToString();
                                    }
                                    else
                                    {
                                        _Cruce.Diferencia = "0";
                                    }
                                    //_Cruce.Diferencia = Test_Range.get_Value(Type.Missing).ToString();
                                    xlSht.Cells[3, 1] = ValorAnterior;// restauro
                                }

                                if (_Cruce.CondicionExcel != "")
                                {
                                    Test_Range                = (Range)xlSht.get_Range("A2");
                                    ValorAnterior             = Test_Range.get_Value(Type.Missing);
                                    Test_Range.Formula        = "=" + _Cruce.CondicionExcel;
                                    _Cruce.ResultadoCondicion = Test_Range.get_Value(Type.Missing).ToString();
                                    xlSht.Cells[2, 1]         = ValorAnterior;// restauro
                                    _Cruce.Condicion          = "[" + _Cruce.Condicion + "] = " + _Cruce.ResultadoCondicion;
                                }
                                else
                                {
                                    _Cruce.ResultadoCondicion = "si";
                                }

                                if ((_Cruce.ResultadoFormula.ToLower() == "false") && ((_Cruce.ResultadoCondicion.ToLower() == "si")))
                                //if ((_Cruce.ResultadoFormula.ToLower() == "false") && ((_Cruce.ResultadoCondicion.ToLower() == "si") || (_Cruce.CondicionExcel == "")))
                                {
                                    if ((_Cruce.Diferencia == "") || (_Cruce.Diferencia == null))
                                    {
                                        _Cruce.Diferencia = "0";
                                    }

                                    if (_Cruce.Diferencia != "0") // puede ser negativa
                                    {
                                        //calculo la diferencia
                                        if (_Cruce.FormulaExcel.Contains("="))
                                        {
                                            if (_Cruce.FormulaExcel.Contains("="))
                                            {
                                                formula            = _Cruce.FormulaExcel.Split('=');
                                                Test_Range         = (Range)xlSht.get_Range("A4");
                                                ValorAnterior      = Test_Range.get_Value(Type.Missing);
                                                Test_Range.Formula = "=" + formula[0];
                                                _Cruce.Grupo1      = Test_Range.get_Value(Type.Missing).ToString();
                                                xlSht.Cells[4, 1]  = ValorAnterior;// restauro

                                                Test_Range         = (Range)xlSht.get_Range("A5");
                                                ValorAnterior      = Test_Range.get_Value(Type.Missing);
                                                Test_Range.Formula = "=" + formula[1];
                                                _Cruce.Grupo2      = Test_Range.get_Value(Type.Missing).ToString();
                                                xlSht.Cells[5, 1]  = ValorAnterior;// restauro
                                            }
                                        }
                                        Globals.ThisAddIn._result.Add(_Cruce);
                                    }
                                    else
                                    {
                                        Globals.ThisAddIn._CrucesSinDiferencia.Add(_Cruce);
                                    }
                                }
                                else
                                {
                                    Globals.ThisAddIn._CrucesQueNoAplican.Add(_Cruce);
                                }

                                if (progress <= 70)
                                {
                                    progress       += 10;
                                    pgbCruces.Value = progress;
                                }
                            }

                            Generales.Proteccion(true);
                        }
                        catch (Exception ex)
                        {
                            Generales.Proteccion(true);
                            MessageBox.Show(ex.Message, "Cruces", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        progress += 5;

                        pgbCruces.Value = progress;
                    }
                    else if (_TemplateType == null)
                    {
                        MessageBox.Show("Archivo no válido, favor de generar el archivo mediante el AddIn D.SAT", "Información Incorrecta", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                progress       += 15;
                pgbCruces.Value = progress;
                if (Globals.ThisAddIn._result.Count > 0 || Globals.ThisAddIn._CrucesQueNoAplican.Count > 0 || Globals.ThisAddIn._CrucesSinDiferencia.Count > 0)
                {
                    Globals.ThisAddIn.TaskPane.Visible = true;
                    FIllValidacionDeCruceUC(Globals.ThisAddIn._result.ToArray());
                    //CreatePDF(Globals.ThisAddIn._result.ToArray(), _Cruces, _Path, _Excel.Name);
                }
                else
                {
                    MessageBox.Show("No se encontraron diferencias", "Información Correcta", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Exception Raised: {ex.Message.ToString()}");
            }
        }