Example #1
0
        public ActionResult Register(RegisterViewModel model)
        {
            using (PSPService pservice = new PSPService())
                using (UserService uservice = new UserService())
                    using (RoleService rservice = new RoleService())
                        using (ClientService cservice = new ClientService())
                            using (AddressService aservice = new AddressService())
                                using (TransactionScope scope = new TransactionScope())
                                    using (DocumentService dservice = new DocumentService())
                                        using (EstimatedLoadService eservice = new EstimatedLoadService())
                                        {
                                            PSP    psp;
                                            User   user;
                                            Client client;

                                            int    objectId;
                                            string objectType;

                                            model.PSPOptions = pservice.List();

                                            if (!ModelState.IsValid)
                                            {
                                                Notify("The supplied information is not complete. Please try again.", NotificationType.Error);

                                                return(View(model));
                                            }

                                            bool isClient = (model.ServiceType == ServiceType.RequirePalletManagement || model.ServiceType == ServiceType.HaveCompany);

                                            #region Validations

                                            // Check company does not exist by Company Name & Registration Number
                                            if (isClient && !string.IsNullOrEmpty(model.CompanyRegistrationNumber) && cservice.ExistByCompanyRegistrationNumber(model.CompanyRegistrationNumber.Trim()))
                                            {
                                                Notify($"A Client with that Company Registration Number already exists. Contact us if you require further assistance.", NotificationType.Error);

                                                return(View(model));
                                            }
                                            else if (!isClient && !string.IsNullOrEmpty(model.CompanyRegistrationNumber) && pservice.ExistByCompanyRegistrationNumber(model.CompanyRegistrationNumber.Trim()))
                                            {
                                                Notify($"A Pooling Service Provider (PSP) with that Company Registration Number already exists. Contact us if you require further assistance.", NotificationType.Error);

                                                return(View(model));
                                            }

                                            #endregion

                                            RoleType roleType = (isClient) ? RoleType.Client : RoleType.PSP;

                                            user = new User()
                                            {
                                                Type    = ( int )roleType,
                                                Name    = model.CompanyName,
                                                Email   = model.AdminEmail,
                                                Cell    = model.ContactNumber,
                                                Surname = model.CompanyName,
                                                Status  = ( int )Status.Pending,
                                            };

                                            // Get Role
                                            Role role = rservice.GetByName(roleType.GetStringValue());

                                            if (isClient)
                                            {
                                                #region Create Client

                                                client = new Client()
                                                {
                                                    PSPName                   = model.PSPName,
                                                    Email                     = model.EmailAddress,
                                                    TradingAs                 = model.TradingAs,
                                                    VATNumber                 = model.VATNumber,
                                                    BBBEELevel                = model.BBBEELevel,
                                                    AdminEmail                = model.AdminEmail,
                                                    CompanyName               = model.CompanyName,
                                                    Description               = model.Description,
                                                    AdminPerson               = model.ContactPerson,
                                                    FinPersonEmail            = model.EmailAddress,
                                                    ContactPerson             = model.ContactPerson,
                                                    ContactNumber             = model.ContactNumber,
                                                    FinancialPerson           = model.ContactPerson,
                                                    CompanyType               = ( int )model.CompanyType,
                                                    PalletType                = ( int )model.TypeOfPalletUse,
                                                    Status                    = ( int )PSPClientStatus.Unverified,
                                                    ServiceRequired           = ( int )model.ServiceType,
                                                    PalletTypeOther           = model.OtherTypeOfPalletUse,
                                                    NumberOfLostPallets       = model.NumberOfLostPallets,
                                                    CompanyRegistrationNumber = model.CompanyRegistrationNumber,
                                                };

                                                if (model.PSPId > 0)
                                                {
                                                    client.PSPClients.Add(new PSPClient()
                                                    {
                                                        ClientId   = client.Id,
                                                        CreatedOn  = DateTime.Now,
                                                        PSPId      = model.PSPId.Value,
                                                        ModifiedOn = DateTime.Now,
                                                        ModifiedBy = model.AdminEmail,
                                                        Status     = ( int )Status.Active,
                                                    });
                                                }

                                                client = cservice.Create(client);

                                                user.ClientUsers.Add(new ClientUser()
                                                {
                                                    UserId     = user.Id,
                                                    ClientId   = client.Id,
                                                    CreatedOn  = DateTime.Now,
                                                    ModifiedOn = DateTime.Now,
                                                    ModifiedBy = model.AdminEmail,
                                                    Status     = ( int )Status.Active,
                                                });

                                                objectId   = client.Id;
                                                objectType = "Client";

                                                #endregion
                                            }
                                            else
                                            {
                                                #region Create PSP

                                                psp = new PSP()
                                                {
                                                    Email                     = model.EmailAddress,
                                                    TradingAs                 = model.TradingAs,
                                                    VATNumber                 = model.VATNumber,
                                                    BBBEELevel                = model.BBBEELevel,
                                                    AdminEmail                = model.AdminEmail,
                                                    CompanyName               = model.CompanyName,
                                                    Description               = model.Description,
                                                    AdminPerson               = model.ContactPerson,
                                                    ContactPerson             = model.ContactPerson,
                                                    ContactNumber             = model.ContactNumber,
                                                    FinPersonEmail            = model.EmailAddress,
                                                    FinancialPerson           = model.ContactPerson,
                                                    CompanyType               = ( int )model.CompanyType,
                                                    PalletType                = ( int )model.TypeOfPalletUse,
                                                    Status                    = ( int )PSPClientStatus.Unverified,
                                                    ServiceRequired           = ( int )model.ServiceType,
                                                    PalletTypeOther           = model.OtherTypeOfPalletUse,
                                                    NumberOfLostPallets       = model.NumberOfLostPallets,
                                                    CompanyRegistrationNumber = model.CompanyRegistrationNumber,
                                                };

                                                psp = pservice.Create(psp);

                                                user.PSPUsers.Add(new PSPUser()
                                                {
                                                    PSPId      = psp.Id,
                                                    UserId     = user.Id,
                                                    CreatedOn  = DateTime.Now,
                                                    ModifiedOn = DateTime.Now,
                                                    ModifiedBy = model.AdminEmail,
                                                    Status     = ( int )Status.Active,
                                                });

                                                objectId   = psp.Id;
                                                objectType = "PSP";

                                                #endregion
                                            }

                                            model.Id = objectId;

                                            #region Create User

                                            user = uservice.Create(user, role.Id);

                                            #endregion


                                            #region Create Estimated Load

                                            if (model.EstimatedLoad != null)
                                            {
                                                EstimatedLoad load = new EstimatedLoad()
                                                {
                                                    ObjectId   = objectId,
                                                    ObjectType = objectType,
                                                    BudgetYear = model.EstimatedLoad.BudgetYear,
                                                    January    = model.EstimatedLoad.January,
                                                    February   = model.EstimatedLoad.February,
                                                    March      = model.EstimatedLoad.March,
                                                    April      = model.EstimatedLoad.April,
                                                    May        = model.EstimatedLoad.May,
                                                    June       = model.EstimatedLoad.June,
                                                    July       = model.EstimatedLoad.July,
                                                    August     = model.EstimatedLoad.August,
                                                    September  = model.EstimatedLoad.September,
                                                    October    = model.EstimatedLoad.October,
                                                    November   = model.EstimatedLoad.November,
                                                    December   = model.EstimatedLoad.December,
                                                    Total      = model.EstimatedLoad.Total,
                                                };

                                                load = eservice.Create(load);
                                            }

                                            #endregion


                                            #region Create Address (s)

                                            if (model.Address != null)
                                            {
                                                Address address = new Address()
                                                {
                                                    ObjectId     = objectId,
                                                    ObjectType   = objectType,
                                                    Town         = model.Address.Town,
                                                    Status       = ( int )Status.Active,
                                                    PostalCode   = model.Address.PostCode,
                                                    Type         = ( int )model.Address.AddressType,
                                                    Addressline1 = model.Address.AddressLine1,
                                                    Addressline2 = model.Address.AddressLine2,
                                                    ProvinceId   = model.Address.ProvinceId,
                                                };

                                                aservice.Create(address);
                                            }

                                            #endregion


                                            #region Any Uploads

                                            if (model.Files.NullableAny(f => f.File != null))
                                            {
                                                // Create folder
                                                string path = Server.MapPath($"~/{VariableExtension.SystemRules.DocumentsLocation}/{objectType}/{model.CompanyName.Trim()}-{model.CompanyRegistrationNumber.Trim().Replace( "/", "_" ).Replace( "\\", "_" )}/");

                                                if (!Directory.Exists(path))
                                                {
                                                    Directory.CreateDirectory(path);
                                                }

                                                string now = DateTime.Now.ToString("yyyyMMddHHmmss");

                                                foreach (FileViewModel f in model.Files.Where(f => f.File != null))
                                                {
                                                    Document doc = new Document()
                                                    {
                                                        ObjectId    = objectId,
                                                        ObjectType  = objectType,
                                                        Status      = ( int )Status.Active,
                                                        Name        = f.Name,
                                                        Category    = f.Name,
                                                        Title       = f.File.FileName,
                                                        Size        = f.File.ContentLength,
                                                        Description = f.Description,
                                                        Type        = Path.GetExtension(f.File.FileName),
                                                        Location    = $"{objectType}/{model.CompanyName.Trim()}-{model.CompanyRegistrationNumber.Trim().Replace( "/", "_" ).Replace( "\\", "_" )}/{now}-{f.File.FileName}"
                                                    };

                                                    dservice.Create(doc);

                                                    string fullpath = Path.Combine(path, $"{now}-{f.File.FileName}");

                                                    try
                                                    {
                                                        f.File.SaveAs(fullpath);
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                    }
                                                }
                                            }

                                            #endregion

                                            // Complete the scope
                                            scope.Complete();
                                        }

            // Send Welcome Email
            bool sent = SendUserWelcome(model);

            if (sent)
            {
                SendUserWelcome1(model);
            }

            Notify("Thank you for your application to register, you will receive an email with further instructions shortly. Please check your spam folder if you do not receive mail within 24 hours.", NotificationType.Success);

            return(RedirectToAction("Login"));
        }