Exemple #1
0
        private void LlenarColumnasAnio(int iAnio)
        {
            // Se borran las columnas de semanas
            for (int iCol = (this.dgvDatos.Columns.Count - 1); iCol >= this.iColumnasFijas; iCol--)
            {
                this.dgvDatos.Columns.RemoveAt(iCol);
            }

            // Se agregan las nuevas columnas
            DateTime dDia    = new DateTime(iAnio, DateTime.Now.Month, DateTime.Now.Day);
            DateTime dIni    = UtilTheos.InicioSemanaSabAVie(new DateTime(iAnio, 1, 1));
            int      iColSem = 0;

            while (dIni.Year <= iAnio)
            {
                string sEnc = string.Format("{0}\n{1}", dIni.ToString("dd/MMM"), dIni.AddDays(6).ToString("dd/MMM"));
                var    oCol = new DataGridViewTextBoxColumn()
                {
                    Name = dIni.ToShortDateString(), HeaderText = sEnc
                };
                oCol.FormatoMoneda();
                this.dgvDatos.Columns.Add(oCol);

                if (dDia >= dIni && dDia < dIni.AddDays(7))
                {
                    iColSem = this.dgvDatos.Columns.Count;
                }

                dIni = dIni.AddDays(7);
            }

            this.dgvDatos.FirstDisplayedScrollingColumnIndex = (iColSem - 1);

            this.FormatoColumnas();
        }
Exemple #2
0
        protected override void LlenarPartes(int iGridFuente, int iIdPrincipal, int iId)
        {
            this.dgvPartes.Rows.Clear();
            if (iId <= 0)
            {
                return;
            }

            var oParams = this.ObtenerParametros();
            var oDatos  = Datos.ExecuteProcedure <pauCuadroDeControlGeneralNuevo_Result>("pauCuadroDeControlGeneral", oParams);

            var oConsulta = oDatos.Where(c => c.VendedorID == iIdPrincipal && c.Fecha >= this.dtpDesde.Value.Date);

            switch (iGridFuente)
            {
            case CuadroMultiple.GridFuente.Grupos:
                this.LlenarDetalle(oConsulta, iId);

                /* foreach (var oReg in oConsulta)
                 * {
                 *  var oVentasDetV = General.GetListOf<VentasDetalleView>(c => c.VentaID == oReg.VentaID && c.LineaID == iId);
                 *  foreach (var oDet in oVentasDetV)
                 *      this.dgvPartes.Rows.Add(oReg.Fecha, oReg.Folio, oDet.NumeroParte, oDet.NombreParte,
                 *          ((oDet.PrecioUnitario - oDet.CostoConDescuento) * oDet.Cantidad));
                 * } */
                return;

            case CuadroMultiple.GridFuente.Meses:
            case CuadroMultiple.GridFuente.Semanas:
                DateTime dIni, dFin;
                if (iGridFuente == CuadroMultiple.GridFuente.Semanas)
                {
                    dIni = UtilTheos.InicioSemanaSabAVie(this.dtpDesde.Value.Year, iId);
                    dFin = dIni.AddDays(6).AddDays(1);
                }
                else
                {
                    dIni = new DateTime(this.dtpDesde.Value.Year, iId, 1);
                    dFin = dIni.DiaUltimo().AddDays(1);
                }

                oConsulta = oConsulta.Where(c => (c.Fecha >= dIni && c.Fecha < dFin));
                break;

            case CuadroMultiple.GridFuente.Sucursales:
                oConsulta = oConsulta.Where(c => c.SucursalID == iId);
                break;
            }

            // Se llena el grid
            this.LlenarDetalle(oConsulta, null);

            /* foreach (var oReg in oConsulta)
             * {
             *  var oVentasDetV = General.GetListOf<VentasDetalleView>(c => c.VentaID == oReg.VentaID);
             *  foreach (var oDet in oVentasDetV)
             *      this.dgvPartes.Rows.Add(oReg.Fecha, oReg.Folio, oDet.NumeroParte, oDet.NombreParte, ((oDet.PrecioUnitario - oDet.CostoConDescuento) * oDet.Cantidad));
             * }*/
        }
Exemple #3
0
        protected void LlenarPartesCompras(int iGridFuente, int iIdPrincipal, int iId)
        {
            this.dgvPartes.Rows.Clear();
            if (iId <= 0)
            {
                return;
            }

            var oParams = this.ObtenerParametrosCompras();
            var oDatos  = Datos.ExecuteProcedure <pauCuadroDeControlCompras_Result>("pauCuadroDeControlCompras", oParams);

            var oConsulta = oDatos.Where(c => c.ProveedorID == iId);

            switch (iGridFuente)
            {
            case CuadroMultiple.GridFuente.Grupos:
                foreach (var oReg in oConsulta)
                {
                    var oDetV = Datos.GetListOf <MovimientoInventarioDetalleView>(c => c.MovimientoInventarioID == oReg.MovimientoInventarioID && c.LineaID == iId);
                    foreach (var oRegDet in oDetV)
                    {
                        this.dgvPartes.Rows.Add(oReg.Fecha, oReg.FolioFactura, oRegDet.NumeroParte, oRegDet.NombreParte
                                                , (oRegDet.PrecioUnitarioConDescuento * oRegDet.Cantidad));
                    }
                }
                return;

            case CuadroMultiple.GridFuente.Meses:
            case CuadroMultiple.GridFuente.Semanas:
                DateTime dIni, dFin;
                if (iGridFuente == CuadroMultiple.GridFuente.Semanas)
                {
                    dIni = UtilTheos.InicioSemanaSabAVie(this.dtpDesde.Value.Year, iId);
                    dFin = dIni.AddDays(6).AddDays(1);
                }
                else
                {
                    dIni = new DateTime(this.dtpDesde.Value.Year, iId, 1);
                    dFin = dIni.DiaUltimo().AddDays(1);
                }

                oConsulta = oConsulta.Where(c => (c.Fecha >= dIni && c.Fecha < dFin));
                break;
            }

            // Se llena el grid
            foreach (var oReg in oConsulta)
            {
                var oDetV = Datos.GetListOf <MovimientoInventarioDetalleView>(c => c.MovimientoInventarioID == oReg.MovimientoInventarioID);
                foreach (var oRegDet in oDetV)
                {
                    this.dgvPartes.Rows.Add(oReg.Fecha, oReg.FolioFactura, oRegDet.NumeroParte, oRegDet.NombreParte
                                            , (oRegDet.PrecioUnitarioConDescuento * oRegDet.Cantidad));
                }
            }
        }
