using FruitBank.Common.Dtos; using FruitBank.Common.Enums; using FruitBank.Common.Interfaces; using LinqToDB; using LinqToDB.Mapping; using Mango.Nop.Core.Entities; using Newtonsoft.Json; namespace FruitBank.Common.Entities; public abstract class MeasuringItemPalletBase : MgEntityBase, IMeasuringItemPalletBase { private double _palletWeight; private double _grossWeight; private double _tareWeight; [NotColumn] protected int ForeignItemId; [NotColumn] public int ForeignKey => ForeignItemId; public int TrayQuantity { get; set; } [Column(DataType = DataType.DecFloat)] public double TareWeight { get => _tareWeight; set => _tareWeight = double.Round(value, 1); } [Column(DataType = DataType.DecFloat)] public double PalletWeight { get => _palletWeight; set => _palletWeight = double.Round(value, 0); } [NotColumn, System.ComponentModel.DataAnnotations.Schema.NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public double NetWeight { get => CalculateNetWeight(); set => throw new Exception($"MeasuringItemPalletBase.NetWeight not set"); } [Column(DataType = DataType.DecFloat, CanBeNull = false)] public double GrossWeight { get => _grossWeight; set => _grossWeight = double.Round(value, 1); } public bool IsMeasured { get; set; } [SkipValuesOnUpdate] public int? CreatorId { get; set; } public int? ModifierId { get; set; } [SkipValuesOnUpdate] public DateTime Created { get; set; } public DateTime Modified { get; set; } [NotColumn, System.ComponentModel.DataAnnotations.Schema.NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public virtual MeasuringStatus MeasuringStatus => IsMeasured ? MeasuringStatus.Finnished : Id > 0 ? MeasuringStatus.Started : MeasuringStatus.NotStarted; public abstract void SetParentPropToNull(); public void SetForeignKey(int foreignKey) => ForeignItemId = foreignKey; public virtual double CalculateNetWeight() => double.Round(GrossWeight - PalletWeight - (TareWeight * TrayQuantity), 1); /// /// Nem lehet nullánál kisebb "Weight" érték és a ShippingId, Quantity nagyobb mint nulla! Megengedőbb mint az IsValidMeasuringValues(bool isMeasurable)... /// /// public virtual bool IsValidSafeMeasuringValues() { return TrayQuantity > 0 && TareWeight >= 0 && PalletWeight >= 0 && NetWeight >= 0 && GrossWeight >= 0; } /// /// "Szigorúbb" mint az IsValidSafeMeasuringValues() /// /// /// public virtual bool IsValidMeasuringValues(bool isMeasurable) { return TrayQuantity > 0 && ((!isMeasurable && NetWeight == 0 && GrossWeight == 0 && PalletWeight == 0 && TareWeight == 0) || (isMeasurable && NetWeight > 0 && GrossWeight > 0 && PalletWeight >= 0 && TareWeight >= 0)); } public bool IsMeasuredAndValid(bool isMeasurable) { return Id > 0 && IsMeasured && IsValidMeasuringValues(isMeasurable); } public virtual void SetupCustomItemPalletMeauringValues(bool isMeasurable) { if (!isMeasurable) { TareWeight = 0; PalletWeight = 0; GrossWeight = 0; } IsMeasured = IsValidMeasuringValues(isMeasurable); } public override string ToString() { return $"{base.ToString()} [ForeignItemId: {ForeignItemId}; IsMeasured: {IsMeasured}; PalletWeight: {PalletWeight}; TareWeight: {TareWeight}; Quantity: {TrayQuantity}; NetWeight: {NetWeight}; GrossWeight: {GrossWeight}]"; } }