StockMovement.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 : DimensionedEntity<StockDimensions>, 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. public ProductLink Product { get; set; }
  57. [EditorSequence(2)]
  58. [EntityRelationship(DeleteAction.SetNull)]
  59. public ProductStyleLink Style { get; set; }
  60. [EntityRelationship(DeleteAction.SetNull)]
  61. public StockLocationLink Location { get; set; }
  62. [DoubleEditor]
  63. [EditorSequence(3)]
  64. public double Received { get; set; }
  65. [DoubleEditor]
  66. [EditorSequence(3)]
  67. public double Issued { get; set; }
  68. // Units = Received - Issued
  69. [Formula(typeof(StockMovementUnitsFormula))]
  70. [EditorSequence(4)]
  71. public double Units { get; set; }
  72. [EditorSequence(5)]
  73. public override StockDimensions Dimensions { get; set; }
  74. // IsRemnant = Dimensions.Value < Product.Dimensions.Value
  75. [CheckBoxEditor(Editable = Editable.Hidden)]
  76. [Condition(typeof(StockMovementIsRemnantCondition))]
  77. [EditorSequence(6)]
  78. public bool IsRemnant { get; set; }
  79. // Qty = Units * Dimensions.Value
  80. [EditorSequence(7)]
  81. [Formula(typeof(StockMovementQuantityFormula))]
  82. [DoubleEditor(Editable = Editable.Hidden)]
  83. public double Qty { get; set; }
  84. [EditorSequence(8)]
  85. [EntityRelationship(DeleteAction.SetNull)]
  86. public JobLink Job { get; set; }
  87. [EditorSequence(9)]
  88. [EntityRelationship(DeleteAction.SetNull)]
  89. public EmployeeLink Employee { get; set; }
  90. [MemoEditor]
  91. [EditorSequence(10)]
  92. public string Notes { get; set; }
  93. [NullEditor]
  94. public Guid Transaction { get; set; }
  95. [NullEditor]
  96. public bool System { get; set; }
  97. [NullEditor]
  98. public bool IsTransfer { get; set; }
  99. [NullEditor]
  100. public PurchaseOrderItemLink OrderItem { get; set; }
  101. [NullEditor]
  102. public JobRequisitionItemLink JobRequisitionItem { get; set; }
  103. [Aggregate(typeof(StockMovementDocumentCount))]
  104. [NullEditor]
  105. public int Documents { get; set; }
  106. /// <summary>
  107. /// Used to Group together movements (particularly images)
  108. /// when transactions are uploaded from Mobile Devices
  109. /// </summary>
  110. [NullEditor]
  111. [EntityRelationship(DeleteAction.Cascade)]
  112. public StockMovementBatchLink Batch { get; set; }
  113. [NullEditor]
  114. [Obsolete("Replaced with Dimensions", true)]
  115. public double UnitSize { get; set; }
  116. [CurrencyEditor(Visible = Visible.Default)]
  117. [EditorSequence(11)]
  118. public double Cost { get; set; }
  119. protected override void Init()
  120. {
  121. base.Init();
  122. Product = new ProductLink();
  123. Product.PropertyChanged += Product_PropertyChanged;
  124. Style = new ProductStyleLink();
  125. Location = new StockLocationLink();
  126. Employee = new EmployeeLink();
  127. Job = new JobLink();
  128. Transaction = Guid.NewGuid();
  129. IsTransfer = false;
  130. OrderItem = new PurchaseOrderItemLink();
  131. Batch = new StockMovementBatchLink();
  132. JobRequisitionItem = new JobRequisitionItemLink();
  133. Dimensions = new StockDimensions();
  134. Cost = 0.0;
  135. }
  136. private void Product_PropertyChanged(object sender, PropertyChangedEventArgs e)
  137. {
  138. // if (new Column<Product>(x=>x.UnitSize).IsEqualTo(e.PropertyName))
  139. // UnitSize = Product.UnitSize;
  140. // else if (new Column<Product>(x=>x.DefaultStyle).IsEqualTo(e.PropertyName))
  141. // Style = Product.DefaultStyle;
  142. }
  143. private static Column<StockMovement> unitsize = new Column<StockMovement>(x => x.Dimensions.Value);
  144. private static Column<StockMovement> issued = new Column<StockMovement>(x => x.Issued);
  145. private static Column<StockMovement> received = new Column<StockMovement>(x => x.Received);
  146. private static Column<StockMovement> dimensions = new Column<StockMovement>(x => x.Product.Dimensions);
  147. private bool bChanging;
  148. protected override void DoPropertyChanged(string name, object before, object after)
  149. {
  150. if (bChanging)
  151. return;
  152. if (unitsize.IsEqualTo(name))
  153. {
  154. bChanging = true;
  155. Qty = (Received - Issued) * (double)after;
  156. bChanging = false;
  157. }
  158. else if (issued.IsEqualTo(name))
  159. {
  160. bChanging = true;
  161. Units = (Received - (double)after);
  162. Qty = Units * Dimensions.Value;
  163. bChanging = false;
  164. }
  165. else if (received.IsEqualTo(name))
  166. {
  167. bChanging = true;
  168. Units = ((double)after - Issued);
  169. Qty = Units * Dimensions.Value;
  170. bChanging = false;
  171. }
  172. else if (dimensions.IsParentOf(name))
  173. {
  174. String col = String.Join(".", name.Split('.').Skip(1));
  175. CoreUtils.SetPropertyValue(this, col, after);
  176. Qty = Units * Dimensions.Value;
  177. }
  178. else
  179. base.DoPropertyChanged(name, before, after);
  180. }
  181. }
  182. }