StockMovement.cs 7.7 KB

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