Пример #1
0
        void GuardarEmpleado(bool nuevo)
        {
            DAL.Empleados emp = new DAL.Empleados();


            bool errores = false; //bandera utilizada para controlar errores y continuar o no con la actulizacion de registros

            int id = 0;

            if (txtCodigo.Text != "") // comprueba que en caso de que se ingrese un codigo manualmente
            {
                try
                {
                    id = Convert.ToInt32(txtCodigo.Text); //el codigo manual debe ser numerico
                }
                catch                                     //caso contrario se da un error e informa al usuario
                {
                    errores = true;
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("El campo código debe ser un valor númerico mayor a cero"), true);
                }
            }

            if (ViewState["EmpleadoID"] != null) //Si se esta actualizando
            {
                if (txtCodigo.Text != ViewState["EmpleadoID"].ToString())
                {
                    errores = true;
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("El legajo no puede ser modificado!"), true);
                }
            }

            if (txtGrupo.Text != "")
            {
                DAL.EmpleadosGrupos grupo = new DAL.EmpleadosGrupos();
                if (!grupo.existe_grupo(txtGrupo.Text))
                {
                    errores = true;
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("El grupo especificado no existe"), true);
                }
            }

            if (nuevo && id > 0) //Si se ingresa un id manual y se trata de un nuevo registro comprueba que dicho id no exista en la bd
            {
                if (emp.existe_id(id.ToString()))
                {
                    errores = true;
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("La operación no pudo completarse porque el codigo ingresado ya existe. Si lo desea puede dejar este campo en blanco para generar un código automaticamente."), true);
                }
            }

            if (txtNombre.Text == "" || txtApellido.Text == "" || txtFechaAlta.Text == "") //Verifico que los campos nombre, apellido y fecha de alta esten completos
            {
                if (txtNombre.Text == "")
                {
                    lblNombre.ForeColor = Color.Red;
                }
                if (txtApellido.Text == "")
                {
                    lblApellido.ForeColor = Color.Red;
                }
                if (txtFechaAlta.Text == "")
                {
                    lblFechaAlta.ForeColor = Color.Red;
                }

                errores = true;
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("Los campos señalados en rojo son requeridos y no pueden estar vacío"), true);
            }

            try
            {
                if (txtFechaAlta.Text != "")
                {
                    Convert.ToDateTime(txtFechaAlta.Text);
                }
            }
            catch
            {
                errores = true;
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("La fecha de alta debe ser una fecha válida"), true);
                lblFechaAlta.ForeColor = Color.Red;
            }

            try
            {
                if (txtFechaCierre.Text != "")
                {
                    Convert.ToDateTime(txtFechaCierre.Text);
                }
            }
            catch
            {
                errores = true;
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("La fecha de cierre debe ser una fecha válida"), true);
                lblFechaCierre.ForeColor = Color.Red;
            }

            try
            {
                if (txtFechaBaja.Text != "")
                {
                    Convert.ToDateTime(txtFechaBaja.Text);
                }
            }
            catch
            {
                errores = true;
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("La fecha de baja debe ser una fecha válida"), true);
                lblFechaBaja.ForeColor = Color.Red;
            }

            if (txtMail.Text != "")
            {
                string pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

                if (!Regex.IsMatch(txtMail.Text, pattern))
                {
                    errores = true;
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "notification", DAL.Varios.crear_mensaje("La direccion de mail no es correcta!. Ingrese una dirección válida"), true);
                }
            }

            if (!errores) //si no se presentan errores continua el guardado/actualizacion de registros
            {
                int hijos = 0;
                int.TryParse(txtHijos.Text, out hijos);

                emp.Id           = id;
                emp.Apellido     = txtApellido.Text;
                emp.Nombre       = txtNombre.Text;
                emp.Fecha_Alta   = txtFechaAlta.Text;
                emp.Fecha_Baja   = txtFechaBaja.Text;
                emp.Fecha_Cierre = txtFechaCierre.Text;
                emp.Mail         = txtMail.Text;
                emp.Grupo        = txtGrupo.SelectedItem.Value;
                if (nuevo)
                {
                    if (txtPassword.Text != "")
                    {
                        emp.Password = DAL.Varios.MD5Hash(txtPassword.Text);
                    }
                }
                else
                {
                    emp.Password = txtPassword.Text;
                }
                emp.Tel_laboral      = txtTelLaboral.Text;
                emp.Tel_personal     = txtTelPersonal.Text;
                emp.CUIL             = txtCUIL.Text;
                emp.Domicilio        = txtDomicilio.Text;
                emp.Tel_Alternativo  = txtTelAlternativo.Text;
                emp.Persona_contacto = txtContacto.Text;

                emp.Provincia    = txtProvincia.SelectedItem.Value;
                emp.Estado_Civil = txtEstadoCivil.SelectedItem.Value;

                emp.hijos            = hijos;
                emp.Tarea            = txtTarea.Text;
                emp.Fecha_Nacimiento = txtFechaNacimiento.Text;

                emp.PercibeAdelantos = chPercibeAdelantos.Checked;

                emp.Guardar(nuevo);

                if (nuevo)
                {
                    logs.modulo  = "RRHH - PERSONAL";
                    logs.accion  = "ALTA DE EMPLEADO: " + txtApellido.Text.ToUpper() + ", " + txtNombre.Text.ToUpper();
                    logs.usuario = Session["usr"].ToString();
                    if (Session["id"] != null)
                    {
                        logs.empleado_id = Session["id"].ToString();
                    }
                }
                else
                {
                    logs.modulo  = "RRHH - PERSONAL";
                    logs.accion  = "MODIFICACION DE EMPLEADO: " + txtApellido.Text.ToUpper() + ", " + txtNombre.Text.ToUpper();
                    logs.usuario = Session["usr"].ToString();
                    if (Session["id"] != null)
                    {
                        logs.empleado_id = Session["id"].ToString();
                    }
                }

                logs.guardar();

                LimpiarControles();

                ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Operacion realizada!", 3000), true);
            }
        }