diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 86d2f70..01f750a 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -46,7 +46,8 @@ namespace CarCareTracker.Controllers UseMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]), UseDescending = bool.Parse(_config[nameof(UserConfig.UseDescending)]), EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]), - HideZero = bool.Parse(_config[nameof(UserConfig.HideZero)]) + HideZero = bool.Parse(_config[nameof(UserConfig.HideZero)]), + UseUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]) }; return PartialView("_Settings", userConfig); } diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index c62504e..2c4b6c3 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -250,6 +250,7 @@ namespace CarCareTracker.Controllers result = result.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList(); //check if the user uses MPG or Liters per 100km. bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]); + bool useUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]); var computedResults = new List(); int previousMileage = 0; decimal unFactoredConsumption = 0.00M; @@ -257,9 +258,19 @@ namespace CarCareTracker.Controllers //perform computation. for (int i = 0; i < result.Count; i++) { + var currentObject = result[i]; + decimal convertedConsumption; + if (useUKMPG && useMPG) + { + //if we're using UK MPG and the user wants imperial calculation insteace of l/100km + //if UK MPG is selected then the gas consumption are stored in liters but need to convert into UK gallons for computation. + convertedConsumption = currentObject.Gallons / 4.546M; + } else + { + convertedConsumption = currentObject.Gallons; + } if (i > 0) { - var currentObject = result[i]; var deltaMileage = currentObject.Mileage - previousMileage; var gasRecordViewModel = new GasRecordViewModel() { @@ -267,22 +278,22 @@ namespace CarCareTracker.Controllers VehicleId = currentObject.VehicleId, Date = currentObject.Date.ToShortDateString(), Mileage = currentObject.Mileage, - Gallons = currentObject.Gallons, + Gallons = convertedConsumption, Cost = currentObject.Cost, DeltaMileage = deltaMileage, - CostPerGallon = (currentObject.Cost / currentObject.Gallons) + CostPerGallon = (currentObject.Cost / convertedConsumption) }; if (currentObject.IsFillToFull) { //if user filled to full. - gasRecordViewModel.MilesPerGallon = useMPG ? ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + currentObject.Gallons)) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + currentObject.Gallons)); + gasRecordViewModel.MilesPerGallon = useMPG ? ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption)) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption)); //reset unFactored vars unFactoredConsumption = 0; unFactoredMileage = 0; } else { - unFactoredConsumption += currentObject.Gallons; + unFactoredConsumption += convertedConsumption; unFactoredMileage += deltaMileage; gasRecordViewModel.MilesPerGallon = 0; } @@ -292,18 +303,18 @@ namespace CarCareTracker.Controllers { computedResults.Add(new GasRecordViewModel() { - Id = result[i].Id, - VehicleId = result[i].VehicleId, - Date = result[i].Date.ToShortDateString(), - Mileage = result[i].Mileage, - Gallons = result[i].Gallons, - Cost = result[i].Cost, + Id = currentObject.Id, + VehicleId = currentObject.VehicleId, + Date = currentObject.Date.ToShortDateString(), + Mileage = currentObject.Mileage, + Gallons = convertedConsumption, + Cost = currentObject.Cost, DeltaMileage = 0, MilesPerGallon = 0, - CostPerGallon = (result[i].Cost / result[i].Gallons) + CostPerGallon = (currentObject.Cost / currentObject.Gallons) }); } - previousMileage = result[i].Mileage; + previousMileage = currentObject.Mileage; } if (_useDescending) { diff --git a/MapProfile/FuellyMappers.cs b/MapProfile/FuellyMappers.cs index fb46846..811da33 100644 --- a/MapProfile/FuellyMappers.cs +++ b/MapProfile/FuellyMappers.cs @@ -9,8 +9,8 @@ namespace CarCareTracker.MapProfile { Map(m => m.Date).Name(["date", "fuelup_date"]); Map(m => m.Odometer).Name(["odometer"]); - Map(m => m.FuelConsumed).Name(["gallons", "liters", "litres"]); - Map(m => m.Cost).Name(["cost", "total cost", "totalcost"]); + Map(m => m.FuelConsumed).Name(["gallons", "liters", "litres", "consumption", "fuelconsumed"]); + Map(m => m.Cost).Name(["cost", "total cost", "totalcost", "total price"]); Map(m => m.Notes).Name("notes", "note"); Map(m => m.Price).Name(["price"]); Map(m => m.PartialFuelUp).Name(["partial_fuelup"]); diff --git a/Models/UserConfig.cs b/Models/UserConfig.cs index 2e80932..b3fba51 100644 --- a/Models/UserConfig.cs +++ b/Models/UserConfig.cs @@ -8,6 +8,7 @@ public bool UseDescending { get; set; } public bool EnableAuth { get; set; } public bool HideZero { get; set; } + public bool UseUKMPG {get;set;} public string UserNameHash { get; set; } public string UserPasswordHash { get; set;} } diff --git a/Views/Home/_Settings.cshtml b/Views/Home/_Settings.cshtml index 160d24a..7266531 100644 --- a/Views/Home/_Settings.cshtml +++ b/Views/Home/_Settings.cshtml @@ -18,6 +18,10 @@ +
+ + +
@@ -81,7 +85,8 @@ enableCsvImports: $("#enableCsvImports").is(':checked'), useMPG: $("#useMPG").is(':checked'), useDescending: $("#useDescending").is(':checked'), - hideZero: $("#hideZero").is(":checked") + hideZero: $("#hideZero").is(":checked"), + useUKMpg: $("#useUKMPG").is(":checked") } $.post('/Home/WriteToSettings', { userConfig: userConfigObject}, function(data){ if (data) { diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index a861433..fd8bfa1 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -3,19 +3,34 @@ @{ var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]); var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]); + var useUKMPG = bool.Parse(Configuration[nameof(UserConfig.UseUKMPG)]); var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]); var useKwh = Model.UseKwh; string consumptionUnit; string fuelEconomyUnit; + string distanceUnit; if (useKwh) { consumptionUnit = "kWh"; fuelEconomyUnit = useMPG ? "mi/kWh" : "kWh/100km"; - } else + } + else if (useMPG && useUKMPG) { - consumptionUnit = useMPG ? "gal" : "l"; + consumptionUnit = "imp gal"; + fuelEconomyUnit = "mpg"; + } + else + { + consumptionUnit = useMPG ? "US gal" : "l"; fuelEconomyUnit = useMPG ? "mpg" : "l/100km"; } + if (useUKMPG) + { + distanceUnit = "mi."; + } else { + distanceUnit = useMPG ? "mi" : "km"; + } + }
@@ -52,7 +67,7 @@ Date Refueled - Odometer(@(useMPG ? "mi." : "km")) + Odometer(@distanceUnit) Consumption(@(consumptionUnit)) Fuel Economy(@(fuelEconomyUnit)) Cost diff --git a/Views/Vehicle/_GasModal.cshtml b/Views/Vehicle/_GasModal.cshtml index dfe3c2a..593dcc1 100644 --- a/Views/Vehicle/_GasModal.cshtml +++ b/Views/Vehicle/_GasModal.cshtml @@ -2,17 +2,30 @@ @model GasRecordInputContainer @{ var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]); + var useUKMPG = bool.Parse(Configuration[nameof(UserConfig.UseUKMPG)]); var useKwh = Model.UseKwh; var isNew = Model.GasRecord.Id == 0; string consumptionUnit; + string distanceUnit; if (useKwh) { consumptionUnit = "kWh"; + } else if (useUKMPG) + { + consumptionUnit = "liters"; } else { consumptionUnit = useMPG ? "gallons" : "liters"; } + if (useUKMPG) + { + distanceUnit = "miles"; + } + else + { + distanceUnit = useMPG ? "miles" : "kilometers"; + } } - + diff --git a/appsettings.json b/appsettings.json index bf22a5e..cc56a77 100644 --- a/appsettings.json +++ b/appsettings.json @@ -12,6 +12,7 @@ "UseDescending": false, "EnableAuth": false, "HideZero": false, + "UseUKMPG": false, "UserNameHash": "", "UserPasswordHash": "" }