diff --git a/Controllers/Vehicle/ReportController.cs b/Controllers/Vehicle/ReportController.cs index f6d1a897..425aa986 100644 --- a/Controllers/Vehicle/ReportController.cs +++ b/Controllers/Vehicle/ReportController.cs @@ -196,28 +196,7 @@ public IActionResult GetCostTableForVehicle(int vehicleId, int year = 0) var vehicleData = _dataAccess.GetVehicleById(vehicleId); var userConfig = _config.GetUserConfig(User); var totalDistanceTraveled = maxMileage - minMileage; - var totalDays = 0; - if (year != default) - { - if (year == DateTime.Now.Year) //current year selected, do math based on how many days have elapsed. - { - totalDays = DateTime.Now.DayOfYear; - if (!string.IsNullOrWhiteSpace(vehicleData.SoldDate)) //if vehicle is sold in current year, cap the number of days to sold date. - { - var endDate = DateTime.Parse(vehicleData.SoldDate); - if (endDate.Year == DateTime.Now.Year) - { - totalDays = endDate.DayOfYear; - } - } - } else - { - totalDays = DateTime.IsLeapYear(year) ? 366 : 365; - } - } else - { - totalDays = _vehicleLogic.GetOwnershipDays(vehicleData.PurchaseDate, vehicleData.SoldDate, serviceRecords, collisionRecords, gasRecords, upgradeRecords, odometerRecords, taxRecords); - } + var totalDays = _vehicleLogic.GetOwnershipDays(vehicleData.PurchaseDate, vehicleData.SoldDate, year, serviceRecords, collisionRecords, gasRecords, upgradeRecords, odometerRecords, taxRecords); var viewModel = new CostTableForVehicle { ServiceRecordSum = serviceRecords.Sum(x => x.Cost), diff --git a/Logic/VehicleLogic.cs b/Logic/VehicleLogic.cs index 9d42c2f0..04dc5b64 100644 --- a/Logic/VehicleLogic.cs +++ b/Logic/VehicleLogic.cs @@ -13,7 +13,7 @@ public interface IVehicleLogic int GetMaxMileage(VehicleRecords vehicleRecords); int GetMinMileage(int vehicleId); int GetMinMileage(VehicleRecords vehicleRecords); - int GetOwnershipDays(string purchaseDate, string soldDate, List serviceRecords, List repairRecords, List gasRecords, List upgradeRecords, List odometerRecords, List taxRecords); + int GetOwnershipDays(string purchaseDate, string soldDate, int year, List serviceRecords, List repairRecords, List gasRecords, List upgradeRecords, List odometerRecords, List taxRecords); bool GetVehicleHasUrgentOrPastDueReminders(int vehicleId, int currentMileage); List GetVehicleInfo(List vehicles); List GetReminders(List vehicles, bool isCalendar); @@ -199,22 +199,44 @@ public int GetMinMileage(VehicleRecords vehicleRecords) } return numbersArray.Any() ? numbersArray.Min() : 0; } - public int GetOwnershipDays(string purchaseDate, string soldDate, List serviceRecords, List repairRecords, List gasRecords, List upgradeRecords, List odometerRecords, List taxRecords) + public int GetOwnershipDays(string purchaseDate, string soldDate, int year, List serviceRecords, List repairRecords, List gasRecords, List upgradeRecords, List odometerRecords, List taxRecords) { var startDate = DateTime.Now; var endDate = DateTime.Now; - if (!string.IsNullOrWhiteSpace(soldDate)) + bool usePurchaseDate = false; + bool useSoldDate = false; + if (!string.IsNullOrWhiteSpace(soldDate) && DateTime.TryParse(soldDate, out DateTime vehicleSoldDate)) { - endDate = DateTime.Parse(soldDate); + if (year == default || year >= vehicleSoldDate.Year) //All Time is selected or the selected year is greater or equal to the year the vehicle is sold + { + endDate = vehicleSoldDate; //cap end date to vehicle sold date. + useSoldDate = true; + } + } + if (!string.IsNullOrWhiteSpace(purchaseDate) && DateTime.TryParse(purchaseDate, out DateTime vehiclePurchaseDate)) + { + if (year == default || year <= vehiclePurchaseDate.Year) //All Time is selected or the selected year is less or equal to the year the vehicle is purchased + { + startDate = vehiclePurchaseDate; //cap start date to vehicle purchase date + usePurchaseDate = true; + } } - if (!string.IsNullOrWhiteSpace(purchaseDate)) + if (year != default) { - //if purchase date is provided, then we just have to subtract the begin date to end date and return number of months - startDate = DateTime.Parse(purchaseDate); + var calendarYearStart = new DateTime(year, 1, 1); + var calendarYearEnd = new DateTime(year + 1, 1, 1); + if (!useSoldDate) + { + endDate = endDate > calendarYearEnd ? calendarYearEnd : endDate; + } + if (!usePurchaseDate) + { + startDate = startDate > calendarYearStart ? calendarYearStart : startDate; + } var timeElapsed = (int)Math.Floor((endDate - startDate).TotalDays); return timeElapsed; } - var dateArray = new List(); + var dateArray = new List() { startDate }; dateArray.AddRange(serviceRecords.Select(x => x.Date)); dateArray.AddRange(repairRecords.Select(x => x.Date)); dateArray.AddRange(gasRecords.Select(x => x.Date));