StockMovement.cs 7.6 KB

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