StockMovement.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Core;
  6. using PRSClasses;
  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 StockMovementValueFormula : IFormula<StockMovement, double>
  25. {
  26. public Expression<Func<StockMovement, double>> Value => x => x.Units;
  27. public Expression<Func<StockMovement, double>>[] Modifiers => new Expression<Func<StockMovement, double>>[] { x => x.Cost };
  28. public FormulaOperator Operator => FormulaOperator.Multiply;
  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. [UserTracking("Warehousing")]
  39. public class StockMovement : StockEntity, IRemotable, IPersistent, IOneToMany<StockLocation>, IOneToMany<Product>,
  40. ILicense<WarehouseLicense>, IStockHolding, IJobMaterial, IExportable, IImportable, IPostable
  41. {
  42. [DateTimeEditor]
  43. [EditorSequence(0)]
  44. [SecondaryIndex]
  45. public DateTime Date { get; set; }
  46. private class ProductLookupGenerator : LookupDefinitionGenerator<Product, StockMovement>
  47. {
  48. public override Filter<Product>? DefineFilter(StockMovement[] items)
  49. => LookupFactory.DefineFilter<Product>().And(x => x.NonStock).IsEqualTo(false);
  50. }
  51. [EditorSequence(1)]
  52. [EntityRelationship(DeleteAction.Cascade)]
  53. [RequiredColumn]
  54. [LookupDefinition(typeof(ProductLookupGenerator))]
  55. public override ProductLink Product { get; set; }
  56. [EditorSequence(2)]
  57. [RequiredColumn]
  58. [DimensionsEditor(typeof(StockDimensions))]
  59. public override StockDimensions Dimensions { get; set; }
  60. [EditorSequence(3)]
  61. [EntityRelationship(DeleteAction.SetNull)]
  62. public ProductStyleLink Style { get; set; }
  63. // Allowed to be negative.
  64. [DoubleEditor(Summary = Summary.Sum)]
  65. [EditorSequence(4)]
  66. public double Received { get; set; }
  67. [DoubleEditor(Summary = Summary.Sum)]
  68. [EditorSequence(5)]
  69. public double Issued { get; set; }
  70. /// <summary>
  71. /// This indicates the balance of the holding at the current point in time for a stock movement, populated for stock takes.
  72. /// </summary>
  73. [DoubleEditor]
  74. [EditorSequence(6)]
  75. public double Balance { get; set; }
  76. private class StockMovementUnitsFormula : ComplexFormulaGenerator<StockMovement, double>
  77. {
  78. public override IComplexFormulaNode<StockMovement, double> GetFormula() =>
  79. Formula(FormulaOperator.Subtract, Property(x => x.Received), Property(x => x.Issued));
  80. }
  81. [ComplexFormula(typeof(StockMovementUnitsFormula))]
  82. [EditorSequence(7)]
  83. [DoubleEditor(Visible=Visible.Optional, Editable = Editable.Hidden, Summary= Summary.Sum)]
  84. /// <summary>
  85. /// Units = Received - Issued
  86. /// </summary>
  87. public double Units { get; set; }
  88. private class IsRemnantCondition : ComplexFormulaGenerator<StockMovement, bool>
  89. {
  90. public override IComplexFormulaNode<StockMovement, bool> GetFormula() =>
  91. If<double>(
  92. x => x.Property(x => x.Dimensions.Value),
  93. Condition.LessThan,
  94. x => x.Property(x => x.Product.DefaultInstance.Dimensions.Value))
  95. .Then(Constant(true))
  96. .Else(Constant(false));
  97. }
  98. // IsRemnant = Dimensions.Value < Product.Dimensions.Value
  99. [CheckBoxEditor(Editable = Editable.Hidden)]
  100. [ComplexFormula(typeof(IsRemnantCondition))]
  101. [EditorSequence(7)]
  102. public bool IsRemnant { get; set; }
  103. // Qty = Units * Dimensions.Value
  104. [EditorSequence(8)]
  105. [Formula(typeof(StockMovementQuantityFormula))]
  106. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  107. public double Qty { get; set; }
  108. [CurrencyEditor(Visible = Visible.Default)]
  109. [EditorSequence(9)]
  110. public double Cost { get; set; } = 0.0;
  111. [EditorSequence(10)]
  112. [EntityRelationship(DeleteAction.SetNull)]
  113. public StockLocationLink Location { get; set; }
  114. [EditorSequence(11)]
  115. [EntityRelationship(DeleteAction.SetNull)]
  116. public JobLink Job { get; set; }
  117. [MemoEditor]
  118. [EditorSequence(12)]
  119. public string Notes { get; set; }
  120. [EditorSequence(13)]
  121. [EnumLookupEditor(typeof(StockMovementType), Visible = Visible.Default)]
  122. public StockMovementType Type { get; set; }
  123. [EditorSequence(14)]
  124. [EntityRelationship(DeleteAction.SetNull)]
  125. public EmployeeLink Employee { get; set; }
  126. [NullEditor]
  127. public Guid Transaction { get; set; } = Guid.NewGuid();
  128. [NullEditor]
  129. public bool System { get; set; }
  130. [NullEditor]
  131. [Obsolete("Replaced with Type", true)]
  132. public bool IsTransfer { get; set; } = false;
  133. [NullEditor]
  134. public PurchaseOrderItemLink OrderItem { get; set; }
  135. [RequiredColumn]
  136. public JobRequisitionItemLink JobRequisitionItem { get; set; }
  137. [NullEditor]
  138. public InvoiceLink Invoice { get; set; }
  139. private class JobScopeLookup : LookupDefinitionGenerator<JobScope, StockMovement>
  140. {
  141. public override Filter<JobScope> DefineFilter(StockMovement[] items)
  142. {
  143. var item = items?.Length == 1 ? items[0] : null;
  144. if (item != null)
  145. return new Filter<JobScope>(x => x.Job.ID).IsEqualTo(item.Job.ID).And(x => x.Status.Approved).IsEqualTo(true);
  146. return new Filter<JobScope>(x => x.ID).None();
  147. }
  148. public override Columns<StockMovement> DefineFilterColumns()
  149. => Columns.None<StockMovement>().Add(x=>x.Job.ID);
  150. }
  151. [LookupDefinition(typeof(JobScopeLookup))]
  152. [EditorSequence(5)]
  153. [EntityRelationship(DeleteAction.SetNull)]
  154. public JobScopeLink JobScope { get; set; }
  155. public ActualCharge Charge { get; set; }
  156. [Aggregate(typeof(StockMovementDocumentCount))]
  157. [NullEditor]
  158. public int Documents { get; set; }
  159. /// <summary>
  160. /// Used to Group together movements (particularly images)
  161. /// when transactions are uploaded from Mobile Devices
  162. /// </summary>
  163. [EntityRelationship(DeleteAction.Cascade)]
  164. public StockMovementBatchLink Batch { get; set; }
  165. [NullEditor]
  166. [Obsolete("Replaced with Dimensions", true)]
  167. public double UnitSize { get; set; }
  168. /// <summary>
  169. /// Value of a stock movement, equal to <see cref="Units"/> * <see cref="Cost"/>.
  170. /// </summary>
  171. [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary=Summary.Sum)]
  172. [Formula(typeof(StockMovementValueFormula))]
  173. public double Value { get; set; } = 0.0;
  174. [NullEditor]
  175. [LoggableProperty]
  176. public DateTime Posted { get; set; }
  177. [NullEditor]
  178. [LoggableProperty]
  179. [RequiredColumn]
  180. public PostedStatus PostedStatus { get; set; }
  181. [NullEditor]
  182. public string PostedNote { get; set; }
  183. [NullEditor]
  184. public string PostedReference { get; set; }
  185. static StockMovement()
  186. {
  187. StockEntity.LinkStockDimensions<StockMovement>();
  188. LinkedProperties.Register<StockMovement, ProductStyleLink, Guid>(x=>x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  189. //LinkedProperties.Register<StockMovement, ProductLink, double>(x => x.Product, x => x.AverageCost, x => x.Cost);
  190. LinkedProperties.Register<StockMovement, JobScopeLink, Guid>(ass => ass.Job.DefaultScope, scope => scope.ID, ass => ass.JobScope.ID);
  191. LinkedProperties.Register<StockMovement, JobScopeLink, String>(ass => ass.Job.DefaultScope, scope => scope.Number, ass => ass.JobScope.Number);
  192. LinkedProperties.Register<StockMovement, JobScopeLink, String>(ass => ass.Job.DefaultScope, scope => scope.Description, ass => ass.JobScope.Description);
  193. }
  194. private static Column<StockMovement> unitsize = new Column<StockMovement>(x => x.Dimensions.Value);
  195. private static Column<StockMovement> issued = new Column<StockMovement>(x => x.Issued);
  196. private static Column<StockMovement> received = new Column<StockMovement>(x => x.Received);
  197. private bool bChanging;
  198. protected override void DoPropertyChanged(string name, object? before, object? after)
  199. {
  200. if (bChanging)
  201. return;
  202. if (unitsize.IsEqualTo(name))
  203. {
  204. bChanging = true;
  205. Qty = (Received - Issued) * (double)after;
  206. bChanging = false;
  207. }
  208. else if (issued.IsEqualTo(name))
  209. {
  210. bChanging = true;
  211. Units = Received - (double)after;
  212. Qty = Units * Dimensions.Value;
  213. bChanging = false;
  214. }
  215. else if (received.IsEqualTo(name))
  216. {
  217. bChanging = true;
  218. Units = ((double)after - Issued);
  219. Qty = Units * Dimensions.Value;
  220. bChanging = false;
  221. }
  222. else
  223. base.DoPropertyChanged(name, before, after);
  224. }
  225. }
  226. }