public ActionResult TaoTaiKhoanKhachHang()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.CVLeTanHopDong))
            {
                return(AccessDeniedView());
            }
            var model = new XuLyHopDongModel.KhachHangModel();

            return(View(model));
        }
        public ActionResult TaoTaiKhoan(XuLyHopDongModel.KhachHangModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.CVLeTanHopDong))
            {
                return(AccessDeniedView());
            }
            var khachhangrole = new CustomerRole();
            var customer      = new Customer
            {
                CustomerGuid        = Guid.NewGuid(),
                Email               = model.Email,
                Active              = true,
                CreatedOnUtc        = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
            };
            var item = _customerService.GetCustomerByEmail(model.Email);

            if (item == null)
            {
                _customerService.InsertCustomer(customer);


                //firstname,lastname
                _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName);
                _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName);
                //mã hóa password
                if (!String.IsNullOrWhiteSpace(model.Password))
                {
                    var changePassRequest = new ChangePasswordRequest(model.Email, false, _customerSettings.DefaultPasswordFormat, model.Password);
                    var changePassResult  = _customerRegistrationService.ChangePassword(changePassRequest);
                    if (!changePassResult.Success)
                    {
                        foreach (var changePassError in changePassResult.Errors)
                        {
                            ErrorNotification(changePassError);
                        }
                    }
                }
                //update khách hàng cho hop dong
                var _hopdong = _chonveService.GetHopDongById(model.HopDongId);

                if (_hopdong != null)
                {
                    if (_hopdong.KhachHangID > 0 && _hopdong.KhachHangID != customer.Id)
                    {
                        var khachhang   = _customerService.GetCustomerById(_hopdong.KhachHangID);
                        var newitemrole = new CustomerRole();
                        foreach (var _item in khachhang.CustomerRoles)
                        {
                            if (_item.SystemName == SystemCustomerRoleNames.HTNhaXeManager)
                            {
                                newitemrole = _item;
                            }
                        }
                        khachhang.CustomerRoles.Remove(newitemrole);
                        _customerService.UpdateCustomer(khachhang);
                    }
                    _hopdong.KhachHangID = customer.Id;
                    _chonveService.UpdateHopDong(_hopdong);
                }
                //customerrole

                var allCustomerRoles = _customerService.GetAllCustomerRoles(true);
                var newCustomerRoles = new List <CustomerRole>();
                foreach (var customerRole in allCustomerRoles)
                {
                    if (customerRole.SystemName == SystemCustomerRoleNames.Registered)
                    {
                        newCustomerRoles.Add(customerRole);
                    }
                    if (customerRole.SystemName == SystemCustomerRoleNames.HTNhaXeManager)
                    {
                        newCustomerRoles.Add(customerRole);
                    }
                }
                foreach (var _customerRole in newCustomerRoles)
                {
                    customer.CustomerRoles.Add(_customerRole);
                }
                _customerService.UpdateCustomer(customer);
                //tạo văn phong với địa chỉ của nhà xe là trụ sở chính
                var truso = new VanPhong();
                truso.NhaXeId = _hopdong.NhaXeID;
                var nhaxe = _nhaxeService.GetNhaXeById(_hopdong.NhaXeID);
                truso.TenVanPhong    = nhaxe.TenNhaXe;
                truso.KieuVanPhongID = (int)ENKieuVanPhong.TruSo;
                truso.DiaChiID       = nhaxe.DiaChiID;
                _nhaxeService.InsertVanPhong(truso);
                // tao nhan vien thuoc van phong
                var nhanvien = new NhanVien();
                nhanvien.HoVaTen           = nhaxe.TenNhaXe;
                nhanvien.Email             = nhaxe.Email;
                nhanvien.CustomerID        = customer.Id;
                nhanvien.KieuNhanVienID    = (int)ENKieuNhanVien.QuanLy;
                nhanvien.GioiTinhID        = (int)ENGioiTinh.Nam;
                nhanvien.TrangThaiID       = (int)ENTrangThaiNhanVien.DangLamViec;
                nhanvien.NgayBatDauLamViec = DateTime.Now;

                nhanvien.VanPhongID = truso.Id;
                nhanvien.NhaXeID    = _hopdong.NhaXeID;
                nhanvien.DiaChiID   = nhaxe.DiaChiID;
                _nhanvienService.Insert(nhanvien);
                return(RedirectToAction("ChiTietHopDong", new { id = model.HopDongId }));
            }
            return(View());
        }