diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index 1bbff24..735137b 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -388,6 +388,7 @@ namespace CarCareTracker.Controllers Tags = string.IsNullOrWhiteSpace(input.Tags) ? new List() : input.Tags.Split(' ').Distinct().ToList() }; _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord); + _vehicleLogic.UpdateRecurringTaxes(vehicleId); StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"Added Tax Record via API - Description: {taxRecord.Description}"); return Json(OperationResponse.Succeed("Tax Record Added")); } diff --git a/Controllers/Vehicle/TaxController.cs b/Controllers/Vehicle/TaxController.cs index d4dcbd1..08584d7 100644 --- a/Controllers/Vehicle/TaxController.cs +++ b/Controllers/Vehicle/TaxController.cs @@ -23,44 +23,18 @@ namespace CarCareTracker.Controllers } return PartialView("_TaxRecords", result); } - private void UpdateRecurringTaxes(int vehicleId) + + [HttpPost] + public IActionResult CheckRecurringTaxRecords(int vehicleId) { - var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId); - var recurringFees = result.Where(x => x.IsRecurring); - if (recurringFees.Any()) + try { - foreach (TaxRecord recurringFee in recurringFees) - { - var newDate = new DateTime(); - if (recurringFee.RecurringInterval != ReminderMonthInterval.Other) - { - newDate = recurringFee.Date.AddMonths((int)recurringFee.RecurringInterval); - } - else - { - newDate = recurringFee.Date.AddMonths(recurringFee.CustomMonthInterval); - } - if (DateTime.Now > newDate) - { - recurringFee.IsRecurring = false; - var newRecurringFee = new TaxRecord() - { - VehicleId = recurringFee.VehicleId, - Date = newDate, - Description = recurringFee.Description, - Cost = recurringFee.Cost, - IsRecurring = true, - Notes = recurringFee.Notes, - RecurringInterval = recurringFee.RecurringInterval, - CustomMonthInterval = recurringFee.CustomMonthInterval, - Files = recurringFee.Files, - Tags = recurringFee.Tags, - ExtraFields = recurringFee.ExtraFields - }; - _taxRecordDataAccess.SaveTaxRecordToVehicle(recurringFee); - _taxRecordDataAccess.SaveTaxRecordToVehicle(newRecurringFee); - } - } + _vehicleLogic.UpdateRecurringTaxes(vehicleId); + return Json(true); + } catch (Exception ex) + { + _logger.LogError(ex.Message); + return Json(false); } } [HttpPost] @@ -77,6 +51,7 @@ namespace CarCareTracker.Controllers } } var result = _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord.ToTaxRecord()); + _vehicleLogic.UpdateRecurringTaxes(taxRecord.VehicleId); if (result) { StaticHelper.NotifyAsync(_config.GetWebHookUrl(), taxRecord.VehicleId, User.Identity.Name, $"{(taxRecord.Id == default ? "Created" : "Edited")} Tax Record - Description: {taxRecord.Description}"); diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 0230b53..11eb075 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -95,7 +95,6 @@ namespace CarCareTracker.Controllers public IActionResult Index(int vehicleId) { var data = _dataAccess.GetVehicleById(vehicleId); - UpdateRecurringTaxes(vehicleId); return View(data); } [HttpGet] diff --git a/Logic/VehicleLogic.cs b/Logic/VehicleLogic.cs index 3e6ad1c..6900240 100644 --- a/Logic/VehicleLogic.cs +++ b/Logic/VehicleLogic.cs @@ -17,6 +17,7 @@ namespace CarCareTracker.Logic List GetVehicleInfo(List vehicles); List GetReminders(List vehicles, bool isCalendar); List GetPlans(List vehicles, bool excludeDone); + void UpdateRecurringTaxes(int vehicleId); } public class VehicleLogic: IVehicleLogic { @@ -29,6 +30,8 @@ namespace CarCareTracker.Logic private readonly IReminderRecordDataAccess _reminderRecordDataAccess; private readonly IPlanRecordDataAccess _planRecordDataAccess; private readonly IReminderHelper _reminderHelper; + private readonly IVehicleDataAccess _dataAccess; + public VehicleLogic( IServiceRecordDataAccess serviceRecordDataAccess, IGasRecordDataAccess gasRecordDataAccess, @@ -38,7 +41,8 @@ namespace CarCareTracker.Logic IOdometerRecordDataAccess odometerRecordDataAccess, IReminderRecordDataAccess reminderRecordDataAccess, IPlanRecordDataAccess planRecordDataAccess, - IReminderHelper reminderHelper + IReminderHelper reminderHelper, + IVehicleDataAccess dataAccess ) { _serviceRecordDataAccess = serviceRecordDataAccess; _gasRecordDataAccess = gasRecordDataAccess; @@ -49,6 +53,7 @@ namespace CarCareTracker.Logic _planRecordDataAccess = planRecordDataAccess; _reminderRecordDataAccess = reminderRecordDataAccess; _reminderHelper = reminderHelper; + _dataAccess = dataAccess; } public VehicleRecords GetVehicleRecords(int vehicleId) { @@ -317,5 +322,45 @@ namespace CarCareTracker.Logic } return plans.OrderBy(x => x.Priority).ThenBy(x=>x.Progress).ToList(); } + public void UpdateRecurringTaxes(int vehicleId) + { + var vehicleData = _dataAccess.GetVehicleById(vehicleId); + if (!string.IsNullOrWhiteSpace(vehicleData.SoldDate)) + { + return; + } + bool RecurringTaxIsOutdated(TaxRecord taxRecord) + { + var monthInterval = taxRecord.RecurringInterval != ReminderMonthInterval.Other ? (int)taxRecord.RecurringInterval : taxRecord.CustomMonthInterval; + return DateTime.Now > taxRecord.Date.AddMonths(monthInterval); + } + var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId); + var outdatedRecurringFees = result.Where(x => x.IsRecurring && RecurringTaxIsOutdated(x)); + if (outdatedRecurringFees.Any()) + { + foreach (TaxRecord recurringFee in outdatedRecurringFees) + { + var monthInterval = recurringFee.RecurringInterval != ReminderMonthInterval.Other ? (int)recurringFee.RecurringInterval : recurringFee.CustomMonthInterval; + bool isOutdated = true; + //update the original outdated tax record + recurringFee.IsRecurring = false; + _taxRecordDataAccess.SaveTaxRecordToVehicle(recurringFee); + //month multiplier for severely outdated monthly tax records. + int monthMultiplier = 1; + var originalDate = recurringFee.Date; + while (isOutdated) + { + var nextDate = originalDate.AddMonths(monthInterval * monthMultiplier); + monthMultiplier++; + var nextnextDate = originalDate.AddMonths(monthInterval * monthMultiplier); + recurringFee.Date = nextDate; + recurringFee.Id = default; //new record + recurringFee.IsRecurring = DateTime.Now <= nextnextDate; + _taxRecordDataAccess.SaveTaxRecordToVehicle(recurringFee); + isOutdated = !recurringFee.IsRecurring; + } + } + } + } } } diff --git a/Views/Vehicle/Index.cshtml b/Views/Vehicle/Index.cshtml index 716c078..bdb0392 100644 --- a/Views/Vehicle/Index.cshtml +++ b/Views/Vehicle/Index.cshtml @@ -173,4 +173,5 @@ return { tab: "@userConfig.DefaultTab" }; } bindWindowResize(); + checkRecurringTaxes(); \ No newline at end of file diff --git a/wwwroot/js/taxrecord.js b/wwwroot/js/taxrecord.js index 257a95a..abccd99 100644 --- a/wwwroot/js/taxrecord.js +++ b/wwwroot/js/taxrecord.js @@ -178,4 +178,12 @@ function getAndValidateTaxRecordValues() { extraFields: extraFields.extraFields, reminderRecordId: recurringReminderRecordId } +} + +function checkRecurringTaxes() { + $.post('/Vehicle/CheckRecurringTaxRecords', { vehicleId: GetVehicleId().vehicleId },function (data) { + if (!data) { + errorToast(genericErrorMessage()); + } + }) } \ No newline at end of file