|
|
@@ -9,19 +9,42 @@ namespace Kitty_Fall.Cms.Controllers;
|
|
|
|
|
|
public class RevenueReportController : CmsAuthorizedController
|
|
|
{
|
|
|
- private const string DefaultServiceFilter = "MYJOB";
|
|
|
- private static readonly HashSet<string> SupportedServiceFilters = new(StringComparer.OrdinalIgnoreCase)
|
|
|
- {
|
|
|
- "MYJOB",
|
|
|
- "MYJOB_DAILY",
|
|
|
- "MYJOB_MONTHLY"
|
|
|
- };
|
|
|
-
|
|
|
private readonly ModelContext _db;
|
|
|
+ private readonly IReadOnlyList<ServiceFilterOptionViewModel> _serviceFilters;
|
|
|
+ private readonly HashSet<string> _supportedServiceFilters;
|
|
|
+ private readonly string _defaultServiceFilter;
|
|
|
|
|
|
- public RevenueReportController(ModelContext db)
|
|
|
+ public RevenueReportController(ModelContext db, IConfiguration configuration)
|
|
|
{
|
|
|
_db = db;
|
|
|
+
|
|
|
+ _serviceFilters = configuration
|
|
|
+ .GetSection("RevenueReport:Services")
|
|
|
+ .Get<List<ServiceFilterOptionViewModel>>()?
|
|
|
+ .Where(x => !string.IsNullOrWhiteSpace(x.Value) && !string.IsNullOrWhiteSpace(x.Name))
|
|
|
+ .Select(x => new ServiceFilterOptionViewModel
|
|
|
+ {
|
|
|
+ Value = x.Value.Trim(),
|
|
|
+ Name = x.Name.Trim()
|
|
|
+ })
|
|
|
+ .DistinctBy(x => x.Value, StringComparer.OrdinalIgnoreCase)
|
|
|
+ .ToList()
|
|
|
+ ?? new List<ServiceFilterOptionViewModel>();
|
|
|
+
|
|
|
+ if (_serviceFilters.Count == 0)
|
|
|
+ {
|
|
|
+ throw new InvalidOperationException("RevenueReport:Services must contain at least one service.");
|
|
|
+ }
|
|
|
+
|
|
|
+ _supportedServiceFilters = _serviceFilters
|
|
|
+ .Select(x => x.Value)
|
|
|
+ .ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
|
+
|
|
|
+ var configuredDefault = configuration["RevenueReport:DefaultService"]?.Trim();
|
|
|
+ _defaultServiceFilter = !string.IsNullOrWhiteSpace(configuredDefault)
|
|
|
+ && _supportedServiceFilters.Contains(configuredDefault)
|
|
|
+ ? _serviceFilters.First(x => string.Equals(x.Value, configuredDefault, StringComparison.OrdinalIgnoreCase)).Value
|
|
|
+ : _serviceFilters[0].Value;
|
|
|
}
|
|
|
|
|
|
[HttpGet("/RevenueReport")]
|
|
|
@@ -59,6 +82,7 @@ public class RevenueReportController : CmsAuthorizedController
|
|
|
FromDate = normalizedFromDate,
|
|
|
ToDate = normalizedToDate,
|
|
|
ServiceFilter = normalizedServiceFilter,
|
|
|
+ ServiceFilters = _serviceFilters,
|
|
|
TotalRows = items.Count,
|
|
|
TotalRevenue = items.Sum(x => x.Revenue),
|
|
|
AverageRevenue = items.Count == 0 ? 0 : items.Average(x => x.Revenue),
|
|
|
@@ -94,6 +118,7 @@ public class RevenueReportController : CmsAuthorizedController
|
|
|
FromDate = normalizedFromDate,
|
|
|
ToDate = normalizedToDate,
|
|
|
ServiceFilter = normalizedServiceFilter,
|
|
|
+ ServiceFilters = _serviceFilters,
|
|
|
TotalColumns = items.Count,
|
|
|
TotalRevenue = items.Sum(x => x.Revenue),
|
|
|
AverageRevenue = items.Count == 0 ? 0 : items.Average(x => x.Revenue),
|
|
|
@@ -125,12 +150,12 @@ public class RevenueReportController : CmsAuthorizedController
|
|
|
return (normalizedFromDate, normalizedToDate);
|
|
|
}
|
|
|
|
|
|
- private static string NormalizeServiceFilter(string? serviceFilter)
|
|
|
+ private string NormalizeServiceFilter(string? serviceFilter)
|
|
|
{
|
|
|
- var normalizedServiceFilter = (serviceFilter ?? string.Empty).Trim().ToUpperInvariant();
|
|
|
- return SupportedServiceFilters.Contains(normalizedServiceFilter)
|
|
|
- ? normalizedServiceFilter
|
|
|
- : DefaultServiceFilter;
|
|
|
+ var normalizedServiceFilter = (serviceFilter ?? string.Empty).Trim();
|
|
|
+ return _supportedServiceFilters.Contains(normalizedServiceFilter)
|
|
|
+ ? _serviceFilters.First(x => string.Equals(x.Value, normalizedServiceFilter, StringComparison.OrdinalIgnoreCase)).Value
|
|
|
+ : _defaultServiceFilter;
|
|
|
}
|
|
|
|
|
|
private IQueryable<ReportByTime> BuildBaseQuery(string reportType, DateTime? fromDate, DateTime? toDate, string serviceFilter)
|