public async Task <IActionResult> Edit(string transId)
        {
            string message   = String.Empty;
            var    transInfo = await _transInfoServices.GetTransport(transId);

            if (transInfo != null)
            {
                if (transInfo.DateCompletedLocal > 0)
                {
                    message = "Không thể chỉnh sửa nếu đã HOÀN THÀNH hoặc HỦY";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index", controllerName: "Home"));
                }
                EditTransInfoViewModel model = new EditTransInfoViewModel()
                {
                    AdvanceMoney     = transInfo.AdvanceMoney,
                    CargoTonnage     = transInfo.CargoTonnage,
                    CargoTypes       = transInfo.CargoTypes,
                    DriverId         = transInfo.DayJob.DriverId,
                    Note             = transInfo.Note,
                    ReturnOfAdvances = transInfo.ReturnOfAdvances,
                    RouteId          = transInfo.RouteId,
                    TransportId      = transInfo.TransportId,
                    VehicleId        = transInfo.VehicleId,
                    Drivers          = _userServices.GetAvailableUsers().ToList(),
                    Routes           = RouteServices.GetAllRoutes().ToList(),
                    Vehicles         = (await _vehicleServices.GetNotUseVehicles()).ToList()
                };
                return(View(model));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index", controllerName: "Home"));
        }
        public async Task <IActionResult> Edit(EditRoleViewModel model)
        {
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                var roleEdit = await _roleManager.FindByIdAsync(model.RoleId);

                if (roleEdit != null)
                {
                    roleEdit.Name         = model.RoleName;
                    roleEdit.RolePriority = model.RolePriority;
                    roleEdit.IsActive     = model.IsActive;
                    var result = await _roleManager.UpdateAsync(roleEdit);

                    if (result.Succeeded)
                    {
                        message = "Phân quyền đã được điều chỉnh";
                        TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                        return(RedirectToAction(actionName: "Index"));
                    }
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Edit(string userId)
        {
            string message    = String.Empty;
            var    userToEdit = await _userManager.FindByIdAsync(userId);

            if (userToEdit != null)
            {
                var user = await _userManager.GetUserAsync(User);

                var roleUserEdit = await _userManager.GetRolesAsync(userToEdit);

                var roleIdUserEdit = await _roleManager.FindByNameAsync(roleUserEdit[0]);

                EditUserViewModel model = new EditUserViewModel()
                {
                    UserId       = userToEdit.Id,
                    ExistsAvatar = userToEdit.Avatar,
                    Email        = userToEdit.Email,
                    FirstName    = userToEdit.FirstName,
                    MiddleName   = userToEdit.MiddleName,
                    LastName     = userToEdit.LastName,
                    PhoneNumber  = userToEdit.PhoneNumber,
                    RoleId       = roleIdUserEdit.Id,
                    Roles        = await _roleServices.GetRoles(user.Id)
                };
                return(View(model));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Delete(string userId)
        {
            string message = String.Empty;
            var    user    = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                user.IsActive = false;
                var result = await _userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    message = $"Đã xóa tài khoản {user.FullName}";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction("Index"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Edit(int vehicleId)
        {
            string message = String.Empty;
            var    vehicle = await _vehicleServices.GetVehicle(vehicleId);

            if (vehicle != null)
            {
                EditVehicleViewModel vehicleEdit = new EditVehicleViewModel()
                {
                    VehicleId              = vehicle.VehicleId,
                    LicensePlate           = vehicle.LicensePlate,
                    VehicleName            = vehicle.VehicleName,
                    FuelConsumptionPerTone = vehicle.FuelConsumptionPerTone,
                    IsAvailable            = vehicle.IsAvailable,
                    IsInUse        = vehicle.IsInUse,
                    VehicleBrandId = vehicle.VehicleBrandId,
                    Specifications = vehicle.Specifications,
                    VehiclePayload = vehicle.VehiclePayload,
                    VehicleBrands  = _brandServices.GetAllBrands().ToList(),
                    Fuels          = _fuelServices.GetFuels().ToList()
                };
                return(View(vehicleEdit));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Details(string transportId)
        {
            string message   = String.Empty;
            var    transInfo = await _transInfoServices.GetTransport(transportId);

            if (transInfo != null)
            {
                DetailTransInfoViewModel model = new DetailTransInfoViewModel()
                {
                    AdvanceMoney       = transInfo.AdvanceMoney,
                    CargoTonnage       = transInfo.CargoTonnage,
                    CargoTypes         = transInfo.CargoTypes,
                    DriverId           = transInfo.DayJob.DriverId,
                    IsCancel           = transInfo.IsCancel,
                    IsCompleted        = transInfo.IsCompleted,
                    Note               = transInfo.Note,
                    ReasonCancel       = transInfo.ReasonCancel,
                    ReturnOfAdvances   = transInfo.ReturnOfAdvances,
                    RouteId            = transInfo.RouteId,
                    TransportId        = transInfo.TransportId,
                    VehicleId          = transInfo.VehicleId,
                    DateCompletedLocal = transInfo.DateCompletedLocal,
                    DateStartLocal     = transInfo.DateStartLocal,
                    Drivers            = _userServices.GetDriverUsers().ToList(),
                    Routes             = _routeServices.GetAllRoutes().ToList(),
                    Vehicles           = (await _vehicleServices.GetAllVehicles()).ToList()
                };
                return(View(model));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Create(CreateRoleViewModel model)
        {
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                AppIdentityRole newRole = new AppIdentityRole()
                {
                    Id           = Guid.NewGuid().ToString(),
                    Name         = model.RoleName,
                    RolePriority = model.RolePriority,
                    IsActive     = true
                };
                var result = await _roleManager.CreateAsync(newRole);

                if (result.Succeeded)
                {
                    message = "Phân quyền mới đã được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Edit(EditTransInfoViewModel model)
        {
            //get data for select elements if error
            model.Drivers  = _userServices.GetAvailableUsers().ToList();
            model.Routes   = _routeServices.GetAllRoutes().ToList();
            model.Vehicles = (await _vehicleServices.GetAllNotDeletedAndAvailableVehicles()).ToList();
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                //check the vehicle is used
                string driverIdUseVehicle = await _vehicleServices.IsVehicleInUsedByAnotherDriver(model.DriverId, model.VehicleId);

                if (!String.IsNullOrEmpty(driverIdUseVehicle))
                {
                    var driverUseVehicle = _userServices.GetUser(driverIdUseVehicle);
                    message = $"Xe đang được sử dụng bởi {driverUseVehicle.FullName}";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }
                //check cancel option and reason cancel
                if (model.IsCancel && String.IsNullOrEmpty(model.ReasonCancel))
                {
                    message = "Không thể để trống lý do hủy khi chọn hủy";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }
                if (!model.IsCancel && !String.IsNullOrEmpty(model.ReasonCancel))
                {
                    message = "Không thể điền lý do hủy nếu chưa chọn hủy";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }
                //if cancel and have reason cancel, write date complete transport
                if (model.IsCancel && !String.IsNullOrEmpty(model.ReasonCancel))
                {
                    DateTime localTimeUTC7 = SystemUtilites.ConvertToTimeZone(DateTime.UtcNow, "SE Asia Standard Time");
                    model.DateCompletedLocal = SystemUtilites.ConvertToTimeStamp(localTimeUTC7);
                    model.DateCompletedUTC   = SystemUtilites.ConvertToTimeStamp(DateTime.UtcNow);
                }
                var transInfo = _transInfoServices.GetTransport(model.TransportId);
                if (transInfo != null)
                {
                    var user = await _userManager.GetUserAsync(User);

                    if (user != null)
                    {
                        if (await _transInfoServices.EditTransInfo(model, user.Id))
                        {
                            message = "Đơn vận chuyển đã được điều chỉnh thông tin";
                            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                            return(RedirectToAction(actionName: "Manage"));
                        }
                    }
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }
        public async Task <IActionResult> Details(string userId)
        {
            string message = String.Empty;
            var    user    = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                var rolesUser = await _userManager.GetRolesAsync(user);

                var roleIdUser = await _roleManager.FindByNameAsync(rolesUser[0]);

                var detailUser = new DetailUserViewModel()
                {
                    UserId      = user.Id,
                    Avatar      = user.Avatar,
                    Email       = user.Email,
                    FirstName   = user.FirstName,
                    MiddleName  = user.MiddleName,
                    LastName    = user.LastName,
                    PhoneNumber = user.PhoneNumber,
                    RoleId      = roleIdUser.Id,
                    Roles       = _roleManager.Roles.ToList(),
                };
                return(View(detailUser));
            }
            message = "Không tìm thấy tài khoản, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Create(CreateVehicleViewModel model)
        {
            model.VehicleBrands = _brandServices.GetAllBrands().ToList();
            model.Fuels         = _fuelServices.GetFuels().ToList();
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                var newVehicle = new Vehicle()
                {
                    LicensePlate           = model.LicensePlate,
                    VehicleName            = model.VehicleName,
                    FuelConsumptionPerTone = model.FuelConsumptionPerTone,
                    IsAvailable            = model.IsAvailable,
                    IsInUse        = model.IsInUse,
                    VehicleBrandId = model.VehicleBrandId,
                    Specifications = model.Specifications,
                    VehiclePayload = model.VehiclePayload,
                    FuelId         = model.FuelId
                };
                if (await _vehicleServices.CreateVehicle(newVehicle))
                {
                    message = "Phương tiện mới đã được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }
        public async Task <IActionResult> DoneTransInfo(string transportId)
        {
            string message = String.Empty;
            var    trans   = await _transInfoServices.GetTransport(transportId);

            var user = await _userManager.GetUserAsync(User);

            if (trans != null)
            {
                if (trans.DateCompletedLocal > 0)
                {
                    message = "Chuyến vận chuyển đã được kết thúc";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index", controllerName: "Home"));
                }
                if (await _transInfoServices.DoneTransInfo(trans, user.Id))
                {
                    message = "Đã hoàn thành chuyến vận chuyển";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index", controllerName: "Home"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index", controllerName: "Home"));
        }
        public async Task <IActionResult> Delete(int vehicleId)
        {
            string message = String.Empty;

            if (await _vehicleServices.DeleteVehicle(vehicleId))
            {
                message = "Phương tiện đã được xóa";
                TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                return(RedirectToAction(actionName: "Index"));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
Exemplo n.º 13
0
        public async Task <IActionResult> Edit(EditRouteViewModel model)
        {
            string message     = String.Empty;
            var    userMessage = new MessageVM();

            if (await _routeServices.EditRoute(model))
            {
                message = "Tuyến vận chuyển đã điều chỉnh thông tin";
                TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                return(RedirectToAction(actionName: "Index"));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> DoneTransInfo(string transportId)
        {
            string message = String.Empty;
            var    trans   = await _transInfoServices.GetTransport(transportId);

            var user = await _userManager.GetUserAsync(User);

            if (trans != null)
            {
                if (trans.DateCompletedLocal > 0)
                {
                    message = "Chuyến vận chuyển đã được kết thúc";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Manage"));
                }
                if (await _transInfoServices.DoneTransInfo(trans, user.Id))
                {
                    var vehicle = await _vehicleServices.GetVehicle(trans.VehicleId);

                    var listTransportByVehicle = await _transInfoServices.GetTransportsNotFinishByVehicle(vehicle.VehicleId);

                    if (listTransportByVehicle != null && vehicle != null)
                    {
                        if (!listTransportByVehicle.Any() && vehicle.IsInUse)
                        {
                            await _vehicleServices.MakeVehicleIsFree(vehicle);
                        }
                    }
                    var driver = await _userManager.FindByIdAsync(trans.DayJob.DriverId);

                    var listTransportByDriver = await _transInfoServices.GetTransportsNotFinishByDriver(driver.Id);

                    if (listTransportByDriver != null && driver != null)
                    {
                        if (!listTransportByDriver.Any() && !driver.IsAvailable)
                        {
                            await _userServices.MakeDriverIsFree(driver);
                        }
                    }
                    message = "Đã hoàn thành chuyến vận chuyển";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Manage"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Manage"));
        }
        public async Task <IActionResult> Edit(EditAccountViewModel model)
        {
            string message = String.Empty;
            var    user    = await _userManager.GetUserAsync(User);

            if (user != null)
            {
                if (ModelState.IsValid)
                {
                    if (user.Id == model.AccountId)
                    {
                        if (!String.IsNullOrEmpty(model.NewPassword) && !String.IsNullOrEmpty(model.ConfirmNewPassword))
                        {
                            if (await _userManager.CheckPasswordAsync(user, model.Password))
                            {
                                var result = await _userManager.ChangePasswordAsync(user, model.Password, model.NewPassword);

                                if (result.Succeeded)
                                {
                                    message = "Mật khẩu đã được thay đổi";
                                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                                    return(RedirectToAction(actionName: "SignOut"));
                                }
                                foreach (var error in result.Errors)
                                {
                                    ModelState.AddModelError("", error.Description);
                                }
                                message = "Lỗi không xác định, xin mời thao tác lại";
                                TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                                return(View(model));
                            }
                            message = "Chỉ được thay đổi mật khẩu khi cung cấp mật khẩu cũ";
                            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                            return(RedirectToAction(actionName: "Profile"));
                        }
                    }
                    message = "Chỉ được chỉnh sửa thông tin của chính mình";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Profile"));
                }
                message = "Lỗi không xác định, xin mời thao tác lại";
                TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                return(RedirectToAction(actionName: "Profile"));
            }
            return(RedirectToAction(actionName: "Login"));
        }
        public async Task <IActionResult> Create(CreateFuelViewModel model)
        {
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                if (await _fuelServices.Create(model))
                {
                    message = "Nhiên liệu mới đã được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Create(CreateRouteViewModel model)
        {
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                if (model.ArrivalPlaceId == model.DeparturePlaceId)
                {
                    message = "Địa điểm xuất phát và địa điểm đến không được trùng nhau";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                if (model.Distance <= 0)
                {
                    message = "Chiều dài tuyến đường phải lớn hơn 0";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                if (_routeServices.IsRouteExists(departureId: model.DeparturePlaceId, arrivalId: model.ArrivalPlaceId))
                {
                    message = "Đã tồn tại tuyến đường vận chuyển này";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                var arrivalPlace          = _locationServices.GetLocation(model.ArrivalPlaceId);
                var departurePlace        = _locationServices.GetLocation(model.DeparturePlaceId);
                RouteInformation newRoute = new RouteInformation()
                {
                    RouteId          = Guid.NewGuid().ToString(),
                    ArrivalPlace     = arrivalPlace.LocationName,
                    DeparturePlace   = departurePlace.LocationName,
                    ArrivalPlaceId   = model.ArrivalPlaceId,
                    DeparturePlaceId = model.DeparturePlaceId,
                    Distance         = model.Distance
                };
                if (await _routeServices.CreateRoute(newRoute))
                {
                    message = "Tuyến vận chuyển được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Create()
        {
            var user = await _userManager.GetUserAsync(User);

            string message = String.Empty;

            if (user != null)
            {
                CreateUserViewModel model = new CreateUserViewModel()
                {
                    Roles = await _roleServices.GetRoles(user.Id)
                };
                return(View(model));
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> DeleteVehicleDB(int vehicleId)
        {
            string message    = String.Empty;
            var    vehicleDel = await _vehicleServices.GetVehicle(vehicleId);

            if (vehicleDel != null)
            {
                if (await _vehicleServices.DeleteVehicleDB(vehicleDel))
                {
                    message = $"Đã xóa phương tiện {vehicleDel.LicensePlate}";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Edit(EditVehicleViewModel model)
        {
            string message = String.Empty;

            model.VehicleBrands = _brandServices.GetAllBrands().ToList();
            model.Fuels         = _fuelServices.GetFuels().ToList();
            if (ModelState.IsValid)
            {
                if (await _vehicleServices.EditVehicle(model))
                {
                    message = "Thông tin phương tiện đã được điều chỉnh";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Delete(string routeId)
        {
            string message     = String.Empty;
            var    userMessage = new MessageVM();
            var    routeDel    = _routeServices.GetRoute(routeId);

            if (routeDel != null)
            {
                if (await _routeServices.DeleteRoute(routeDel))
                {
                    message = "Tuyến vận chuyển đã được xóa";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Edit(EditFuelViewModel model)
        {
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                var fuel = _fuelServices.GetFuel(model.FuelId);
                if (fuel != null)
                {
                    if (await _fuelServices.Edit(model))
                    {
                        message = "Thông tin nhiên liệu đã được chỉnh sửa";
                        TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                        return(RedirectToAction(actionName: "Index"));
                    }
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> DeleteRoleDB(string roleId)
        {
            string message = String.Empty;
            var    roleDel = await _roleManager.FindByIdAsync(roleId);

            if (roleDel != null)
            {
                var result = await _roleManager.DeleteAsync(roleDel);

                if (result.Succeeded)
                {
                    message = "Phân quyền đã được xóa";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(RedirectToAction(actionName: "Index"));
        }
        public async Task <IActionResult> Create(CreateUserViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            model.Roles = await _roleServices.GetRoles(user.Id);

            string message = String.Empty;

            if (ModelState.IsValid)
            {
                string folderPath = Path.Combine(_webEnv.WebRootPath, "images", "account");
                string fileName   = String.Empty;
                //Upload avatar if user using avatar
                if (model.Avatar != null)
                {
                    string[] typeAvatar = model.Avatar.ContentType.Split(@"\");
                    fileName = $"{Guid.NewGuid()}_{typeAvatar[typeAvatar.Length - 1]}";
                }
                else
                {
                    fileName = "noavatar.png";
                }
                string filePath = Path.Combine(folderPath, fileName);
                //Get RolePriority of new user
                var userRole = await _roleManager.FindByIdAsync(model.RoleId);

                //Create new user
                AppIdentityUser newUser = new AppIdentityUser()
                {
                    Id           = Guid.NewGuid().ToString(),
                    Email        = model.Email,
                    FirstName    = model.FirstName,
                    MiddleName   = model.MiddleName,
                    LastName     = model.LastName,
                    UserName     = model.UserName,
                    IsActive     = model.IsActive,
                    IsAvailable  = model.IsAvailable,
                    PhoneNumber  = model.PhoneNumber,
                    Avatar       = fileName,
                    RolePriority = userRole.RolePriority,
                    JobTitle     = userRole.Name
                };
                try
                {
                    var result = await _userManager.CreateAsync(newUser, model.Password);

                    if (result.Succeeded)
                    {
                        //upload avatar if new user has avatar
                        if (model.Avatar != null)
                        {
                            using (FileStream fs = new FileStream(filePath, FileMode.Create))
                            {
                                try
                                {
                                    await model.Avatar.CopyToAsync(fs);
                                }
                                catch (Exception)
                                {
                                    message = "Lỗi không xác định, xin mời thao tác lại";
                                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                                    return(View(model));
                                }
                            }
                        }
                        //Add role to new user
                        result = await _userManager.AddToRoleAsync(newUser, userRole.Name);

                        if (result.Succeeded)
                        {
                            message = "Tài khoản mới đã được tạo";
                            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                            return(RedirectToAction("Index"));
                        }
                    }
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
                catch (Exception)
                {
                    message = "Lỗi không xác định, xin mời thao tác lại";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }
        public async Task <IActionResult> Edit(EditUserViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            model.Roles = await _roleServices.GetRoles(user.Id);

            string message = String.Empty;

            if (ModelState.IsValid)
            {
                var userEdit = await _userManager.FindByIdAsync(model.UserId);

                if (userEdit == null)
                {
                    message = "Không tìm thấy tài khoản, xin mời thao tác lại";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                var roleUserEdit = await _userManager.GetRolesAsync(userEdit);

                var roleIdUserEdit = await _roleManager.FindByNameAsync(roleUserEdit[0]);

                userEdit.FirstName  = model.FirstName;
                userEdit.MiddleName = model.MiddleName;
                userEdit.LastName   = model.LastName;
                userEdit.Email      = model.Email;
                if (model.Avatar != null)
                {
                    string folderPath = Path.Combine(_webEnv.WebRootPath, "images", "account");
                    string filePath   = String.Empty;
                    if (model.ExistsAvatar != "noavatar.png")
                    {
                        filePath = Path.Combine(folderPath, model.ExistsAvatar);
                        System.IO.File.Delete(filePath);
                    }
                    string[] typeAvatar = model.Avatar.ContentType.Split(@"/");
                    var      fileName   = $"{Guid.NewGuid()}.{typeAvatar[typeAvatar.Length - 1]}";
                    filePath = Path.Combine(folderPath, fileName);
                    using (FileStream fs = new FileStream(filePath, FileMode.Create))
                    {
                        try
                        {
                            await model.Avatar.CopyToAsync(fs);

                            userEdit.Avatar = fileName;
                        }
                        catch (Exception)
                        {
                            message = "Lỗi không xác định, xin mời thao tác lại";
                            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                            return(View(model));
                        }
                    }
                }
                if (roleIdUserEdit.Id != model.RoleId)
                {
                    var roleEdit = await _roleManager.FindByIdAsync(model.RoleId);

                    if (roleEdit != null)
                    {
                        var result = await _userManager.AddToRoleAsync(userEdit, roleEdit.Name);

                        if (result.Succeeded)
                        {
                            result = await _userManager.RemoveFromRoleAsync(userEdit, roleIdUserEdit.Name);
                        }
                    }
                }
                userEdit.PhoneNumber = model.PhoneNumber;
                if (!String.IsNullOrEmpty(model.Password))
                {
                    var token = await _userManager.GeneratePasswordResetTokenAsync(userEdit);

                    var result = await _userManager.ResetPasswordAsync(userEdit, token, model.Password);

                    if (!result.Succeeded)
                    {
                        message = "Lỗi không xác định, xin mời thao tác lại";
                        TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                        return(View(model));
                    }
                }
                var resultEditUser = await _userManager.UpdateAsync(userEdit);

                if (resultEditUser.Succeeded)
                {
                    message = "Thông tin tài khoản đã được đổi thành công";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Index"));
                }
                foreach (var error in resultEditUser.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }
        public async Task <IActionResult> Create(CreateTransInfoViewModel model)
        {
            //get local time at Timezone UTC 7
            DateTime localTimeUTC7 = SystemUtilites.ConvertToTimeZone(DateTime.UtcNow, "SE Asia Standard Time");
            //get timestamp of day at 0 AM
            double TStodayUTC7At0Am = SystemUtilites.ConvertToTimeStamp(localTimeUTC7.Date);
            //get timestamp now at utc
            double TSUTCNow = SystemUtilites.ConvertToTimeStamp(DateTime.UtcNow);

            //get data for select elements
            model.Drivers  = _userServices.GetDriverUsers().ToList();
            model.Routes   = _routeServices.GetAllRoutes().ToList();
            model.Vehicles = (await _vehicleServices.GetAllNotDeletedAndAvailableVehicles()).ToList();
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                //check the vehicle is used
                string driverIdUseVehicle = await _vehicleServices.IsVehicleInUsedByAnotherDriver(model.DriverId, model.VehicleId);

                if (!String.IsNullOrEmpty(driverIdUseVehicle))
                {
                    var driverUseVehicle = _userServices.GetUser(driverIdUseVehicle);
                    message = $"Xe đang được sử dụng bởi {driverUseVehicle.FullName}";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }

                //create new TransportInformation
                var user = await _userManager.GetUserAsync(User);

                TransportInformation newTrans = new TransportInformation()
                {
                    TransportId    = Guid.NewGuid().ToString(),
                    AdvanceMoney   = model.AdvanceMoney,
                    DateStartUTC   = TSUTCNow,
                    DateStartLocal = SystemUtilites.ConvertToTimeStamp(localTimeUTC7),
                    TimeZone       = "SE Asia Standard Time",
                    CargoTypes     = model.CargoTypes,
                    Note           = model.Note,
                    VehicleId      = model.VehicleId,
                    RouteId        = model.RouteId,
                    UserCreateId   = user.Id
                };
                //get or create if not dayjob has date match today timeStamp
                DayJob driverDayJob = _dayJobServices.GetDayJob(model.DriverId, TStodayUTC7At0Am);
                if (driverDayJob == null)
                {
                    driverDayJob = new DayJob()
                    {
                        DayJobId = Guid.NewGuid().ToString(),
                        DriverId = model.DriverId,
                        Date     = TStodayUTC7At0Am
                    };
                    if (!(await _dayJobServices.Create(driverDayJob)))
                    {
                        message = "Lỗi không xác định, xin mời thao tác lại";
                        TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                        return(View(model));
                    }
                }
                newTrans.DayJobId = driverDayJob.DayJobId;
                //create new TransInfo in SQL
                if (await _transInfoServices.CreateNewTransInfo(newTrans))
                {
                    var vehicle = await _vehicleServices.GetVehicle(newTrans.VehicleId);

                    if (vehicle != null)
                    {
                        if (!vehicle.IsInUse)
                        {
                            await _vehicleServices.MakeVehicleInUsed(vehicle);
                        }
                    }
                    var driver = await _userManager.FindByIdAsync(model.DriverId);

                    if (driver != null)
                    {
                        if (driver.IsAvailable)
                        {
                            await _userServices.MakeDriverIsBusy(driver);
                        }
                    }
                    message = "Chuyến vận chuyển đã được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Manage"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }