public bool addCargoHistorial(SCPM_PUESTO_HIST historial, int per_id, int cargo_id, int rel_lab_id) { //compruebo si persona ya tiene un cargo activo var historial_cargos_persona = (from h in db.SCPM_PUESTO_HIST.Include("SCPM_PERSONALES") where h.SCPM_PERSONALES.PER_ID == per_id select h).ToList(); SCPM_PUESTO_HIST currentCargo = historial_cargos_persona.Count > 0 ? historial_cargos_persona.OrderByDescending(c => c.PST_HIS_FEC_INI).FirstOrDefault() : historial_cargos_persona.FirstOrDefault(); if (currentCargo != null) { currentCargo.SCPM_CARGOSReference.Load(); currentCargo.SCPM_RELACIONES_LABORALESReference.Load(); } if (currentCargo != null && currentCargo.SCPM_CARGOS.CAR_ID != cargo_id && (currentCargo.PST_HIS_FEC_FIN == null || DateTime.Now.CompareTo(currentCargo.PST_HIS_FEC_FIN) <= 0)) { HelperUtil.showNotifi("persona ya tiene un cargo activo, finalicelo para poder asignarlo a otro cargo"); return false; } // var historial_cargos_cargo_id = (from h in db.SCPM_PUESTO_HIST.Include("SCPM_CARGOS") where h.SCPM_CARGOS.CAR_ID == cargo_id select h).ToList(); var nowTime = DateTime.Now; SCPM_PUESTO_HIST lastCargo = historial_cargos_cargo_id.Count > 0 ? historial_cargos_cargo_id.OrderByDescending(c => c.PST_HIS_FEC_INI).FirstOrDefault() : historial_cargos_cargo_id.FirstOrDefault(); if (lastCargo != null && (lastCargo.PST_HIS_FEC_FIN == null || DateTime.Now.CompareTo(lastCargo.PST_HIS_FEC_FIN) <= 0)) {//el cargo esta actualmente ocupado lastCargo.SCPM_PERSONALESReference.Load(); lastCargo.SCPM_RELACIONES_LABORALESReference.Load(); if (lastCargo.SCPM_PERSONALES.PER_ID != per_id) { //el cargo esta ocupado por otra persona HelperUtil.showNotifi("el cargo esta ocupado por otra persona"); return false; } else if (lastCargo.SCPM_RELACIONES_LABORALES.REL_LAB_ID == rel_lab_id) {// el cargo esta ocupado por la misma persona y relacion laboral es la misma solo se actualizara sus campos //fecha fin no puede ser menor a fecha inicio if (historial.PST_HIS_FEC_FIN != null && historial.PST_HIS_FEC_INI.Value.CompareTo(historial.PST_HIS_FEC_FIN) > 0) { HelperUtil.showNotifi("fecha inicio no puede ser mayor que final"); return false; } //checa si rango de fecha no overlap con alguno del historial del cargo o la persona if (!isRangoFechaCargoValid(historial_cargos_persona, historial_cargos_cargo_id, historial.PST_HIS_FEC_INI.Value, historial.PST_HIS_FEC_FIN, lastCargo)) { return false; } lastCargo.PST_HIS_FEC_FIN = historial.PST_HIS_FEC_FIN; lastCargo.PST_HIS_FEC_INI = historial.PST_HIS_FEC_INI; db.SaveChanges(); HelperUtil.showNotifi("Solo fecha final actualizada"); return true; } else { // el cargo esta ocupado por la misma persona y relacion laboral no es la misma if (historial.PST_HIS_FEC_FIN == null || historial.PST_HIS_FEC_FIN.Value.CompareTo(nowTime) > 0) {//quiere cambiar de relacion laboral al cargo que actualmente tiene, se finaliza este historial y añade otro con la nueva relacion laboral //entonces fecha inicio del nuevo historial debe ser mayor a fecha inicio del cargo actual if (historial.PST_HIS_FEC_INI.Value.CompareTo(lastCargo.PST_HIS_FEC_INI.Value) < 0) { HelperUtil.showNotifi("Fecha inico de la nueva relacion laboral debe ser mayor a fecha inicio de la actual relacion laboral."); return false; } //fecha fin no puede ser menor a fecha inicio if (historial.PST_HIS_FEC_FIN != null && nowTime.CompareTo(historial.PST_HIS_FEC_FIN) > 0) { HelperUtil.showNotifi("fecha inicio no puede ser mayor que final"); return false; } lastCargo.PST_HIS_FEC_FIN = nowTime; historial.PST_HIS_FEC_INI = nowTime; db.SaveChanges(); HelperUtil.showNotifi("Relacion laboral anterior finalizada."); } else {//el cargo esta actualmente ocupado se desea añadir un historial antiguo //entonces fecha fin debe ser menor a fecha inicio del cargo actual if (historial.PST_HIS_FEC_FIN.Value.CompareTo(lastCargo.PST_HIS_FEC_INI) > 0) { HelperUtil.showNotifi("Intenta añadir un historial antiguo. fecha final debe ser menor a fecha inicial del cargo actual"); return false; } //fecha fin no puede ser menor a fecha inicio if (historial.PST_HIS_FEC_INI.Value.CompareTo(historial.PST_HIS_FEC_FIN) > 0) { HelperUtil.showNotifi("fecha inicio no puede ser mayor que final"); return false; } //checa si rango de fecha no overlap con alguno del historial del cargo o la persona if (!isRangoFechaCargoValid(historial_cargos_persona, historial_cargos_cargo_id, historial.PST_HIS_FEC_INI.Value, historial.PST_HIS_FEC_FIN, null)) { return false; } } //add new relacion historial.SCPM_PERSONALES = getPersonasByID(per_id).ToList().First(); historial.SCPM_RELACIONES_LABORALES = getRalacionLabByID(rel_lab_id); historial.SCPM_CARGOS = getCargoByID(cargo_id); db.AddToSCPM_PUESTO_HIST(historial); //save db.SaveChanges(); HelperUtil.showNotifi("Nueva Relacion laboral añadida."); return true; } } else {//el cargo no esta ocupado actualmente, se procede a añadir el historial nuevo //fecha fin no puede ser menor a fecha inicio if (historial.PST_HIS_FEC_FIN != null && historial.PST_HIS_FEC_INI.Value.CompareTo(historial.PST_HIS_FEC_FIN) > 0) { HelperUtil.showNotifi("fecha inicio no puede ser mayor que final"); return false; } //checa si rango de fecha no overlap con alguno del historial del cargo o la persona if (!isRangoFechaCargoValid(historial_cargos_persona, historial_cargos_cargo_id, historial.PST_HIS_FEC_INI.Value, historial.PST_HIS_FEC_FIN, null)) { return false; } //add new relacion historial.SCPM_PERSONALES = getPersonasByID(per_id).ToList().First(); historial.SCPM_RELACIONES_LABORALES = getRalacionLabByID(rel_lab_id); historial.SCPM_CARGOS = getCargoByID(cargo_id); db.AddToSCPM_PUESTO_HIST(historial); //save db.SaveChanges(); HelperUtil.showNotifi("Nueva Relacion laboral añadida."); return true; } }
private bool isRangoFechaCargoValid(List<SCPM_PUESTO_HIST> historial_cargos_persona, List<SCPM_PUESTO_HIST> historial_cargos_cargo_id, DateTime startB, DateTime? endB, SCPM_PUESTO_HIST lastCargo) { foreach (var item in historial_cargos_persona) { if (lastCargo == null || item.PST_HIS_ID != lastCargo.PST_HIS_ID) { var startA = item.PST_HIS_FEC_INI.Value; var endA = item.PST_HIS_FEC_FIN; if (endA == null && endB == null) { if (startA > startB) { endA = startA; endB = startA; } else { endA = startB; endB = startB; } } if ((startA <= endB) && (endA >= startB)) { HelperUtil.showNotifi("persona ya posee un cargo en esas fechas"); return false; } } } foreach (var item in historial_cargos_cargo_id) { if (lastCargo == null || item.PST_HIS_ID != lastCargo.PST_HIS_ID) { var startA = item.PST_HIS_FEC_INI.Value; var endA = item.PST_HIS_FEC_FIN; if (endA == null && endB == null) { if (startA > startB) { endA = startA; endB = startA; } else { endA = startB; endB = startB; } } if ((startA <= endB) && (endA >= startB)) { HelperUtil.showNotifi("cargo ya posee un funcionario en esas fechas"); return false; } } } return true; }