Exemple #4
0
        private void CargarDatos()
        {
            Cargando.Mostrar();

            var oFuenteT = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold);

            // Se limpian los datos
            this.dgvDatos.Rows.Clear();
            foreach (var oSerie in this.chrPorSemana.Series)
            {
                oSerie.Points.Clear();
            }

            // Se llenan las columnas del año
            int iAnio = Util.Entero(this.cmbAnio.Text);

            this.LlenarColumnasAnio(iAnio);

            // Se agrega la fila de ingresos
            int iFilaIngresos = this.dgvDatos.Rows.Add("+ Ingresos");

            this.dgvDatos.Rows[iFilaIngresos].DefaultCellStyle.Font = oFuenteT;
            decimal mTotal = 0, mPromedio = 0;

            // Se agrega la fila de saldo inicial
            int iFilaSaldoInicial = this.dgvDatos.Rows.Add("Saldo inicial");

            // Se obtienen los datos para las ventas
            // int iSucursalID = Util.ConvertirEntero(this.cmbSucursal.SelectedValue);
            DateTime dDesde       = new DateTime(iAnio, 1, 1);
            DateTime dHasta       = new DateTime(iAnio, 12, 31);
            DateTime dDesdeSemUno = UtilTheos.InicioSemanaSabAVie(dDesde);
            var      oParams      = new Dictionary <string, object>();

            // oParams.Add("SucursalID", (iSucursalID == 0 ? null : (int?)iSucursalID));
            oParams.Add("Pagadas", true);
            oParams.Add("Cobradas", false);
            oParams.Add("Solo9500", false);
            oParams.Add("OmitirDomingo", false);
            oParams.Add("Desde", dDesdeSemUno);
            oParams.Add("Hasta", dHasta);
            var oDatos   = Datos.ExecuteProcedure <pauCuadroDeControlGeneralNuevo_Result>("pauCuadroDeControlGeneral", oParams);
            var oSemanas = oDatos.Where(c => c.Fecha >= dDesdeSemUno)
                           .GroupBy(c => new { Semana = UtilTheos.InicioSemanaSabAVie(c.Fecha) })
                           .Select(c => new { c.Key.Semana, PrecioSinIva = c.Sum(s => s.PrecioSinIvaActual) })
                           .OrderBy(c => c.Semana);

            mTotal    += oSemanas.Sum(c => c.PrecioSinIva).Valor();
            mPromedio += oSemanas.Average(c => c.PrecioSinIva).Valor();
            // Se agrega la fila de ventas
            int iFila = this.dgvDatos.Rows.Add("Ventas", oSemanas.Sum(c => c.PrecioSinIva), oSemanas.Average(c => c.PrecioSinIva));

            foreach (var oReg in oSemanas)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value         = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.PrecioSinIva);
                this.dgvDatos[sSemana, iFilaIngresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaIngresos].Value) + oReg.PrecioSinIva);
            }

            // Se obtienen datos para varios grupos
            DateTime dHastaMas1 = dHasta.AddDays(1);
            // DateTime dDesdeMas1 = dDesde.AddDays(1);
            var oReinversiones = Datos.GetListOf <ContaPolizasDetalleAvanzadoView>(c => c.FechaPoliza >= dDesdeSemUno && c.FechaPoliza < dHastaMas1 &&
                                                                                   (
                                                                                       c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarLargoPlazo ||
                                                                                       (c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarCortoPlazo && c.ContaCuentaAuxiliarID != Cat.ContaCuentasAuxiliares.TarjetaDeCredito) ||
                                                                                       c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.AcreedoresDiversos ||
                                                                                       c.ContaSubcuentaID == Cat.ContaSubcuentas.ActivoFijo
                                                                                   )
                                                                                   );

            // Se agrega la fila de préstamos
            var oPrestamos = oReinversiones.Where(c => c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarLargoPlazo ||
                                                  c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarCortoPlazo)
                             .GroupBy(c => UtilTheos.InicioSemanaSabAVie(c.FechaPoliza.Valor()))
                             .Select(c => new { Semana = c.Key, Importe = c.Sum(s => s.Abono) });

            mTotal += oPrestamos.Sum(c => c.Importe);
            decimal mPromedioAct = (oPrestamos.Count() > 0 ? oPrestamos.Average(c => c.Importe) : 0);

            mPromedio += mPromedioAct;
            iFila      = this.dgvDatos.Rows.Add("Préstamos", oPrestamos.Sum(c => c.Importe), mPromedioAct);
            foreach (var oReg in oPrestamos)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value         = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaIngresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaIngresos].Value) + oReg.Importe);
            }

            // Se llenan los totales de ingresos
            this.dgvDatos["Total", iFilaIngresos].Value    = mTotal;
            this.dgvDatos["Promedio", iFilaIngresos].Value = (mPromedio / 2);

            // Se agrega la fila de egresos
            int iFilaEgresos = this.dgvDatos.Rows.Add("- Egresos");

            this.dgvDatos.Rows[iFilaEgresos].DefaultCellStyle.Font = oFuenteT;
            mTotal = mPromedio = 0;

            // Se obtienen los datos para los gastos
            oParams.Clear();
            oParams.Add("Desde", dDesdeSemUno);
            oParams.Add("Hasta", dHasta);
            var oGastos    = Datos.ExecuteProcedure <pauContaCuentasPorSemana_Result>("pauContaCuentasPorSemana", oParams);
            var oGastosSem = ContaProc.GastosSemanalizados(oGastos, Util.FechaHora(this.dgvDatos.Columns[this.dgvDatos.Columns.Count - 1].Name));

            oGastosSem = oGastosSem.Where(c => c.Semana >= dDesdeSemUno)
                         .GroupBy(c => new { c.Semana }).Select(c => new ContaProc.GastoSem()
            {
                Semana = c.Key.Semana, Importe = c.Sum(s => s.Importe)
            }).ToList();
            mTotal    += oGastosSem.Sum(c => c.Importe);
            mPromedio += oGastosSem.Average(c => c.Importe);
            // Se agrega la fila de los Gastos
            iFila = this.dgvDatos.Rows.Add("Gastos", oGastosSem.Sum(c => c.Importe), oGastosSem.Average(c => c.Importe));
            foreach (var oReg in oGastosSem)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value        = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEgresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEgresos].Value) + oReg.Importe);
            }

            // Para las compras
            var oCompras = Datos.GetListOf <ProveedoresPolizasDetalleAvanzadoView>(c => c.Fecha >= dDesdeSemUno && c.Fecha < dHastaMas1 &&
                                                                                   (c.OrigenID == Cat.OrigenesPagosAProveedores.PagoDirecto || c.OrigenID == Cat.OrigenesPagosAProveedores.PagoDeCaja))
                           .GroupBy(c => new { Semana = UtilTheos.InicioSemanaSabAVie(c.Fecha.Valor()) })
                           .Select(c => new { c.Key.Semana, Importe = c.Sum(s => s.Subtotal) })
                           .OrderBy(c => c.Semana);

            mTotal      += oCompras.Sum(c => c.Importe);
            mPromedioAct = (oCompras.Count() > 0 ? oCompras.Average(c => c.Importe) : 0);
            mPromedio   += mPromedioAct;
            // Se agrega la fila de compras
            iFila = this.dgvDatos.Rows.Add("Compras", oCompras.Sum(c => c.Importe), mPromedioAct);
            foreach (var oReg in oCompras)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value        = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEgresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEgresos].Value) + oReg.Importe);
            }

            // Para las deudas
            var oDeudas = oReinversiones.Where(c => c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarLargoPlazo ||
                                               c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarCortoPlazo || c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.AcreedoresDiversos)
                          .GroupBy(c => UtilTheos.InicioSemanaSabAVie(c.FechaPoliza.Valor()))
                          .Select(c => new { Semana = c.Key, Importe = c.Sum(s => s.Cargo) });

            mTotal      += oDeudas.Sum(c => c.Importe);
            mPromedioAct = (oDeudas.Count() > 0 ? oDeudas.Average(c => c.Importe) : 0);
            mPromedio   += mPromedioAct;
            // Se agrega la fila de deudas
            iFila = this.dgvDatos.Rows.Add("Deudas", oDeudas.Sum(c => c.Importe), mPromedioAct);
            foreach (var oReg in oDeudas)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value        = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEgresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEgresos].Value) + oReg.Importe);
            }

            // Para las inversiones
            var oInversiones = oReinversiones.Where(c => c.ContaSubcuentaID == Cat.ContaSubcuentas.ActivoFijo)
                               .GroupBy(c => UtilTheos.InicioSemanaSabAVie(c.FechaPoliza.Valor()))
                               .Select(c => new { Semana = c.Key, Importe = c.Sum(s => s.Cargo) });

            mTotal      += oInversiones.Sum(c => c.Importe);
            mPromedioAct = (oInversiones.Count() > 0 ? oInversiones.Average(c => c.Importe) : 0);
            mPromedio   += mPromedioAct;
            // Se agrega la fila de deudas
            iFila = this.dgvDatos.Rows.Add("Inversiones", oInversiones.Sum(c => c.Importe), mPromedioAct);
            foreach (var oReg in oInversiones)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value        = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEgresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEgresos].Value) + oReg.Importe);
            }

            // Para lo de Isidro y Don Isidro
            var oGastosSemEsp = ContaProc.GastosSemanalizados(Datos.GetListOf <ContaEgresosDevengadoEspecialCuentasView>(c => c.Fecha >= dDesdeSemUno && c.Fecha < dHastaMas1)
                                                              , Util.FechaHora(this.dgvDatos.Columns[this.dgvDatos.Columns.Count - 1].Name));

            oGastosSemEsp = oGastosSemEsp.Where(c => c.Semana >= dDesdeSemUno).ToList();
            mTotal       += oGastosSemEsp.Sum(c => c.Importe);
            mPromedioAct  = (oGastosSemEsp.Count() > 0 ? oGastosSemEsp.Average(c => c.Importe) : 0);
            mPromedio    += mPromedioAct;
            // Se agregan las filas
            string sDuenio = "";

            foreach (var oReg in oGastosSemEsp)
            {
                if (sDuenio != oReg.Grupo)
                {
                    sDuenio = oReg.Grupo;
                    iFila   = this.dgvDatos.Rows.Add(sDuenio, oGastosSemEsp.Where(c => c.Grupo == sDuenio).Sum(c => c.Importe)
                                                     , oGastosSemEsp.Where(c => c.Grupo == sDuenio).Average(c => c.Importe));
                }

                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value        = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEgresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEgresos].Value) + oReg.Importe);
            }

            // Se llenan los totales de egresos
            this.dgvDatos["Total", iFilaEgresos].Value    = mTotal;
            this.dgvDatos["Promedio", iFilaEgresos].Value = (mPromedio / 2);

            // Ajuste especial para el año 2015, se quitan los datos antes del 01 de Julio
            if (dDesde.Year == 2015)
            {
                DateTime dInicio = new DateTime(2015, 6, 1);
                foreach (DataGridViewRow oFila in this.dgvDatos.Rows)
                {
                    mPromedio = 0;
                    for (int iCol = this.iColumnasFijas; iCol < this.dgvDatos.Columns.Count; iCol++)
                    {
                        DateTime dSem = Util.FechaHora(this.dgvDatos.Columns[iCol].Name);
                        oFila.Cells["Total"].Value = 0;
                        if (dSem >= dInicio)
                        {
                            mPromedio++;
                            oFila.Cells["Total"].Value = (Util.Decimal(oFila.Cells["Total"].Value) + Util.Decimal(oFila.Cells[iCol].Value));
                        }
                        else
                        {
                            oFila.Cells[iCol].Value = null;
                        }
                    }
                    oFila.Cells["Promedio"].Value = (Util.Decimal(oFila.Cells["Total"].Value) / mPromedio);
                }
            }

            // Se agrega el saldo final
            int iFilaSaldo = this.dgvDatos.Rows.Add("= Saldo final");

            this.dgvDatos.Rows[iFilaSaldo].DefaultCellStyle.Font = oFuenteT;
            foreach (DataGridViewColumn oCol in this.dgvDatos.Columns)
            {
                if (oCol.Index == 0)
                {
                    continue;
                }

                // Se modifica el saldo inicial de la semana actual, y se suma a la fila de ingresos
                if (oCol.Index >= this.iColumnasFijas)
                {
                    this.dgvDatos[oCol.Index, iFilaIngresos].Value = (
                        Util.Decimal(this.dgvDatos[oCol.Index, iFilaIngresos].Value)
                        + Util.Decimal(this.dgvDatos[oCol.Index, iFilaSaldoInicial].Value)
                        );
                }

                decimal mSaldo = (
                    Util.Decimal(this.dgvDatos[oCol.Index, iFilaIngresos].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaEgresos].Value)
                    );
                this.dgvDatos[oCol.Index, iFilaSaldo].Value = mSaldo;

                // Se llena el saldo inicial de la siguiente semana, si hay
                if (oCol.Index >= this.iColumnasFijas && oCol.Index < (this.dgvDatos.Columns.Count - 1))
                {
                    this.dgvDatos[oCol.Index + 1, iFilaSaldoInicial].Value = mSaldo;
                }
            }

            // Se llena la gráfica, en base al grid ya cargado
            for (int iCol = this.iColumnasFijas; iCol < this.dgvDatos.Columns.Count; iCol++)
            {
                this.chrPorSemana.Series["Ventas"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaIngresos + 1].Value));
                this.chrPorSemana.Series["Prestamos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaIngresos + 2].Value));
                this.chrPorSemana.Series["Gastos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 1].Value));
                this.chrPorSemana.Series["Compras"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 2].Value));
                this.chrPorSemana.Series["Deudas"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 3].Value));
                this.chrPorSemana.Series["Inversiones"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 4].Value));
                if (this.dgvDatos.Rows.Count > 10)
                {
                    this.chrPorSemana.Series["Isidro"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 5].Value));
                    this.chrPorSemana.Series["DonIsidro"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEgresos + 6].Value));
                }
            }

            Cargando.Cerrar();
        }
