JobRequisitionItem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using PRSClasses;
  9. namespace Comal.Classes
  10. {
  11. public enum JobRequisitionItemStatus
  12. {
  13. NotChecked, // Default
  14. /// <summary>
  15. /// All required stock has been received, and is in the correct <see cref="ProductStyle"/>.
  16. /// </summary>
  17. /// <remarks>
  18. /// This can be set even if there are unreceived <see cref="JobRequisitionItemPurchaseOrderItem"/>,
  19. /// since if we got the stock some other way, we still think of it as allocated.
  20. /// </remarks>
  21. Allocated,
  22. /// <summary>
  23. /// All required stock has been received, but some is not in the correct <see cref="ProductStyle"/>, meaning a treatment is required.
  24. /// </summary>
  25. /// <remarks>
  26. /// This can be set even if there are unreceived <see cref="JobRequisitionItemPurchaseOrderItem"/>,
  27. /// since if we got the stock some other way, we still think of it as having the stock allocated.
  28. /// </remarks>
  29. TreatmentRequired,
  30. /// <summary>
  31. /// The <see cref="JobRequisitionItem.OrderRequired"/> has been set, but there are no <see cref="JobRequisitionItemPurchaseOrderItem"/>s for
  32. /// this <see cref="JobRequisitionItem"/>.
  33. /// </summary>
  34. OrderRequired,
  35. /// <summary>
  36. /// We don't yet have all the stock, and there is at least one unreceived <see cref="JobRequisitionItemPurchaseOrderItem"/> of type
  37. /// <see cref="JobRequisitionItemPurchaseOrderItemType.Stock"/>.
  38. /// </summary>
  39. OnOrder,
  40. /// <summary>
  41. /// We don't yet have all the stock, and there is at least one unreceived <see cref="JobRequisitionItemPurchaseOrderItem"/> of type
  42. /// <see cref="JobRequisitionItemPurchaseOrderItemType.Treatment"/> and none of type <see cref="JobRequisitionItemPurchaseOrderItemType.Stock"/>.
  43. /// </summary>
  44. TreatmentOnOrder,
  45. [Obsolete]
  46. Received,// Drop
  47. [Obsolete]
  48. TreatmentReceived,// Drop
  49. /// <summary>
  50. /// The <see cref="JobRequisitionItem"/> has been cancelled, meaning it has a non-empty <see cref="JobRequisitionItem.Cancelled"/>.
  51. /// </summary>
  52. Cancelled,
  53. /// <summary>
  54. /// The <see cref="JobRequisitionItem/"> has been archived, meaning it has a non-empty <see cref="JobRequisitionItem.Archived"/>.
  55. /// </summary>
  56. Archived
  57. }
  58. public class JobRequisitionItemTotalQtyFormula : IFormula<JobRequisitionItem, double>
  59. {
  60. public Expression<Func<JobRequisitionItem, double>> Value => x => x.Qty;
  61. public Expression<Func<JobRequisitionItem, double>>[] Modifiers => new Expression<Func<JobRequisitionItem, double>>[] { x => x.Dimensions.Value };
  62. public FormulaOperator Operator => FormulaOperator.Multiply;
  63. public FormulaType Type => FormulaType.Virtual;
  64. }
  65. [Caption("Items")]
  66. [UserTracking(typeof(Job))]
  67. public class JobRequisitionItem : StockEntity, IRemotable, IPersistent, IOneToMany<JobRequisition>,
  68. ILicense<ProjectManagementLicense>, IJobMaterial, ISequenceable
  69. {
  70. [EntityRelationship(DeleteAction.Cascade)]
  71. [Editable(Editable.Hidden)]
  72. public JobLink Job { get; set; }
  73. [EntityRelationship(DeleteAction.Cascade)]
  74. [Editable(Editable.Hidden)]
  75. public JobRequisitionLink Requisition { get; set; }
  76. [EntityRelationship(DeleteAction.SetNull)]
  77. [EditorSequence(1)]
  78. [RequiredColumn]
  79. public override ProductLink Product { get; set; }
  80. [EditorSequence(2)]
  81. public ProductStyleLink Style { get; set; }
  82. [NullEditor]
  83. [Obsolete("Replaced with Dimensions", true)]
  84. public double UnitSize { get; set; }
  85. [EditorSequence(3)]
  86. [RequiredColumn]
  87. [DimensionsEditor(typeof(StockDimensions), AllowEditingUnit = false)]
  88. public override StockDimensions Dimensions { get; set; }
  89. [EditorSequence(4)]
  90. public double Qty { get; set; }
  91. [DoubleEditor(Editable = Editable.Hidden)]
  92. [Formula(typeof(JobRequisitionItemTotalQtyFormula))]
  93. public double TotalQty { get; set; }
  94. [EditorSequence(5)]
  95. public double UnitCost { get; set; }
  96. [EditorSequence(6)]
  97. [CurrencyEditor(Summary = Summary.Sum)]
  98. public double TotalCost { get; set; }
  99. [EditorSequence(7)]
  100. [MemoEditor]
  101. public string Notes { get; set; }
  102. [EditorSequence(8)]
  103. public SupplierLink Supplier { get; set; }
  104. [EnumLookupEditor(typeof(JobRequisitionItemStatus))]
  105. public JobRequisitionItemStatus Status { get; set; } = JobRequisitionItemStatus.NotChecked;
  106. [EntityRelationship(DeleteAction.SetNull)]
  107. [RequiredColumn]
  108. [Obsolete("Replaced with JobRequisitionItemPurchaseOrderItem")]
  109. public PurchaseOrderItemLink PurchaseOrderItem { get; set; }
  110. [RequiredColumn]
  111. public DateTime Cancelled { get; set; } = DateTime.MinValue;
  112. [RequiredColumn]
  113. public DateTime Archived { get; set; } = DateTime.MinValue;
  114. [RequiredColumn]
  115. public DateTime Ordered { get; set; } = DateTime.MinValue;
  116. [RequiredColumn]
  117. public DateTime OrderRequired { get; set; } = DateTime.MinValue;
  118. [NullEditor]
  119. public long Sequence { get; set; }
  120. static JobRequisitionItem()
  121. {
  122. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  123. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  124. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  125. LinkedProperties.Register<JobRequisitionItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  126. x => x.UnitCost);
  127. StockEntity.LinkStockDimensions<JobRequisitionItem>();
  128. }
  129. private bool bChanging;
  130. protected override void DoPropertyChanged(string name, object? before, object? after)
  131. {
  132. if (bChanging)
  133. return;
  134. try
  135. {
  136. bChanging = true;
  137. if (name.Equals(nameof(Qty)) && after is double qty)
  138. TotalCost = UnitCost * qty;
  139. else if (name.Equals(nameof(UnitCost)) && after is double cost)
  140. TotalCost = cost * Qty;
  141. else if (name.Equals(nameof(TotalCost)) && after is double total)
  142. {
  143. if (Qty == 0)
  144. Qty = 1;
  145. UnitCost = total / Qty;
  146. }
  147. }
  148. finally
  149. {
  150. bChanging = false;
  151. }
  152. base.DoPropertyChanged(name, before, after);
  153. }
  154. public static void UpdateCosts(IEnumerable<JobRequisitionItem> items, Dictionary<String,object?> changes)
  155. {
  156. void UpdateValue<TType>(JobRequisitionItem item,Expression<Func<JobRequisitionItem, TType>> property, TType value)
  157. {
  158. CoreUtils.MonitorChanges(
  159. item,
  160. () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),
  161. changes
  162. );
  163. }
  164. var productids = items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  165. MultiQuery query = new MultiQuery();
  166. query.Add(
  167. new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),
  168. new Columns<SupplierProduct>(x=>x.Product.ID)
  169. .Add(x=>x.SupplierLink.ID)
  170. .Add(x=>x.Style.ID)
  171. .Add(x=>x.Dimensions.UnitSize)
  172. .Add(x=>x.Job.ID)
  173. .Add(x=>x.SupplierCode)
  174. .Add(x=>x.SupplierDescription)
  175. .Add(x=>x.CostPrice)
  176. );
  177. query.Add(
  178. new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),
  179. new Columns<ProductInstance>(x=>x.Product.ID)
  180. .Add(x => x.Style.ID)
  181. .Add(x => x.Dimensions.UnitSize)
  182. .Add(x => x.NettCost)
  183. );
  184. query.Query();
  185. foreach (var item in items)
  186. {
  187. //Check Supplier / Job Specific Pricing
  188. CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  189. (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
  190. && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)
  191. && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)
  192. && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  193. && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)
  194. );
  195. if (row != null)
  196. {
  197. UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));
  198. continue;
  199. }
  200. // Check Supplier Pricing
  201. row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  202. (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
  203. && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)
  204. && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)
  205. && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  206. && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)
  207. );
  208. if (row != null)
  209. {
  210. UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));
  211. continue;
  212. }
  213. // Check Specific Product Instance
  214. row = query.Get<ProductInstance>()?.Rows.FirstOrDefault(r =>
  215. (r.Get<ProductInstance, Guid>(c => c.Product.ID) == item.Product.ID)
  216. && (r.Get<ProductInstance, Guid>(c => c.Style.ID) == item.Style.ID)
  217. && (string.Equals(r.Get<ProductInstance,string>(c => c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  218. );
  219. if (row != null)
  220. UpdateValue<double>(item, x => x.UnitCost, row.Get<ProductInstance, double>(c => c.NettCost));
  221. else
  222. UpdateValue<double>(item, x => x.UnitCost, 0.0F);
  223. }
  224. }
  225. }
  226. }