public bool UpdateBayerEmployee(BayerEmployee model, List <int?> selectedZones) { if (model == null) { return(false); } //business validations //*is email already used if (IsMailAddressCurrentlyUsed(model.EMail, model.Id)) { ResultManager.Add(ErrorDefault, Trace + "La direccion de email '" + model.EMail + "' actualmente esta asignada a otro usuario, por favor ingresa una diferente."); return(false); } //*is zone(s) already assigned foreach (int item in selectedZones) { MunicipalitiesXEmployee auxMunicipality = null; switch (model.User.RolesXUser.First().Cat_UserRoleId) { case 3: auxMunicipality = Repository.BayerEmployees.GRVAssignedToZone(item); if (auxMunicipality != null && auxMunicipality.BayerEmployeeId != model.Id) { ResultManager.Add("La zona '" + auxMunicipality.Municipality.Zone.Name + "' ya esta asignada al GRV '" + auxMunicipality.BayerEmployee.NameDisplay + "'", Trace + "CreateBayerEmployee.311 Zona en conflicto 'id:" + item + "'"); return(false); } break; case 5: auxMunicipality = Repository.BayerEmployees.RTVAssignedToZone(item); if (auxMunicipality != null && auxMunicipality.BayerEmployeeId != model.Id) { ResultManager.Add("La zona '" + auxMunicipality.Municipality.Zone.Name + "' ya esta asignada al RTV '" + auxMunicipality.BayerEmployee.NameDisplay + "'", Trace + "CreateBayerEmployee.321 Zona en conflicto 'id:" + item + "'"); return(false); } break; default: ResultManager.Add(ErrorDefault, Trace + "CreateBayerEmployee.391 rol de usuario no esperado'" + model.User.RolesXUser.First().Cat_UserRoleId + "'"); return(false); } } BayerEmployee auxEmployee = null; try { auxEmployee = Repository.BayerEmployees.Get(model.Id); //User.Cat_UserStatusId = model.Cat_UserStatusId; auxEmployee.IdB = model.IdB; auxEmployee.User.NickName = model.EMail; auxEmployee.EMail = model.EMail; auxEmployee.Name = model.Name; auxEmployee.LastNameF = model.LastNameF; auxEmployee.LastNameM = model.LastNameM; auxEmployee.PhoneNumber = model.PhoneNumber; List <MunicipalitiesXEmployee> auxMXE = new List <MunicipalitiesXEmployee>(); bool auxExist; //update selected zones if (selectedZones == null) //is there a zone selected on this last change? { //none selected, remove all foreach (MunicipalitiesXEmployee item in auxEmployee.MunicipalitiesXEmployee) { //auxEmployee.MunicipalitiesXEmployee.Remove(item); Repository.MunicipalitiesXEmployee.Remove(item.Id); } } else {//some selected, update //identify records to delete (not selected anymore) foreach (MunicipalitiesXEmployee item in auxEmployee.MunicipalitiesXEmployee) { auxExist = false; foreach (int itemSelectedZoneId in selectedZones) { if (item.Municipality.Cat_ZoneId == itemSelectedZoneId) { auxExist = true; break; } } if (!auxExist) { auxMXE.Add(item); } } //delete records (not selected anymore) foreach (MunicipalitiesXEmployee item in auxMXE) { Repository.MunicipalitiesXEmployee.Remove(item.Id); } auxMXE.Clear(); //identify records to add (not yet in db) List <AddressMunicipality> municipalitiesXZone = (List <AddressMunicipality>)Repository.AddressMunicipalities.GetByZoneIds(selectedZones); foreach (AddressMunicipality item in municipalitiesXZone) { auxExist = false; foreach (MunicipalitiesXEmployee itemMXE in auxEmployee.MunicipalitiesXEmployee) { if (item.Id == itemMXE.AddressMunicipalityId) { auxExist = true; break; } } if (!auxExist) { auxMXE.Add(new MunicipalitiesXEmployee() { AddressMunicipalityAddressStateId = item.AddressStateId, AddressMunicipalityId = item.Id, BayerEmployeeId = auxEmployee.Id }); } } //insert records (not yet in db) foreach (MunicipalitiesXEmployee item in auxMXE) { auxEmployee.MunicipalitiesXEmployee.Add(item); } auxMXE.Clear(); } Repository.Complete(); ResultManager.Add("Perfil actualizado", ""); ResultManager.IsCorrect = true; return(true); } catch (Exception ex) { ErrorManager.Add("", ErrorDefault, "", ex); ResultManager.Add(ErrorDefault, ""); } return(false); }
public bool CreateEmployee(Model.BayerEmployee model /*, string selectedRoleIdB*/)//Model.Employee employee) { ResultManager.Clear(); string token = PSD.Common.Random.Token(20); Model.User user = null; try { user = new User(); user.Cat_UserStatusId = 4; user.Cat_UserStatus = Repository.UserStatuses.Get(4);///TODO: why do I need this if i already set statusId (doesn't update automatically unless i restart app) user.NickName = model.EMail; user.FailedLoginAttempts = 0; user.LoginToken = token; user.LoginTokenGeneratedDate = PSD.Common.Dates.Today; Model.RolesXUser rolesXUser = new RolesXUser(); rolesXUser.UserId = user.Id; rolesXUser.Cat_UserRoleId = model.User.RolesXUser.First().Cat_UserRoleId;//Repository.UserRoles.GetByRoleIdB(selectedRoleIdB).Id; //Repository.RolesXUser.Add(rolesXUser); user.RolesXUser = new List <RolesXUser>() { rolesXUser }; Model.BayerEmployee employee = new BayerEmployee(); employee.IdB = model.IdB; employee.Name = string.IsNullOrWhiteSpace(model.Name) ? "" : model.Name; employee.LastNameF = string.IsNullOrWhiteSpace(model.LastNameF) ? "" : model.LastNameF; employee.LastNameM = string.IsNullOrWhiteSpace(model.LastNameM) ? "" : model.LastNameM; employee.EMail = model.EMail; employee.User = user; Model.MunicipalitiesXEmployee municipalityXEmployee; List <Model.MunicipalitiesXEmployee> municipalitiesXEmployee = new List <MunicipalitiesXEmployee>(); foreach (MunicipalitiesXEmployee item in model.MunicipalitiesXEmployee) { municipalityXEmployee = new MunicipalitiesXEmployee(); municipalityXEmployee.AddressMunicipalityAddressStateId = item.AddressMunicipalityAddressStateId; municipalityXEmployee.AddressMunicipalityId = item.AddressMunicipalityId; municipalityXEmployee.BayerEmployeeId = employee.Id; municipalitiesXEmployee.Add(municipalityXEmployee); } employee.MunicipalitiesXEmployee = municipalitiesXEmployee; Repository.BayerEmployees.Add(employee); Repository.Complete(); ResultManager.IsCorrect = true; } catch (Exception ex) { ErrorManager.Add(Trace + "", ErrorDefault, "exception while creating employee", ex); ResultManager.Add(ErrorDefault, "No se ha creado el nuevo usuario"); return(false); } if (SendUserInvitationEmail(user)) { ResultManager.Add("El usuario se ha creado correctamente", ""); } else { //ResultManager.IsCorrect = false; ResultManager.Add("El usuario se ha creado correctamente, sin embargo hubo un problema al enviar la invitación de correo", "Puede reenviar la invitación desde la página de detalle de usuario"); } return(ResultManager.IsCorrect); }
public bool CreateBayerEmployee(Model.BayerEmployee model, List <int?> zones = null)//Model.Employee employee) { if (zones == null) { zones = new List <int?>(); } string token = PSD.Common.Random.Token(20); Model.User user = null; //business validations //*is email already used if (IsMailAddressCurrentlyUsed(model.EMail)) { ResultManager.Add(ErrorDefault, Trace + "La direccion de email '" + model.EMail + "' actualmente esta asignada a otro usuario, por favor ingresa una diferente."); return(false); } //*is zone(s) already assigned foreach (int item in zones) { MunicipalitiesXEmployee auxMunicipality = null; switch (model.User.RolesXUser.First().Cat_UserRoleId) { case 3: auxMunicipality = Repository.BayerEmployees.GRVAssignedToZone(item); if (auxMunicipality != null) { ResultManager.Add("La zona '" + auxMunicipality.Municipality.Zone.Name + "' ya esta asignada al GRV '" + auxMunicipality.BayerEmployee.NameDisplay + "'", Trace + "CreateBayerEmployee.311 Zona en conflicto 'id:" + item + "'"); return(false); } break; case 5: auxMunicipality = Repository.BayerEmployees.GRVAssignedToZone(item); if (auxMunicipality != null) { ResultManager.Add("La zona '" + auxMunicipality.Municipality.Zone.Name + "' ya esta asignada al RTV '" + auxMunicipality.BayerEmployee.NameDisplay + "'", Trace + "CreateBayerEmployee.321 Zona en conflicto 'id:" + item + "'"); return(false); } break; default: ResultManager.Add(ErrorDefault, Trace + "CreateBayerEmployee.391 rol de usuario no esperado'" + model.User.RolesXUser.First().Cat_UserRoleId + "'"); return(false); } } try { user = new User(); user.Cat_UserStatusId = 4; user.Cat_UserStatus = Repository.UserStatuses.Get(4);///TODO: why do I need this if i already set statusId (doesn't update automatically unless i restart app) user.NickName = model.EMail; user.FailedLoginAttempts = 0; user.LoginToken = token; user.LoginTokenGeneratedDate = PSD.Common.Dates.Today; Model.RolesXUser rolesXUser = new RolesXUser(); rolesXUser.UserId = user.Id; rolesXUser.Cat_UserRoleId = model.User.RolesXUser.First().Cat_UserRoleId;//Repository.UserRoles.GetByRoleIdB(selectedRoleIdB).Id; //Repository.RolesXUser.Add(rolesXUser); user.RolesXUser = new List <RolesXUser>() { rolesXUser }; Model.BayerEmployee employee = new BayerEmployee(); employee.IdB = model.IdB; employee.Name = string.IsNullOrWhiteSpace(model.Name) ? "" : model.Name; employee.LastNameF = string.IsNullOrWhiteSpace(model.LastNameF) ? "" : model.LastNameF; employee.LastNameM = string.IsNullOrWhiteSpace(model.LastNameM) ? "" : model.LastNameM; employee.EMail = model.EMail; employee.User = user; //zones IEnumerable <AddressMunicipality> municipalitiesXZone = Repository.AddressMunicipalities.GetByZoneIds(zones); MunicipalitiesXEmployee municipalityXEmployee; List <MunicipalitiesXEmployee> municipalitiesXEmployee = new List <MunicipalitiesXEmployee>(); foreach (AddressMunicipality item in municipalitiesXZone) { municipalityXEmployee = new MunicipalitiesXEmployee(); municipalityXEmployee.AddressMunicipalityAddressStateId = item.AddressStateId; municipalityXEmployee.AddressMunicipalityId = item.Id; municipalityXEmployee.BayerEmployeeId = employee.Id; municipalitiesXEmployee.Add(municipalityXEmployee); } employee.MunicipalitiesXEmployee = municipalitiesXEmployee; Repository.BayerEmployees.Add(employee); Repository.Complete(); ResultManager.IsCorrect = true; } catch (Exception ex) { ErrorManager.Add(Trace + "", ErrorDefault, "exception while creating employee", ex); ResultManager.IsCorrect = false; ResultManager.Add(ErrorDefault, "No se ha creado el nuevo usuario"); return(false); } if (SendUserInvitationEmail(user)) { ResultManager.Add("El usuario se ha creado correctamente, se le ha enviado un correo con la invitación a activar su cuenta", ""); } else { ResultManager.Add("El usuario se ha creado correctamente, sin embargo hubo un problema al enviar la invitación de correo"); } return(ResultManager.IsCorrect); }