added option to mark gas record as not filled to full so that MPG calculations are deferred

This commit is contained in:
DESKTOP-GENO133\IvanPlex
2024-01-05 22:53:27 -07:00
parent 208eb22b8c
commit 1c2368f5a1
6 changed files with 65 additions and 25 deletions

View File

@@ -160,7 +160,8 @@ namespace CarCareTracker.Controllers
_gasRecordDataAccess.SaveGasRecordToVehicle(convertedRecord);
}
}
} else if (mode == "servicerecord")
}
else if (mode == "servicerecord")
{
var records = csv.GetRecords<ServiceRecordImport>().ToList();
if (records.Any())
@@ -179,7 +180,8 @@ namespace CarCareTracker.Controllers
_serviceRecordDataAccess.SaveServiceRecordToVehicle(convertedRecord);
}
}
} else if (mode == "repairrecord")
}
else if (mode == "repairrecord")
{
var records = csv.GetRecords<ServiceRecordImport>().ToList();
if (records.Any())
@@ -198,7 +200,8 @@ namespace CarCareTracker.Controllers
_collisionRecordDataAccess.SaveCollisionRecordToVehicle(convertedRecord);
}
}
} else if (mode == "taxrecord")
}
else if (mode == "taxrecord")
{
var records = csv.GetRecords<TaxRecordImport>().ToList();
if (records.Any())
@@ -239,6 +242,8 @@ namespace CarCareTracker.Controllers
bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]);
var computedResults = new List<GasRecordViewModel>();
int previousMileage = 0;
decimal unFactoredConsumption = 0.00M;
int unFactoredMileage = 0;
//perform computation.
for (int i = 0; i < result.Count; i++)
{
@@ -246,7 +251,7 @@ namespace CarCareTracker.Controllers
{
var currentObject = result[i];
var deltaMileage = currentObject.Mileage - previousMileage;
computedResults.Add(new GasRecordViewModel()
var gasRecordViewModel = new GasRecordViewModel()
{
Id = currentObject.Id,
VehicleId = currentObject.VehicleId,
@@ -255,10 +260,24 @@ namespace CarCareTracker.Controllers
Gallons = currentObject.Gallons,
Cost = currentObject.Cost,
DeltaMileage = deltaMileage,
MilesPerGallon = useMPG ? (deltaMileage / currentObject.Gallons) : 100 / (deltaMileage / currentObject.Gallons),
CostPerGallon = (currentObject.Cost / currentObject.Gallons)
});
};
if (currentObject.IsFillToFull)
{
//if user filled to full.
gasRecordViewModel.MilesPerGallon = useMPG ? ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + currentObject.Gallons)) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + currentObject.Gallons));
//reset unFactored vars
unFactoredConsumption = 0;
unFactoredMileage = 0;
} else
{
unFactoredConsumption += currentObject.Gallons;
unFactoredMileage += deltaMileage;
gasRecordViewModel.MilesPerGallon = 0;
}
computedResults.Add(gasRecordViewModel);
}
else
{
computedResults.Add(new GasRecordViewModel()
{
@@ -305,7 +324,8 @@ namespace CarCareTracker.Controllers
Cost = result.Cost,
Date = result.Date.ToShortDateString(),
Files = result.Files,
Gallons = result.Gallons
Gallons = result.Gallons,
IsFillToFull = result.IsFillToFull
};
return PartialView("_GasModal", convertedResult);
}
@@ -349,7 +369,9 @@ namespace CarCareTracker.Controllers
{
var result = _serviceRecordDataAccess.GetServiceRecordById(serviceRecordId);
//convert to Input object.
var convertedResult = new ServiceRecordInput { Id = result.Id,
var convertedResult = new ServiceRecordInput
{
Id = result.Id,
Cost = result.Cost,
Date = result.Date.ToShortDateString(),
Description = result.Description,
@@ -508,7 +530,8 @@ namespace CarCareTracker.Controllers
{
gasRecords.RemoveAll(x => x.Date.Year != year);
}
var groupedGasRecord = gasRecords.GroupBy(x => x.Date.Month).OrderBy(x=>x.Key).Select(x => new GasCostForVehicleByMonth {
var groupedGasRecord = gasRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new GasCostForVehicleByMonth
{
MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key),
Cost = x.Sum(y => y.Cost)
}).ToList();

View File

@@ -14,6 +14,7 @@
/// </summary>
public decimal Gallons { get; set; }
public decimal Cost { get; set; }
public bool IsFillToFull { get; set; } = true;
public List<UploadedFiles> Files { get; set; } = new List<UploadedFiles>();
}
}

View File

@@ -14,7 +14,17 @@
/// </summary>
public decimal Gallons { get; set; }
public decimal Cost { get; set; }
public bool IsFillToFull { get; set; } = true;
public List<UploadedFiles> Files { get; set; } = new List<UploadedFiles>();
public GasRecord ToGasRecord() { return new GasRecord { Id = Id, Cost = Cost, Date = DateTime.Parse(Date), Gallons = Gallons, Mileage = Mileage, VehicleId = VehicleId, Files = Files }; }
public GasRecord ToGasRecord() { return new GasRecord {
Id = Id,
Cost = Cost,
Date = DateTime.Parse(Date),
Gallons = Gallons,
Mileage = Mileage,
VehicleId = VehicleId,
Files = Files,
IsFillToFull = IsFillToFull
}; }
}
}

