public ActionResult EditCamposQuincena(EditCamposQuincenaViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    PostMessage(MessageType.Error, i18n.ValidationStrings.DatosIncorrectos);
                    return(RedirectToAction("AddEditTrabajador", "Employee", new { TrabajadorId = model.TrabajadorId }));
                }

                DetalleQuincena detalle = context.DetalleQuincena.Find(model.DetalleQuincenaId);
                detalle.BonoPorMovilidad = model.tieneMovilidad;
                detalle.Bonificacion     = model.tieneBonificacion;
                detalle.Seguro           = model.tieneSeguro;

                context.SaveChanges();
                PostMessage(MessageType.Success, "Datos guardados correctamente");
                return(RedirectToAction("AddEditTrabajador", "Employee", new { TrabajadorId = model.TrabajadorId }));
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error, "Ocurrió un error. Inténtelo más tarde");
                return(RedirectToAction("AddEditTrabajador", "Employee", new { TrabajadorId = model.TrabajadorId }));
            }
        }
        public ActionResult AddEditTrabajador(AddEditTrabajadorViewModel ViewModel)
        {
            if (!ModelState.IsValid)
            {
                ViewModel.Fill(CargarDatosContext());
                TryUpdateModel(ViewModel);
                PostMessage(MessageType.Error);
                return(View(ViewModel));
            }

            if (ViewModel.FotoFile != null)
            {
                string extension = Path.GetExtension(ViewModel.FotoFile.FileName).ToLower();
                if (extension != ".jpg" && extension != ".jpeg")
                {
                    ViewModel.Fill(CargarDatosContext());
                    TryUpdateModel(ViewModel);
                    PostMessage(MessageType.Info, "Solo se aceptan los formatos de imagen .jpg y .jpeg");
                    return(View(ViewModel));
                }
            }



            try
            {
                using (var transaction = new TransactionScope())
                {
                    if (ViewModel.TrabajadorId.HasValue)
                    {
                        Trabajador trabajador = context.Trabajador.FirstOrDefault(x => x.TrabajadorId == ViewModel.TrabajadorId.Value);
                        trabajador.Nombres   = ViewModel.Nombres;
                        trabajador.Apellidos = ViewModel.Apellidos;
                        if (ViewModel.AFP != 0)
                        {
                            trabajador.AFPId = ViewModel.AFP;
                        }
                        else
                        {
                            trabajador.AFPId = null;
                        }
                        trabajador.DNI             = ViewModel.DNI;
                        trabajador.FechaNacimiento = ViewModel.FechaNacimiento.ToDateTime();

                        if (ViewModel.FotoFile != null)
                        {
                            string ruta = Path.Combine(Server.MapPath("~/Resources/Fotos"), Path.GetFileNameWithoutExtension(ViewModel.FotoFile.FileName) + Path.GetExtension(ViewModel.FotoFile.FileName));
                            trabajador.Foto = Path.GetFileName(ViewModel.FotoFile.FileName);
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Fotos"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Fotos")));
                            }
                            ViewModel.FotoFile.SaveAs(ruta);

                            ruta = Path.Combine(Server.MapPath("~/Resources/Files"), Path.GetFileNameWithoutExtension(ViewModel.FotoFile.FileName) + Path.GetExtension(ViewModel.FotoFile.FileName));

                            ViewModel.FotoFile.SaveAs(ruta);
                        }


                        if (ViewModel.AntecedenteFile != null)
                        {
                            string nombre = Guid.NewGuid().ToString().Substring(0, 6) + Path.GetExtension(ViewModel.AntecedenteFile.FileName);
                            string ruta   = Path.Combine(Server.MapPath("~/Resources/Files"), nombre);
                            trabajador.AntecedentesPoliciales = nombre;
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Files"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Files")));
                            }
                            ViewModel.AntecedenteFile.SaveAs(ruta);
                        }

                        if (ViewModel.PartidaFile != null)
                        {
                            string nombre = Guid.NewGuid().ToString().Substring(0, 6) + Path.GetExtension(ViewModel.PartidaFile.FileName);

                            string ruta = Path.Combine(Server.MapPath("~/Resources/Files"), nombre);
                            trabajador.PartidaNacimiento = nombre;
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Files"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Files")));
                            }
                            ViewModel.PartidaFile.SaveAs(ruta);
                        }

                        trabajador.Cargo      = ViewModel.Cargo;
                        trabajador.CUSSP      = "";
                        trabajador.EdificioId = ViewModel.EdificioId;
                        if (ViewModel.Comision.HasValue)
                        {
                            trabajador.Comision = 0;
                        }
                        trabajador.FechaIngreso     = ViewModel.FechaIngreso;
                        trabajador.Modalidad        = "";
                        trabajador.SueldoBase       = 0;
                        trabajador.MontoHoras25     = 0;
                        trabajador.MontoHoras35     = 0;
                        trabajador.MontoFeriado     = 0;
                        trabajador.AdelantoQuincena = 0;
                        trabajador.ComisionFlujo    = "";

                        context.Entry(trabajador).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        DetalleQuincena detalle = new DetalleQuincena();
                        detalle.BonoPorMovilidad = true;
                        detalle.Bonificacion     = true;
                        detalle.Seguro           = true;
                        detalle.TotalQuincena    = true;
                        context.DetalleQuincena.Add(detalle);
                        context.SaveChanges();

                        DetalleMensualidad detalleMensualidad = new DetalleMensualidad();
                        detalleMensualidad.CTS     = true;
                        detalleMensualidad.Essalud = true;
                        context.DetalleMensualidad.Add(detalleMensualidad);
                        context.SaveChanges();


                        Trabajador trabajador = new Trabajador();
                        trabajador.EdificioId = ViewModel.EdificioId;
                        trabajador.Estado     = ConstantHelpers.EstadoActivo;
                        trabajador.Nombres    = ViewModel.Nombres;
                        trabajador.Apellidos  = ViewModel.Apellidos;
                        if (ViewModel.AFP != 0)
                        {
                            trabajador.AFPId = ViewModel.AFP;
                        }
                        else
                        {
                            trabajador.AFPId = null;
                        }
                        trabajador.EdificioId      = ViewModel.EdificioId;
                        trabajador.DNI             = ViewModel.DNI;
                        trabajador.FechaNacimiento = ViewModel.FechaNacimiento.ToDateTime();

                        if (ViewModel.FotoFile != null)
                        {
                            string ruta = Path.Combine(Server.MapPath("~/Resources/Fotos"), Path.GetFileNameWithoutExtension(ViewModel.FotoFile.FileName) + Path.GetExtension(ViewModel.FotoFile.FileName));
                            trabajador.Foto = Path.GetFileName(ViewModel.FotoFile.FileName);
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Fotos"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Fotos")));
                            }
                            ViewModel.FotoFile.SaveAs(ruta);
                        }

                        if (ViewModel.AntecedenteFile != null)
                        {
                            string ruta = Path.Combine(Server.MapPath("~/Resources/Files"), Path.GetFileNameWithoutExtension(ViewModel.AntecedenteFile.FileName) + Path.GetExtension(ViewModel.AntecedenteFile.FileName));
                            trabajador.AntecedentesPoliciales = Path.GetFileName(ViewModel.AntecedenteFile.FileName);
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Files"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Files")));
                            }
                            ViewModel.AntecedenteFile.SaveAs(ruta);
                        }

                        if (ViewModel.PartidaFile != null)
                        {
                            string ruta = Path.Combine(Server.MapPath("~/Resources/Files"), Path.GetFileNameWithoutExtension(ViewModel.PartidaFile.FileName) + Path.GetExtension(ViewModel.PartidaFile.FileName));
                            trabajador.PartidaNacimiento = Path.GetFileName(ViewModel.PartidaFile.FileName);
                            if (!System.IO.Directory.Exists(Path.Combine(Server.MapPath("~/Resources/Files"))))
                            {
                                Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Resources/Files")));
                            }
                            ViewModel.PartidaFile.SaveAs(ruta);
                        }

                        trabajador.Cargo = ViewModel.Cargo;
                        trabajador.CUSSP = ViewModel.CUSSP;
                        if (ViewModel.Comision.HasValue)
                        {
                            trabajador.Comision = ViewModel.Comision.ToDecimal();
                        }
                        trabajador.FechaIngreso         = ViewModel.FechaIngreso;
                        trabajador.Modalidad            = "REG";
                        trabajador.SueldoBase           = 0;
                        trabajador.MontoHoras25         = ViewModel.MontoHoras25.ToDecimal();
                        trabajador.MontoHoras35         = ViewModel.MontoHoras35.ToDecimal();
                        trabajador.MontoFeriado         = ViewModel.MontoFeriado.ToDecimal();
                        trabajador.AdelantoQuincena     = null;
                        trabajador.ComisionFlujo        = "REG";
                        trabajador.DetalleQuincenaId    = null;
                        trabajador.DetalleMensualidadId = null;
                        context.Trabajador.Add(trabajador);
                    }
                    context.SaveChanges();
                    transaction.Complete();
                    PostMessage(MessageType.Success);
                }
            }
            catch
            {
                InvalidarContext();
                PostMessage(MessageType.Error);
            }
            return(RedirectToAction("LstTrabajadorAdmin"));
        }
        public ActionResult EditPlanillaQuincena(EditPlanillaQuincenaViewModel model, FormCollection formCollection)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    model.Fill(CargarDatosContext(), model.EdificioId, model.UnidadTiempoId);
                    TryUpdateModel(model);
                    PostMessage(MessageType.Error, i18n.ValidationStrings.DatosIncorrectos);
                    return(View(model));
                }
                using (TransactionScope transaction = new TransactionScope())
                {
                    var LstTrabajadores = context.Trabajador.Where(x => x.EdificioId == model.EdificioId && x.Estado == ConstantHelpers.EstadoActivo).ToList();

                    foreach (var trabajador in LstTrabajadores)
                    {
                        DetalleQuincena detalle = context.DetalleQuincena.Find(trabajador.DetalleQuincenaId);

                        var movilidad    = formCollection["planilla-bonus-movilidad-" + trabajador.TrabajadorId];
                        var bonificacion = formCollection["planilla-bonificacion-" + trabajador.TrabajadorId];
                        var quincena     = formCollection["planilla-total-quincena-" + trabajador.TrabajadorId];
                        var seguro       = formCollection["planilla-seguro-" + trabajador.TrabajadorId];

                        bool             esNuevo  = false;
                        PlanillaQuincena planilla = context.PlanillaQuincena.Where(x => x.TrabajadorId == trabajador.TrabajadorId && x.UnidadTiempoId == model.UnidadTiempoId).FirstOrDefault();
                        if (planilla == null)
                        {
                            planilla = new PlanillaQuincena();
                            esNuevo  = true;
                        }
                        planilla.TrabajadorId   = trabajador.TrabajadorId;
                        planilla.UnidadTiempoId = model.UnidadTiempoId.Value;
                        if (detalle.BonoPorMovilidad)
                        {
                            planilla.BonoPorMovilidad = movilidad.ToDecimal();
                        }
                        if (detalle.Bonificacion)
                        {
                            planilla.Bonificacion = bonificacion.ToDecimal();
                        }
                        planilla.TotalQuincena = quincena.ToDecimal();
                        if (detalle.Seguro)
                        {
                            planilla.Seguro = seguro.ToDecimal();
                        }

                        if (esNuevo)
                        {
                            context.PlanillaQuincena.Add(planilla);
                        }
                    }
                    PostMessage(MessageType.Success, "Guardado Correctamente");
                    context.SaveChanges();
                    transaction.Complete();
                    return(RedirectToAction("LstEdificio", "Building"));
                }
            }
            catch (Exception ex)
            {
                model.Fill(CargarDatosContext(), model.EdificioId, model.UnidadTiempoId);
                TryUpdateModel(model);
                PostMessage(MessageType.Error, "Ocurrió un error, por favor inténtelo más tarde");
                return(View(model));
            }
            return(View());
        }