Create past records for outdated tax records.

This commit is contained in:
DESKTOP-T0O5CDB\DESK-555BD
2024-11-29 13:37:37 -07:00
parent bd7cbffe10
commit 9421cb57ca
6 changed files with 67 additions and 38 deletions

View File

@@ -388,6 +388,7 @@ namespace CarCareTracker.Controllers
Tags = string.IsNullOrWhiteSpace(input.Tags) ? new List<string>() : input.Tags.Split(' ').Distinct().ToList() Tags = string.IsNullOrWhiteSpace(input.Tags) ? new List<string>() : input.Tags.Split(' ').Distinct().ToList()
}; };
_taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord); _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord);
_vehicleLogic.UpdateRecurringTaxes(vehicleId);
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"Added Tax Record via API - Description: {taxRecord.Description}"); StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"Added Tax Record via API - Description: {taxRecord.Description}");
return Json(OperationResponse.Succeed("Tax Record Added")); return Json(OperationResponse.Succeed("Tax Record Added"));
} }

View File

@@ -23,44 +23,18 @@ namespace CarCareTracker.Controllers
} }
return PartialView("_TaxRecords", result); return PartialView("_TaxRecords", result);
} }
private void UpdateRecurringTaxes(int vehicleId)
[HttpPost]
public IActionResult CheckRecurringTaxRecords(int vehicleId)
{ {
var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId); try
var recurringFees = result.Where(x => x.IsRecurring);
if (recurringFees.Any())
{ {
foreach (TaxRecord recurringFee in recurringFees) _vehicleLogic.UpdateRecurringTaxes(vehicleId);
{ return Json(true);
var newDate = new DateTime(); } catch (Exception ex)
if (recurringFee.RecurringInterval != ReminderMonthInterval.Other) {
{ _logger.LogError(ex.Message);
newDate = recurringFee.Date.AddMonths((int)recurringFee.RecurringInterval); return Json(false);
}
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);
}
}
} }
} }
[HttpPost] [HttpPost]
@@ -77,6 +51,7 @@ namespace CarCareTracker.Controllers
} }
} }
var result = _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord.ToTaxRecord()); var result = _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord.ToTaxRecord());
_vehicleLogic.UpdateRecurringTaxes(taxRecord.VehicleId);
if (result) if (result)
{ {
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), taxRecord.VehicleId, User.Identity.Name, $"{(taxRecord.Id == default ? "Created" : "Edited")} Tax Record - Description: {taxRecord.Description}"); StaticHelper.NotifyAsync(_config.GetWebHookUrl(), taxRecord.VehicleId, User.Identity.Name, $"{(taxRecord.Id == default ? "Created" : "Edited")} Tax Record - Description: {taxRecord.Description}");

View File

@@ -95,7 +95,6 @@ namespace CarCareTracker.Controllers
public IActionResult Index(int vehicleId) public IActionResult Index(int vehicleId)
{ {
var data = _dataAccess.GetVehicleById(vehicleId); var data = _dataAccess.GetVehicleById(vehicleId);
UpdateRecurringTaxes(vehicleId);
return View(data); return View(data);
} }
[HttpGet] [HttpGet]

View File

@@ -17,6 +17,7 @@ namespace CarCareTracker.Logic
List<VehicleInfo> GetVehicleInfo(List<Vehicle> vehicles); List<VehicleInfo> GetVehicleInfo(List<Vehicle> vehicles);
List<ReminderRecordViewModel> GetReminders(List<Vehicle> vehicles, bool isCalendar); List<ReminderRecordViewModel> GetReminders(List<Vehicle> vehicles, bool isCalendar);
List<PlanRecord> GetPlans(List<Vehicle> vehicles, bool excludeDone); List<PlanRecord> GetPlans(List<Vehicle> vehicles, bool excludeDone);
void UpdateRecurringTaxes(int vehicleId);
} }
public class VehicleLogic: IVehicleLogic public class VehicleLogic: IVehicleLogic
{ {
@@ -29,6 +30,8 @@ namespace CarCareTracker.Logic
private readonly IReminderRecordDataAccess _reminderRecordDataAccess; private readonly IReminderRecordDataAccess _reminderRecordDataAccess;
private readonly IPlanRecordDataAccess _planRecordDataAccess; private readonly IPlanRecordDataAccess _planRecordDataAccess;
private readonly IReminderHelper _reminderHelper; private readonly IReminderHelper _reminderHelper;
private readonly IVehicleDataAccess _dataAccess;
public VehicleLogic( public VehicleLogic(
IServiceRecordDataAccess serviceRecordDataAccess, IServiceRecordDataAccess serviceRecordDataAccess,
IGasRecordDataAccess gasRecordDataAccess, IGasRecordDataAccess gasRecordDataAccess,
@@ -38,7 +41,8 @@ namespace CarCareTracker.Logic
IOdometerRecordDataAccess odometerRecordDataAccess, IOdometerRecordDataAccess odometerRecordDataAccess,
IReminderRecordDataAccess reminderRecordDataAccess, IReminderRecordDataAccess reminderRecordDataAccess,
IPlanRecordDataAccess planRecordDataAccess, IPlanRecordDataAccess planRecordDataAccess,
IReminderHelper reminderHelper IReminderHelper reminderHelper,
IVehicleDataAccess dataAccess
) { ) {
_serviceRecordDataAccess = serviceRecordDataAccess; _serviceRecordDataAccess = serviceRecordDataAccess;
_gasRecordDataAccess = gasRecordDataAccess; _gasRecordDataAccess = gasRecordDataAccess;
@@ -49,6 +53,7 @@ namespace CarCareTracker.Logic
_planRecordDataAccess = planRecordDataAccess; _planRecordDataAccess = planRecordDataAccess;
_reminderRecordDataAccess = reminderRecordDataAccess; _reminderRecordDataAccess = reminderRecordDataAccess;
_reminderHelper = reminderHelper; _reminderHelper = reminderHelper;
_dataAccess = dataAccess;
} }
public VehicleRecords GetVehicleRecords(int vehicleId) public VehicleRecords GetVehicleRecords(int vehicleId)
{ {
@@ -317,5 +322,45 @@ namespace CarCareTracker.Logic
} }
return plans.OrderBy(x => x.Priority).ThenBy(x=>x.Progress).ToList(); 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;
}
}
}
}
} }
} }

View File

@@ -173,4 +173,5 @@
return { tab: "@userConfig.DefaultTab" }; return { tab: "@userConfig.DefaultTab" };
} }
bindWindowResize(); bindWindowResize();
checkRecurringTaxes();
</script> </script>

View File

@@ -178,4 +178,12 @@ function getAndValidateTaxRecordValues() {
extraFields: extraFields.extraFields, extraFields: extraFields.extraFields,
reminderRecordId: recurringReminderRecordId reminderRecordId: recurringReminderRecordId
} }
}
function checkRecurringTaxes() {
$.post('/Vehicle/CheckRecurringTaxRecords', { vehicleId: GetVehicleId().vehicleId },function (data) {
if (!data) {
errorToast(genericErrorMessage());
}
})
} }