using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; namespace Sarand.BusinessMgr.Net.Standard.Base.Bus { //======================================================================= public abstract class SbmStockLevel : SbmHierarchyItem { //------------------------------------------------------------------- // Columns. public enum Column { //Hidden. Pos, Id, // StockLevel. CommodityId, LocationId, Type, Type_Desc, Source, Source_Desc, Quantity, Recorded, RecordedDate, RecordedTime, // Commodity details. Commodity_Code, Commodity_DescBrief, // Location details. Location_CategoryDesc, Location_Code, Location_Name, // Common properties. Notes, Flag, Status, Status_Desc, Status_Ind, CreateOn_Date, UpdateOn_Date, } //------------------------------------------------------------------- /// Properties other than common. public class DataProperties { public SysId CommodityId { get; set; } = new SysId(); public SysId LocationId { get; set; } = new SysId(); public Sbm.StockLevelType Type { get; set; } = Sbm.StockLevelType.Unknown; public Sbm.StockLevelSource Source { get; set; } = Sbm.StockLevelSource.Unknown; public decimal Quantity { get; set; } = 0.0m; public DateTime Recorded { get; set; } = DateTime.MinValue; public String Bay1 { get; set; } = ""; public String Bay2 { get; set; } = ""; public String Bay3 { get; set; } = ""; } // Data. public new class Data : SacsHierarchyItem.Data { // Data members. private readonly DataProperties data = new DataProperties(); // Construction. public Data() { Status = DefaultStatus; } public SysId CommodityId { get { return data.CommodityId; } set { CheckChanged(data.CommodityId, value); data.CommodityId = value; } } public SysId LocationId { get { return data.LocationId; } set { CheckChanged(data.LocationId, value); data.LocationId = value; } } public Sbm.StockLevelType Type { get { return data.Type; } set { CheckChanged((int)data.Type, (int)value); data.Type = value; } } public Sbm.StockLevelSource Source { get { return data.Source; } set { CheckChanged((int)data.Source, (int)value); data.Source = value; } } public decimal Quantity { get { return data.Quantity; } set { CheckChanged(data.Quantity, value); data.Quantity = value; } } public DateTime Recorded { get { return data.Recorded; } set { CheckChanged(data.Recorded, value); data.Recorded = value; } } public String Bay1 { get { return data.Bay1; } set { CheckChanged(data.Bay1, value); data.Bay1 = value; } } public String Bay2 { get { return data.Bay2; } set { CheckChanged(data.Bay2, value); data.Bay2 = value; } } public String Bay3 { get { return data.Bay3; } set { CheckChanged(data.Bay3, value); data.Bay3 = value; } } internal void PopulateFrom(Data data) { CommodityId = data.CommodityId; LocationId = data.LocationId; Type = data.Type; Source = data.Source; Quantity = data.Quantity; Recorded = data.Recorded; Bay1 = data.Bay1; Bay2 = data.Bay2; Bay3 = data.Bay3; base.PopulateFrom(data); } public Data Clone() { Data data = new Data(); data.PopulateFrom(this); return data; } } public new class ErrField : SacsHierarchyItem.ErrField { // Identifies the field that has invalid data. public ValidValue CommodityId { get; set; } = ValidValue.Valid; public ValidValue LocationId { get; set; } = ValidValue.Valid; public ValidValue Type { get; set; } = ValidValue.Valid; public ValidValue Source { get; set; } = ValidValue.Valid; public ValidValue Quantity { get; set; } = ValidValue.Valid; public ValidValue Recorded { get; set; } = ValidValue.Valid; public ValidValue Bay1 { get; set; } = ValidValue.Valid; public ValidValue Bay2 { get; set; } = ValidValue.Valid; public ValidValue Bay3 { get; set; } = ValidValue.Valid; } //------------------------------------------------------------------- // Data members. private readonly Data data = new Data(); //------------------------------------------------------------------- // Construction. protected SbmStockLevel(SysId id, SysId idCommodity, SysId idLocation, Sbm.StockLevelType type, Sbm.StockLevelSource source) : base(id) { base.SetData(data); CommodityId = idCommodity; LocationId = idLocation; Type = type; Source = source; Status = DefaultStatus; } //------------------------------------------------------------------- // StockLevel status. private static readonly List stockLevelStatuses = new List() { SaStatus.Current, SaStatus.Confirmed, SaStatus.Queried, SaStatus.Cancelled, SaStatus.Deleted, }; public static SaStatus DefaultStatus { get { return SaStatus.Current; } } public static List GetValidStatusesByType(Sbm.StockLevelType type) { switch (type) { case Sbm.StockLevelType.CurrentLevel: return new List() { SaStatus.Current }; case Sbm.StockLevelType.StockTake: return new List() { SaStatus.Confirmed, SaStatus.Queried, SaStatus.Cancelled, SaStatus.Deleted }; } return new List() { SaStatus.Deleted }; } public static DataValue.TriStateType GetStatusTriState(SaStatus status) { switch (status) { case SaStatus.Confirmed: case SaStatus.Current: return DataValue.TriStateType.Green; case SaStatus.Queried: return DataValue.TriStateType.Amber; case SaStatus.Cancelled: return DataValue.TriStateType.Red; case SaStatus.Deleted: return DataValue.TriStateType.Purple; } return DataValue.TriStateType.Unknown; } //------------------------------------------------------------------- // Utilities. public SbmItemId GetItemId() { return new SbmItemId(Sbm.ItemType.StockLevel, Id); } //------------------------------------------------------------------- // Properties. public Data GetData() { return data.Clone(); } public void SetData(Data data) { // Properties that cannot be changed if previously specified. if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id)) throw new NotSupportedException($"{SysUtil.Fn} Id cannot be changed"); // Populate individual properties to ensure that 'Changed' indicator is kept up to date. this.data.PopulateFrom(data); } //------------------------------------------------------------------- // Attributes. public SysId CommodityId { get { return data.CommodityId; } set { data.CommodityId = value; } } public SysId LocationId { get { return data.LocationId; } set { data.LocationId = value; } } public Sbm.StockLevelType Type { get { return data.Type; } set { data.Type = value; } } public Sbm.StockLevelSource Source { get { return data.Source; } set { data.Source = value; } } public decimal Quantity { get { return data.Quantity; } set { data.Quantity = value; } } public DateTime Recorded { get { return data.Recorded; } set { data.Recorded = value; } } public String Bay1 { get { return data.Bay1; } set { data.Bay1 = value; } } public String Bay2 { get { return data.Bay2; } set { data.Bay2 = value; } } public String Bay3 { get { return data.Bay3; } set { data.Bay3 = value; } } //------------------------------------------------------------------- // A s s o c i a t e d D a t a E l e m e n t s //------------------------------------------------------------------- public virtual bool HasStockLevels() { return true; } //------------------------------------------------------------------- // P a r a m s //------------------------------------------------------------------- /// Parameters - used to define the filters that should be applied to a List of Stock Levels // List parameters. public enum StatusCat { Any, /// Not cancelled or deleted. ActiveOnly, ConfirmedOnly, QueriedOnly, DeletedOnly, NotDeleted, } /// /// Item list parameters. /// public new class Params : SacsHierarchyItem.Params { public Params() { } public Params(Params prm) { IdsIncluded.UnionWith(prm.IdsIncluded); IdsExcluded.UnionWith(prm.IdsExcluded); // Stock Level. CommodityId = prm.CommodityId; CommodityCodePartial = prm.CommodityCodePartial; LocationId = prm.LocationId; RestrictToSalesStockLocations = prm.RestrictToSalesStockLocations; RestrictToCalculateStockLocations = prm.RestrictToCalculateStockLocations; Type = prm.Type; RecordedFrom = prm.RecordedFrom; RecordedTo = prm.RecordedTo; MostRecentOnly = prm.MostRecentOnly; StatusCat = prm.StatusCat; // Table aliases. tableAlias_Commodity = prm.tableAlias_Commodity; tableAlias_Location = prm.tableAlias_Location; } // List filtering. public HashSet IdsIncluded { get; } = new HashSet(); public HashSet IdsExcluded { get; } = new HashSet(); // Stock Level. public SysId CommodityId { get; set; } public String CommodityCodePartial { get; set; } public SysId LocationId { get; set; } public bool RestrictToSalesStockLocations { get; set; } public bool RestrictToCalculateStockLocations { get; set; } public Sbm.StockLevelType Type { get; set; } = Sbm.StockLevelType.Any; public DateTime RecordedFrom { get; set; } public DateTime RecordedTo { get; set; } public bool MostRecentOnly { get; set; } /// Stock Level status. public StatusCat StatusCat { get; set; } = StatusCat.NotDeleted; // Table aliases. public String tableAlias_Commodity { get; set; } public String tableAlias_Location { get; set; } } //------------------------------------------------------------------- } //======================================================================= // S t o c k L e v e l L i s t //======================================================================= public static class SbmStockLevelList { public static bool Find(this List values, SbmStockLevel stockLevel) { if (stockLevel == null) return false; return values.Any(item => item == stockLevel); } public static SbmStockLevel FindById(this List values, SysId id) { if (!SysId.IsValid(id)) return null; return values.FirstOrDefault(item => item.Id.Equals(id)); } public static SbmStockLevel GetByCommodityId(this List values, SysId commodityId) { if (!SysId.IsValid(commodityId)) return null; return values.FirstOrDefault(item => item.CommodityId.Equals(commodityId)); } public static SbmStockLevel GetByLocationId(this List values, SysId locationId) { if (!SysId.IsValid(locationId)) return null; return values.FirstOrDefault(item => item.LocationId.Equals(locationId)); } } //======================================================================= }