public async Task <IActionResult> TransFinish(FilterTransPortModel filter)
        {
            var user = await _userManager.GetUserAsync(User);

            //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 TSNowUTC7 = SystemUtilites.ConvertToTimeStamp(localTimeUTC7);
            //get timestamp 0 AM 1st day of month
            DateTime startMonth  = new DateTime(localTimeUTC7.Year, localTimeUTC7.Month, 01);
            double   TSMonthUTC7 = SystemUtilites.ConvertToTimeStamp(startMonth);
            double   TSstartDate = 0;
            double   TSendDate   = 0;
            PaginationViewModel <TransInfoViewModel> model = new PaginationViewModel <TransInfoViewModel>();

            if (SystemUtilites.ConvertToTimeStamp(filter.StartDate) == -62135596800)
            {
                TSstartDate = TSMonthUTC7;
            }
            else
            {
                TSstartDate = SystemUtilites.ConvertToTimeStamp(filter.StartDate);
            }
            if (SystemUtilites.ConvertToTimeStamp(filter.EndDate) == -62135596800)
            {
                TSendDate = TSNowUTC7;
            }
            else
            {
                TSendDate = SystemUtilites.ConvertToTimeStamp(filter.EndDate.AddDays(1));
            }
            if (filter.Page == 0)
            {
                filter.Page = 1;
            }
            if (filter.PageSize == 0)
            {
                filter.PageSize = model.PageSizeItem.Min();
            }
            if (String.IsNullOrEmpty(filter.Search))
            {
                filter.Search = "";
            }

            model.Items = await _transInfoServices.GetTransportsDoneByDriver(TSstartDate, TSendDate, user.Id);

            int countItems = 0;

            if (model.Items != null)
            {
                if (model.Items.Any())
                {
                    countItems = model.Items.Count();
                }
            }
            model.Search    = filter.Search;
            model.StartDate = filter.StartDate;
            model.EndDate   = filter.EndDate;
            model.Items     = model.Items.Skip((filter.Page - 1) * filter.PageSize).Take(filter.PageSize);
            model.Pager     = new Pager(countItems, filter.Page, filter.PageSize);
            ViewBag.Search  = filter.Search;
            return(View(model));
        }
예제 #2
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);
        }
        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));
        }