public async Task <IActionResult> Edit(int id, [Bind("HistorialERId,Fecha,VentasNetas,CostosVentas,GastosAdmin,GastosVentas,GastosOperativos,OtrosGastos")] HistorialER historialER)
        {
            if (id != historialER.HistorialERId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(historialER);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HistorialERExists(historialER.HistorialERId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(historialER));
        }
 public async Task <IActionResult> Create([Bind("HistorialERId,Fecha,VentasNetas,CostosVentas,GastosAdmin,GastosVentas,GastosOperativos,OtrosGastos,Utilidad,IR,UtilidadNeta")] HistorialER historialER)
 {
     if (ModelState.IsValid)
     {
         ModeloProyeccion modproyeccion;
         //TODO:Primero Ingresar un ModeloPRoyeccion de 0.0%
         if (_context.ModeloProyeccion.Count() == 0)
         {
             modproyeccion = new ModeloProyeccion {
                 Fecha               = DateTime.Today,
                 ModVentasNetas      = Convert.ToDecimal(0.00),
                 ModCostosVentas     = Convert.ToDecimal(0.00),
                 ModGastosAdmin      = Convert.ToDecimal(0.00),
                 ModGastosVentas     = Convert.ToDecimal(0.00),
                 ModGastosOperativos = Convert.ToDecimal(0.00),
                 ModOtrosGastos      = Convert.ToDecimal(0.00),
                 ModUtilidad         = Convert.ToDecimal(0.00),
                 ModIR               = Convert.ToDecimal(0.00),
                 ModUtilidadNeta     = Convert.ToDecimal(0.00),
                 HistotialesER       = new List <HistorialER>
                 {
                     historialER
                 }
             };
             _context.Add(modproyeccion);
             _context.SaveChanges();
         }
         else
         {
             var modelo  = _context.ModeloProyeccion.Include(a => a.HistotialesER).FirstOrDefault(a => a.ModeloProyeccionId == 1);
             var firstEr = modelo.HistotialesER.FirstOrDefault(a => a.HistorialERId == 1);
             //modelo.ModVentasNetas = firstEr.VentasNetas;
             //modelo.ModCostosVentas = firstEr.CostosVentas;
             //modelo.ModGastosAdmin = firstEr.GastosAdmin;
             //modelo.ModGastosVentas = firstEr.GastosVentas;
             //modelo.ModGastosOperativos = firstEr.GastosOperativos;
             //modelo.ModOtrosGastos = firstEr.OtrosGastos;
             //modelo.ModUtilidad = firstEr.Utilidad;
             //modelo.ModIR = firstEr.IR;
             //modelo.ModUtilidadNeta = firstEr.UtilidadNeta;
             modelo.HistotialesER.Add(historialER);
             List <HistorialER> lister = modelo.HistotialesER.Where(a => a.HistorialERId != 1).ToList();
             foreach (var item in lister)
             {
                 modelo.ModVentasNetas      = Convert.ToDecimal(Math.Round((item.VentasNetas / firstEr.VentasNetas), 2));
                 modelo.ModCostosVentas     = Convert.ToDecimal(Math.Round((item.CostosVentas / firstEr.CostosVentas), 2));
                 modelo.ModGastosAdmin      = Convert.ToDecimal(Math.Round((item.GastosAdmin / firstEr.GastosAdmin), 2));
                 modelo.ModGastosVentas     = Convert.ToDecimal(Math.Round((item.GastosVentas / firstEr.GastosVentas), 2));
                 modelo.ModGastosOperativos = Convert.ToDecimal(Math.Round((item.GastosOperativos / firstEr.GastosOperativos), 2));
                 modelo.ModOtrosGastos      = Convert.ToDecimal(Math.Round((item.OtrosGastos / firstEr.OtrosGastos), 2));
                 modelo.ModUtilidad         = Convert.ToDecimal(Math.Round((item.Utilidad / firstEr.Utilidad), 2));
                 modelo.ModIR           = Convert.ToDecimal(Math.Round((item.IR / firstEr.IR), 2));
                 modelo.ModUtilidadNeta = Convert.ToDecimal(Math.Round((item.UtilidadNeta / firstEr.UtilidadNeta), 2));
             }
             _context.Update(modelo);
             await _context.SaveChangesAsync();
         }
         return(RedirectToAction(nameof(Index)));
     }
     return(View(historialER));
 }
        // GET: Proyecciones/Create
        public IActionResult Create()
        {
            //    @*Ventas netas
            //Costos de ventas
            //Gastos Administrativos
            //Gastos de venta
            //Gastos de Operacion
            //Otros Gastos
            //Utilidad
            //IR
            //Utilidad Neta*@

            ModeloProyeccion modpro;

            HistorialER hr;


            if (_context.ModeloProyeccion.Count() <= 1)
            {
                hr                  = new HistorialER();
                hr.Fecha            = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).Fecha;
                hr.VentasNetas      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).VentasNetas;
                hr.CostosVentas     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).CostosVentas;
                hr.GastosAdmin      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosAdmin;
                hr.GastosVentas     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosVentas;
                hr.GastosOperativos = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosOperativos;
                hr.OtrosGastos      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).OtrosGastos;
                hr.Utilidad         = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).Utilidad;
                hr.IR               = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).IR;
                hr.UtilidadNeta     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).UtilidadNeta;

                modpro = new ModeloProyeccion {
                    ModVentasNetas      = 0.00m, ModCostosVentas = 0.00m, ModGastosAdmin = 0.00m,
                    ModGastosOperativos = 0.00m, ModGastosVentas = 0.00m, ModOtrosGastos = 0.00m,
                    ModUtilidad         = 0.00m, ModIR = 0.00m, ModUtilidadNeta = 0.00m, HistotialesER = new List <HistorialER> {
                        hr
                    }
                };
                _context.ModeloProyeccion.Add(modpro);
                _context.SaveChanges();
                ViewBag.modelo = _context.ModeloProyeccion.Include(m => m.HistotialesER).FirstOrDefault(a => a.ModeloProyeccionId == 2);//
            }
            else
            {
                hr                  = _context.HistorialER.FirstOrDefault(a => a.ModeloProyeccionId == 2);
                hr.Fecha            = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).Fecha;
                hr.VentasNetas      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).VentasNetas;
                hr.CostosVentas     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).CostosVentas;
                hr.GastosAdmin      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosAdmin;
                hr.GastosVentas     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosVentas;
                hr.GastosOperativos = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).GastosOperativos;
                hr.OtrosGastos      = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).OtrosGastos;
                hr.Utilidad         = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).Utilidad;
                hr.IR               = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).IR;
                hr.UtilidadNeta     = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1).UtilidadNeta;
                _context.HistorialER.Update(hr);
                _context.SaveChanges();
            }
            ViewBag.modelo           = _context.ModeloProyeccion.Include(m => m.HistotialesER).FirstOrDefault(a => a.ModeloProyeccionId == 2);
            ViewBag.ultimaproyeccion = _context.Proyecciones.LastOrDefault();
            ViewBag.lastER           = _context.HistorialER.LastOrDefault(a => a.ModeloProyeccionId == 1);
            ViewBag.cantidad         = _context.Proyecciones.ToList();

            return(View());
        }