StockMovement.cs 8.2 KB

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