Пример #1
0
        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 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);
        }
Пример #3
0
        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);
        }