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); } }
void guardar(bool nuevo) { bool errores = false; if (nuevo) { DAL.EmpleadosGrupos grupos = new DAL.EmpleadosGrupos(); int registros = 0; if (txtCodigo.Text != "") { registros = grupos.obtener(txtCodigo.Text).Count; } if (registros > 0) { ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("La operación no pudo realizarse ya que el código ingresado existe en la base de datos!.", 3000), true); errores = true; } } if (ViewState["GrupoID"] == null || ViewState["GrupoID"].ToString() == "") { errores = true; ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Debe seleccionar un grupo para poder editarlo", 3000), true); } if (txtEmpleado.SelectedValue == null || txtEmpleado.SelectedValue.ToString() == "0") { errores = true; ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Debe seleccionar un lider para el grupo", 3000), true); } if (txtDescripcion.Text == "") { if (txtDescripcion.Text == "") { lblDescripcion.ForeColor = Color.Red; } errores = true; ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Los campos señalados en rojo no pueden estar vacíos", 3000), true); } DAL.Empleados emp = new DAL.Empleados(); emp = emp.Obtener(txtEmpleado.SelectedValue.ToString()); if (emp.es_lider(emp.Id.ToString())) { errores = true; ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("El empleado seleccionado ya es lider de un grupo, elimine el grupo o bien cambie de lider e intentelo nuevamente", 3000), true); } int last_id = 0; if (!errores) { lblDescripcion.ForeColor = Color.Black; DAL.EmpleadosGrupos grupos = new DAL.EmpleadosGrupos(); grupos.Id = txtCodigo.Text; grupos.Nombre = txtDescripcion.Text; grupos.lider = txtEmpleado.SelectedValue.ToString(); if (grupos.Guardar(nuevo, out last_id)) { ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Operacion realizada!", 3000), true); } else { ScriptManager.RegisterStartupScript(Page, this.GetType(), "toast", DAL.Varios.crear_mensaje("Error al realizar el proceso", 3000), true); } if (nuevo) { logs.modulo = "RRHH - GRUPOS"; logs.accion = "AGREGO EL GRUPO: " + grupos.Nombre.ToUpper(); logs.usuario = Session["usr"].ToString(); if (Session["id"] != null) { logs.empleado_id = Session["id"].ToString(); } logs.guardar(); emp.Grupo = last_id.ToString(); emp.Guardar(false); } else { logs.modulo = "RRHH - GRUPOS"; logs.accion = "MODIFICO EL GRUPO: " + grupos.Nombre.ToUpper(); logs.usuario = Session["usr"].ToString(); if (Session["id"] != null) { logs.empleado_id = Session["id"].ToString(); } logs.guardar(); emp.Grupo = txtCodigo.Text; emp.Guardar(false); } limpiar(); } }