View File

@@ -8,7 +8,7 @@
<div class="d-flex justify-content-between">
<div class="d-flex align-items-center flex-wrap">
<span class="ms-2 badge bg-success">@($"# of Gas Records: {Model.Count()}")</span>
@if (Model.Count() > 1)
@if (Model.Where(x=>x.MilesPerGallon > 0).Any())
{
<span class="ms-2 badge bg-primary">@($"Average Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Average(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Min Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Min(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>

View File

@@ -22,9 +22,13 @@
<label for="gasRecordMileage">Odometer Reading(@(useMPG ? "miles" : "kilometers"))</label>
<input type="number" id="gasRecordMileage" class="form-control" placeholder="Odometer reading when refueled" value="@(isNew ? "" : Model.Mileage)">
<label for="gasRecordGallons">Fuel Consumption(@(useMPG ? "gallons" : "liters"))</label>
<input type="text" id="gasRecordGallons" class="form-control" placeholder="Amount of gas it takes to fill back up to full" value="@(isNew ? "" : Model.Gallons)">
<input type="text" id="gasRecordGallons" class="form-control" placeholder="Amount of gas refueled" value="@(isNew ? "" : Model.Gallons)">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="gasIsFillToFull" checked="@Model.IsFillToFull">
<label class="form-check-label" for="gasIsFillToFull">Is Filled To Full</label>
</div>
<label for="GasRecordCost">Cost</label>
<input type="number" id="gasRecordCost" class="form-control" placeholder="Cost of gas it takes to fill back up to full" value="@(isNew ? "" : Model.Cost)">
<input type="number" id="gasRecordCost" class="form-control" placeholder="Cost of gas refueled" value="@(isNew ? "" : Model.Cost)">
</div>
<div class="col-md-6 col-12">
@if (Model.Files.Any())

View File

@@ -74,6 +74,7 @@ function getAndValidateGasRecordValues() {
var gasMileage = $("#gasRecordMileage").val();
var gasGallons = $("#gasRecordGallons").val();
var gasCost = $("#gasRecordCost").val();
var gasIsFillToFull = $("#gasIsFillToFull").is(":checked");
var vehicleId = GetVehicleId().vehicleId;
var gasRecordId = getGasRecordModelData().id;
//validation
@@ -110,7 +111,8 @@ function getAndValidateGasRecordValues() {
mileage: gasMileage,
gallons: gasGallons,
cost: gasCost,
files: uploadedFiles
files: uploadedFiles,
isFillToFull: gasIsFillToFull
}
}
function deleteGasRecordFile(fileLocation, event) {