} // haac 15/01/2014

        public Boolean GuardarDB(ro_periodo_x_ro_Nomina_TipoLiqui_Info Info)
        {
            List <ro_periodo_x_ro_Nomina_TipoLiqui_Info> Lst = new List <ro_periodo_x_ro_Nomina_TipoLiqui_Info>();

            try
            {
                using (EntitiesRoles Context = new EntitiesRoles())
                {
                    ro_periodo_x_ro_Nomina_TipoLiqui Data = new ro_periodo_x_ro_Nomina_TipoLiqui();

                    Data.IdEmpresa          = Info.IdEmpresa;
                    Data.IdNomina_Tipo      = Info.IdNomina_Tipo;
                    Data.IdNomina_TipoLiqui = Info.IdNomina_TipoLiqui;
                    Data.IdPeriodo          = Info.IdPeriodo;
                    Data.Cerrado            = "N";
                    Data.Procesado          = "N";
                    Data.Contabilizado      = "N";

                    // Data.orden = getId(Info.IdEmpresa);
                    Context.ro_periodo_x_ro_Nomina_TipoLiqui.Add(Data);
                    Context.SaveChanges();
                }
                return(true);
            }

            catch (Exception ex)
            {
                string array = ToString();
                tb_sis_Log_Error_Vzen_Data oDataLog      = new tb_sis_Log_Error_Vzen_Data();
                tb_sis_Log_Error_Vzen_Info Log_Error_sis = new tb_sis_Log_Error_Vzen_Info(ex.ToString(), "", array, "", "", "", "", "", DateTime.Now);
                oDataLog.Guardar_Log_Error(Log_Error_sis, ref mensaje);
                mensaje = ex.InnerException + " " + ex.Message;
                throw new Exception(ex.InnerException.ToString());
            }
        }
        public bool modificarDB(ro_participacion_utilidad_Info info)
        {
            try
            {
                using (Entities_rrhh Context = new Entities_rrhh())
                {
                    int IdNomina_General;
                    int IdNomina_TipoLiqui_PagoUtilidad;

                    ro_rubros_calculados RubrosCalculados = Context.ro_rubros_calculados.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa);
                    if (RubrosCalculados == null | RubrosCalculados.IdRubro_utilidad == null)
                    {
                        return(false);
                    }
                    #region Nomina y periodo
                    DateTime fi = new DateTime(info.IdPeriodo, 1, 1);
                    DateTime ff = new DateTime(info.IdPeriodo, 12, 31);

                    ro_Parametros param = Context.ro_Parametros.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa);
                    if (param == null || param.IdNomina_General == null || param.IdNomina_TipoLiqui_PagoUtilidad == null)
                    {
                        return(false);
                    }
                    IdNomina_General = Convert.ToInt32(param.IdNomina_General);
                    IdNomina_TipoLiqui_PagoUtilidad = Convert.ToInt32(param.IdNomina_TipoLiqui_PagoUtilidad);

                    ro_periodo Entity_periodo = Context.ro_periodo.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa && q.IdPeriodo == info.IdPeriodo);
                    if (Entity_periodo == null)
                    {
                        Context.ro_periodo.Add(new ro_periodo
                        {
                            IdEmpresa     = info.IdEmpresa,
                            IdPeriodo     = info.IdPeriodo,
                            pe_FechaIni   = new DateTime(info.IdPeriodo, 1, 1),
                            pe_FechaFin   = new DateTime(info.IdPeriodo, 12, 31),
                            pe_anio       = info.IdPeriodo,
                            pe_estado     = "A",
                            IdUsuario     = info.UsuarioIngresa,
                            Fecha_Transac = DateTime.Now
                        });
                        ro_periodo_x_ro_Nomina_TipoLiqui Entity_periodo_nom = Context.ro_periodo_x_ro_Nomina_TipoLiqui.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa && q.IdPeriodo == info.IdPeriodo &&
                                                                                                                                      q.IdNomina_Tipo == IdNomina_General && q.IdNomina_TipoLiqui == IdNomina_TipoLiqui_PagoUtilidad);

                        Context.ro_periodo_x_ro_Nomina_TipoLiqui.Add(new ro_periodo_x_ro_Nomina_TipoLiqui
                        {
                            IdEmpresa          = info.IdEmpresa,
                            IdPeriodo          = info.IdPeriodo,
                            IdNomina_Tipo      = IdNomina_General,
                            IdNomina_TipoLiqui = IdNomina_TipoLiqui_PagoUtilidad,
                            Cerrado            = "N",
                            Procesado          = "S",
                            Contabilizado      = "N"
                        });
                    }

                    #endregion
                    #region Utilidad cabecera

                    ro_participacion_utilidad Entity = Context.ro_participacion_utilidad.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa && q.IdUtilidad == info.IdUtilidad);
                    if (Entity == null)
                    {
                        return(false);
                    }
                    Entity.IdUsuarioModifica         = info.IdUsuarioModifica;
                    Entity.BaseUtilidad              = info.BaseUtilidad;
                    Entity.UtilidadDerechoIndividual = info.UtilidadDerechoIndividual;
                    Entity.UtilidadCargaFamiliar     = info.UtilidadCargaFamiliar;
                    Entity.Utilidad           = info.Utilidad;
                    Entity.Fecha_ultima_modif = DateTime.Now;



                    #endregion

                    #region Rol
                    ro_rol Entity_rol = Context.ro_rol.FirstOrDefault(q => q.IdEmpresa == info.IdEmpresa &&
                                                                      q.IdNominaTipo == IdNomina_General &&
                                                                      q.IdNominaTipoLiqui == IdNomina_TipoLiqui_PagoUtilidad);

                    decimal rolId = 0;
                    if (Entity_rol == null)
                    {
                        var lst = from q in Context.ro_rol
                                  where q.IdEmpresa == info.IdEmpresa
                                  select q;
                        rolId = lst.Max(q => q.IdRol) + 1;
                        Context.ro_rol.Add(new ro_rol
                        {
                            IdRol             = rolId,
                            IdSucursal        = info.IdSucursal,
                            IdEmpresa         = info.IdEmpresa,
                            IdNominaTipo      = IdNomina_General,
                            IdNominaTipoLiqui = IdNomina_TipoLiqui_PagoUtilidad,
                            Cerrado           = "N",
                            Descripcion       = "PAGO DE UTILIDA " + info.IdPeriodo.ToString(),
                            UsuarioIngresa    = info.UsuarioIngresa,
                            FechaIngresa      = DateTime.Now
                        });
                    }
                    else
                    {
                        Entity_rol.Observacion   = "PAGO DE UTILIDA " + info.IdPeriodo.ToString();
                        Entity_rol.FechaModifica = DateTime.Now;
                        rolId = Entity_rol.IdRol;
                        var rol_detalle = Context.ro_rol_detalle.Where(s => s.IdEmpresa == info.IdEmpresa && s.IdRol == Entity_rol.IdRol);
                        Context.ro_rol_detalle.RemoveRange(rol_detalle);
                    }

                    #region INSERTANDO ROL DETALLE
                    var utilidad_detalle = Context.ro_participacion_utilidad_empleado.Where(s => s.IdEmpresa == info.IdEmpresa && s.IdUtilidad == info.IdUtilidad);
                    Context.ro_participacion_utilidad_empleado.RemoveRange(utilidad_detalle);


                    foreach (var item in info.detalle)// LISTADO DE EMPLEADOS CON BENEFICIOS DE UTILIDAS
                    {
                        double TotalEgreso = 0;
                        // DIAS TRABAJADOS
                        Context.ro_rol_detalle.Add(new ro_rol_detalle
                        {
                            IdEmpresa           = info.IdEmpresa,
                            IdRol               = rolId,
                            IdEmpleado          = item.IdEmpleado,
                            IdRubro             = RubrosCalculados.IdRubro_dias_trabajados,
                            Observacion         = " ",
                            IdSucursal          = item.IdSucursal,
                            rub_visible_reporte = true,
                            Orden               = 0,
                            Valor               = item.DiasTrabajados
                        });
                        // TOTAL DE UTILIDAD
                        Context.ro_rol_detalle.Add(new ro_rol_detalle
                        {
                            IdEmpresa           = info.IdEmpresa,
                            IdRol               = rolId,
                            IdEmpleado          = item.IdEmpleado,
                            IdRubro             = RubrosCalculados.IdRubro_utilidad,
                            Observacion         = " ",
                            IdSucursal          = item.IdSucursal,
                            rub_visible_reporte = true,
                            Orden               = 0,
                            Valor               = item.ValorTotal
                        });
                        // TOTAL DE INGRESO
                        Context.ro_rol_detalle.Add(new ro_rol_detalle
                        {
                            IdEmpresa           = info.IdEmpresa,
                            IdRol               = rolId,
                            IdEmpleado          = item.IdEmpleado,
                            IdRubro             = RubrosCalculados.IdRubro_tot_ing,
                            Observacion         = " ",
                            IdSucursal          = item.IdSucursal,
                            rub_visible_reporte = true,
                            Orden               = 0,
                            Valor               = item.ValorTotal
                        });

                        #region Novedades y prestamos POR EMPLEADO
                        string sql = "";
                        sql = "  SELECT  n.IdEmpresa, n.IdEmpleado,IdSucursal, nd.IdRubro,sum( nd.Valor)Valor " +
                              "FROM dbo.ro_empleado_Novedad AS n INNER JOIN " +
                              "dbo.ro_empleado_novedad_det AS nd ON n.IdEmpresa = nd.IdEmpresa AND n.IdNovedad = nd.IdNovedad " +
                              "where nd.EstadoCobro = 'PEN' and n.IdEmpresa='" + info.IdEmpresa + "' and IdNomina_Tipo=" + param.IdNomina_General + " and n.IdNomina_TipoLiqui=" + param.IdNomina_TipoLiqui_PagoUtilidad + "" +
                              "and  nd.FechaPago between '" + fi.ToString("yyyy-MM-dd") + "'  and '" + ff.ToString("yyyy-MM-dd") + "'" +
                              " and n.IdEmpleado='" + item.IdEmpleado + "'" +
                              "GROUP BY n.IdEmpresa, n.IdEmpleado,IdSucursal, nd.IdRubro, nd.Valor " +
                              "union all " +
                              "SELECT p.IdEmpresa, p.IdEmpleado,IdSucursal, p.IdRubro, pd.TotalCuota " +
                              "FROM dbo.ro_prestamo AS p INNER JOIN " +
                              " dbo.ro_prestamo_detalle AS pd ON p.IdEmpresa = pd.IdEmpresa AND p.IdPrestamo = pd.IdPrestamo INNER JOIN " +
                              " dbo.ro_empleado AS e ON p.IdEmpresa = e.IdEmpresa AND p.IdEmpleado = e.IdEmpleado " +
                              " where pd.EstadoPago = 'PEN' and p.IdEmpresa='" + info.IdEmpresa + "' and descuento_ben_soc='1' and pd.IdNominaTipoLiqui=" + param.IdNomina_TipoLiqui_PagoUtilidad + "" +
                              " and  pd.FechaPago between '" + fi.ToString("yyyy-MM-dd") + "'  and '" + ff.ToString("yyyy-MM-dd") + "'" +
                              " and p.IdEmpleado='" + item.IdEmpleado + "'" +
                              " GROUP BY p.IdEmpresa, p.IdEmpleado,e.IdSucursal, pd.TotalCuota, p.IdRubro";
                        var lstNovedades = Context.Database.SqlQuery <ro_empleado_novedad_det_Info>(sql).ToList();
                        foreach (var item_ in lstNovedades)
                        {
                            Context.ro_rol_detalle.Add(new ro_rol_detalle
                            {
                                IdEmpresa           = info.IdEmpresa,
                                IdRol               = rolId,
                                IdEmpleado          = item_.IdEmpleado,
                                IdRubro             = item_.IdRubro,
                                Observacion         = " ",
                                IdSucursal          = item_.IdSucursal,
                                rub_visible_reporte = true,
                                Orden               = 0,
                                Valor               = item_.Valor
                            });
                        }
                        #endregion

                        #region Total de egreso por empleado
                        var lst_total_egreso = lstNovedades.GroupBy(c => new
                        {
                            c.IdEmpleado,
                            c.IdSucursal
                        })
                                               .Select(x => new
                        {
                            IdEmpleado = x.Key.IdEmpleado,
                            IdSucursal = x.Key.IdSucursal,
                            Valor      = x.Sum(y => y.Valor)
                        }).ToList();
                        foreach (var item_egr in lst_total_egreso)
                        {
                            TotalEgreso = item_egr.Valor;
                            Context.ro_rol_detalle.Add(new ro_rol_detalle
                            {
                                IdEmpresa           = info.IdEmpresa,
                                IdRol               = rolId,
                                IdEmpleado          = item_egr.IdEmpleado,
                                IdRubro             = RubrosCalculados.IdRubro_tot_egr,
                                Observacion         = " ",
                                IdSucursal          = item_egr.IdSucursal,
                                rub_visible_reporte = true,
                                Orden               = 0,
                                Valor               = item_egr.Valor
                            });
                        }
                        #endregion

                        #region liquido a recibir en rol
                        Context.ro_rol_detalle.Add(new ro_rol_detalle
                        {
                            IdEmpresa           = info.IdEmpresa,
                            IdRol               = rolId,
                            IdEmpleado          = item.IdEmpleado,
                            IdRubro             = RubrosCalculados.IdRubro_tot_pagar,
                            Observacion         = " ",
                            IdSucursal          = item.IdSucursal,
                            rub_visible_reporte = true,
                            Orden               = 0,
                            Valor               = item.ValorTotal - TotalEgreso
                        });
                        #endregion

                        #region detalle de utilidad
                        ro_participacion_utilidad_empleado Entity_det = new ro_participacion_utilidad_empleado
                        {
                            IdEmpresa          = info.IdEmpresa,
                            IdUtilidad         = info.IdUtilidad,
                            IdEmpleado         = item.IdEmpleado,
                            CargasFamiliares   = item.CargasFamiliares,
                            ValorCargaFamiliar = item.ValorCargaFamiliar,
                            ValorIndividual    = item.ValorIndividual,
                            ValorTotal         = item.ValorTotal,
                            DiasTrabajados     = item.DiasTrabajados,
                            Descuento          = TotalEgreso,
                            NetoRecibir        = (item.ValorTotal - TotalEgreso)
                        };
                        Context.ro_participacion_utilidad_empleado.Add(Entity_det);
                        #endregion



                        #endregion
                    }


                    #endregion

                    Context.SaveChanges();
                }

                return(true);
            }
            catch (Exception e)
            {
                throw;
            }
        }