Files
BusinessMgr-2019/SaBusMgrStandardBase/Base/Bus/SbmItemPrice.cs
T
2021-05-05 21:00:33 +01:00

266 lines
8.0 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.CodeTypes;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
namespace Sarand.BusinessMgr.Net.Standard.Base.Bus
{
//=======================================================================
public class SbmItemPrice : SbmHierarchyItem
{
//-------------------------------------------------------------------
// Columns.
public enum Column
{
Hidden,
Pos,
// ItemPrice.
// Common properties.
Notes,
Status,
Status_Ind,
Status_Desc,
CreateOn_Date,
UpdateOn_Date,
}
//-------------------------------------------------------------------
/// <summary>Properties other than common.</summary>
public class DataProperties
{
public String Type { get; set; } = "";
public decimal Amount { get; set; } = 0.0m;
public ushort CodeSeq { get; set; } = 0;
}
// Data.
public new class Data : SacsHierarchyItem.Data
{
// Data members.
private readonly DataProperties data = new DataProperties();
// Construction.
public Data()
{
Status = DefaultStatus;
}
public String Type
{
get { return data.Type; }
set { CheckChanged(data.Type, value); data.Type = value; }
}
public decimal Amount
{
get { return data.Amount; }
set { CheckChanged(data.Amount, value); data.Amount = value; }
}
public ushort CodeSeq
{
get { return data.CodeSeq; }
set { CheckChanged(data.CodeSeq, value); data.CodeSeq = value; }
}
internal void PopulateFrom(Data data)
{
Type = data.Type;
Amount = data.Amount;
CodeSeq = data.CodeSeq;
base.PopulateFrom(data);
}
public Data Clone()
{
Data data = new Data();
data.PopulateFrom(this);
return data;
}
}
//-------------------------------------------------------------------
// Data members.
private readonly Data data = new Data();
private readonly String type;
private readonly decimal amount = 0.0m;
private readonly ushort codeSeq = 0;
// Static Definition.
private static String itemPrice = "ItemPrice";
private static String delimArchiveOld = "#~#";
private static String delimArchive = "#~#";
//-------------------------------------------------------------------
// Construction.
public SbmItemPrice(SbmUserContext userContext) : base(userContext)
{
Status = DefaultStatus;
}
// Conversion from ItemLink.
public SbmItemPrice(SbmUserContext userContext, String archive) : base(userContext)
{
String[] data = archive.Split(new String[] { delimArchive }, StringSplitOptions.None);
type = (data.Length > 1 ? data[1] : "");
amount = (data.Length > 2 ? StringUtil.ToDecimal(data[2]) : 0.0m);
}
//-------------------------------------------------------------------
// ItemPrice status.
private static readonly List<SaStatus> containerItemStatuses = new List<SaStatus>()
{
SaStatus.Active,
SaStatus.Disabled,
};
public static SaStatus DefaultStatus { get { return SaStatus.Current; } }
public static List<SaStatus> ItemPriceStatuses { get { return new List<SaStatus>(containerItemStatuses); } }
public static DataValue.TriStateType GetStatusTriState(SaStatus status)
{
switch (status)
{
case SaStatus.Active:
return DataValue.TriStateType.Green;
case SaStatus.OnHold:
return DataValue.TriStateType.Amber;
case SaStatus.Disabled:
return DataValue.TriStateType.Red;
case SaStatus.Deleted:
return DataValue.TriStateType.Purple;
}
return DataValue.TriStateType.Unknown;
}
//-------------------------------------------------------------------
// Utilities.
// Matching.
public bool MatchesByType(String type)
{
if (Type != type) return false;
return true;
}
// Cloning - make a copy of the item.
public SbmItemPrice Clone()
{
SbmItemPrice itemPrice = new SbmItemPrice(UserContext);
//itemPriceCost.CopyAttributesFrom(this);
itemPrice.Type = Type;
itemPrice.Amount = Amount;
//itemPricePayment.Changed = false;
return itemPrice;
}
// Conversion.
public String Conversion()
{
String archive = "";
archive += Type.ToString() + delimArchive;
archive += StringUtil.FormatNumeric(Amount, 4) + delimArchive;
return archive;
}
//-------------------------------------------------------------------
// Properties.
public Data GetData() { return data.Clone(); }
public void SetData(Data data)
{
String fn = GetType().Name + ".SetData";
// Properties that cannot be changed if previously specified.
if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id))
throw new NotSupportedException(fn + " Id cannot be changed");
// Populate individual properties to ensure that 'Changed' indicator is kept up to date.
this.data.PopulateFrom(data);
}
//-------------------------------------------------------------------
// Attributes.
public String Type
{
get { return data.Type; }
set { data.Type = value; }
}
public decimal Amount
{
get { return data.Amount; }
set { data.Amount = value; }
}
public ushort CodeSeq
{
get { return data.CodeSeq; }
set { data.CodeSeq = value; }
}
// Calculated costs.
//-------------------------------------------------------------------
}
//=======================================================================
// I t e m P r i c e D a t a L i s t
//=======================================================================
public static class SbmItemPriceList
{
/// <summary>Check whether currency exists/locate in list with specified Id.</summary>
//-------------------------------------------------------------------
//Find.
public static bool Find(this List<SbmItemPrice> values, SbmItemPrice containerItem)
{
if (containerItem == null) return false;
return (values.Find(item => item == containerItem) != null);
}
public static bool FindByType(this List<SbmItemPrice> values, String type)
{
if (StringUtil.IsEmpty(type)) return false;
return values.Exists(item => item.MatchesByType(type));
}
//-------------------------------------------------------------------
public static void SetTypeCodeSequence(this List<SbmItemPrice> values, List<SaCodeDfn> typeCodeDfns)
{
if (typeCodeDfns == null) return;
foreach (SbmItemPrice itemPrice in values)
{
// Retrieve the next item in the list.
SbmItemPrice itemPriceEst = values[StringUtil.ToInt(itemPrice)];
if (typeCodeDfns != null && typeCodeDfns.Exists(item => item.Code.Equals(itemPriceEst.Type)))
itemPriceEst.CodeSeq = typeCodeDfns.Find(item => item.Code.Equals(itemPriceEst.CodeSeq)).Sequence;
}
}
public static int CompareCatType(this List<SbmItemPrice> values, SbmItemPrice itemPrice1, SbmItemPrice itemPrice)
{
if (itemPrice1 == null || itemPrice == null) return 0;
if (itemPrice1 == itemPrice) return 0;
// Compare the sequence derived from the typed code.
if (itemPrice1.CodeSeq > itemPrice.CodeSeq) return 1;
if (itemPrice1.CodeSeq < itemPrice.CodeSeq) return -1;
// Code sequences are the same - compare the type code.
if (itemPrice1.Type != itemPrice.Type)
return itemPrice1.Type.CompareTo(itemPrice.Type);
return -1;
}
public static void Sort(this List<SbmItemPrice> values)
{
values.Sort
(
delegate (SbmItemPrice itemPrice1, SbmItemPrice itemPrice2)
{
//return CompareCatType(values, -1, 0);
return CompareCatType(values, itemPrice1, itemPrice2);
}
);
}
//-------------------------------------------------------------------
public static List<SbmItemPrice> Clone(this List<SbmItemPrice> values)
{
List<SbmItemPrice> itemPrices = new List<SbmItemPrice>();
//if (itemPrices.Count > 0)
// Clone each item in the list.
for (int pos = 1; pos <= values.Count; pos++)
itemPrices.Add(values[pos].Clone());
return itemPrices;
}
//-------------------------------------------------------------------
}
//=======================================================================
}