Filter vehicle services

This commit is contained in:
2015-10-12 18:07:20 -04:00
parent 22e4089fba
commit e008bc2242
14 changed files with 227 additions and 119 deletions
+4 -12
View File
@@ -21,17 +21,13 @@ namespace MileageTraker.Web.Controllers
return View("Empty");
// default parameter processing
if (!query.HasParameters())
{
query.FiscalYear = validLogYearMonths.First().Year;
query.Month = validLogYearMonths.First().Month;
}
query.SetDefaultParameters(validLogYearMonths);
var fuelLogs = DataService.GetFuelLogs();
var flvm = from fl in DataService.FilterFuelLogs(fuelLogs, query).ToList()
orderby fl.Date ascending
select new FuelLogIndexViewModel(fl);
orderby fl.Date ascending
select new FuelLogIndexViewModel(fl);
var viewModel = new FuelLogResultsViewModel(flvm, query, CustomExtensions.YearMonthList(validLogYearMonths));
@@ -44,11 +40,7 @@ namespace MileageTraker.Web.Controllers
var validLogYearMonths = DataService.GetValidFuelLogMonths();
// default parameter processing
if (!query.HasParameters())
{
query.FiscalYear = validLogYearMonths.First().Year;
query.Month = validLogYearMonths.First().Month;
}
query.SetDefaultParameters(validLogYearMonths);
var fuelLogs = DataService.GetFuelLogs();
var filteredLogs =
+17 -6
View File
@@ -2,6 +2,8 @@
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.VehicleService;
namespace MileageTraker.Web.Controllers
@@ -11,11 +13,21 @@ namespace MileageTraker.Web.Controllers
{
public ActionResult Index(VehicleServiceQueryViewModel query)
{
var vehicleServices = DataService.GetVehicleServices().ToList();
var viewModel = new VehicleServiceResultsViewModel
{
ServiceItems = vehicleServices.Select(s => new VehicleServiceViewModel(s))
};
var validVehicleServiceYearMonths = DataService.GetValidVehicleServiceMonths();
if (!validVehicleServiceYearMonths.Any()) // this means no logs in DB
return View("Empty");
// default parameter processing
query.SetDefaultParameters(validVehicleServiceYearMonths);
var vehicleServices = DataService.FilterVehicleServices(DataService.GetVehicleServices(), query).ToList();
var viewModel = new VehicleServiceResultsViewModel(
vehicleServices.Select(s => new VehicleServiceViewModel(s)),
query,
CustomExtensions.YearMonthList(validVehicleServiceYearMonths));
return View(viewModel);
}
@@ -51,7 +63,6 @@ namespace MileageTraker.Web.Controllers
return View(viewModel);
}
public ActionResult Edit(int id)
{
var vehicleService = DataService.GetVehicleService(id);
+35 -12
View File
@@ -10,6 +10,7 @@ using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.FuelLog;
using MileageTraker.Web.ViewModels.Log;
using MileageTraker.Web.ViewModels.Vehicle;
using MileageTraker.Web.ViewModels.VehicleService;
namespace MileageTraker.Web.DAL
{
@@ -661,19 +662,12 @@ namespace MileageTraker.Web.DAL
public static IQueryable<FuelLog> FilterFuelLogs(IQueryable<FuelLog> fuelLogs, FuelLogQueryViewModel query)
{
const int fiscalYearStartMonth = 7;
// date filtering
if (query.FiscalYear.HasValue & query.Month.HasValue)
if (query.Year.HasValue & query.Month.HasValue)
{
var start = new DateTime(query.FiscalYear.Value, query.Month.Value, 1);
var end = start.AddMonths(1);
fuelLogs = fuelLogs.Where(l => l.Date >= start && l.Date < end);
}
else if (query.FiscalYear.HasValue)
{
var start = new DateTime(query.FiscalYear.Value, fiscalYearStartMonth, 1);
var end = start.AddYears(1);
var start = new DateTime(query.Year.Value, query.Month.Value, 1);
var monthRange = query.MonthRange.HasValue ? query.MonthRange.Value : 1;
var end = start.AddMonths(monthRange);
fuelLogs = fuelLogs.Where(l => l.Date >= start && l.Date < end);
}
if (query.Unmatched)
@@ -817,7 +811,7 @@ namespace MileageTraker.Web.DAL
_db.SaveChanges();
}
public IEnumerable<VehicleService> GetVehicleServices()
public IQueryable<VehicleService> GetVehicleServices()
{
var vehicleServices = _db.VehicleServices;
return vehicleServices;
@@ -836,6 +830,35 @@ namespace MileageTraker.Web.DAL
_db.SaveChanges();
}
public IList<DateTime> GetValidVehicleServiceMonths()
{
var months =
from l in GetVehicleServices()
group l by new {l.InvoiceDate.Year, l.InvoiceDate.Month}
into g
select g.Key;
var ym =
from m in months.ToList()
select new DateTime(m.Year, m.Month, 1);
return ym.OrderByDescending(dt => dt).ToList();
}
public static IQueryable<VehicleService> FilterVehicleServices(IQueryable<VehicleService> vehicleServices, VehicleServiceQueryViewModel query)
{
// date filtering
if (query.Year.HasValue & query.Month.HasValue)
{
var start = new DateTime(query.Year.Value, query.Month.Value, 1);
var monthRange = query.MonthRange.HasValue ? query.MonthRange.Value : 1;
var end = start.AddMonths(monthRange);
vehicleServices = vehicleServices.Where(l => l.InvoiceDate >= start && l.InvoiceDate < end);
}
return vehicleServices;
}
#endregion
}
}
+70
View File
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace MileageTraker.Web.ViewModels
{
public class DateQueryViewModel
{
public int? Year { get; set; }
public int? Month { get; set; }
public int? MonthRange { get; set; }
public string YearMonthStart
{
get { return string.Format("{0}-{1:00}", Year, Month); }
}
public string YearMonthEnd
{
get
{
var dateTime = new DateTime(Year.Value, Month.Value, 1);
var time = dateTime.AddMonths(MonthRange.Value - 1);
return string.Format("{0}-{1:00}", time.Year, time.Month);
}
}
public override string ToString()
{
var v = new List<string>();
if (Year.HasValue && Month.HasValue)
{
var str = YearMonthStart;
if (MonthRange.HasValue && MonthRange > 1)
str += "to" + YearMonthEnd;
v.Add(str);
}
return String.Join("_", v).Replace(' ', '-');
}
public virtual bool HasParameters()
{
return Year.HasValue;
}
/// <summary>
/// Use the given list of valid dates, set the default options
/// </summary>
/// <param name="validYearMonths"></param>
public virtual void SetDefaultParameters(IList<DateTime> validYearMonths)
{
if (!HasParameters())
{
Year = validYearMonths.First().Year;
Month = validYearMonths.First().Month;
MonthRange = 1;
}
if (Year.HasValue && !Month.HasValue)
{
var validLogMonths = validYearMonths.Where(dt => dt.Year == Year).ToList();
Month = validLogMonths.Min(dt => dt.Month);
MonthRange = validLogMonths.Max(dt => dt.Month) - Month + 1;
}
}
}
}
@@ -1,40 +1,7 @@
using System;
using System.Collections.Generic;
namespace MileageTraker.Web.ViewModels.FuelLog
{
public class FuelLogQueryViewModel
public class FuelLogQueryViewModel : DateQueryViewModel
{
public int? FiscalYear { get; set; }
public int? Month { get; set; }
public bool Unmatched { get; set; }
public string YearMonthStart {get
{
return string.Format("{0}-{1:00}", FiscalYear, Month);
}}
public override string ToString()
{
var v = new List<string>();
if (FiscalYear.HasValue && Month.HasValue)
{
var str = YearMonthStart;
v.Add(str);
}
else if (FiscalYear.HasValue)
{
v.Add(FiscalYear.ToString());
}
return String.Join("_", v).Replace(' ', '-');
}
public bool HasParameters()
{
return FiscalYear.HasValue;
}
}
}
@@ -7,24 +7,32 @@ namespace MileageTraker.Web.ViewModels.FuelLog
{
public IEnumerable<FuelLogIndexViewModel> FuelLogs { get; set; }
public Dictionary<string, List<string>> AvailableYearMonths { get; set; }
public IEnumerable<string> SelectedYearMonths{get
public IEnumerable<string> SelectedYearMonths
{
if (!string.IsNullOrEmpty(FiscalYear))
return AvailableYearMonths[FiscalYear];
return new List<string>();
}}
get
{
if (!string.IsNullOrEmpty(Year))
return AvailableYearMonths[Year];
return new List<string>();
}
}
// filter parameters
public string FiscalYear { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string MonthRange { get; set; }
public bool Unmatched { get; set; }
public FuelLogResultsViewModel(IEnumerable<FuelLogIndexViewModel> fuelLogs, FuelLogQueryViewModel query, Dictionary<string, List<string>> availableYearMonths)
public FuelLogResultsViewModel(
IEnumerable<FuelLogIndexViewModel> fuelLogs,
FuelLogQueryViewModel query,
Dictionary<string, List<string>> availableYearMonths)
{
FuelLogs = fuelLogs;
AvailableYearMonths = availableYearMonths;
FiscalYear = query.FiscalYear.HasValue ? query.FiscalYear.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
Year = query.Year.HasValue ? query.Year.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
Month = query.Month.HasValue ? query.Month.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
MonthRange = query.MonthRange.HasValue ? query.MonthRange.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
Unmatched = query.Unmatched;
}
}
-1
View File
@@ -32,7 +32,6 @@ namespace MileageTraker.Web.ViewModels.Log
public override string ToString()
{
var v = new List<string>();
//string.Format("{0}-{1}{2}", Year, Month, LogType.HasValue ? "-" + LogType.Value.GetDisplayShortName() : "");
if (Year.HasValue && Month.HasValue)
{
@@ -1,39 +1,6 @@
using System;
using System.Collections.Generic;
namespace MileageTraker.Web.ViewModels.VehicleService
{
public class VehicleServiceQueryViewModel
public class VehicleServiceQueryViewModel : DateQueryViewModel
{
public int? Year { get; set; }
public int? Month { get; set; }
public string YearMonthStart {get
{
return string.Format("{0}-{1:00}", Year, Month);
}}
public override string ToString()
{
var v = new List<string>();
if (Year.HasValue && Month.HasValue)
{
var str = YearMonthStart;
v.Add(str);
}
else if (Year.HasValue)
{
v.Add(Year.ToString());
}
return String.Join("_", v).Replace(' ', '-');
}
public bool HasParameters()
{
return Year.HasValue;
}
}
}
@@ -1,9 +1,37 @@
using System.Collections.Generic;
using System.Globalization;
namespace MileageTraker.Web.ViewModels.VehicleService
{
public class VehicleServiceResultsViewModel
{
public IEnumerable<VehicleServiceViewModel> ServiceItems { get; set; }
public Dictionary<string, List<string>> AvailableYearMonths { get; set; }
public IEnumerable<string> SelectedYearMonths
{
get
{
if (!string.IsNullOrEmpty(Year))
return AvailableYearMonths[Year];
return new List<string>();
}
}
// filter parameters
public string Year { get; set; }
public string Month { get; set; }
public string MonthRange { get; set; }
public VehicleServiceResultsViewModel(
IEnumerable<VehicleServiceViewModel> serviceItems,
VehicleServiceQueryViewModel query,
Dictionary<string, List<string>> availableYearMonths)
{
ServiceItems = serviceItems;
AvailableYearMonths = availableYearMonths;
Year = query.Year.HasValue ? query.Year.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
Month = query.Month.HasValue ? query.Month.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
MonthRange = query.MonthRange.HasValue ? query.MonthRange.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
}
}
}
+4 -6
View File
@@ -4,17 +4,15 @@
@{
ViewBag.Title = "Fuel Logs";
var grid = new WebGrid(Model.FuelLogs, rowsPerPage: 45);
var parameters = new {FiscalYear = Model.FiscalYear, Model.Month, Model.Unmatched};
}
@section Styles {
<link href="@Url.Content("~/Content/VehicleColors.css")" rel="stylesheet" type="text/css" />
var parameters = new {FiscalYear = Model.Year, Model.Month, Model.Unmatched};
}
@section Scripts {
<script type="text/javascript">
var availableLogYearMonths = @Html.Raw(Json.Encode(Model.AvailableYearMonths));
@if (Model.FiscalYear != null)
@if (Model.Year != null)
{
<text>var selectedYear = "@Model.FiscalYear";</text>
<text>var selectedYear = "@Model.Year";</text>
<text>var selectedMonth = "@Model.Month";</text>
}
</script>
@@ -3,13 +3,17 @@
@model MileageTraker.Web.ViewModels.FuelLog.FuelLogResultsViewModel
<div class="row-fluid">
<div class="span4">
@Html.Label("FiscalYear", "Fiscal Year")
@Html.DropDownList("FiscalYear", new SelectList(Model.AvailableYearMonths.Keys, Model.FiscalYear), "Select Year", new { @class = "input-small" })
</div>
<div class="span3">
@Html.Label("Year", "Year")
@Html.DropDownList("Year", new SelectList(Model.AvailableYearMonths.Keys, Model.Year), "Select Year", new { @class = "input-small" })
</div>
<div class="span2">
@Html.Label("Month", "Month")
@Html.DropDownList("Month", new SelectList(Model.SelectedYearMonths, Model.Month), "All Months", new { @class = "input-small" })
@Html.DropDownList("Month", new SelectList(Model.SelectedYearMonths, Model.Month), "All Months", new { @class = "input-mini" })
</div>
<div class="span2">
@Html.Label("MonthRange", "Range")
@Html.DropDownList("MonthRange", new SelectList(Enumerable.Range(1,12), Model.MonthRange), new { @class = "input-mini" })
</div>
<div class="span3" >
@Html.Label("Unmatched", "Unmatched Only", new{style = "padding-top:22px"})
@@ -0,0 +1,21 @@
@using MileageTraker.Web.Models
@using MileageTraker.Web.Utility
@model MileageTraker.Web.ViewModels.VehicleService.VehicleServiceResultsViewModel
<div class="row-fluid">
<div class="span4">
@Html.Label("Year", "Year")
@Html.DropDownList("Year", new SelectList(Model.AvailableYearMonths.Keys, Model.Year), "Select Year", new { @class = "input-small" })
</div>
<div class="span3">
@Html.Label("Month", "Month")
@Html.DropDownList("Month", new SelectList(Model.SelectedYearMonths, Model.Month), "All Months", new { @class = "input-small" })
</div>
<div class="span2">
@Html.Label("MonthRange", "Range")
@Html.DropDownList("MonthRange", new SelectList(Enumerable.Range(1,12), Model.MonthRange), new { @class = "input-mini" })
</div>
<div class="span2">
<input type="submit" value="Filter" class="btn" style="margin-top:1.75em" />
</div>
</div>
+19 -1
View File
@@ -5,8 +5,26 @@
var grid = new WebGrid(Model.ServiceItems, rowsPerPage: 45);
}
@section Scripts {
<script type="text/javascript">
var availableLogYearMonths = @Html.Raw(Json.Encode(Model.AvailableYearMonths));
@if (Model.Year != null)
{
<text>var selectedYear = "@Model.Year";</text>
<text>var selectedMonth = "@Model.Month";</text>
}
</script>
}
@Html.Partial("_StatusMessage")
<div class="btn-toolbar pull-right" style="width:400px">
@using (Html.BeginForm("Index", "VehicleService", FormMethod.Get, new { id = "filter", @class = "form" }))
{
@Html.EditorForModel()
}
</div>
<h2 id="vehicle-title"><i class="fa fa-wrench"></i> @ViewBag.Title</h2>
<div class="btn-toolbar pull-left">
@@ -16,7 +34,7 @@
@grid.GetHtml(columns:
grid.Columns(
grid.Column("InvoiceDate", "Invoice Date"),
grid.Column("InvoiceDate", "Invoice Date", item => item.InvoiceDate.ToString("d")),
grid.Column("VehicleID", "Vehicle Id"),
grid.Column("ServiceCenterName", "Service Center Name"),
grid.Column("Price", "Price"),
+2
View File
@@ -255,6 +255,7 @@
<Compile Include="ViewModels\Log\LogIndexViewModel.cs" />
<Compile Include="ViewModels\Log\LogPartialDetails.cs" />
<Compile Include="Models\VehicleService.cs" />
<Compile Include="ViewModels\DateQueryViewModel.cs" />
<Compile Include="ViewModels\VehicleService\VehicleServiceQueryViewModel.cs" />
<Compile Include="ViewModels\VehicleService\VehicleServiceResultsViewModel.cs" />
<Compile Include="ViewModels\VehicleService\VehicleServiceViewModel.cs" />
@@ -333,6 +334,7 @@
<Content Include="Views\VehicleService\Create.cshtml" />
<Content Include="Views\VehicleService\Edit.cshtml" />
<Content Include="Views\VehicleService\Details.cshtml" />
<Content Include="Views\Shared\EditorTemplates\VehicleServiceResultsViewModel.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\Account.Login.css" />