StockMovement.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using InABox.Core;
  7. using PRSClasses;
  8. namespace Comal.Classes
  9. {
  10. public class StockMovementDocumentCount : CoreAggregate<StockMovement, StockMovementBatchDocument, Guid>
  11. {
  12. public override Expression<Func<StockMovementBatchDocument, Guid>> Aggregate => x => x.ID;
  13. public override AggregateCalculation Calculation => AggregateCalculation.Count;
  14. public override Dictionary<Expression<Func<StockMovementBatchDocument, object>>, Expression<Func<StockMovement, object>>> Links =>
  15. new Dictionary<Expression<Func<StockMovementBatchDocument, object>>, Expression<Func<StockMovement, object>>>()
  16. {
  17. { StockMovementBatchDocument => StockMovementBatchDocument.EntityLink.ID, StockMovement => StockMovement.Batch.ID }
  18. };
  19. }
  20. public class StockMovementLink : EntityLink<StockMovement>
  21. {
  22. [NullEditor]
  23. public override Guid ID { get; set; }
  24. }
  25. public class StockMovementUnitsFormula : IFormula<StockMovement, double>
  26. {
  27. public Expression<Func<StockMovement, double>> Value => x => x.Received;
  28. public Expression<Func<StockMovement, double>>[] Modifiers => new Expression<Func<StockMovement, double>>[] { x => x.Issued };
  29. public FormulaOperator Operator => FormulaOperator.Subtract;
  30. public FormulaType Type => FormulaType.Virtual;
  31. }
  32. public class StockMovementValueFormula : IFormula<StockMovement, double>
  33. {
  34. public Expression<Func<StockMovement, double>> Value => x => x.Units;
  35. public Expression<Func<StockMovement, double>>[] Modifiers => new Expression<Func<StockMovement, double>>[] { x => x.Cost };
  36. public FormulaOperator Operator => FormulaOperator.Multiply;
  37. public FormulaType Type => FormulaType.Virtual;
  38. }
  39. public class StockMovementQuantityFormula : IFormula<StockMovement, double>
  40. {
  41. public Expression<Func<StockMovement, double>> Value => x => x.Units;
  42. public Expression<Func<StockMovement, double>>[] Modifiers => new Expression<Func<StockMovement, double>>[] { x => x.Dimensions.Value };
  43. public FormulaOperator Operator => FormulaOperator.Multiply;
  44. public FormulaType Type => FormulaType.Virtual;
  45. }
  46. public class StockMovementIsRemnantCondition : ICondition<StockHolding, double, object>
  47. {
  48. public Expression<Func<StockHolding, double>> Left => x => x.Dimensions.Value;
  49. public Condition Condition => Condition.LessThan;
  50. public Expression<Func<StockHolding, double>> Right => x => x.Product.Dimensions.Value;
  51. public Expression<Func<StockHolding, object>> True => x => true;
  52. public Expression<Func<StockHolding, object>> False => x => null;
  53. public ConditionType Type => ConditionType.Virtual;
  54. }
  55. [UserTracking("Warehousing")]
  56. public class StockMovement : StockEntity, IRemotable, IPersistent, IOneToMany<StockLocation>, IOneToMany<Product>,
  57. ILicense<WarehouseLicense>, IStockHolding, IJobMaterial, IExportable, IImportable, IPostable
  58. {
  59. [DateTimeEditor]
  60. [EditorSequence(0)]
  61. public DateTime Date { get; set; }
  62. [EditorSequence(1)]
  63. [EntityRelationship(DeleteAction.Cascade)]
  64. [RequiredColumn]
  65. public override ProductLink Product { get; set; }
  66. [EditorSequence(2)]
  67. [EntityRelationship(DeleteAction.SetNull)]
  68. public ProductStyleLink Style { get; set; }
  69. [EntityRelationship(DeleteAction.SetNull)]
  70. public StockLocationLink Location { get; set; }
  71. [DoubleEditor(Summary = Summary.Sum)]
  72. [EditorSequence(3)]
  73. public double Received { get; set; }
  74. [DoubleEditor(Summary = Summary.Sum)]
  75. [EditorSequence(3)]
  76. public double Issued { get; set; }
  77. // Units = Received - Issued
  78. [Formula(typeof(StockMovementUnitsFormula))]
  79. [EditorSequence(4)]
  80. [DoubleEditor(Visible=Visible.Optional, Editable = Editable.Hidden, Summary= Summary.Sum)]
  81. public double Units { get; set; }
  82. [EditorSequence(5)]
  83. [RequiredColumn]
  84. [DimensionsEditor(typeof(StockDimensions), AllowEditingUnit = false)]
  85. public override StockDimensions Dimensions { get; set; }
  86. // IsRemnant = Dimensions.Value < Product.Dimensions.Value
  87. [CheckBoxEditor(Editable = Editable.Hidden)]
  88. [Condition(typeof(StockMovementIsRemnantCondition))]
  89. [EditorSequence(6)]
  90. public bool IsRemnant { get; set; }
  91. // Qty = Units * Dimensions.Value
  92. [EditorSequence(7)]
  93. [Formula(typeof(StockMovementQuantityFormula))]
  94. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  95. public double Qty { get; set; }
  96. [EditorSequence(8)]
  97. [EntityRelationship(DeleteAction.SetNull)]
  98. public JobLink Job { get; set; }
  99. [EditorSequence(9)]
  100. [EntityRelationship(DeleteAction.SetNull)]
  101. public EmployeeLink Employee { get; set; }
  102. [MemoEditor]
  103. [EditorSequence(10)]
  104. public string Notes { get; set; }
  105. [NullEditor]
  106. public Guid Transaction { get; set; } = Guid.NewGuid();
  107. [NullEditor]
  108. public bool System { get; set; }
  109. [NullEditor]
  110. public bool IsTransfer { get; set; } = false;
  111. [NullEditor]
  112. public PurchaseOrderItemLink OrderItem { get; set; }
  113. [NullEditor]
  114. public JobRequisitionItemLink JobRequisitionItem { get; set; }
  115. [Aggregate(typeof(StockMovementDocumentCount))]
  116. [NullEditor]
  117. public int Documents { get; set; }
  118. /// <summary>
  119. /// Used to Group together movements (particularly images)
  120. /// when transactions are uploaded from Mobile Devices
  121. /// </summary>
  122. [EntityRelationship(DeleteAction.Cascade)]
  123. public StockMovementBatchLink Batch { get; set; }
  124. [NullEditor]
  125. [Obsolete("Replaced with Dimensions", true)]
  126. public double UnitSize { get; set; }
  127. [CurrencyEditor(Visible = Visible.Default)]
  128. [EditorSequence(11)]
  129. public double Cost { get; set; } = 0.0;
  130. [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary=Summary.Sum)]
  131. [Formula(typeof(StockMovementValueFormula))]
  132. public double Value { get; set; } = 0.0;
  133. [NullEditor]
  134. [LoggableProperty]
  135. public DateTime Posted { get; set; }
  136. [NullEditor]
  137. [LoggableProperty]
  138. public PostedStatus PostedStatus { get; set; }
  139. [NullEditor]
  140. public string PostedNote { get; set; }
  141. [NullEditor]
  142. public string PostedReference { get; set; }
  143. static StockMovement()
  144. {
  145. StockEntity.LinkStockDimensions<StockMovement>();
  146. LinkedProperties.Register<StockMovement, ProductStyleLink, Guid>(x=>x.Product.DefaultStyle, x => x.ID, x => x.Style.ID);
  147. LinkedProperties.Register<StockMovement, ProductLink, double>(x => x.Product, x => x.AverageCost, x => x.Cost);
  148. }
  149. private static Column<StockMovement> unitsize = new Column<StockMovement>(x => x.Dimensions.Value);
  150. private static Column<StockMovement> issued = new Column<StockMovement>(x => x.Issued);
  151. private static Column<StockMovement> received = new Column<StockMovement>(x => x.Received);
  152. private bool bChanging;
  153. protected override void DoPropertyChanged(string name, object? before, object? after)
  154. {
  155. if (bChanging)
  156. return;
  157. if (unitsize.IsEqualTo(name))
  158. {
  159. bChanging = true;
  160. Qty = (Received - Issued) * (double)after;
  161. bChanging = false;
  162. }
  163. else if (issued.IsEqualTo(name))
  164. {
  165. bChanging = true;
  166. Units = (Received - (double)after);
  167. Qty = Units * Dimensions.Value;
  168. bChanging = false;
  169. }
  170. else if (received.IsEqualTo(name))
  171. {
  172. bChanging = true;
  173. Units = ((double)after - Issued);
  174. Qty = Units * Dimensions.Value;
  175. bChanging = false;
  176. }
  177. else
  178. base.DoPropertyChanged(name, before, after);
  179. }
  180. }
  181. }