Vehicle cost report
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.SqlServer;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using MileageTraker.Web.Context;
|
||||
@@ -865,6 +866,67 @@ namespace MileageTraker.Web.DAL
|
||||
|
||||
#endregion
|
||||
|
||||
#region Vehicle Cost Report
|
||||
|
||||
private const int FiscalYearStartMonth = 7;
|
||||
|
||||
public IQueryable<VehicleCostItem> GetFuelLogCost(int fiscalYear)
|
||||
{
|
||||
var fiscalYearStart = new DateTime(fiscalYear - 1, FiscalYearStartMonth, 1);
|
||||
var fiscalYearEnd = new DateTime(fiscalYear, FiscalYearStartMonth, 1);
|
||||
|
||||
return
|
||||
from fuelLog in _db.FuelLogs
|
||||
where fuelLog.Date >= fiscalYearStart && fuelLog.Date < fiscalYearEnd
|
||||
group fuelLog by fuelLog.TagNumber
|
||||
into tagGroup
|
||||
let totalFuelPrice = tagGroup.Sum(i => i.TotalPrice)
|
||||
select new VehicleCostItem { CostType = "Fuel", TotalPrice = totalFuelPrice, TagNumber = tagGroup.Key};
|
||||
}
|
||||
|
||||
public IQueryable<VehicleCostItem> GetVehicleServiceCost(int fiscalYear)
|
||||
{
|
||||
var fiscalYearStart = new DateTime(fiscalYear - 1, FiscalYearStartMonth, 1);
|
||||
var fiscalYearEnd = new DateTime(fiscalYear, FiscalYearStartMonth, 1);
|
||||
|
||||
return
|
||||
from vehicleService in _db.VehicleServices
|
||||
where vehicleService.InvoiceDate >= fiscalYearStart && vehicleService.InvoiceDate < fiscalYearEnd
|
||||
group vehicleService by vehicleService.Vehicle.TagNumber
|
||||
into tagGroup
|
||||
let totalServicePrice = tagGroup.Sum(i => i.Price)
|
||||
select new VehicleCostItem { CostType = "Service", TotalPrice = totalServicePrice, TagNumber = tagGroup.Key };
|
||||
}
|
||||
|
||||
public IList<VehicleCostItem> GetVehicleCostItems(int fiscalYear)
|
||||
{
|
||||
var costItems = GetFuelLogCost(fiscalYear).Union(GetVehicleServiceCost(fiscalYear)).ToList();
|
||||
foreach (var costItem in costItems)
|
||||
{
|
||||
costItem.VehicleId = GetVehicleIdByTag(costItem.TagNumber);
|
||||
}
|
||||
return costItems;
|
||||
}
|
||||
|
||||
public IList<int> GetValidVehicleCostItemsYears()
|
||||
{
|
||||
var fuelYears =
|
||||
(from fuelLog in _db.FuelLogs
|
||||
let fiscalYear = SqlFunctions.DateAdd("month", FiscalYearStartMonth - 1, fuelLog.Date).Value.Year
|
||||
select fiscalYear).Distinct();
|
||||
|
||||
var serviceYears =
|
||||
(from vehicleService in _db.VehicleServices
|
||||
let fiscalYear = SqlFunctions.DateAdd("month", FiscalYearStartMonth - 1, vehicleService.InvoiceDate).Value.Year
|
||||
select fiscalYear).Distinct();
|
||||
|
||||
var years = fuelYears.Union(serviceYears).Distinct().OrderByDescending(y => y);
|
||||
|
||||
return years.ToList();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Service Reminder
|
||||
public void AddServiceReminder(ServiceReminder serviceReminder)
|
||||
{
|
||||
@@ -920,5 +982,6 @@ namespace MileageTraker.Web.DAL
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user