Exemple #5
0
        public void CargarDatos()
        {
            Cargando.Mostrar();

            var      oParams = new Dictionary <string, object>();
            int      iAnio   = Util.Entero(this.cmbAnio.Text);
            DateTime dDesde  = new DateTime(iAnio, 1, 1);
            DateTime dHasta  = new DateTime(iAnio, 12, 31);

            oParams.Add("Desde", dDesde);
            oParams.Add("Hasta", dHasta);
            oParams.Add("AfectaMetas", this.chkAfectaMetas.Checked);

            // Se llenan los datos
            var oDatos = Datos.ExecuteProcedure <pauContaCuentasPorSemana_Result>("pauContaCuentasPorSemana", oParams);

            this.tgvDatos.Nodes.Clear();
            TreeGridNode oNodoSucursal = null, oNodoCuenta = null, oNodoSubcuenta = null, oNodoCuentaDeMayor = null, oNodoCuentaAuxiliar = null;
            string       sSucursal = "", sCuenta = "", sSubcuenta = "", sCuentaDeMayor = "", sCuentaAuxiliar = "";

            foreach (var oReg in oDatos)
            {
                // Nodo de Sucursal
                if (oReg.Sucursal != sSucursal)
                {
                    sSucursal         = oReg.Sucursal;
                    oNodoSucursal     = this.tgvDatos.Nodes.Add(sSucursal);
                    oNodoSucursal.Tag = false; // Se marca como false para indicar que no es devengado especial
                    sCuenta           = "";
                }
                // Nodo de Cuenta
                if (oReg.Cuenta != sCuenta)
                {
                    sCuenta     = oReg.Cuenta;
                    oNodoCuenta = oNodoSucursal.Nodes.Add(sCuenta);
                    sSubcuenta  = "";
                }
                // Nodo de Subcuenta
                if (oReg.Subcuenta != sSubcuenta)
                {
                    sSubcuenta     = oReg.Subcuenta;
                    oNodoSubcuenta = oNodoCuenta.Nodes.Add(sSubcuenta);
                    sCuentaDeMayor = "";
                }
                // Nodo de Cuenta de mayor
                if (oReg.CuentaDeMayor != sCuentaDeMayor)
                {
                    sCuentaDeMayor     = oReg.CuentaDeMayor;
                    oNodoCuentaDeMayor = oNodoSubcuenta.Nodes.Add(sCuentaDeMayor);
                    sCuentaAuxiliar    = "";
                }
                // Se agrega la cuenta auxiliar
                if (oReg.CuentaAuxiliar != sCuentaAuxiliar)
                {
                    sCuentaAuxiliar     = oReg.CuentaAuxiliar;
                    oNodoCuentaAuxiliar = oNodoCuentaDeMayor.Nodes.Add(oReg.CuentaAuxiliar);
                }
                // Se meten los valores de las semanas, y los totales
                if (oReg.PeriodicidadMes.HasValue)
                {
                    DateTime dInicioPer = oReg.Fecha.DiaPrimero().Date;
                    DateTime dFinPer = dInicioPer.AddMonths(oReg.PeriodicidadMes.Valor()).AddDays(-1);
                    decimal  mImporteDiario = (oReg.ImporteDev.Valor() / ((dFinPer - dInicioPer).Days + 1));
                    decimal  mImporte; int iDias;
                    DateTime dIniSem = UtilTheos.InicioSemanaSabAVie(dInicioPer).Date;
                    for (int iCol = (this.tgvDatos.Columns["Sem" + dIniSem.ToString("d")].Index); iCol < this.tgvDatos.Columns.Count; iCol++)
                    {
                        // Se verifica si se debe de seguir semanalizando
                        if (oReg.FinSemanalizar.HasValue && oReg.FinSemanalizar <= dIniSem)
                        {
                            break;
                        }
                        // Se verifica la fecha final,
                        if (oNodoCuentaAuxiliar.Tag != null && dIniSem > dFinPer)
                        {
                            break;
                        }

                        // Se calcula el importe correspondiente
                        DateTime dFinSem = dIniSem.AddDays(6);
                        if (dIniSem < dInicioPer)
                        {
                            iDias = dFinSem.Day;
                        }
                        else if (dIniSem <= dFinPer && dFinSem > dFinPer)
                        {
                            iDias = ((dIniSem.DiaUltimo().Day - dIniSem.Day) + 1);
                        }
                        else if (dIniSem > dFinPer && (dIniSem - dFinPer).Days < 7)
                        {
                            iDias = (dIniSem.Day - 1);
                            iCol--;
                        }
                        else
                        {
                            iDias = 7;
                            // Se verifica si es

                            /* if (oReg.CuentaAuxiliar == sCuentaAuxiliar)
                             * {
                             *  oNodoCuentaAuxiliar.Cells[iCol].Value = 0;
                             *  oNodoCuentaAuxiliar.Cells[iCol].Tag = null;
                             * } */
                        }
                        // int iDias = (dIniSem < dInicioPer ? dFinSem.Day : (dFinSem > dFinPer ? ((dIniSem.DiaUltimo().Day - dIniSem.Day) + 1) : 7));
                        mImporte = (mImporteDiario * iDias);
                        dIniSem  = dIniSem.AddDays(7);

                        // Para guardar los datos relacionados
                        if (oNodoCuentaAuxiliar.Cells[iCol].Tag == null)
                        {
                            oNodoCuentaAuxiliar.Cells[iCol].Tag = new List <int>();
                        }
                        (oNodoCuentaAuxiliar.Cells[iCol].Tag as List <int>).Add(oReg.ContaEgresoDevengadoID);

                        // Para llenar las celdas
                        oNodoCuentaAuxiliar.Cells[iCol].Value = (Util.Decimal(oNodoCuentaAuxiliar.Cells[iCol].Value) + mImporte); // mImporte;

                        /*
                         * // Para los niveles superiores
                         * oNodoCuentaDeMayor.Cells[iCol].Value = (Util.ConvertirDecimal(oNodoCuentaDeMayor.Cells[iCol].Value) + mImporte);
                         * oNodoSubcuenta.Cells[iCol].Value = (Util.ConvertirDecimal(oNodoSubcuenta.Cells[iCol].Value) + mImporte);
                         * oNodoCuenta.Cells[iCol].Value = (Util.ConvertirDecimal(oNodoCuenta.Cells[iCol].Value) + mImporte);
                         * oNodoSucursal.Cells[iCol].Value = (Util.ConvertirDecimal(oNodoSucursal.Cells[iCol].Value) + mImporte);
                         *
                         * // Para la columna de totales
                         * oNodoCuentaAuxiliar.Cells[1].Value = (Util.ConvertirDecimal(oNodoCuentaAuxiliar.Cells[1].Value) + mImporte); // mImporteTotal;
                         * oNodoCuentaDeMayor.Cells[1].Value = (Util.ConvertirDecimal(oNodoCuentaDeMayor.Cells[1].Value) + mImporte);
                         * oNodoSubcuenta.Cells[1].Value = (Util.ConvertirDecimal(oNodoSubcuenta.Cells[1].Value) + mImporte);
                         * oNodoCuenta.Cells[1].Value = (Util.ConvertirDecimal(oNodoCuenta.Cells[1].Value) + mImporte);
                         * oNodoSucursal.Cells[1].Value = (Util.ConvertirDecimal(oNodoSucursal.Cells[1].Value) + mImporte);
                         */
                    }

                    // Se marca la cuenta, para que ya no se semanalice hasta el final
                    if (oNodoCuentaAuxiliar.Tag == null)
                    {
                        oNodoCuentaAuxiliar.Tag = true;
                    }
                }
                else
                {
                    DateTime dIniSem = UtilTheos.InicioSemanaSabAVie(oReg.Fecha).Date;
                    int      iCol    = this.tgvDatos.Columns["Sem" + dIniSem.ToString("d")].Index;
                    // Para guardar los datos relacionados
                    if (oNodoCuentaAuxiliar.Cells[iCol].Tag == null)
                    {
                        oNodoCuentaAuxiliar.Cells[iCol].Tag = new List <int>();
                    }
                    (oNodoCuentaAuxiliar.Cells[iCol].Tag as List <int>).Add(oReg.ContaEgresoDevengadoID);
                    // Para llenar el importe
                    oNodoCuentaAuxiliar.Cells[iCol].Value = (Util.Decimal(oNodoCuentaAuxiliar.Cells[iCol].Value) + oReg.ImporteDev);
                }
            }

            // Se llenan los datos de devengados especiales
            DateTime dHastaMas1 = dHasta.AddDays(1);
            var      oDevEsp    = Datos.GetListOf <ContaEgresosDevengadoEspecialCuentasView>(c => c.Fecha >= dDesde && c.Fecha < dHastaMas1)
                                  .OrderBy(c => c.Duenio).ThenBy(c => c.Cuenta).ThenBy(c => c.Subcuenta).ThenBy(c => c.CuentaDeMayor).ThenBy(c => c.CuentaAuxiliar)
                                  .ThenByDescending(c => c.Fecha);
            TreeGridNode oNodoDuenio = oNodoCuenta = oNodoSubcuenta = oNodoCuentaDeMayor = oNodoCuentaAuxiliar = null;
            string       sDuenio     = sCuenta = sSubcuenta = sCuentaDeMayor = sCuentaAuxiliar = "";

            foreach (var oReg in oDevEsp)
            {
                // Nodo de Sucursal
                if (oReg.Duenio != sDuenio)
                {
                    sDuenio         = oReg.Duenio;
                    oNodoDuenio     = this.tgvDatos.Nodes.Add(sDuenio);
                    oNodoDuenio.Tag = true; // Se marca como true para indicar que es devengado especial
                    sCuenta         = "";
                }
                // Nodo de Cuenta
                if (oReg.Cuenta != sCuenta)
                {
                    sCuenta     = oReg.Cuenta;
                    oNodoCuenta = oNodoDuenio.Nodes.Add(sCuenta);
                    sSubcuenta  = "";
                }
                // Nodo de Subcuenta
                if (oReg.Subcuenta != sSubcuenta)
                {
                    sSubcuenta     = oReg.Subcuenta;
                    oNodoSubcuenta = oNodoCuenta.Nodes.Add(sSubcuenta);
                    sCuentaDeMayor = "";
                }
                // Nodo de Cuenta de mayor
                if (oReg.CuentaDeMayor != sCuentaDeMayor)
                {
                    sCuentaDeMayor     = oReg.CuentaDeMayor;
                    oNodoCuentaDeMayor = oNodoSubcuenta.Nodes.Add(sCuentaDeMayor);
                    sCuentaAuxiliar    = "";
                }
                // Se agrega la cuenta auxiliar
                if (oReg.CuentaAuxiliar != sCuentaAuxiliar)
                {
                    sCuentaAuxiliar     = oReg.CuentaAuxiliar;
                    oNodoCuentaAuxiliar = oNodoCuentaDeMayor.Nodes.Add(oReg.CuentaAuxiliar);
                }

                // Se meten los valores de las semanas, y los totales

                /* DateTime dIniSem = UtilTheos.InicioSemanaSabAVie(oReg.Fecha).Date;
                 * int iCol = this.tgvDatos.Columns["Sem" + dIniSem.ToString("d")].Index;
                 * // Para llenar el importe
                 * oNodoCuentaAuxiliar.Cells[iCol].Value = (Util.ConvertirDecimal(oNodoCuentaAuxiliar.Cells[iCol].Value) + oReg.ImporteDev);
                 */
                if (oReg.PeriodicidadMes.HasValue)
                {
                    DateTime dInicioPer = oReg.Fecha.DiaPrimero().Date;
                    DateTime dFinPer = dInicioPer.AddMonths(oReg.PeriodicidadMes.Valor()).AddDays(-1);
                    decimal  mImporteDiario = (oReg.ImporteDev / ((dFinPer - dInicioPer).Days + 1));
                    decimal  mImporte; int iDias;
                    DateTime dIniSem = UtilTheos.InicioSemanaSabAVie(dInicioPer).Date;
                    for (int iCol = (this.tgvDatos.Columns["Sem" + dIniSem.ToString("d")].Index); iCol < this.tgvDatos.Columns.Count; iCol++)
                    {
                        // Se verifica si se debe de seguir semanalizando
                        if (oReg.FinSemanalizar.HasValue && oReg.FinSemanalizar <= dIniSem)
                        {
                            break;
                        }
                        // Se verifica la fecha final,
                        if (oNodoCuentaAuxiliar.Tag != null && dIniSem > dFinPer)
                        {
                            break;
                        }

                        // Se calcula el importe correspondiente
                        DateTime dFinSem = dIniSem.AddDays(6);
                        if (dIniSem < dInicioPer)
                        {
                            iDias = dFinSem.Day;
                        }
                        else if (dIniSem <= dFinPer && dFinSem > dFinPer)
                        {
                            iDias = ((dIniSem.DiaUltimo().Day - dIniSem.Day) + 1);
                        }
                        else if (dIniSem > dFinPer && (dIniSem - dFinPer).Days < 7)
                        {
                            iDias = (dIniSem.Day - 1);
                            iCol--;
                        }
                        else
                        {
                            iDias = 7;
                        }
                        // int iDias = (dIniSem < dInicioPer ? dFinSem.Day : (dFinSem > dFinPer ? ((dIniSem.DiaUltimo().Day - dIniSem.Day) + 1) : 7));
                        mImporte = (mImporteDiario * iDias);
                        dIniSem  = dIniSem.AddDays(7);

                        // Para guardar los datos relacionados, para el grid de detalle devengado
                        if (oNodoCuentaAuxiliar.Cells[iCol].Tag == null)
                        {
                            oNodoCuentaAuxiliar.Cells[iCol].Tag = new List <int>();
                        }
                        (oNodoCuentaAuxiliar.Cells[iCol].Tag as List <int>).Add(oReg.ContaEgresoDevengadoEspecialID);

                        // Para llenar las celdas
                        oNodoCuentaAuxiliar.Cells[iCol].Value = (Util.Decimal(oNodoCuentaAuxiliar.Cells[iCol].Value) + mImporte); // mImporte;
                    }

                    // Se marca la cuenta, para que ya no se semanalice hasta el final
                    if (oNodoCuentaAuxiliar.Tag == null)
                    {
                        oNodoCuentaAuxiliar.Tag = true;
                    }
                }
                else
                {
                    DateTime dIniSem = UtilTheos.InicioSemanaSabAVie(oReg.Fecha).Date;
                    int      iCol    = this.tgvDatos.Columns["Sem" + dIniSem.ToString("d")].Index;
                    // Para guardar los datos relacionados, para el grid de detalle devengado
                    if (oNodoCuentaAuxiliar.Cells[iCol].Tag == null)
                    {
                        oNodoCuentaAuxiliar.Cells[iCol].Tag = new List <int>();
                    }
                    (oNodoCuentaAuxiliar.Cells[iCol].Tag as List <int>).Add(oReg.ContaEgresoDevengadoEspecialID);
                    // Para llenar el importe
                    oNodoCuentaAuxiliar.Cells[iCol].Value = (Util.Decimal(oNodoCuentaAuxiliar.Cells[iCol].Value) + oReg.ImporteDev);
                }
            }

            // Se llena el nuevo grupo de Empresa (Totales)
            int iColCuenta = ColsCuentas.Cuenta;

            this.tgvDatos.Nodes.Insert(0, new TreeGridNode());
            var oNodEmpresa = this.tgvDatos.Nodes[0];

            oNodEmpresa.Cells[iColCuenta].Value = "EMPRESA";
            foreach (var oNodSucursal in this.tgvDatos.Nodes)
            {
                if (Util.Cadena(oNodSucursal.Cells[iColCuenta].Value) == "EMPRESA")
                {
                    continue;
                }
                foreach (var oNodCuenta in oNodSucursal.Nodes)
                {
                    var oNECuenta = oNodEmpresa.Nodes.FirstOrDefault(c => c.Cells[iColCuenta].Value.ToString() == oNodCuenta.Cells[iColCuenta].Value.ToString());
                    if (oNECuenta == null)
                    {
                        oNECuenta = oNodEmpresa.Nodes.Add(oNodCuenta.Cells[iColCuenta].Value);
                    }
                    foreach (var oNodSubcuenta in oNodCuenta.Nodes)
                    {
                        var oNESubcuenta = oNECuenta.Nodes.FirstOrDefault(c => c.Cells[iColCuenta].Value.ToString() == oNodSubcuenta.Cells[iColCuenta].Value.ToString());
                        if (oNESubcuenta == null)
                        {
                            oNESubcuenta = oNECuenta.Nodes.Add(oNodSubcuenta.Cells[iColCuenta].Value);
                        }
                        foreach (var oNodCuentaDeMayor in oNodSubcuenta.Nodes)
                        {
                            var oNECuentaDeMayor = oNESubcuenta.Nodes.FirstOrDefault(c =>
                                                                                     c.Cells[iColCuenta].Value.ToString() == oNodCuentaDeMayor.Cells[iColCuenta].Value.ToString());
                            if (oNECuentaDeMayor == null)
                            {
                                oNECuentaDeMayor = oNESubcuenta.Nodes.Add(oNodCuentaDeMayor.Cells[iColCuenta].Value);
                            }
                            foreach (var oNodCuentaAuxiliar in oNodCuentaDeMayor.Nodes)
                            {
                                var oNECuentaAuxiliar = oNECuentaDeMayor.Nodes.FirstOrDefault(c =>
                                                                                              c.Cells[iColCuenta].Value.ToString() == oNodCuentaAuxiliar.Cells[iColCuenta].Value.ToString());
                                if (oNECuentaAuxiliar == null)
                                {
                                    oNECuentaAuxiliar = oNECuentaDeMayor.Nodes.Add(oNodCuentaAuxiliar.Cells[iColCuenta].Value);
                                }

                                for (int iCol = 2; iCol < this.tgvDatos.Columns.Count; iCol++)
                                {
                                    decimal mImporte = Util.Decimal(oNodCuentaAuxiliar.Cells[iCol].Value);
                                    oNECuentaAuxiliar.Cells[iCol].Value = (Util.Decimal(oNECuentaAuxiliar.Cells[iCol].Value) + mImporte);
                                }
                            }
                        }
                    }
                }
            }

            // Se llenan los totales
            foreach (var oNodSucursal in this.tgvDatos.Nodes)
            {
                foreach (var oNodCuenta in oNodSucursal.Nodes)
                {
                    foreach (var oNodSubcuenta in oNodCuenta.Nodes)
                    {
                        foreach (var oNodCuentaDeMayor in oNodSubcuenta.Nodes)
                        {
                            foreach (var oNodCuentaAuxiliar in oNodCuentaDeMayor.Nodes)
                            {
                                for (int iCol = 2; iCol < this.tgvDatos.Columns.Count; iCol++)
                                {
                                    decimal mImporte = Util.Decimal(oNodCuentaAuxiliar.Cells[iCol].Value);
                                    // Para los niveles superiores
                                    oNodCuentaDeMayor.Cells[iCol].Value = (Util.Decimal(oNodCuentaDeMayor.Cells[iCol].Value) + mImporte);
                                    oNodSubcuenta.Cells[iCol].Value     = (Util.Decimal(oNodSubcuenta.Cells[iCol].Value) + mImporte);
                                    oNodCuenta.Cells[iCol].Value        = (Util.Decimal(oNodCuenta.Cells[iCol].Value) + mImporte);
                                    oNodSucursal.Cells[iCol].Value      = (Util.Decimal(oNodSucursal.Cells[iCol].Value) + mImporte);
                                    // Para la columna de totales
                                    oNodCuentaAuxiliar.Cells[1].Value = (Util.Decimal(oNodCuentaAuxiliar.Cells[1].Value) + mImporte); // mImporteTotal;
                                    oNodCuentaDeMayor.Cells[1].Value  = (Util.Decimal(oNodCuentaDeMayor.Cells[1].Value) + mImporte);
                                    oNodSubcuenta.Cells[1].Value      = (Util.Decimal(oNodSubcuenta.Cells[1].Value) + mImporte);
                                    oNodCuenta.Cells[1].Value         = (Util.Decimal(oNodCuenta.Cells[1].Value) + mImporte);
                                    oNodSucursal.Cells[1].Value       = (Util.Decimal(oNodSucursal.Cells[1].Value) + mImporte);
                                }
                            }
                        }
                    }
                }
            }

            // Se aplica el formato y el color
            foreach (var oNodSucursal in this.tgvDatos.Nodes)
            {
                // Se aplica el formato
                oNodSucursal.DefaultCellStyle.BackColor = Color.FromArgb(58, 79, 109);
                oNodSucursal.DefaultCellStyle.ForeColor = Color.White;
                oNodSucursal.DefaultCellStyle.Font      = new Font(this.tgvDatos.Font, FontStyle.Bold);
                //
                oNodSucursal.Expand();
                // Si su valor es cero, se pone un guión
                this.PonerGuionCeldas(oNodSucursal);
                foreach (var oNodCuenta in oNodSucursal.Nodes)
                {
                    // Se aplica el formato
                    oNodCuenta.DefaultCellStyle.BackColor = Color.FromArgb(67, 87, 123);
                    oNodCuenta.DefaultCellStyle.ForeColor = Color.White;
                    oNodCuenta.DefaultCellStyle.Font      = new Font(this.tgvDatos.Font, FontStyle.Bold);
                    oNodCuenta.DefaultCellStyle.Font      = new Font(this.tgvDatos.Font.FontFamily, (float)7.75);
                    //
                    oNodCuenta.Expand();
                    // Si su valor es cero, se pone un guión
                    this.PonerGuionCeldas(oNodCuenta);
                    foreach (var oNodSubcuenta in oNodCuenta.Nodes)
                    {
                        // Se aplica el formato
                        oNodSubcuenta.DefaultCellStyle.BackColor = Color.FromArgb(0, 112, 192);
                        oNodSubcuenta.DefaultCellStyle.ForeColor = Color.White;
                        //
                        oNodSubcuenta.Expand();
                        // Si su valor es cero, se pone un guión
                        this.PonerGuionCeldas(oNodSubcuenta);
                        foreach (var oNodCuentaDeMayor in oNodSubcuenta.Nodes)
                        {
                            // Se aplica el formato
                            oNodCuentaDeMayor.DefaultCellStyle.ForeColor = Color.FromArgb(58, 79, 109);
                            oNodCuentaDeMayor.DefaultCellStyle.Font      = new Font(this.tgvDatos.Font, FontStyle.Bold);
                            oNodCuentaDeMayor.DefaultCellStyle.Font      = new Font(this.tgvDatos.Font.FontFamily, (float)7.75);
                            // Si su valor es cero, se pone un guión
                            this.PonerGuionCeldas(oNodCuentaDeMayor);
                            foreach (var oNodCuentaAuxiliar in oNodCuentaDeMayor.Nodes)
                            {
                                oNodCuentaAuxiliar.DefaultCellStyle.ForeColor = Color.FromArgb(58, 79, 109);
                                // Si su valor es cero, se pone un guión
                                this.PonerGuionCeldas(oNodCuentaAuxiliar);
                            }
                        }
                    }
                }
            }

            Cargando.Cerrar();
        }
        private void CargarDatos()
        {
            Cargando.Mostrar();

            var oFuenteT = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold);

            // Se limpian los datos
            this.dgvDatos.Rows.Clear();
            foreach (var oSerie in this.chrPorSemana.Series)
            {
                oSerie.Points.Clear();
            }

            // Se llenan las columnas del año
            int iAnio = Util.Entero(this.cmbAnio.Text);

            this.LlenarColumnasAnio(iAnio);

            // Se obtienen los datos para los ingresos
            int      iSucursalID = Util.Entero(this.cmbSucursal.SelectedValue);
            DateTime dDesde      = new DateTime(iAnio, 1, 1);
            DateTime dHasta      = new DateTime(iAnio, 12, 31);
            var      oParams     = new Dictionary <string, object>();

            oParams.Add("SucursalID", (iSucursalID == 0 ? null : (int?)iSucursalID));
            oParams.Add("Pagadas", true);
            oParams.Add("Cobradas", false);
            oParams.Add("Solo9500", false);
            oParams.Add("OmitirDomingo", false);
            oParams.Add("Desde", dDesde);
            oParams.Add("Hasta", dHasta);

            var oDatos   = Datos.ExecuteProcedure <pauCuadroDeControlGeneralNuevo_Result>("pauCuadroDeControlGeneral", oParams);
            var oSemanas = oDatos.Where(c => c.Fecha >= dDesde)
                           .GroupBy(c => new { Semana = UtilTheos.InicioSemanaSabAVie(c.Fecha), c.Sucursal })
                           .Select(c => new { c.Key.Semana, c.Key.Sucursal, PrecioSinIva = c.Sum(s => s.PrecioSinIvaActual), Costo = c.Sum(s => s.CostoDescActual) })
                           .OrderBy(c => c.Sucursal).ThenBy(c => c.Semana);

            // Se agrega la fila de ingresos
            int iFilaIngresos = this.dgvDatos.Rows.Add("+ Ingresos", oSemanas.Sum(c => c.PrecioSinIva), oSemanas.Average(c => c.PrecioSinIva));

            this.dgvDatos.Rows[iFilaIngresos].DefaultCellStyle.Font = oFuenteT;
            // Se llenan los ingresos
            string sSucursal = "";
            int    iFila     = 0;

            foreach (var oReg in oSemanas)
            {
                if (sSucursal != oReg.Sucursal)
                {
                    sSucursal = oReg.Sucursal;
                    iFila     = this.dgvDatos.Rows.Add(sSucursal, oSemanas.Where(c => c.Sucursal == sSucursal).Sum(c => c.PrecioSinIva)
                                                       , oSemanas.Where(c => c.Sucursal == sSucursal).Average(c => c.PrecioSinIva));
                }

                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value         = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.PrecioSinIva);
                this.dgvDatos[sSemana, iFilaIngresos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaIngresos].Value) + oReg.PrecioSinIva);
            }

            // Se agrega la fila de Costos
            int iFilaCostos = this.dgvDatos.Rows.Add("- Costos", oSemanas.Sum(c => c.Costo), oSemanas.Average(c => c.Costo));

            this.dgvDatos.Rows[iFilaCostos].DefaultCellStyle.Font = oFuenteT;
            // Se llenan los costos
            sSucursal = "";
            foreach (var oReg in oSemanas)
            {
                if (sSucursal != oReg.Sucursal)
                {
                    sSucursal = oReg.Sucursal;
                    iFila     = this.dgvDatos.Rows.Add(sSucursal, oSemanas.Where(c => c.Sucursal == sSucursal).Sum(c => c.Costo)
                                                       , oSemanas.Where(c => c.Sucursal == sSucursal).Average(c => c.Costo));
                }

                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value       = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Costo);
                this.dgvDatos[sSemana, iFilaCostos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaCostos].Value) + oReg.Costo);
            }

            // Se agrega la fila de margen bruto
            int iFilaMargen = this.dgvDatos.Rows.Add("= Margen Bruto");

            this.dgvDatos.Rows[iFilaMargen].DefaultCellStyle.Font = oFuenteT;
            foreach (DataGridViewColumn oCol in this.dgvDatos.Columns)
            {
                if (oCol.Index == 0)
                {
                    continue;
                }
                this.dgvDatos[oCol.Index, iFilaMargen].Value = (
                    Util.Decimal(this.dgvDatos[oCol.Index, iFilaIngresos].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaCostos].Value)
                    );
            }

            // Se obtienen los datos para los gastos
            oParams.Clear();
            oParams.Add("Desde", dDesde);
            oParams.Add("Hasta", dHasta);
            var oGastos        = Datos.ExecuteProcedure <pauContaCuentasPorSemana_Result>("pauContaCuentasPorSemana", oParams);
            var oGastosSemFijo = oGastos.GroupBy(c => new { Semana = UtilTheos.InicioSemanaSabAVie(c.Fecha), c.Sucursal })
                                 .Select(c => new ContaProc.GastoSem()
            {
                Semana = c.Key.Semana, Grupo = c.Key.Sucursal, Importe = c.Sum(s => s.ImporteDev).Valor()
            });
            // Se obtiene los datos según el tipo de semanalización
            List <ContaProc.GastoSem> oGastosSem;

            if (this.rdbSemanalizar.Checked)
            {
                oGastosSem = ContaProc.GastosSemanalizados(oGastos, Util.FechaHora(this.dgvDatos.Columns[this.dgvDatos.Columns.Count - 1].Name));
            }
            else
            {
                oGastosSem = oGastosSemFijo.OrderBy(c => c.Grupo).ThenBy(c => c.Semana).ToList();
            }

            // Se agrega la fila de los Gastos
            int iFilaGastos = this.dgvDatos.Rows.Add("- Gastos", oGastosSem.Sum(c => c.Importe), oGastosSem.Average(c => c.Importe));

            this.dgvDatos.Rows[iFilaGastos].DefaultCellStyle.Font = oFuenteT;
            // Se llenan los gastos
            sSucursal = "";
            foreach (var oReg in oGastosSem)
            {
                if (sSucursal != oReg.Grupo)
                {
                    sSucursal = oReg.Grupo;
                    iFila     = this.dgvDatos.Rows.Add(sSucursal, oGastosSem.Where(c => c.Grupo == sSucursal).Sum(c => c.Importe)
                                                       , oGastosSem.Where(c => c.Grupo == sSucursal).Average(c => c.Importe));
                }

                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value       = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaGastos].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaGastos].Value) + oReg.Importe);
            }

            // Se agrega la fila de utilidad, con cada una de las sucursales
            int iFilaUtilidad = this.dgvDatos.Rows.Add("= Utilidad");

            this.dgvDatos.Rows[iFilaUtilidad].DefaultCellStyle.Font = oFuenteT;
            // Sucursales
            var oSucursales = Datos.GetListOf <Sucursal>(c => c.Estatus).OrderBy(c => c.NombreSucursal).ToList();

            foreach (var oReg in oSucursales)
            {
                this.dgvDatos.Rows.Add(oReg.NombreSucursal);
            }
            // Se llenan los datos
            foreach (DataGridViewColumn oCol in this.dgvDatos.Columns)
            {
                if (oCol.Index == 0)
                {
                    continue;
                }
                // Utilidad total
                this.dgvDatos[oCol.Index, iFilaUtilidad].Value = (
                    Util.Decimal(this.dgvDatos[oCol.Index, iFilaMargen].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaGastos].Value)
                    );
                // Sucursales
                for (int i = 1; i <= oSucursales.Count; i++)
                {
                    this.dgvDatos[oCol.Index, iFilaUtilidad + i].Value = (
                        Util.Decimal(this.dgvDatos[oCol.Index, iFilaIngresos + i].Value)
                        - Util.Decimal(this.dgvDatos[oCol.Index, iFilaCostos + i].Value)
                        - Util.Decimal(this.dgvDatos[oCol.Index, iFilaGastos + i].Value)
                        );
                }
            }

            // Se obtienen los datos de gastos especiales
            DateTime dHastaMas1 = dHasta.AddDays(1);
            // Se obtiene los datos según el tipo de semanalización
            List <ContaProc.GastoSem> oGastosSemEsp;
            var oGastosSemEspFijo = Datos.GetListOf <ContaEgresosDevengadoEspecialCuentasView>(c => c.Fecha >= dDesde && c.Fecha < dHastaMas1)
                                    .GroupBy(c => new { c.Duenio, Semana = UtilTheos.InicioSemanaSabAVie(c.Fecha) })
                                    .Select(c => new ContaProc.GastoSem()
            {
                Semana = c.Key.Semana, Grupo = c.Key.Duenio, Importe = c.Sum(s => s.ImporteDev)
            });

            if (this.rdbSemanalizar.Checked)
            {
                oGastosSemEsp = ContaProc.GastosSemanalizados(Datos.GetListOf <ContaEgresosDevengadoEspecialCuentasView>(c => c.Fecha >= dDesde && c.Fecha < dHastaMas1)
                                                              , Util.FechaHora(this.dgvDatos.Columns[this.dgvDatos.Columns.Count - 1].Name));
            }
            else
            {
                oGastosSemEsp = oGastosSemEspFijo.OrderBy(c => c.Grupo).ThenBy(c => c.Semana).ToList();
            }

            // Se agrega la fila de Especiales
            bool bHay     = (oGastosSemEspFijo.Count() > 0);
            int  iFilaEsp = this.dgvDatos.Rows.Add("- Especiales", (bHay ? oGastosSemEspFijo.Sum(c => c.Importe) : 0)
                                                   , (bHay ? oGastosSemEspFijo.Average(c => c.Importe) : 0));

            this.dgvDatos.Rows[iFilaEsp].DefaultCellStyle.Font = oFuenteT;
            // Se llenan los gastos
            string sDuenio = "";

            foreach (var oReg in oGastosSemEsp)
            {
                if (sDuenio != oReg.Grupo)
                {
                    sDuenio = oReg.Grupo;
                    iFila   = this.dgvDatos.Rows.Add(sDuenio, oGastosSemEspFijo.Where(c => c.Grupo == sDuenio).Sum(c => c.Importe)
                                                     , oGastosSemEspFijo.Where(c => c.Grupo == sDuenio).Average(c => c.Importe));
                }

                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value    = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaEsp].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaEsp].Value) + oReg.Importe);
            }

            // Se agrega la fila de utilidad neta
            int iFilaDividendos = this.dgvDatos.Rows.Add("= Dividendos");

            this.dgvDatos.Rows[iFilaDividendos].DefaultCellStyle.Font = oFuenteT;
            foreach (DataGridViewColumn oCol in this.dgvDatos.Columns)
            {
                if (oCol.Index == 0)
                {
                    continue;
                }
                this.dgvDatos[oCol.Index, iFilaDividendos].Value = (
                    Util.Decimal(this.dgvDatos[oCol.Index, iFilaMargen].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaGastos].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaEsp].Value)
                    );
            }

            // Se obtienen los datos para lo de reinversión
            var oReinversiones = Datos.GetListOf <ContaPolizasDetalleAvanzadoView>(c => c.FechaPoliza >= dDesde && c.FechaPoliza < dHastaMas1 &&
                                                                                   (
                                                                                       c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarLargoPlazo ||
                                                                                       (c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarCortoPlazo && c.ContaCuentaAuxiliarID != Cat.ContaCuentasAuxiliares.TarjetaDeCredito) ||
                                                                                       c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.AcreedoresDiversos ||
                                                                                       (c.ContaSubcuentaID == Cat.ContaSubcuentas.ActivoFijo && c.ContaCuentaDeMayorID != Cat.ContaCuentasDeMayor.Edificios)
                                                                                   ));
            var oDeudas = oReinversiones.Where(c => c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarLargoPlazo ||
                                               c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.CuentasPorPagarCortoPlazo || c.ContaCuentaDeMayorID == Cat.ContaCuentasDeMayor.AcreedoresDiversos)
                          .GroupBy(c => UtilTheos.InicioSemanaSabAVie(c.FechaPoliza.Valor()))
                          .Select(c => new { Semana = c.Key, Importe = c.Sum(s => s.Cargo) });
            var oInversiones = oReinversiones.Where(c => c.ContaSubcuentaID == Cat.ContaSubcuentas.ActivoFijo)
                               .GroupBy(c => UtilTheos.InicioSemanaSabAVie(c.FechaPoliza.Valor())).Select(c => new { Semana = c.Key, Importe = c.Sum(s => s.Cargo) });
            // Se agrega la fila de Reinversión
            decimal mDeudas          = (oDeudas.Count() > 0 ? oDeudas.Sum(c => c.Importe) : 0);
            decimal mDeudasProm      = (oDeudas.Count() > 0 ? oDeudas.Average(c => c.Importe) : 0);
            decimal mInversiones     = (oInversiones.Count() > 0 ? oInversiones.Sum(c => c.Importe) : 0);
            decimal mInversionesProm = (oInversiones.Count() > 0 ? oInversiones.Average(c => c.Importe) : 0);
            int     iFilaReinversion = this.dgvDatos.Rows.Add("- Reinversión", (mDeudas + mInversiones), ((mDeudasProm + mInversionesProm) / 2));

            this.dgvDatos.Rows[iFilaReinversion].DefaultCellStyle.Font = oFuenteT;
            // Se llenan los datos de reinversión
            // Deudas
            iFila = this.dgvDatos.Rows.Add("Deudas", mDeudas, mDeudasProm);
            foreach (var oReg in oDeudas)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value            = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaReinversion].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaReinversion].Value) + oReg.Importe);
            }
            // Inversión
            iFila = this.dgvDatos.Rows.Add("Inversiones", mInversiones, mInversionesProm);
            foreach (var oReg in oInversiones)
            {
                string sSemana = oReg.Semana.ToShortDateString();
                this.dgvDatos[sSemana, iFila].Value            = (Util.Decimal(this.dgvDatos[sSemana, iFila].Value) + oReg.Importe);
                this.dgvDatos[sSemana, iFilaReinversion].Value = (Util.Decimal(this.dgvDatos[sSemana, iFilaReinversion].Value) + oReg.Importe);
            }

            // Se agrega el saldo final
            int iFilaSaldo = this.dgvDatos.Rows.Add("= Saldo final");

            this.dgvDatos.Rows[iFilaSaldo].DefaultCellStyle.Font = oFuenteT;
            foreach (DataGridViewColumn oCol in this.dgvDatos.Columns)
            {
                if (oCol.Index == 0)
                {
                    continue;
                }
                this.dgvDatos[oCol.Index, iFilaSaldo].Value = (
                    Util.Decimal(this.dgvDatos[oCol.Index, iFilaDividendos].Value)
                    - Util.Decimal(this.dgvDatos[oCol.Index, iFilaReinversion].Value)
                    );
            }

            // Se llena la gráfica, en base al grid ya cargado
            for (int iCol = this.iColumnasFijas; iCol < this.dgvDatos.Columns.Count; iCol++)
            {
                this.chrPorSemana.Series["Ingresos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaIngresos].Value));
                this.chrPorSemana.Series["Costos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaCostos].Value));
                this.chrPorSemana.Series["Margen"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaMargen].Value));
                this.chrPorSemana.Series["Gastos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaGastos].Value));
                this.chrPorSemana.Series["Utilidad"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaUtilidad].Value));
                this.chrPorSemana.Series["Especiales"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaEsp].Value));
                this.chrPorSemana.Series["Dividendos"].Points.AddY(Util.Decimal(this.dgvDatos[iCol, iFilaDividendos].Value));
            }

            Cargando.Cerrar();
        }