134 lines
6.0 KiB
C#
134 lines
6.0 KiB
C#
using System.Collections.Generic;
|
|
using Newtonsoft.Json.Linq;
|
|
using AyaNova.Biz;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AyaNova.Models;
|
|
using AyaNova.Util;
|
|
using System.Linq;
|
|
namespace AyaNova.PickList
|
|
{
|
|
internal class ServiceRatePickList : AyaPickList, IAyaPickListVariant
|
|
{
|
|
public ServiceRatePickList()
|
|
{
|
|
|
|
DefaultListAType = AyaType.ServiceRate;
|
|
SQLFrom = "from aservicerate";
|
|
AllowedRoles = BizRoles.GetRoleSet(DefaultListAType).Select;
|
|
dynamic dTemplate = new JArray();
|
|
|
|
dynamic cm = new JObject();
|
|
cm.fld = "Name";
|
|
dTemplate.Add(cm);
|
|
|
|
cm = new JObject();
|
|
cm.fld = "Tags";
|
|
dTemplate.Add(cm);
|
|
|
|
base.DefaultTemplate = dTemplate.ToString(Newtonsoft.Json.Formatting.None);
|
|
|
|
//NOTE: Due to the join, all the sql id and name fields that can conflict with the joined table need to be specified completely
|
|
ColumnDefinitions = new List<AyaPickListFieldDefinition>();
|
|
ColumnDefinitions.Add(new AyaPickListFieldDefinition
|
|
{
|
|
TKey = "Active",
|
|
FieldKey = "Active",
|
|
ColumnDataType = UiFieldDataType.Bool,
|
|
SqlValueColumnName = "aservicerate.active",
|
|
IsActiveColumn = true
|
|
});
|
|
ColumnDefinitions.Add(new AyaPickListFieldDefinition
|
|
{
|
|
TKey = "Name",
|
|
FieldKey = "Name",
|
|
ColumnDataType = UiFieldDataType.Text,
|
|
SqlIdColumnName = "aservicerate.id",
|
|
SqlValueColumnName = "aservicerate.name",
|
|
IsRowId = true
|
|
});
|
|
|
|
ColumnDefinitions.Add(new AyaPickListFieldDefinition
|
|
{
|
|
TKey = "Tags",
|
|
FieldKey = "Tags",
|
|
ColumnDataType = UiFieldDataType.Tags,
|
|
SqlValueColumnName = "aservicerate.tags"
|
|
});
|
|
|
|
}
|
|
|
|
public string GetVariantCriteria(string variant)
|
|
{
|
|
/*
|
|
Need variant that shows only rates with contractonly=true (for selection in contract)
|
|
Need variant that shows only rates for a specific contract (limit to contract rates contract in effect on workorder)
|
|
Need variant that shows all rates *without* any contract rates (no contract workorder)
|
|
Need variant that shows all regular rates and also specific contract rates
|
|
Perhaps all of these will work with the addition of just the contract id?
|
|
can join in the contract table and items and only show if present or whatever the query needs to be
|
|
return results are same just the where criteria or server criteria and FROM part of query
|
|
As non contract scenario is just all items minus any contract only rates
|
|
Contract may or may not be all items plus contract or just contract (different queries)
|
|
But it all needs to go through same picklist code for client simplicity
|
|
*/
|
|
|
|
//No variant = all items
|
|
//"contractrates" variant = all items defined as contract only = true
|
|
//"contractid:[id]" variant = whatever the contract allows, could be only limited items or all items plus special contract or just all items nothing special
|
|
|
|
string defaultNoContractRatesCriteria = "aservicerate.contractonly=false";
|
|
|
|
//variant empty, return all that are not contract rates
|
|
if (string.IsNullOrWhiteSpace(variant))
|
|
return defaultNoContractRatesCriteria;
|
|
|
|
if (variant == "contractrates")
|
|
return "aservicerate.contractonly=true";
|
|
|
|
if (!variant.Contains(":"))
|
|
throw new System.ArgumentOutOfRangeException($"ServiceRatePickList variant specified \"{variant}\" not valid");
|
|
|
|
var v = variant.Split(":");
|
|
switch (v[0])
|
|
{
|
|
case "contractid":
|
|
{
|
|
if (string.IsNullOrWhiteSpace(v[1]))
|
|
return defaultNoContractRatesCriteria;
|
|
long contractId = 0;
|
|
if (!long.TryParse(v[1], out contractId))
|
|
return defaultNoContractRatesCriteria;
|
|
if (contractId == 0)
|
|
return defaultNoContractRatesCriteria;
|
|
|
|
//have a contract id, let's pull it and build the rate id's needed
|
|
using (AyContext ct = ServiceProviderProvider.DBContext)
|
|
{
|
|
var c = ct.Contract
|
|
.Include(z => z.ServiceRateItems)
|
|
.AsNoTracking()
|
|
.Where(z => z.Id == contractId)
|
|
.FirstOrDefault();
|
|
|
|
if (c == null)
|
|
return defaultNoContractRatesCriteria;
|
|
|
|
//only fetch items that are in the contract rates
|
|
if (c.ContractServiceRatesOnly)
|
|
return $"aservicerate.id in ({string.Join(",", c.ServiceRateItems.Select(z => z.ServiceRateId).ToArray())})";
|
|
|
|
//contract allows any items plus contract items if exist
|
|
//If no service rate items in contract then short circuit to regular list
|
|
if (c.ServiceRateItems.Count == 0)
|
|
return defaultNoContractRatesCriteria;
|
|
|
|
//Ok, a bit more complex then, all non contract rates plus a select few contract rates
|
|
return $"(aservicerate.contractonly=false) or (aservicerate.id in ({string.Join(",", c.ServiceRateItems.Select(z => z.ServiceRateId).ToArray())}))";
|
|
|
|
}
|
|
}
|
|
}
|
|
return defaultNoContractRatesCriteria;
|
|
}
|
|
}//eoc
|
|
}//eons |