Skip to content

Commit

Permalink
Merge pull request #54 from hargata/Hargata/consolidated.report
Browse files Browse the repository at this point in the history
Hargata/consolidated.report
  • Loading branch information
hargata authored Jan 10, 2024
2 parents 9837172 + e3bf6f0 commit ed10666
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Controllers/APIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public IActionResult Reminders(int vehicleId)
{
var currentMileage = GetMaxMileage(vehicleId);
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes});
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes});
return Json(results);
}
private int GetMaxMileage(int vehicleId)
Expand Down
126 changes: 112 additions & 14 deletions Controllers/VehicleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using CsvHelper;
using System.Globalization;
using Microsoft.AspNetCore.Authorization;
using CarCareTracker.External.Implementations;
using CarCareTracker.MapProfile;

namespace CarCareTracker.Controllers
Expand All @@ -29,11 +28,13 @@ public class VehicleController : Controller
private readonly IFileHelper _fileHelper;
private readonly IGasHelper _gasHelper;
private readonly IReminderHelper _reminderHelper;
private readonly IReportHelper _reportHelper;

public VehicleController(ILogger<VehicleController> logger,
IFileHelper fileHelper,
IGasHelper gasHelper,
IReminderHelper reminderHelper,
IReportHelper reportHelper,
IVehicleDataAccess dataAccess,
INoteDataAccess noteDataAccess,
IServiceRecordDataAccess serviceRecordDataAccess,
Expand All @@ -51,6 +52,7 @@ public VehicleController(ILogger<VehicleController> logger,
_fileHelper = fileHelper;
_gasHelper = gasHelper;
_reminderHelper = reminderHelper;
_reportHelper = reportHelper;
_serviceRecordDataAccess = serviceRecordDataAccess;
_gasRecordDataAccess = gasRecordDataAccess;
_collisionRecordDataAccess = collisionRecordDataAccess;
Expand Down Expand Up @@ -571,9 +573,71 @@ public IActionResult DeleteTaxRecordById(int taxRecordId)
#endregion
#region "Reports"
[HttpGet]
public IActionResult GetReportPartialView()
public IActionResult GetReportPartialView(int vehicleId)
{
return PartialView("_Report");
//get records
var serviceRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId);
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
var collisionRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId);
var taxRecords = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
var upgradeRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId);
var viewModel = new ReportViewModel();
//get totalCostMakeUp
viewModel.CostMakeUpForVehicle = new CostMakeUpForVehicle
{
ServiceRecordSum = serviceRecords.Sum(x => x.Cost),
GasRecordSum = gasRecords.Sum(x => x.Cost),
CollisionRecordSum = collisionRecords.Sum(x => x.Cost),
TaxRecordSum = taxRecords.Sum(x => x.Cost),
UpgradeRecordSum = upgradeRecords.Sum(x => x.Cost)
};
//get costbymonth
List<CostForVehicleByMonth> allCosts = new List<CostForVehicleByMonth>();
allCosts.AddRange(_reportHelper.GetServiceRecordSum(serviceRecords, 0));
allCosts.AddRange(_reportHelper.GetRepairRecordSum(collisionRecords, 0));
allCosts.AddRange(_reportHelper.GetUpgradeRecordSum(upgradeRecords, 0));
allCosts.AddRange(_reportHelper.GetUpgradeRecordSum(upgradeRecords, 0));
allCosts.AddRange(_reportHelper.GetGasRecordSum(gasRecords, 0));
allCosts.AddRange(_reportHelper.GetTaxRecordSum(taxRecords, 0));
viewModel.CostForVehicleByMonth = allCosts.GroupBy(x => x.MonthName).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = x.Key,
Cost = x.Sum(y => y.Cost)
}).ToList();
//get reminders
var reminders = GetRemindersAndUrgency(vehicleId, DateTime.Now);
viewModel.ReminderMakeUpForVehicle = new ReminderMakeUpForVehicle
{
NotUrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.NotUrgent).Count(),
UrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.Urgent).Count(),
VeryUrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.VeryUrgent).Count(),
PastDueCount = reminders.Where(x => x.Urgency == ReminderUrgency.PastDue).Count()
};
//populate year dropdown.
var numbersArray = new List<int>();
if (serviceRecords.Any())
{
numbersArray.Add(serviceRecords.Min(x => x.Date.Year));
}
if (collisionRecords.Any())
{
numbersArray.Add(collisionRecords.Min(x => x.Date.Year));
}
if (gasRecords.Any())
{
numbersArray.Add(gasRecords.Min(x => x.Date.Year));
}
if (upgradeRecords.Any())
{
numbersArray.Add(upgradeRecords.Min(x => x.Date.Year));
}
var minYear = numbersArray.Any() ? numbersArray.Min() : DateTime.Now.AddYears(-5).Year;
var yearDifference = DateTime.Now.Year - minYear + 1;
for(int i = 0; i < yearDifference; i++)
{
viewModel.Years.Add(DateTime.Now.AddYears(i * -1).Year);
}
return PartialView("_Report", viewModel);
}
[HttpGet]
public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0)
Expand Down Expand Up @@ -601,19 +665,53 @@ public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0)
};
return PartialView("_CostMakeUpReport", viewModel);
}
public IActionResult GetFuelCostByMonthByVehicle(int vehicleId, int year = 0)
public IActionResult GetReminderMakeUpByVehicle(int vehicleId, int daysToAdd)
{
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
if (year != default)
var reminders = GetRemindersAndUrgency(vehicleId, DateTime.Now.AddDays(daysToAdd));
var viewModel = new ReminderMakeUpForVehicle
{
gasRecords.RemoveAll(x => x.Date.Year != year);
NotUrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.NotUrgent).Count(),
UrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.Urgent).Count(),
VeryUrgentCount = reminders.Where(x => x.Urgency == ReminderUrgency.VeryUrgent).Count(),
PastDueCount = reminders.Where(x => x.Urgency == ReminderUrgency.PastDue).Count()
};
return PartialView("_ReminderMakeUpReport", viewModel);
}
[HttpPost]
public IActionResult GetCostByMonthByVehicle(int vehicleId, List<ImportMode> selectedMetrics, int year = 0)
{
List<CostForVehicleByMonth> allCosts = new List<CostForVehicleByMonth>();
if (selectedMetrics.Contains(ImportMode.ServiceRecord) || selectedMetrics.Contains(ImportMode.All))
{
var serviceRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId);
allCosts.AddRange(_reportHelper.GetServiceRecordSum(serviceRecords, year));
}
if (selectedMetrics.Contains(ImportMode.RepairRecord) || selectedMetrics.Contains(ImportMode.All))
{
var repairRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId);
allCosts.AddRange(_reportHelper.GetRepairRecordSum(repairRecords, year));
}
if (selectedMetrics.Contains(ImportMode.UpgradeRecord) || selectedMetrics.Contains(ImportMode.All))
{
var upgradeRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId);
allCosts.AddRange(_reportHelper.GetUpgradeRecordSum(upgradeRecords, year));
}
if (selectedMetrics.Contains(ImportMode.GasRecord) || selectedMetrics.Contains(ImportMode.All))
{
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
allCosts.AddRange(_reportHelper.GetGasRecordSum(gasRecords, year));
}
if (selectedMetrics.Contains(ImportMode.TaxRecord) || selectedMetrics.Contains(ImportMode.All))
{
var taxRecords = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
allCosts.AddRange(_reportHelper.GetTaxRecordSum(taxRecords, year));
}
var groupedGasRecord = gasRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new GasCostForVehicleByMonth
var groupedRecord = allCosts.GroupBy(x => x.MonthName).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
MonthName = x.Key,
Cost = x.Sum(y => y.Cost)
}).ToList();
return PartialView("_GasCostByMonthReport", groupedGasRecord);
return PartialView("_GasCostByMonthReport", groupedRecord);
}
#endregion
#region "Reminders"
Expand Down Expand Up @@ -642,17 +740,17 @@ private int GetMaxMileage(int vehicleId)
}
return numbersArray.Any() ? numbersArray.Max() : 0;
}
private List<ReminderRecordViewModel> GetRemindersAndUrgency(int vehicleId)
private List<ReminderRecordViewModel> GetRemindersAndUrgency(int vehicleId, DateTime dateCompare)
{
var currentMileage = GetMaxMileage(vehicleId);
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
List<ReminderRecordViewModel> results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage);
List<ReminderRecordViewModel> results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, dateCompare);
return results;
}
[HttpGet]
public IActionResult GetVehicleHaveUrgentOrPastDueReminders(int vehicleId)
{
var result = GetRemindersAndUrgency(vehicleId);
var result = GetRemindersAndUrgency(vehicleId, DateTime.Now);
if (result.Where(x => x.Urgency == ReminderUrgency.VeryUrgent || x.Urgency == ReminderUrgency.PastDue).Any())
{
return Json(true);
Expand All @@ -662,7 +760,7 @@ public IActionResult GetVehicleHaveUrgentOrPastDueReminders(int vehicleId)
[HttpGet]
public IActionResult GetReminderRecordsByVehicleId(int vehicleId)
{
var result = GetRemindersAndUrgency(vehicleId);
var result = GetRemindersAndUrgency(vehicleId, DateTime.Now);
result = result.OrderByDescending(x => x.Urgency).ToList();
return PartialView("_ReminderRecords", result);
}
Expand Down
3 changes: 2 additions & 1 deletion Enum/ImportMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum ImportMode
RepairRecord = 1,
GasRecord = 2,
TaxRecord = 3,
UpgradeRecord = 4
UpgradeRecord = 4,
All = 5
}
}
16 changes: 8 additions & 8 deletions Helper/ReminderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace CarCareTracker.Helper
{
public interface IReminderHelper
{
List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRecord> reminders, int currentMileage);
List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRecord> reminders, int currentMileage, DateTime dateCompare);
}
public class ReminderHelper: IReminderHelper
{
public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRecord> reminders, int currentMileage)
public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRecord> reminders, int currentMileage, DateTime dateCompare)
{
List<ReminderRecordViewModel> reminderViewModels = new List<ReminderRecordViewModel>();
foreach (var reminder in reminders)
Expand All @@ -25,7 +25,7 @@ public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRe
};
if (reminder.Metric == ReminderMetric.Both)
{
if (reminder.Date < DateTime.Now)
if (reminder.Date < dateCompare)
{
reminderViewModel.Urgency = ReminderUrgency.PastDue;
reminderViewModel.Metric = ReminderMetric.Date;
Expand All @@ -35,7 +35,7 @@ public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRe
reminderViewModel.Urgency = ReminderUrgency.PastDue;
reminderViewModel.Metric = ReminderMetric.Odometer;
}
else if (reminder.Date < DateTime.Now.AddDays(7))
else if (reminder.Date < dateCompare.AddDays(7))
{
//if less than a week from today or less than 50 miles from current mileage then very urgent.
reminderViewModel.Urgency = ReminderUrgency.VeryUrgent;
Expand All @@ -47,7 +47,7 @@ public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRe
reminderViewModel.Urgency = ReminderUrgency.VeryUrgent;
reminderViewModel.Metric = ReminderMetric.Odometer;
}
else if (reminder.Date < DateTime.Now.AddDays(30))
else if (reminder.Date < dateCompare.AddDays(30))
{
reminderViewModel.Urgency = ReminderUrgency.Urgent;
reminderViewModel.Metric = ReminderMetric.Date;
Expand All @@ -60,15 +60,15 @@ public List<ReminderRecordViewModel> GetReminderRecordViewModels(List<ReminderRe
}
else if (reminder.Metric == ReminderMetric.Date)
{
if (reminder.Date < DateTime.Now)
if (reminder.Date < dateCompare)
{
reminderViewModel.Urgency = ReminderUrgency.PastDue;
}
else if (reminder.Date < DateTime.Now.AddDays(7))
else if (reminder.Date < dateCompare.AddDays(7))
{
reminderViewModel.Urgency = ReminderUrgency.VeryUrgent;
}
else if (reminder.Date < DateTime.Now.AddDays(30))
else if (reminder.Date < dateCompare.AddDays(30))
{
reminderViewModel.Urgency = ReminderUrgency.Urgent;
}
Expand Down
77 changes: 77 additions & 0 deletions Helper/ReportHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using CarCareTracker.Models;
using System.Globalization;

namespace CarCareTracker.Helper
{
public interface IReportHelper
{
IEnumerable<CostForVehicleByMonth> GetServiceRecordSum(List<ServiceRecord> serviceRecords, int year = 0);
IEnumerable<CostForVehicleByMonth> GetRepairRecordSum(List<CollisionRecord> repairRecords, int year = 0);
IEnumerable<CostForVehicleByMonth> GetUpgradeRecordSum(List<UpgradeRecord> upgradeRecords, int year = 0);
IEnumerable<CostForVehicleByMonth> GetGasRecordSum(List<GasRecord> gasRecords, int year = 0);
IEnumerable<CostForVehicleByMonth> GetTaxRecordSum(List<TaxRecord> taxRecords, int year = 0);
}
public class ReportHelper: IReportHelper
{
public IEnumerable<CostForVehicleByMonth> GetServiceRecordSum(List<ServiceRecord> serviceRecords, int year = 0)
{
if (year != default)
{
serviceRecords.RemoveAll(x => x.Date.Year != year);
}
return serviceRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
});
}
public IEnumerable<CostForVehicleByMonth> GetRepairRecordSum(List<CollisionRecord> repairRecords, int year = 0)
{
if (year != default)
{
repairRecords.RemoveAll(x => x.Date.Year != year);
}
return repairRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
});
}
public IEnumerable<CostForVehicleByMonth> GetUpgradeRecordSum(List<UpgradeRecord> upgradeRecords, int year = 0)
{
if (year != default)
{
upgradeRecords.RemoveAll(x => x.Date.Year != year);
}
return upgradeRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
});
}
public IEnumerable<CostForVehicleByMonth> GetGasRecordSum(List<GasRecord> gasRecords, int year = 0)
{
if (year != default)
{
gasRecords.RemoveAll(x => x.Date.Year != year);
}
return gasRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
});
}
public IEnumerable<CostForVehicleByMonth> GetTaxRecordSum(List<TaxRecord> taxRecords, int year = 0)
{
if (year != default)
{
taxRecords.RemoveAll(x => x.Date.Year != year);
}
return taxRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CarCareTracker.Models
{
public class GasCostForVehicleByMonth
public class CostForVehicleByMonth
{
public string MonthName { get; set; }
public decimal Cost { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions Models/Report/ReminderMakeUpForVehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CarCareTracker.Models
{
public class ReminderMakeUpForVehicle
{
public int NotUrgentCount { get; set; }
public int UrgentCount { get; set; }
public int VeryUrgentCount { get; set; }
public int PastDueCount { get; set; }
}
}
10 changes: 10 additions & 0 deletions Models/Report/ReportViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CarCareTracker.Models
{
public class ReportViewModel
{
public List<CostForVehicleByMonth> CostForVehicleByMonth { get; set; } = new List<CostForVehicleByMonth>();
public CostMakeUpForVehicle CostMakeUpForVehicle { get; set; } = new CostMakeUpForVehicle();
public ReminderMakeUpForVehicle ReminderMakeUpForVehicle { get; set; } = new ReminderMakeUpForVehicle();
public List<int> Years { get; set; } = new List<int>();
}
}
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
builder.Services.AddSingleton<IGasHelper, GasHelper>();
builder.Services.AddSingleton<IReminderHelper, ReminderHelper>();
builder.Services.AddSingleton<ILoginHelper, LoginHelper>();
builder.Services.AddSingleton<IReportHelper, ReportHelper>();

if (!Directory.Exists("data"))
{
Expand Down
Loading

0 comments on commit ed10666

Please sign in to comment.