diff --git a/Web/Controllers/FuelLogController.cs b/Web/Controllers/FuelLogController.cs index ddbc27b..e0c0831 100644 --- a/Web/Controllers/FuelLogController.cs +++ b/Web/Controllers/FuelLogController.cs @@ -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 = diff --git a/Web/Controllers/VehicleServiceController.cs b/Web/Controllers/VehicleServiceController.cs index b5b42e3..df4f9ff 100644 --- a/Web/Controllers/VehicleServiceController.cs +++ b/Web/Controllers/VehicleServiceController.cs @@ -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); diff --git a/Web/DAL/DataService.cs b/Web/DAL/DataService.cs index 79d544e..82e9e6e 100644 --- a/Web/DAL/DataService.cs +++ b/Web/DAL/DataService.cs @@ -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 FilterFuelLogs(IQueryable 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 GetVehicleServices() + public IQueryable GetVehicleServices() { var vehicleServices = _db.VehicleServices; return vehicleServices; @@ -836,6 +830,35 @@ namespace MileageTraker.Web.DAL _db.SaveChanges(); } + public IList 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 FilterVehicleServices(IQueryable 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 + } } \ No newline at end of file diff --git a/Web/ViewModels/DateQueryViewModel.cs b/Web/ViewModels/DateQueryViewModel.cs new file mode 100644 index 0000000..f521f7a --- /dev/null +++ b/Web/ViewModels/DateQueryViewModel.cs @@ -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(); + + 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; + } + + /// + /// Use the given list of valid dates, set the default options + /// + /// + public virtual void SetDefaultParameters(IList 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; + } + } + } +} \ No newline at end of file diff --git a/Web/ViewModels/FuelLog/FuelLogQueryViewModel.cs b/Web/ViewModels/FuelLog/FuelLogQueryViewModel.cs index c7ea0d7..16fe71d 100644 --- a/Web/ViewModels/FuelLog/FuelLogQueryViewModel.cs +++ b/Web/ViewModels/FuelLog/FuelLogQueryViewModel.cs @@ -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(); - - 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; - } } } \ No newline at end of file diff --git a/Web/ViewModels/FuelLog/FuelLogResultsViewModel.cs b/Web/ViewModels/FuelLog/FuelLogResultsViewModel.cs index 2e8dd67..fb79035 100644 --- a/Web/ViewModels/FuelLog/FuelLogResultsViewModel.cs +++ b/Web/ViewModels/FuelLog/FuelLogResultsViewModel.cs @@ -7,24 +7,32 @@ namespace MileageTraker.Web.ViewModels.FuelLog { public IEnumerable FuelLogs { get; set; } public Dictionary> AvailableYearMonths { get; set; } - public IEnumerable SelectedYearMonths{get + public IEnumerable SelectedYearMonths { - if (!string.IsNullOrEmpty(FiscalYear)) - return AvailableYearMonths[FiscalYear]; - return new List(); - }} + get + { + if (!string.IsNullOrEmpty(Year)) + return AvailableYearMonths[Year]; + return new List(); + } + } // 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 fuelLogs, FuelLogQueryViewModel query, Dictionary> availableYearMonths) + public FuelLogResultsViewModel( + IEnumerable fuelLogs, + FuelLogQueryViewModel query, + Dictionary> 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; } } diff --git a/Web/ViewModels/Log/LogQueryViewModel.cs b/Web/ViewModels/Log/LogQueryViewModel.cs index 8324e9d..b6c4c10 100644 --- a/Web/ViewModels/Log/LogQueryViewModel.cs +++ b/Web/ViewModels/Log/LogQueryViewModel.cs @@ -32,7 +32,6 @@ namespace MileageTraker.Web.ViewModels.Log public override string ToString() { var v = new List(); - //string.Format("{0}-{1}{2}", Year, Month, LogType.HasValue ? "-" + LogType.Value.GetDisplayShortName() : ""); if (Year.HasValue && Month.HasValue) { diff --git a/Web/ViewModels/VehicleService/VehicleServiceQueryViewModel.cs b/Web/ViewModels/VehicleService/VehicleServiceQueryViewModel.cs index 326e1f7..859dd3c 100644 --- a/Web/ViewModels/VehicleService/VehicleServiceQueryViewModel.cs +++ b/Web/ViewModels/VehicleService/VehicleServiceQueryViewModel.cs @@ -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(); - - 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; - } } } \ No newline at end of file diff --git a/Web/ViewModels/VehicleService/VehicleServiceResultsViewModel.cs b/Web/ViewModels/VehicleService/VehicleServiceResultsViewModel.cs index a817fef..29d59ab 100644 --- a/Web/ViewModels/VehicleService/VehicleServiceResultsViewModel.cs +++ b/Web/ViewModels/VehicleService/VehicleServiceResultsViewModel.cs @@ -1,9 +1,37 @@ using System.Collections.Generic; +using System.Globalization; namespace MileageTraker.Web.ViewModels.VehicleService { public class VehicleServiceResultsViewModel { public IEnumerable ServiceItems { get; set; } + public Dictionary> AvailableYearMonths { get; set; } + public IEnumerable SelectedYearMonths + { + get + { + if (!string.IsNullOrEmpty(Year)) + return AvailableYearMonths[Year]; + return new List(); + } + } + + // filter parameters + public string Year { get; set; } + public string Month { get; set; } + public string MonthRange { get; set; } + + public VehicleServiceResultsViewModel( + IEnumerable serviceItems, + VehicleServiceQueryViewModel query, + Dictionary> 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; + } } } diff --git a/Web/Views/FuelLog/Index.cshtml b/Web/Views/FuelLog/Index.cshtml index 592792c..3e272e6 100644 --- a/Web/Views/FuelLog/Index.cshtml +++ b/Web/Views/FuelLog/Index.cshtml @@ -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 { - + var parameters = new {FiscalYear = Model.Year, Model.Month, Model.Unmatched}; } + @section Scripts { diff --git a/Web/Views/Shared/EditorTemplates/FuelLogResultsViewModel.cshtml b/Web/Views/Shared/EditorTemplates/FuelLogResultsViewModel.cshtml index dc8ef09..a468305 100644 --- a/Web/Views/Shared/EditorTemplates/FuelLogResultsViewModel.cshtml +++ b/Web/Views/Shared/EditorTemplates/FuelLogResultsViewModel.cshtml @@ -3,13 +3,17 @@ @model MileageTraker.Web.ViewModels.FuelLog.FuelLogResultsViewModel
-
- @Html.Label("FiscalYear", "Fiscal Year") - @Html.DropDownList("FiscalYear", new SelectList(Model.AvailableYearMonths.Keys, Model.FiscalYear), "Select Year", new { @class = "input-small" }) -
+ @Html.Label("Year", "Year") + @Html.DropDownList("Year", new SelectList(Model.AvailableYearMonths.Keys, Model.Year), "Select Year", new { @class = "input-small" }) +
+
@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" }) +
+
+ @Html.Label("MonthRange", "Range") + @Html.DropDownList("MonthRange", new SelectList(Enumerable.Range(1,12), Model.MonthRange), new { @class = "input-mini" })
@Html.Label("Unmatched", "Unmatched Only", new{style = "padding-top:22px"}) diff --git a/Web/Views/Shared/EditorTemplates/VehicleServiceResultsViewModel.cshtml b/Web/Views/Shared/EditorTemplates/VehicleServiceResultsViewModel.cshtml new file mode 100644 index 0000000..52cd093 --- /dev/null +++ b/Web/Views/Shared/EditorTemplates/VehicleServiceResultsViewModel.cshtml @@ -0,0 +1,21 @@ +@using MileageTraker.Web.Models +@using MileageTraker.Web.Utility +@model MileageTraker.Web.ViewModels.VehicleService.VehicleServiceResultsViewModel + +
+
+ @Html.Label("Year", "Year") + @Html.DropDownList("Year", new SelectList(Model.AvailableYearMonths.Keys, Model.Year), "Select Year", new { @class = "input-small" }) +
+
+ @Html.Label("Month", "Month") + @Html.DropDownList("Month", new SelectList(Model.SelectedYearMonths, Model.Month), "All Months", new { @class = "input-small" }) +
+
+ @Html.Label("MonthRange", "Range") + @Html.DropDownList("MonthRange", new SelectList(Enumerable.Range(1,12), Model.MonthRange), new { @class = "input-mini" }) +
+
+ +
+
\ No newline at end of file diff --git a/Web/Views/VehicleService/Index.cshtml b/Web/Views/VehicleService/Index.cshtml index 6eae855..783c0c2 100644 --- a/Web/Views/VehicleService/Index.cshtml +++ b/Web/Views/VehicleService/Index.cshtml @@ -5,8 +5,26 @@ var grid = new WebGrid(Model.ServiceItems, rowsPerPage: 45); } +@section Scripts { + +} + @Html.Partial("_StatusMessage") +
+ @using (Html.BeginForm("Index", "VehicleService", FormMethod.Get, new { id = "filter", @class = "form" })) + { + @Html.EditorForModel() + } +
+

@ViewBag.Title

@@ -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"), diff --git a/Web/Web.csproj b/Web/Web.csproj index 8eaa352..98f2bb2 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -255,6 +255,7 @@ + @@ -333,6 +334,7 @@ +