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(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));
        }
예제 #3
0
        public async Task <bool> EditTransInfo(EditTransInfoViewModel transEdit, string userId)
        {
            var transInfo = await GetTransport(transEdit.TransportId);

            if (transInfo != null)
            {
                string editContent = String.Empty;
                _context.TransportInformations.Attach(transInfo);
                if (transInfo.AdvanceMoney != transEdit.AdvanceMoney)
                {
                    editContent           += $" Sửa tiền tạm ứng từ \"{transInfo.AdvanceMoney}\" thành \"{transEdit.AdvanceMoney}\" |";
                    transInfo.AdvanceMoney = transEdit.AdvanceMoney;
                }
                if (transInfo.CargoTonnage != transEdit.CargoTonnage)
                {
                    editContent           += $" Sửa khối lượng hàng hóa từ \"{transInfo.CargoTonnage}\" thành \"{transEdit.CargoTonnage}\" |";
                    transInfo.CargoTonnage = transEdit.CargoTonnage;
                }
                if (transInfo.CargoTypes != transEdit.CargoTypes)
                {
                    editContent         += $" Sửa loại hàng hóa từ \"{transInfo.CargoTypes}\" thành \"{transEdit.CargoTypes}\" |";
                    transInfo.CargoTypes = transEdit.CargoTypes;
                }
                if (transInfo.IsCancel != transEdit.IsCancel)
                {
                    editContent       += $" Sửa trạng thái hủy từ \"{transInfo.IsCancel}\" thành \"{transEdit.IsCancel}\" |";
                    transInfo.IsCancel = transEdit.IsCancel;
                }
                if (transInfo.Note != transEdit.Note)
                {
                    editContent   += $" Sửa ghi chú từ \"{transInfo.Note}\" thành \"{transEdit.Note}\" |";
                    transInfo.Note = transEdit.Note;
                }
                if (transInfo.ReasonCancel != transEdit.ReasonCancel)
                {
                    editContent           += $" Sửa lý do hủy từ \"{transInfo.ReasonCancel}\" thành \"{transEdit.ReasonCancel}\" |";
                    transInfo.ReasonCancel = transEdit.ReasonCancel;
                }
                if (transInfo.ReturnOfAdvances != transEdit.ReturnOfAdvances)
                {
                    editContent += $" Sửa tiền hoàn ứng từ \"{transInfo.ReturnOfAdvances}\" thành \"{transEdit.ReturnOfAdvances}\" |";
                    transInfo.ReturnOfAdvances = transEdit.ReturnOfAdvances;
                }
                if (transInfo.DateCompletedUTC != transEdit.DateCompletedUTC)
                {
                    transInfo.DateCompletedUTC = transEdit.DateCompletedUTC;
                }
                if (transInfo.DateCompletedLocal != transEdit.DateCompletedLocal)
                {
                    editContent += $"Kết thúc chuyến vận chuyển";
                    transInfo.DateCompletedLocal = transEdit.DateCompletedLocal;
                }
                try
                {
                    if (!String.IsNullOrEmpty(editContent))
                    {
                        DateTime localTimeUTC7           = SystemUtilites.ConvertToTimeZone(DateTime.UtcNow, "SE Asia Standard Time");
                        EditTransportInformation newEdit = new EditTransportInformation()
                        {
                            DateEditLocal = SystemUtilites.ConvertToTimeStamp(localTimeUTC7),
                            DateEditUTC   = SystemUtilites.ConvertToTimeStamp(DateTime.UtcNow),
                            EditContent   = editContent,
                            EditId        = Guid.NewGuid().ToString(),
                            TimeZone      = "SE Asia Standard Time",
                            TransportId   = transEdit.TransportId,
                            UserEditId    = userId
                        };
                        _context.EditTransportInformations.Add(newEdit);
                    }
                    var result = await _context.SaveChangesAsync();

                    return(result > 0);
                }
                catch (Exception)
                {
                    return(false);
                }
            }
            return(false);
        }