using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using InABox.Core; namespace Comal.Classes { public class StockMovementDocumentCount : CoreAggregate { public override Expression> Aggregate => x => x.ID; public override AggregateCalculation Calculation => AggregateCalculation.Count; public override Dictionary>, Expression>> Links => new Dictionary>, Expression>>() { { StockMovementBatchDocument => StockMovementBatchDocument.EntityLink.ID, StockMovement => StockMovement.Batch.ID } }; } public class StockMovementLink : EntityLink { [NullEditor] public override Guid ID { get; set; } } public class StockMovementUnitsFormula : IFormula { public Expression> Value => x => x.Received; public Expression>[] Modifiers => new Expression>[] { x => x.Issued }; public FormulaOperator Operator => FormulaOperator.Subtract; public FormulaType Type => FormulaType.Virtual; } public class StockMovementQuantityFormula : IFormula { public Expression> Value => x => x.Units; public Expression>[] Modifiers => new Expression>[] { x => x.Dimensions.Value }; public FormulaOperator Operator => FormulaOperator.Multiply; public FormulaType Type => FormulaType.Virtual; } public class StockMovementIsRemnantCondition : ICondition { public Expression> Left => x => x.Dimensions.Value; public Condition Condition => Condition.LessThan; public Expression> Right => x => x.Product.Dimensions.Value; public Expression> True => x => true; public Expression> False => x => null; public ConditionType Type => ConditionType.Virtual; } [UserTracking("Warehousing")] public class StockMovement : StockEntity, IRemotable, IPersistent, IOneToMany, IOneToMany, ILicense, IStockHolding, IJobMaterial, IExportable, IImportable { [DateTimeEditor] [EditorSequence(0)] public DateTime Date { get; set; } [EditorSequence(1)] [EntityRelationship(DeleteAction.Cascade)] [RequiredColumn] public override ProductLink Product { get; set; } [EditorSequence(2)] [EntityRelationship(DeleteAction.SetNull)] public ProductStyleLink Style { get; set; } [EntityRelationship(DeleteAction.SetNull)] public StockLocationLink Location { get; set; } [DoubleEditor] [EditorSequence(3)] public double Received { get; set; } [DoubleEditor] [EditorSequence(3)] public double Issued { get; set; } // Units = Received - Issued [Formula(typeof(StockMovementUnitsFormula))] [EditorSequence(4)] [DoubleEditor(Visible=Visible.Optional, Editable = Editable.Hidden)] public double Units { get; set; } [EditorSequence(5)] [RequiredColumn] public override StockDimensions Dimensions { get; set; } // IsRemnant = Dimensions.Value < Product.Dimensions.Value [CheckBoxEditor(Editable = Editable.Hidden)] [Condition(typeof(StockMovementIsRemnantCondition))] [EditorSequence(6)] public bool IsRemnant { get; set; } // Qty = Units * Dimensions.Value [EditorSequence(7)] [Formula(typeof(StockMovementQuantityFormula))] [DoubleEditor(Editable = Editable.Hidden)] public double Qty { get; set; } [EditorSequence(8)] [EntityRelationship(DeleteAction.SetNull)] public JobLink Job { get; set; } [EditorSequence(9)] [EntityRelationship(DeleteAction.SetNull)] public EmployeeLink Employee { get; set; } [MemoEditor] [EditorSequence(10)] public string Notes { get; set; } [NullEditor] public Guid Transaction { get; set; } [NullEditor] public bool System { get; set; } [NullEditor] public bool IsTransfer { get; set; } [NullEditor] public PurchaseOrderItemLink OrderItem { get; set; } [NullEditor] public JobRequisitionItemLink JobRequisitionItem { get; set; } [Aggregate(typeof(StockMovementDocumentCount))] [NullEditor] public int Documents { get; set; } /// /// Used to Group together movements (particularly images) /// when transactions are uploaded from Mobile Devices /// [NullEditor] [EntityRelationship(DeleteAction.Cascade)] public StockMovementBatchLink Batch { get; set; } [NullEditor] [Obsolete("Replaced with Dimensions", true)] public double UnitSize { get; set; } [CurrencyEditor(Visible = Visible.Default)] [EditorSequence(11)] public double Cost { get; set; } protected override void Init() { base.Init(); Style = new ProductStyleLink(() => this); Location = new StockLocationLink(); Employee = new EmployeeLink(); Job = new JobLink(); Transaction = Guid.NewGuid(); IsTransfer = false; OrderItem = new PurchaseOrderItemLink(); Batch = new StockMovementBatchLink(); JobRequisitionItem = new JobRequisitionItemLink(); Cost = 0.0; } static StockMovement() { StockEntity.LinkStockDimensions(); LinkedProperties.Register(x=>x.Product.DefaultStyle, x => x.ID, x => x.Style.ID); } private static Column unitsize = new Column(x => x.Dimensions.Value); private static Column issued = new Column(x => x.Issued); private static Column received = new Column(x => x.Received); private bool bChanging; protected override void DoPropertyChanged(string name, object before, object after) { if (bChanging) return; if (unitsize.IsEqualTo(name)) { bChanging = true; Qty = (Received - Issued) * (double)after; bChanging = false; } else if (issued.IsEqualTo(name)) { bChanging = true; Units = (Received - (double)after); Qty = Units * Dimensions.Value; bChanging = false; } else if (received.IsEqualTo(name)) { bChanging = true; Units = ((double)after - Issued); Qty = Units * Dimensions.Value; bChanging = false; } else base.DoPropertyChanged(name, before, after); } } }