public bool CreateSubdistributor(Subdistributor model) { ResultManager.Clear(); //sys validations if (model == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.111 No se recibió el modelo"); return(false); } if (model.BNAddress == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.151 El modelo recibido no contiene el campo BNAddress"); return(false); } if (model.SubdistributorEmployees == null || model.SubdistributorEmployees.FirstOrDefault() == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.171 El modelo recibido no contiene el campo SubdistributorEmployee[0]"); return(false); } //business validations if (model.SubdistributorEmployees == null || model.SubdistributorEmployees.FirstOrDefault() == null || string.IsNullOrWhiteSpace(model.SubdistributorEmployees.FirstOrDefault().EMail)) { ResultManager.Add("El correo electrónico del dueño no puede estar vacio", Trace + "UpdateSubdistributor.211 El campo 'SubdistributorEmployees[0].EMail' esta vacio"); return(false); } if (model.BNAddress.AddressColonyId == null || model.BNAddress.AddressColonyId < 1) { ResultManager.Add("Se debe seleccionar la dirección del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNAddress.AddressColonyId' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BNAddress.Street)) { ResultManager.Add("Se debe seleccionar la calle, en la dirección del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNAddress.Street' esta vacio"); return(false); } if (model.RTV_BayerEmployeeId < 1) { ResultManager.Add("Se debe tener asignado un RTV", Trace + "UpdateSubdistributor.211 El campo 'RTV_BayerEmployeeId' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.Type) || model.Type == "-1") { ResultManager.Add("Se debe seleccionar el tipo Subdistribuidor ó Agricultor", Trace + "UpdateSubdistributor.211 El campo 'Type' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BusinessName)) { ResultManager.Add("Se debe indicar la razón social", Trace + "UpdateSubdistributor.211 El campo 'BusinessName' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BNLegalRepresentative)) { ResultManager.Add("Se debe indicar el nombre del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNLegalRepresentative' esta vacio"); return(false); } if (model.CommercialNames.Count <= 0 || model.CommercialNames.FirstOrDefault().Name.Length == 0) { ResultManager.Add("Se debe indicar al menos un nombre comercial", Trace + "UpdateSubdistributor.211 El campo 'CommercialNames' esta vacio"); return(false); } string emailToCheck = model.SubdistributorEmployees.Count > 0 ? model.SubdistributorEmployees.FirstOrDefault().EMail : string.Empty; if (IsMailAddressCurrentlyUsed(emailToCheck)) { ResultManager.Add(ErrorDefault, Trace + "La direccion de email '" + emailToCheck + "' actualmente esta asignada a otro usuario, por favor ingresa una diferente."); return(false); } string token = PSD.Common.Random.Token(20); Model.SubdistributorEmployee subdistributorEmployee = null; try { //user principal User user = User.NewEmpty(); user.Person = null; user.Cat_UserStatusId = 4; //created 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.SubdistributorEmployees.FirstOrDefault().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 = 9;//9:subdistributor (principal) user.RolesXUser = new List <RolesXUser>() { rolesXUser }; subdistributorEmployee = new SubdistributorEmployee(); subdistributorEmployee.Subdistributor = null; subdistributorEmployee.EMail = model.SubdistributorEmployees.FirstOrDefault().EMail; subdistributorEmployee.Name = model.CommercialNames.FirstOrDefault().Name; subdistributorEmployee.User = user; /* * //user view * User userView = User.NewEmpty(); * userView.Person = null; * userView.Cat_UserStatusId = 4;//created * userView.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) * userView.NickName = "";//no email at first * userView.FailedLoginAttempts = 0; * userView.LoginToken = ""; * userView.LoginTokenGeneratedDate = null; * * Model.RolesXUser rolesXUserView = new RolesXUser(); * rolesXUserView.UserId = userView.Id; * rolesXUserView.Cat_UserRoleId = 8;//8:distributor(view) * userView.RolesXUser = new List<RolesXUser>() { rolesXUserView }; * * Model.SubdistributorEmployee distributorEmployeeView = SubdistributorEmployee(); * distributorEmployeeView.Distributor = null; * distributorEmployeeView.EMail = model.EMail; * distributorEmployeeView.Name = model.Distributor.CommercialName + "(consulta)"; * distributorEmployeeView.User = userView; * //employee.Cat_ZoneId = model.Cat_ZoneId == -1? null : model.Cat_ZoneId; */ //TODO:create from model info Address bnAddress = new Address(); AddressColony auxColony = Repository.AddressColonies.Get((int)model.BNAddress.AddressColonyId); bnAddress.AddressStateId = auxColony.AddressStateId; bnAddress.AddressMunicipalityId = auxColony.AddressMunicipalityId; bnAddress.AddressPostalCodeId = auxColony.AddressPostalCodeId; bnAddress.AddressColonyId = auxColony.Id; bnAddress.Street = model.BNAddress.Street; bnAddress.NumberExt = model.BNAddress.NumberExt; bnAddress.NumberInt = model.BNAddress.NumberInt; //subdistributor Model.Subdistributor subddistributor = new Subdistributor(); //TODO:set address subddistributor.BNAddress = bnAddress; // add set of additional address Address address1 = new Address(); address1.AddressStateId = null; address1.AddressMunicipalityId = null; address1.AddressPostalCodeId = null; address1.AddressColonyId = null; address1.Street = string.Empty; address1.NumberExt = string.Empty; address1.NumberInt = string.Empty; Address address2 = new Address(); address2.AddressStateId = null; address2.AddressMunicipalityId = null; address2.AddressPostalCodeId = null; address2.AddressColonyId = null; address2.Street = string.Empty; address2.NumberExt = string.Empty; address2.NumberInt = string.Empty; Address address3 = new Address(); address3.AddressStateId = null; address3.AddressMunicipalityId = null; address3.AddressPostalCodeId = null; address3.AddressColonyId = null; address3.Street = string.Empty; address3.NumberExt = string.Empty; address3.NumberInt = string.Empty; AddressesXSubdistributor addressPerSubdistributor1 = new AddressesXSubdistributor(); addressPerSubdistributor1.Address = address1; addressPerSubdistributor1.Subdistributor = subddistributor; AddressesXSubdistributor addressPerSubdistributor2 = new AddressesXSubdistributor(); addressPerSubdistributor2.Address = address2; addressPerSubdistributor2.Subdistributor = subddistributor; AddressesXSubdistributor addressPerSubdistributor3 = new AddressesXSubdistributor(); addressPerSubdistributor3.Address = address3; addressPerSubdistributor3.Subdistributor = subddistributor; subddistributor.Addresses.Add(addressPerSubdistributor1); subddistributor.Addresses.Add(addressPerSubdistributor2); subddistributor.Addresses.Add(addressPerSubdistributor3); subddistributor.IdB = Repository.AppConfigurations.IdBCounterGetNextSubdistributor(); subddistributor.Type = model.Type; subddistributor.BNLegalRepresentative = model.BNLegalRepresentative; subddistributor.RTV_BayerEmployeeId = model.RTV_BayerEmployeeId; subddistributor.RTVCreator_BayerEmployeeId = (int)model.RTV_BayerEmployeeId; subddistributor.BusinessName = model.BusinessName; subddistributor.CommercialNames = model.CommercialNames; subddistributor.WebSite = model.WebSite; subddistributor.SubdistributorEmployees.Add(subdistributorEmployee); //subddistributor.DistributorUsers.Add(distributorEmployeeView); subddistributor.CropsXMunicipality = model.CropsXMunicipality; Repository.Subdistributors.Add(subddistributor); Repository.Complete(); ResultManager.IsCorrect = true; } catch (Exception ex) { ResultManager.Add(ErrorDefault, "exception while creating subddistributor", ex); return(false); } if (SendUserInvitationEmail(subdistributorEmployee.User)) { ResultManager.Add("El subdistribuidor se ha creado correctamente", ""); } else { ResultManager.Add("El subdistribuidor 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 UpdateSubdistributorEmployee(SubdistributorEmployee model) { ResultManager.Clear(); //sys validations if (model == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributorEmployee.111 No se recibió el modelo"); return(false); } if (model.Id == 0 || model.Id == -1) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributorEmployee.121 El modelo recibido no contiene el id a actualizar"); return(false); } if (model.Subdistributor.BNAddress == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributorEmployee.211 El modelo recibido no contiene el campo BNAddress"); return(false); } //business validation if (string.IsNullOrWhiteSpace(model.EMail)) { ResultManager.Add("El correo electrónico no puede estar vacio", Trace + "UpdateSubdistributorEmployee.211 El campo EMail esta vacio"); return(false); } if (model.Subdistributor.BNAddress.AddressColonyId <= 0) { ResultManager.Add("Se debe seleccionar la dirección del representante legal", Trace + "UpdateSubdistributorEmployee.211 El campo BNAddress esta vacio"); return(false); } if (IsMailAddressCurrentlyUsed(model.EMail, CurrentUser.Id)) { ResultManager.Add(ErrorDefault, Trace + "La direccion de email '" + model.EMail + "' actualmente esta asignada a otro usuario, por favor ingresa una diferente."); return(false); } SubdistributorEmployee auxEmployee = null; try { auxEmployee = Repository.SubdistributorEmployees.Get(model.Id); 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; //auxEmployee.Subdistributor.BusinessName = model.Subdistributor.BusinessName; //auxEmployee.Subdistributor.CommercialNames = model.Distributor.CommercialName; auxEmployee.Subdistributor.WebSite = model.Subdistributor.WebSite; //-BNAddress AddressColony auxColony = Repository.AddressColonies.Get((int)model.Subdistributor.BNAddress.AddressColonyId); auxEmployee.Subdistributor.BNAddress.AddressStateId = auxColony.AddressStateId; auxEmployee.Subdistributor.BNAddress.AddressMunicipalityId = auxColony.AddressMunicipalityId; auxEmployee.Subdistributor.BNAddress.AddressPostalCodeId = auxColony.AddressPostalCodeId; auxEmployee.Subdistributor.BNAddress.AddressColonyId = auxColony.Id; auxEmployee.Subdistributor.BNAddress.Street = model.Subdistributor.BNAddress.Street; auxEmployee.Subdistributor.BNAddress.NumberExt = model.Subdistributor.BNAddress.NumberExt; auxEmployee.Subdistributor.BNAddress.NumberInt = model.Subdistributor.BNAddress.NumberInt; //if profile was not completed, complete if (auxEmployee.User.Cat_UserStatusId == 5) { auxEmployee.User.Cat_UserStatusId = 1; } Repository.Complete(); ResultManager.IsCorrect = true; } catch (Exception ex) { ResultManager.Add(ErrorDefault, Trace + "911. Excepción al actualizar la información Mi Cuenta del subdistribuidor", ex); } return(ResultManager.IsCorrect); }
public bool UpdateSubdistributor(Subdistributor model) { ResultManager.Clear(); //sys validations if (model == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.111 No se recibió el modelo"); return(false); } if (model.Id < 1) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.311 El modelo recibido no contiene el id a actualizar"); return(false); } if (model.BNAddress == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.211 El modelo recibido no contiene el campo BNAddress"); return(false); } //business validations if (string.IsNullOrWhiteSpace(model.SubdistributorEmployees.FirstOrDefault().EMail)) { ResultManager.Add("El correo electrónico del dueño no puede estar vacio", Trace + "UpdateSubdistributor.211 El campo 'SubdistributorEmployees[0].EMail' esta vacio"); return(false); } if (model.BNAddress.AddressColonyId <= 0) { ResultManager.Add("Se debe seleccionar la dirección del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNAddress.AddressColonyId' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BNAddress.Street)) { ResultManager.Add("Se debe seleccionar la calle, en la dirección del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNAddress.Street' esta vacio"); return(false); } if (model.RTV_BayerEmployeeId <= 0) { ResultManager.Add("Se debe tener asignado un RTV", Trace + "UpdateSubdistributor.211 El campo 'RTV_BayerEmployeeId' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.Type)) { ResultManager.Add("Se debe seleccionar el tipo Subdistribuidor ó Agricultor", Trace + "UpdateSubdistributor.211 El campo 'Type' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BusinessName)) { ResultManager.Add("Se debe indicar la razón social", Trace + "UpdateSubdistributor.211 El campo 'BusinessName' esta vacio"); return(false); } if (string.IsNullOrWhiteSpace(model.BNLegalRepresentative)) { ResultManager.Add("Se debe indicar el nombre del representante legal", Trace + "UpdateSubdistributor.211 El campo 'BNLegalRepresentative' esta vacio"); return(false); } if (model.CommercialNames.Count <= 0) { ResultManager.Add("Se debe indicar al menos un nombre comercial", Trace + "UpdateSubdistributor.211 El campo 'CommercialNames' esta vacio"); return(false); } string emailToCheck = model.SubdistributorEmployees.Count > 0 ? model.SubdistributorEmployees.FirstOrDefault().EMail : string.Empty; int subdistributorPersonId = model.SubdistributorEmployees.Count > 0 ? model.SubdistributorEmployees.FirstOrDefault().Id : 0; if (IsMailAddressCurrentlyUsed(emailToCheck, subdistributorPersonId)) { ResultManager.Add(ErrorDefault, Trace + "La direccion de email '" + emailToCheck + "' actualmente esta asignada a otro usuario, por favor ingresa una diferente."); return(false); } Subdistributor auxSubdistributor = null; int auxSubdistributorId = -1; bool auxFoundItem = false; try { //retrieve subdistributor from db auxSubdistributor = Repository.Subdistributors.Get(model.Id); //sys validations (continue) //-subdistributor id exists in db if (auxSubdistributor == null) { ResultManager.Add(ErrorDefault, Trace + "UpdateSubdistributor.311 No se encontró un subdistribuidor con id '" + model.Id + "' en la base de datos"); } //business validations //TODO: business validations (empty fields, etc.) //update subdistributor details //auxSubdistributor.IdB = model.IdB; auxSubdistributor.Type = model.Type; auxSubdistributor.BNLegalRepresentative = model.BNLegalRepresentative; auxSubdistributor.RTV_BayerEmployeeId = model.RTV_BayerEmployeeId; auxSubdistributor.BusinessName = model.BusinessName; auxSubdistributor.WebSite = model.WebSite; //update commercial names if (auxSubdistributor.CommercialNames.Count == model.CommercialNames.Count) {//same number of commercial names //update names List <SubdistributorCommercialName> auxNewCommercialNames = model.CommercialNames.ToList(); int counter = 0; foreach (SubdistributorCommercialName item in auxSubdistributor.CommercialNames) { item.Name = auxNewCommercialNames[counter++].Name; } } else {//different number of commercial names //remove old commercial names while (auxSubdistributor.CommercialNames.Count > 0) { Repository.SubdistributorCommercialNames.Remove(auxSubdistributor.CommercialNames.FirstOrDefault()); } //assign current list auxSubdistributor.CommercialNames = model.CommercialNames; } //update cropsxmunicipality if (model.CropsXMunicipality.Count == 0) { auxSubdistributor.CropsXMunicipality.Clear(); } else { //add new items foreach (SubdistributorCropsXMunicipality itemNow in model.CropsXMunicipality) { if (itemNow.Id > 0) {//preexistent item, do nothing } else {//new item, add auxSubdistributor.CropsXMunicipality.Add(new SubdistributorCropsXMunicipality() { AddressMunicipalityAddressStateId = itemNow.AddressMunicipalityAddressStateId, AddressMunicipalityId = itemNow.AddressMunicipalityId, Cat_CropId = itemNow.Cat_CropId, SubdistributorId = model.Id }); } } //delete removed items List <SubdistributorCropsXMunicipality> auxCropsXMunicipalityToRemove = new List <SubdistributorCropsXMunicipality>(); foreach (SubdistributorCropsXMunicipality itemOld in auxSubdistributor.CropsXMunicipality) { foreach (SubdistributorCropsXMunicipality itemNow in model.CropsXMunicipality) { if (itemNow.Id == itemOld.Id) { auxFoundItem = true; break; } } if (!auxFoundItem) { auxCropsXMunicipalityToRemove.Add(itemOld); } } foreach (SubdistributorCropsXMunicipality item in auxCropsXMunicipalityToRemove) { auxSubdistributor.CropsXMunicipality.Remove(item); } } //-BNAddress AddressColony auxColony = Repository.AddressColonies.Get((int)model.BNAddress.AddressColonyId); auxSubdistributor.BNAddress.AddressStateId = auxColony.AddressStateId; auxSubdistributor.BNAddress.AddressMunicipalityId = auxColony.AddressMunicipalityId; auxSubdistributor.BNAddress.AddressPostalCodeId = auxColony.AddressPostalCodeId; auxSubdistributor.BNAddress.AddressColonyId = auxColony.Id; auxSubdistributor.BNAddress.Street = model.BNAddress.Street; auxSubdistributor.BNAddress.NumberExt = model.BNAddress.NumberExt; auxSubdistributor.BNAddress.NumberInt = model.BNAddress.NumberInt; // Update Addresses related via AddressesXSubdistributor entity foreach (AddressesXSubdistributor address in model.Addresses) { Address relatedAddress = Repository.Addresses.Get(address.Address.Id); relatedAddress.AddressColonyId = address.Address.AddressColonyId; relatedAddress.AddressMunicipalityId = address.Address.AddressMunicipalityId; relatedAddress.AddressPostalCodeId = address.Address.AddressPostalCodeId; relatedAddress.AddressStateId = address.Address.AddressStateId; relatedAddress.NumberExt = address.Address.NumberExt ?? string.Empty; relatedAddress.NumberInt = address.Address.NumberInt ?? string.Empty; relatedAddress.Street = address.Address.Street ?? string.Empty; } //TODO: update employees //Model.SubdistributorEmployee subdistributorEmployee = null; //update subdistributor users info SubdistributorEmployee auxSubdistributorEmployeeOwner = auxSubdistributor.SubdistributorEmployees.FirstOrDefault(); if (auxSubdistributorEmployeeOwner.EMail != model.SubdistributorEmployees.FirstOrDefault().EMail) { auxSubdistributorEmployeeOwner.EMail = model.SubdistributorEmployees.FirstOrDefault().EMail; auxSubdistributorEmployeeOwner.User.NickName = auxSubdistributorEmployeeOwner.EMail; auxSubdistributorId = auxSubdistributorEmployeeOwner.Id; } model = null; Repository.Complete(); ResultManager.IsCorrect = true; } catch (Exception ex) { ResultManager.Add(ErrorDefault, "exception while creating subddistributor", ex); return(false); } if (true)//TODO:send email to subdistributor about update (it could be the user email changed, what to do then, send another token?) SendNotificationUpdatedEmail(auxSubdistributorId)) { ResultManager.Add("El subdistribuidor se ha actualizado correctamente", ""); } else { ResultManager.Add("El subdistribuidor se ha actualizado correctamente, sin embargo hubo un problema al enviar la notificación de correo"); } return(ResultManager.IsCorrect); }
public bool UpdateDistributorEmployee(DistributorEmployee model) { if (model == null) { return(false); } 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); } DistributorEmployee auxEmployee = null; try { auxEmployee = Repository.DistributorEmployees.Get(model.Id); 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; //auxEmployee.Distributor.IdB = model.Distributor.IdB; auxEmployee.Distributor.BusinessName = model.Distributor.BusinessName; auxEmployee.Distributor.CommercialName = model.Distributor.CommercialName; auxEmployee.Distributor.WebSite = model.Distributor.WebSite; //-address AddressColony auxColony = Repository.AddressColonies.Get((int)model.Distributor.Address.AddressColonyId); auxEmployee.Distributor.Address.AddressStateId = auxColony.AddressStateId; auxEmployee.Distributor.Address.AddressMunicipalityId = auxColony.AddressMunicipalityId; auxEmployee.Distributor.Address.AddressPostalCodeId = auxColony.AddressPostalCodeId; auxEmployee.Distributor.Address.AddressColonyId = auxColony.Id; auxEmployee.Distributor.Address.Street = model.Distributor.Address.Street; auxEmployee.Distributor.Address.NumberExt = model.Distributor.Address.NumberExt; auxEmployee.Distributor.Address.NumberInt = model.Distributor.Address.NumberInt; //if profile was not completed, complete if (auxEmployee.User.Cat_UserStatusId == 5) { auxEmployee.User.Cat_UserStatusId = 1; } Repository.Complete(); ResultManager.Add("Perfil actualizado", ""); ResultManager.IsCorrect = true; return(true); } catch (Exception ex) { //Repository.Dispose(); ErrorManager.Add("", ErrorDefault, "", ex); ResultManager.Add(ErrorDefault, ""); ResultManager.IsCorrect = false; } return(false); }