private bool WeightIsValid(decimal grossWeightIn, decimal grossWeightOut, uint currentYear, ushort currentQuarter, string port, string senderId) { if (CorrectWeight) { return(true); } var(previousYearGWTInward, thresoldInward, invalidWeightIn) = _msd2DataService.ValidateGrossWeightInwards(grossWeightIn, currentYear, currentQuarter, port, senderId); if (invalidWeightIn) { ModelState.AddModelError("GrossWeightInward", $"The previously entered weight for this quater last year was {previousYearGWTInward}. The amount you entered is outside the threshold of: {thresoldInward}. If this is correct, check the box at the bottom of the page."); showCheckbox = true; } var(previousYearGWTOutward, thresoldOutward, invalidWeightOut) = _msd2DataService.ValidateGrossWeightOutwards(grossWeightOut, currentYear, currentQuarter, port, senderId); if (invalidWeightOut) { ModelState.AddModelError("GrossWeightOutward", $"The previously entered weight for this quater last year was {previousYearGWTOutward}. The amount you entered is outside the threshold of: {thresoldOutward}. If this is correct, check the box at the bottom of the page"); showCheckbox = true; } return(!invalidWeightIn && !invalidWeightOut); }
public async Task <IActionResult> OnPostAsync() { LoggedInUser = await _userManager.GetUserAsync(HttpContext.User); MSD23 = CustomExtensions.NotNullOrEmpty(TempData) && TempData[MSD23Key] != null ? new MSD23(TempData.GetKeep <MSD23>(MSD23Key)) : new MSD23(); if (Input.PassengerVehiclesInwards > Input.UnitsInwards) { ModelState.AddModelError("Input.PassengerVehiclesInwards", $"Passenger Vehicle Inwards cannot be greater than total number of units inwards"); } if (Input.PassengerVehiclesOutwards > Input.UnitsOutwards) { ModelState.AddModelError("Input.PassengerVehiclesOutwards", $"Passenger Vehicle Outwards cannot be greater than total number of units outwards"); } if (Input.GrossWeightInwards != null) { var(previousYearGWTInward, thresoldInward, boxVisibleInward) = _msd2DataService.ValidateGrossWeightInwards((decimal)Input.GrossWeightInwards, MSD23.Year, MSD23.Quarter, MSD23.Port, LoggedInUser.SenderId); PreviousYearGWTInward = previousYearGWTInward; if (boxVisibleInward) { IsInwardBoxVisible = boxVisibleInward; if (Input.GrossWeightInwardsDescription == null) { ModelState.AddModelError("Input_GrossWeightInwardsDescription", $"Check gross weight (inwards) and enter description if correct"); } } else { ModelState.Remove("Input_InwardsUnitDescription"); Input.GrossWeightInwardsDescription = null; } } if (Input.GrossWeightOutwards != null) { var(previousYearGWTOutward, thresoldOutward, boxVisibleOutward) = _msd2DataService.ValidateGrossWeightOutwards((decimal)Input.GrossWeightOutwards, MSD23.Year, MSD23.Quarter, MSD23.Port, LoggedInUser.SenderId); PreviousYearGWTOutward = previousYearGWTOutward; if (boxVisibleOutward) { IsOutwardBoxVisible = boxVisibleOutward; if (Input.GrossWeightOutwardsDescription == null) { ModelState.AddModelError("Input_GrossWeightOutwardsDescription", $"Check gross weight (outwards) and enter description if correct"); } } else { Input.GrossWeightOutwardsDescription = null; } } if (Input.UnitsInwards != null) { var(previousYearUnitsInward, thresoldInward, boxVisibleUintInward) = _msd2DataService.ValidateUnitsInwards(Input.UnitsInwards, MSD23.Year, MSD23.Quarter, MSD23.Port, LoggedInUser.SenderId); PreviousYearUintInward = previousYearUnitsInward; if (boxVisibleUintInward) { IsInwardUnitBoxVisible = boxVisibleUintInward; if (Input.InwardsUnitDescription == null) { ModelState.AddModelError("Input_InwardsUnitDescription", $"Check units (inwards) and enter description if correct"); } } else { Input.InwardsUnitDescription = null; } } if (Input.UnitsOutwards != null) { var(previousYearUnitsOutward, thresoldOutward, boxVisibleUintOutward) = _msd2DataService.ValidateUnitsOutwards(Input.UnitsOutwards, MSD23.Year, MSD23.Quarter, MSD23.Port, LoggedInUser.SenderId); PreviousYearUintOutward = previousYearUnitsOutward; if (boxVisibleUintOutward) { IsOutwardUnitBoxVisible = boxVisibleUintOutward; if (Input.OutwardsUnitDescription == null) { ModelState.AddModelError("Input_OutwardsUnitDescription", $"Check units (outwards) and enter description if correct"); } } else { Input.OutwardsUnitDescription = null; } } if (!ModelState.IsValid) { InitialisePage(LoggedInUser); return(Page()); } var MSD2Data = new Msd2 { SenderId = LoggedInUser.SenderId, ReportingPort = MSD23.Port, Year = MSD23.Year, Quarter = MSD23.Quarter, GrossWeightInward = (decimal)Input.GrossWeightInwards, InwardGrossWeightDescription = Input.GrossWeightInwardsDescription, TotalUnitsInward = (uint)Input.UnitsInwards, InwardUnitDescription = Input.InwardsUnitDescription, PassengerVehiclesInward = Input.UnitsInwards == 0 ? 0 : Input.PassengerVehiclesInwards ?? 0, GrossWeightOutward = (decimal)Input.GrossWeightOutwards, OutwardGrossWeightDescription = Input.GrossWeightOutwardsDescription, TotalUnitsOutward = (uint)Input.UnitsOutwards, OutwardUnitDescription = Input.OutwardsUnitDescription, PassengerVehiclesOutward = Input.UnitsOutwards == 0 ? 0 : Input.PassengerVehiclesOutwards ?? 0, DataSourceId = (uint)DataSource.WEB, CreatedDate = DateTime.SpecifyKind(currentDateTime, DateTimeKind.Utc), ModifiedDate = DateTime.SpecifyKind(currentDateTime, DateTimeKind.Utc), CreatedBy = LoggedInUser.Email.ToString(), LastUpdatedBy = LoggedInUser.Email.ToString() }; var existingParent = FindSubmission(); if (existingParent != null) { MSD2Data.Id = existingParent.Id; MSD2Data.ReportingPort = existingParent.ReportingPort; MSD2Data.Year = existingParent.Year; MSD2Data.Quarter = existingParent.Quarter; MSD2Data.DataSourceId = existingParent.DataSourceId; MSD2Data.CreatedDate = existingParent.CreatedDate; MSD2Data.CreatedBy = existingParent.CreatedBy; _context.Entry(existingParent).CurrentValues.SetValues(MSD2Data); } else { await _context.AddAsync(MSD2Data); } await _context.SaveChangesAsync(); return(RedirectToPage("./Success")); }