StockSummary.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Runtime.CompilerServices;
  6. using InABox.Core;
  7. namespace Comal.Classes
  8. {
  9. public class StockSummaryBOMAggregate : CoreAggregate<StockSummary, JobBillOfMaterialsItem, double>
  10. {
  11. public override Expression<Func<JobBillOfMaterialsItem, double>> Aggregate => x => x.Quantity;
  12. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  13. public override Dictionary<Expression<Func<JobBillOfMaterialsItem, object>>, Expression<Func<StockSummary, object>>> Links =>
  14. new Dictionary<Expression<Func<JobBillOfMaterialsItem, object>>, Expression<Func<StockSummary, object>>>()
  15. {
  16. { JobBillOfMaterialsItem => JobBillOfMaterialsItem.Job.ID, summary => summary.Job.ID },
  17. { JobBillOfMaterialsItem => JobBillOfMaterialsItem.Product.ID, summary => summary.Product.ID },
  18. { JobBillOfMaterialsItem => JobBillOfMaterialsItem.Style.ID, summary => summary.Style.ID },
  19. { JobBillOfMaterialsItem => JobBillOfMaterialsItem.Dimensions.UnitSize, summary => summary.Dimensions.UnitSize }
  20. };
  21. public override Filter<JobBillOfMaterialsItem> Filter =>
  22. new Filter<JobBillOfMaterialsItem>(x => x.BillOfMaterials.Approved).IsNotEqualTo(DateTime.MinValue);
  23. }
  24. public class StockSummaryIssuedAggregate : CoreAggregate<StockSummary, StockMovement, double>
  25. {
  26. public override Expression<Func<StockMovement, double>> Aggregate => x => x.Issued;
  27. public override Filter<StockMovement> Filter => new Filter<StockMovement>(x => x.IsTransfer).IsEqualTo(false);
  28. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  29. public override Dictionary<Expression<Func<StockMovement, object>>, Expression<Func<StockSummary, object>>> Links =>
  30. new Dictionary<Expression<Func<StockMovement, object>>, Expression<Func<StockSummary, object>>>()
  31. {
  32. { StockMovement => StockMovement.Job.ID, summary => summary.Job.ID },
  33. { StockMovement => StockMovement.Product.ID, summary => summary.Product.ID },
  34. { StockMovement => StockMovement.Style.ID, summary => summary.Style.ID },
  35. { StockMovement => StockMovement.Dimensions.UnitSize, summary => summary.Dimensions.UnitSize }
  36. };
  37. }
  38. public class StockSummaryTotalRequiredFormula : IFormula<StockSummary, double>
  39. {
  40. public Expression<Func<StockSummary, double>> Value => x => x.BillOfMaterials;
  41. public Expression<Func<StockSummary, double>>[] Modifiers => new Expression<Func<StockSummary, double>>[]
  42. {
  43. x => x.Issued
  44. };
  45. public FormulaOperator Operator => FormulaOperator.Subtract;
  46. public FormulaType Type => FormulaType.Virtual;
  47. }
  48. public class StockSummaryNettRequiredFormula : IFormula<StockSummary, double>
  49. {
  50. public Expression<Func<StockSummary, double>> Value => x => x.TotalRequired;
  51. public Expression<Func<StockSummary, double>>[] Modifiers => new Expression<Func<StockSummary, double>>[]
  52. {
  53. x => 0.0
  54. };
  55. public FormulaOperator Operator => FormulaOperator.Maximum;
  56. public FormulaType Type => FormulaType.Virtual;
  57. }
  58. public class StockSummaryAllStockAggregate : CoreAggregate<StockSummary, StockMovement, double>
  59. {
  60. public override Expression<Func<StockMovement, double>> Aggregate => x => x.Units;
  61. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  62. public override Dictionary<Expression<Func<StockMovement, object>>, Expression<Func<StockSummary, object>>> Links =>
  63. new Dictionary<Expression<Func<StockMovement, object>>, Expression<Func<StockSummary, object>>>()
  64. {
  65. { StockMovement => StockMovement.Product.ID, summary => summary.Product.ID },
  66. { StockMovement => StockMovement.Style.ID, summary => summary.Style.ID },
  67. { StockMovement => StockMovement.Dimensions.UnitSize, summary => summary.Dimensions.UnitSize }
  68. };
  69. }
  70. public class StockSummaryOrderAggregate : CoreAggregate<StockSummary, PurchaseOrderItem, double>
  71. {
  72. public override Expression<Func<PurchaseOrderItem, double>> Aggregate => x => x.Qty;
  73. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  74. public override Dictionary<Expression<Func<PurchaseOrderItem, object>>, Expression<Func<StockSummary, object>>> Links =>
  75. new Dictionary<Expression<Func<PurchaseOrderItem, object>>, Expression<Func<StockSummary, object>>>()
  76. {
  77. { PurchaseOrderItem => PurchaseOrderItem.Product.ID, summary => summary.Product.ID },
  78. { PurchaseOrderItem => PurchaseOrderItem.Style.ID, summary => summary.Style.ID },
  79. { PurchaseOrderItem => PurchaseOrderItem.Dimensions.UnitSize, summary => summary.Dimensions.UnitSize }
  80. };
  81. public override Filter<PurchaseOrderItem> Filter => new Filter<PurchaseOrderItem>(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue);
  82. }
  83. public class StockSummaryTotalStockFormula : IFormula<StockSummary, double>
  84. {
  85. public Expression<Func<StockSummary, double>> Value => x => x.AllStock;
  86. public Expression<Func<StockSummary, double>>[] Modifiers => new Expression<Func<StockSummary, double>>[]
  87. {
  88. x => x.OnOrder
  89. };
  90. public FormulaOperator Operator => FormulaOperator.Add;
  91. public FormulaType Type => FormulaType.Virtual;
  92. }
  93. public class StockSummaryBalanceAvailableFormula : IFormula<StockSummary, double>
  94. {
  95. public Expression<Func<StockSummary, double>> Value => x => x.TotalStock;
  96. public Expression<Func<StockSummary, double>>[] Modifiers => new Expression<Func<StockSummary, double>>[]
  97. {
  98. x => x.Product.MinimumStockLevel,
  99. x => x.NettRequired
  100. };
  101. public FormulaOperator Operator => FormulaOperator.Subtract;
  102. public FormulaType Type => FormulaType.Virtual;
  103. }
  104. public class StockSummaryUnionGenerator : AutoEntityUnionGenerator<IJobMaterial>
  105. {
  106. protected override void Configure()
  107. {
  108. // BOM
  109. AddTable<JobBillOfMaterialsItem>();
  110. // Stock Holdings
  111. AddTable<StockMovement>();
  112. // On Order
  113. AddTable<PurchaseOrderItem>();
  114. }
  115. public override bool Distinct => true;
  116. public override Column<IJobMaterial>[] IDColumns => new Column<IJobMaterial>[]
  117. {
  118. new Column<IJobMaterial>(x => x.Job.ID),
  119. new Column<IJobMaterial>(x => x.Product.ID),
  120. new Column<IJobMaterial>(x => x.Style.ID),
  121. new Column<IJobMaterial>(x => x.Dimensions.Unit.ID),
  122. new Column<IJobMaterial>(x => x.Dimensions.Quantity),
  123. new Column<IJobMaterial>(x => x.Dimensions.Length),
  124. new Column<IJobMaterial>(x => x.Dimensions.Width),
  125. new Column<IJobMaterial>(x => x.Dimensions.Height),
  126. new Column<IJobMaterial>(x => x.Dimensions.Weight)
  127. };
  128. }
  129. [UserTracking(typeof(Product))]
  130. [AutoEntity(typeof(StockSummaryUnionGenerator))]
  131. public class StockSummary : StockEntity, IJobMaterial, IRemotable, IPersistent, ILicense<WarehouseLicense>
  132. {
  133. [NullEditor]
  134. public JobLink Job { get; set; }
  135. [EditorSequence(1)]
  136. public override ProductLink Product { get; set; }
  137. [EditorSequence(2)]
  138. public ProductStyleLink Style { get; set; }
  139. [EditorSequence(3)]
  140. [RequiredColumn]
  141. [DimensionsEditor(typeof(StockDimensions), AllowEditingUnit = false)]
  142. public override StockDimensions Dimensions { get; set; }
  143. [EditorSequence(4)]
  144. [DoubleEditor]
  145. [Aggregate(typeof(StockSummaryBOMAggregate))]
  146. public double BillOfMaterials { get; set; }
  147. [EditorSequence(5)]
  148. [Aggregate(typeof(StockSummaryIssuedAggregate))]
  149. [DoubleEditor]
  150. public double Issued { get; set; }
  151. [EditorSequence(6)]
  152. [Formula(typeof(StockSummaryTotalRequiredFormula))]
  153. public double TotalRequired { get; set; }
  154. [EditorSequence(7)]
  155. [Formula(typeof(StockSummaryNettRequiredFormula))]
  156. public double NettRequired { get; set; }
  157. [EditorSequence(8)]
  158. [Aggregate(typeof(StockSummaryAllStockAggregate))]
  159. [DoubleEditor]
  160. public double AllStock { get; set; }
  161. [EditorSequence(9)]
  162. [DoubleEditor]
  163. [Aggregate(typeof(StockSummaryOrderAggregate))]
  164. public double OnOrder { get; set; }
  165. [EditorSequence(10)]
  166. [Formula(typeof(StockSummaryTotalStockFormula))]
  167. [DoubleEditor]
  168. public double TotalStock { get; set; }
  169. [EditorSequence(11)]
  170. [DoubleEditor]
  171. [Formula(typeof(StockSummaryBalanceAvailableFormula))]
  172. public double BalanceAvailable { get; set; }
  173. }
  174. }