public ActionResult Create([Bind(Include = "CustomerId,TrebId,WebsiteUrl,ApplicationID,Password,CompanyID,UserId,PhotoPath,FirstName,LastName,MiddleName,EmailID,MobileNo,CountryID,StateID,CityID,ZipCode,Latitude,Longitude,CreatedOn,LastUpdatedOn,MobileVerifyCode,EmailVerifyCode,IsMobileVerified,IsEmailVerified,IsActive,AdminCompanyAddress")] AdminModel AdminModel, HttpPostedFileBase file, HttpPostedFileBase Logo)
        {
            TempData["ShowMessage"] = "";
            TempData["MessageBody"] = "";
            UserPermissionAction("admin", RoleAction.create.ToString());
            CheckPermission();
            Mapper.CreateMap <CommunicationApp.Models.AdminModel, CommunicationApp.Entity.Customer>();
            CommunicationApp.Entity.Customer Customer = Mapper.Map <CommunicationApp.Models.AdminModel, CommunicationApp.Entity.Customer>(AdminModel);

            if (ModelState.IsValid)
            {
                var customerFound = _CustomerService.GetCustomers().Where(x => x.EmailId == Customer.EmailId || x.MobileNo == Customer.MobileNo || x.WebsiteUrl == Customer.WebsiteUrl).FirstOrDefault();
                if (customerFound == null)
                {
                    //Save Company of admin

                    Company Company = new Entity.Company();
                    Company.WebSite        = Customer.WebsiteUrl;
                    Company.CountryID      = 1;
                    Company.StateID        = 1;
                    Company.CityID         = 1;
                    Company.CompanyAddress = AdminModel.AdminCompanyAddress != "" ? AdminModel.AdminCompanyAddress : "*****@*****.**";
                    Company.CompanyName    = AdminModel.CompanyName != null ? AdminModel.CompanyName : "Company";
                    Company.EmailID        = "*****@*****.**";
                    Company.PhoneNo        = "123456987";
                    Company.IsActive       = true;
                    if (Logo != null)
                    {
                        Company.LogoPath = Savefile(Logo);
                    }

                    _CompanyService.InsertCompany(Company);

                    //Insert User first
                    CommunicationApp.Entity.User user = new CommunicationApp.Entity.User();
                    //user.UserId =0; //New Case
                    user.FirstName        = Customer.FirstName;
                    user.TrebId           = Customer.TrebId;
                    user.LastName         = Customer.LastName;
                    user.UserName         = Customer.EmailId;
                    user.Password         = SecurityFunction.EncryptString(AdminModel.Password);
                    user.UserEmailAddress = Customer.EmailId;
                    user.CompanyID        = Company.CompanyID;
                    user.CreatedOn        = DateTime.Now;
                    user.LastUpdatedOn    = DateTime.Now;
                    user.TrebId           = "0000000";
                    user.IsActive         = true;
                    _UserService.InsertUser(user);
                    //End : Insert User first

                    var UserID = user.UserId;
                    if (user.UserId > 0)
                    {
                        //Insert User Role
                        CommunicationApp.Entity.UserRole userRole = new CommunicationApp.Entity.UserRole();
                        userRole.UserId = user.UserId;
                        userRole.RoleId = 2; //By Default set new Admin/user role id=2
                        _UserRoleService.InsertUserRole(userRole);
                        //End : Insert User Role

                        //Insert the Customer
                        Customer.FirstName        = Customer.FirstName;
                        Customer.UserId           = user.UserId;
                        Customer.Designation      = "Admin";
                        Customer.MobileVerifyCode = CommonCls.GetNumericCode();
                        Customer.EmailVerifyCode  = CommonCls.GetNumericCode();
                        Customer.MobileVerifyCode = "9999";
                        Customer.EmailVerifyCode  = "9999";
                        Customer.CreatedOn        = DateTime.Now;
                        Customer.CompanyID        = Company.CompanyID;
                        Customer.Address          = "";
                        Customer.ZipCode          = "";
                        Customer.IsEmailVerified  = true;

                        var PhotoPath = "";
                        if (file != null)
                        {
                            PhotoPath = Savefile(file);
                        }

                        Customer.PhotoPath             = PhotoPath;
                        Customer.IsMobileVerified      = false;
                        Customer.ApplicationId         = AdminModel.ApplicationId;
                        Customer.DeviceSerialNo        = AdminModel.DeviceSerialNo;
                        Customer.DeviceType            = AdminModel.DeviceType;
                        Customer.IsUpdated             = false;
                        Customer.IsNotificationSoundOn = true;
                        if (Customer.FirstName == null)
                        {
                            Customer.FirstName = "";
                        }
                        if (Customer.LastName == null)
                        {
                            Customer.LastName = "";
                        }
                        if (Customer.MiddleName == null)
                        {
                            Customer.MiddleName = "";
                        }
                        _CustomerService.InsertCustomer(Customer);

                        var CustomerID = Customer.CustomerId.ToString();
                        AdminModel.CustomerId   = Customer.CustomerId;
                        TempData["ShowMessage"] = "Success";
                        TempData["MessageBody"] = "Admin successfully register.";
                    }
                }
                else
                {
                    if (customerFound.EmailId == AdminModel.EmailID)
                    {
                        TempData["ShowMessage"] = "Error";
                        TempData["MessageBody"] = "Email is already exist.";
                    }
                    else if (customerFound.MobileNo == AdminModel.MobileNo)
                    {
                        TempData["ShowMessage"] = "Error";
                        TempData["MessageBody"] = "MobileNos is already exist.";
                    }
                    else
                    {
                        TempData["ShowMessage"] = "Error";
                        TempData["MessageBody"] = "Some error occured.";
                    }
                }
            }
            else
            {
                var errors           = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray();
                var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
                TempData["ShowMessage"] = "Error";
                TempData["MessageBody"] = "Please fill the required data.";
                return(View(AdminModel));
            }


            return(RedirectToAction("Index"));
        }
        public ActionResult Register([Bind(Include = "CustomerId,FirstName,LastName,EmailId,MobileNo,Password")] CustomerModel Customermodel)
        {
            try
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Please fill the required field with valid data";
                if (ModelState.IsValid)
                {
                    Mapper.CreateMap <CommunicationApp.Models.CustomerModel, CommunicationApp.Entity.Customer>();
                    CommunicationApp.Entity.Customer Customer = Mapper.Map <CommunicationApp.Models.CustomerModel, CommunicationApp.Entity.Customer>(Customermodel);


                    Customer Customers = _CustomerService.GetCustomers().Where(c => c.FirstName.Trim() == Customer.FirstName.Trim() || c.EmailId.Trim() == Customer.EmailId.Trim() || c.MobileNo.Trim() == Customer.MobileNo.Trim()).FirstOrDefault();

                    if (Customers == null)
                    {
                        //There is no session in API Controller. So we will find solution in future
                        Customer.CompanyID = 1;
                        //Insert User first
                        string Password = Customermodel.Password;
                        CommunicationApp.Entity.User user = new CommunicationApp.Entity.User();
                        //user.UserId =0; //New Case
                        user.FirstName        = Customer.FirstName;
                        user.LastName         = Customer.LastName;
                        user.UserName         = Customer.MobileNo;                        // insert mobile number//
                        user.Password         = SecurityFunction.EncryptString(Password); //No password right now. We will create send email procedure for password recovery
                        user.UserEmailAddress = Customer.EmailId;
                        user.CompanyID        = Customer.CompanyID;
                        user.IsActive         = true;
                        _UserService.InsertUser(user);
                        //End : Insert User first

                        if (user.UserId > 0)
                        {
                            //Insert User Role
                            CommunicationApp.Entity.UserRole userRole = new CommunicationApp.Entity.UserRole();
                            userRole.UserId = user.UserId;
                            userRole.RoleId = 3; //By Default set new Customer/user role id=3
                            _UserroleService.InsertUserRole(userRole);
                            //End : Insert User Role

                            //Insert the Customer

                            //Save the Driver photo in Folder


                            //Check SubPath Exist or Not


                            Customer.UserId = user.UserId;

                            Customer.Longitude     = 0;
                            Customer.Latitude      = 0;
                            Customer.ApplicationId = "";
                            _CustomerService.InsertCustomer(Customer); //Save Operation
                            //End : Insert the Customer
                        }
                        TempData["ShowMessage"] = "success";
                        TempData["MessageBody"] = Customer.FirstName + " is saved successfully.";
                        ModelState.Clear();
                        return(RedirectToAction("LogOn", "Account"));
                    }
                    else
                    {
                        TempData["ShowMessage"] = "error";
                        if (Customers.FirstName.Trim() == Customermodel.FirstName.Trim())
                        {
                            TempData["MessageBody"] = Customermodel.FirstName + " is already exists.";
                        }
                        else if (Customers.EmailId.Trim() == Customermodel.EmailID.Trim())
                        {
                            TempData["MessageBody"] = Customermodel.EmailID + " is already exists.";
                        }
                        else if (Customers.MobileNo.Trim() == Customermodel.MobileNo.Trim())
                        {
                            TempData["MessageBody"] = Customermodel.MobileNo + " is already exists.";
                        }
                        else
                        {
                            TempData["MessageBody"] = "Please fill the required field with valid data";
                        }

                        //_DriverService.UpdateDriver(driver); //Update Operation
                    }
                }
            }

            catch (RetryLimitExceededException)
            {
                TempData["ShowMessage"] = "error";
                TempData["MessageBody"] = "Some unknown problem occured while proccessing save operation on " + Customermodel.FirstName + " ";
            }


            return(View(Customermodel));
        }