JobRequisitionItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. [Aggregate(typeof(JobRequisitionItemPurchaseOrderNumberAggregate))]
  111. [TextBoxEditor(Editable = Editable.Hidden)]
  112. public string PurchaseOrderNumbers { get; set; }
  113. [RequiredColumn]
  114. public DateTime Cancelled { get; set; } = DateTime.MinValue;
  115. [RequiredColumn]
  116. public DateTime Archived { get; set; } = DateTime.MinValue;
  117. [RequiredColumn]
  118. [Obsolete("Replaced with JobRequisitionItemPurchaseOrderItem")]
  119. public DateTime Ordered { get; set; } = DateTime.MinValue;
  120. [RequiredColumn]
  121. public DateTime OrderRequired { get; set; } = DateTime.MinValue;
  122. [NullEditor]
  123. public long Sequence { get; set; }
  124. static JobRequisitionItem()
  125. {
  126. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  127. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  128. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  129. LinkedProperties.Register<JobRequisitionItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  130. x => x.UnitCost);
  131. StockEntity.LinkStockDimensions<JobRequisitionItem>();
  132. }
  133. private bool bChanging;
  134. protected override void DoPropertyChanged(string name, object? before, object? after)
  135. {
  136. if (bChanging)
  137. return;
  138. try
  139. {
  140. bChanging = true;
  141. if (name.Equals(nameof(Qty)) && after is double qty)
  142. TotalCost = UnitCost * qty;
  143. else if (name.Equals(nameof(UnitCost)) && after is double cost)
  144. TotalCost = cost * Qty;
  145. else if (name.Equals(nameof(TotalCost)) && after is double total)
  146. {
  147. if (Qty == 0)
  148. Qty = 1;
  149. UnitCost = total / Qty;
  150. }
  151. }
  152. finally
  153. {
  154. bChanging = false;
  155. }
  156. base.DoPropertyChanged(name, before, after);
  157. }
  158. public static void UpdateCosts(IEnumerable<JobRequisitionItem> items, Dictionary<String,object?> changes)
  159. {
  160. void UpdateValue<TType>(JobRequisitionItem item,Expression<Func<JobRequisitionItem, TType>> property, TType value)
  161. {
  162. CoreUtils.MonitorChanges(
  163. item,
  164. () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),
  165. changes
  166. );
  167. }
  168. var productids = items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  169. MultiQuery query = new MultiQuery();
  170. query.Add(
  171. new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),
  172. new Columns<SupplierProduct>(x=>x.Product.ID)
  173. .Add(x=>x.SupplierLink.ID)
  174. .Add(x=>x.Style.ID)
  175. .Add(x=>x.Dimensions.UnitSize)
  176. .Add(x=>x.Job.ID)
  177. .Add(x=>x.SupplierCode)
  178. .Add(x=>x.SupplierDescription)
  179. .Add(x=>x.CostPrice)
  180. );
  181. query.Add(
  182. new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),
  183. new Columns<ProductInstance>(x=>x.Product.ID)
  184. .Add(x => x.Style.ID)
  185. .Add(x => x.Dimensions.UnitSize)
  186. .Add(x => x.NettCost)
  187. );
  188. query.Query();
  189. foreach (var item in items)
  190. {
  191. //Check Supplier / Job Specific Pricing
  192. CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  193. (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
  194. && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)
  195. && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)
  196. && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  197. && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)
  198. );
  199. if (row != null)
  200. {
  201. UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));
  202. continue;
  203. }
  204. // Check Supplier Pricing
  205. row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  206. (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
  207. && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)
  208. && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)
  209. && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  210. && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)
  211. );
  212. if (row != null)
  213. {
  214. UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));
  215. continue;
  216. }
  217. // Check Specific Product Instance
  218. row = query.Get<ProductInstance>()?.Rows.FirstOrDefault(r =>
  219. (r.Get<ProductInstance, Guid>(c => c.Product.ID) == item.Product.ID)
  220. && (r.Get<ProductInstance, Guid>(c => c.Style.ID) == item.Style.ID)
  221. && (string.Equals(r.Get<ProductInstance,string>(c => c.Dimensions.UnitSize),item.Dimensions.UnitSize))
  222. );
  223. if (row != null)
  224. UpdateValue<double>(item, x => x.UnitCost, row.Get<ProductInstance, double>(c => c.NettCost));
  225. else
  226. UpdateValue<double>(item, x => x.UnitCost, 0.0F);
  227. }
  228. }
  229. }
  230. public class JobRequisitionItemPurchaseOrderNumberAggregate : CoreAggregate<JobRequisitionItem, JobRequisitionItemPurchaseOrderItem, string>
  231. {
  232. public override Expression<Func<JobRequisitionItemPurchaseOrderItem, string>> Aggregate => x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber;
  233. public override Dictionary<Expression<Func<JobRequisitionItemPurchaseOrderItem, object>>, Expression<Func<JobRequisitionItem, object>>> Links { get; } = new Dictionary<Expression<Func<JobRequisitionItemPurchaseOrderItem, object>>, Expression<Func<JobRequisitionItem, object>>>
  234. {
  235. { x => x.JobRequisitionItem.ID, x => x.ID }
  236. };
  237. public override AggregateCalculation Calculation => AggregateCalculation.Concat;
  238. }
  239. }