JobRequisitionItem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /// <summary>
  58. /// The <see cref="JobRequisitionItem/"> has been issued, meaning that it has been allocated, and there are stock movements of type <see cref="StockMovementType.Issue"/> adding up to the correct total.
  59. /// </summary>
  60. Issued
  61. }
  62. public interface IJobRequisitionItem : IEntity
  63. {
  64. }
  65. [Caption("Items")]
  66. [UserTracking(typeof(Job))]
  67. public class JobRequisitionItem : StockEntity, IRemotable, IPersistent, IOneToMany<JobRequisition>,
  68. ILicense<ProjectManagementLicense>, IJobMaterial, ISequenceable, IIssues, IJobRequisitionItem
  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. [RequiredColumn]
  82. public ProductStyleLink Style { get; set; }
  83. [NullEditor]
  84. [Obsolete("Replaced with Dimensions", true)]
  85. public double UnitSize { get; set; }
  86. [EditorSequence(3)]
  87. [RequiredColumn]
  88. [DimensionsEditor(typeof(StockDimensions))]
  89. public override StockDimensions Dimensions { get; set; }
  90. [EditorSequence(4)]
  91. [RequiredColumn]
  92. public double Qty { get; set; }
  93. private class TotalQtyFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  94. {
  95. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  96. Formula(FormulaOperator.Multiply, Property(x => x.Qty), Property(x => x.Dimensions.Value));
  97. }
  98. [DoubleEditor(Editable = Editable.Hidden)]
  99. [ComplexFormula(typeof(TotalQtyFormula))]
  100. public double TotalQty { get; set; }
  101. [EditorSequence(5)]
  102. public double UnitCost { get; set; }
  103. [EditorSequence(6)]
  104. [CurrencyEditor(Summary = Summary.Sum)]
  105. public double TotalCost { get; set; }
  106. [EditorSequence(7)]
  107. [MemoEditor]
  108. public string Notes { get; set; }
  109. [EditorSequence(8)]
  110. public SupplierLink Supplier { get; set; }
  111. [NullEditor]
  112. [EditorSequence(9)]
  113. [LoggableProperty]
  114. [RequiredColumn]
  115. public JobRequisitionItemStatus Status { get; set; } = JobRequisitionItemStatus.NotChecked;
  116. private class InStockFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  117. {
  118. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  119. Aggregate<StockMovement>(AggregateCalculation.Sum, x => x.Property(x => x.Units))
  120. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID);
  121. }
  122. [ComplexFormula(typeof(InStockFormula))]
  123. [DoubleEditor(Editable = Editable.Disabled)]
  124. [EditorSequence(10)]
  125. /// <summary>
  126. /// The amount of this requisition item that is currently in stock, which is an aggregate of the <see cref="StockMovement.Units"/> property.
  127. /// </summary>
  128. public double InStock { get; set; }
  129. private class TotalOrdersFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  130. {
  131. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  132. Aggregate<JobRequisitionItemPurchaseOrderItem>(
  133. AggregateCalculation.Sum,
  134. x => x.Property(x => x.PurchaseOrderItem.Qty),
  135. new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.PurchaseOrderItem.ReceivedDate).IsEqualTo(null))
  136. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID);
  137. }
  138. [ComplexFormula(typeof(TotalOrdersFormula))]
  139. [DoubleEditor(Editable = Editable.Disabled, Visible = Visible.Optional)]
  140. [EditorSequence(11)]
  141. public double TotalOrders { get; set; }
  142. private class OnOrderFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  143. {
  144. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  145. Aggregate<JobRequisitionItemPurchaseOrderItem>(
  146. AggregateCalculation.Sum,
  147. x => x.Property(x => x.PurchaseOrderItem.Qty),
  148. new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.PurchaseOrderItem.ReceivedDate).IsEqualTo(null))
  149. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID)
  150. .WithLink(x => x.PurchaseOrderItem.Product.ID, x => x.Product.ID);
  151. }
  152. [ComplexFormula(typeof(OnOrderFormula))]
  153. [DoubleEditor(Editable = Editable.Disabled)]
  154. [EditorSequence(12)]
  155. public double OnOrder { get; set; }
  156. private class TreatmentRequiredFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  157. {
  158. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  159. Formula(FormulaOperator.Subtract, Property(x => x.InStock), Property(x => x.Allocated));
  160. }
  161. [ComplexFormula(typeof(TreatmentRequiredFormula))]
  162. [DoubleEditor(Editable = Editable.Disabled)]
  163. [EditorSequence(13)]
  164. public double TreatmentRequired { get; set; }
  165. private class TreatmentOnOrderFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  166. {
  167. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  168. Formula(FormulaOperator.Subtract, Property(x => x.TotalOrders), Property(x => x.OnOrder));
  169. }
  170. [ComplexFormula(typeof(TreatmentOnOrderFormula))]
  171. [DoubleEditor(Editable = Editable.Disabled)]
  172. [EditorSequence(14)]
  173. public double TreatmentOnOrder { get; set; }
  174. private class AllocatedFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  175. {
  176. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  177. Aggregate<StockMovement>(
  178. AggregateCalculation.Sum,
  179. x => x.Property(x => x.Units))
  180. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID)
  181. .WithLink(x => x.Style.ID, x => x.Style.ID);
  182. }
  183. [ComplexFormula(typeof(AllocatedFormula))]
  184. [DoubleEditor(Editable = Editable.Disabled)]
  185. [EditorSequence(15)]
  186. public double Allocated { get; set; }
  187. private class PickRequestedFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  188. {
  189. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  190. Aggregate<RequisitionItem>(AggregateCalculation.Sum, x => x.Property(x => x.Quantity), new Filter<RequisitionItem>(x => x.RequisitionLink.StockUpdated).IsEqualTo(DateTime.MinValue))
  191. .WithLink(x => x.SourceJRI.ID, x => x.ID);
  192. }
  193. [ComplexFormula(typeof(PickRequestedFormula))]
  194. [DoubleEditor(Editable = Editable.Disabled)]
  195. [EditorSequence(16)]
  196. public double PickRequested { get; set; }
  197. private class IssuedFormula : ComplexFormulaGenerator<JobRequisitionItem, double>
  198. {
  199. public override IComplexFormulaNode<JobRequisitionItem, double> GetFormula() =>
  200. Aggregate<StockMovement>(
  201. AggregateCalculation.Sum,
  202. x => x.Property(x => x.Issued),
  203. new Filter<StockMovement>(x => x.Type).IsEqualTo(StockMovementType.Issue))
  204. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID)
  205. .WithLink(x => x.Style.ID, x => x.Style.ID);
  206. }
  207. [ComplexFormula(typeof(IssuedFormula))]
  208. [DoubleEditor(Editable = Editable.Disabled)]
  209. [EditorSequence(17)]
  210. public double Issued { get; set; }
  211. [EntityRelationship(DeleteAction.SetNull)]
  212. [RequiredColumn]
  213. [Obsolete("Replaced with JobRequisitionItemPurchaseOrderItem")]
  214. [NullEditor]
  215. public PurchaseOrderItemLink PurchaseOrderItem { get; set; }
  216. private class PurchaseOrderNumbersFormula : ComplexFormulaGenerator<JobRequisitionItem, string>
  217. {
  218. public override IComplexFormulaNode<JobRequisitionItem, string> GetFormula() =>
  219. Aggregate<JobRequisitionItemPurchaseOrderItem>(
  220. AggregateCalculation.Concat,
  221. x => x.Property(x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber))
  222. .WithLink(x => x.JobRequisitionItem.ID, x => x.ID);
  223. }
  224. [ComplexFormula(typeof(PurchaseOrderNumbersFormula))]
  225. [TextBoxEditor(Editable = Editable.Hidden)]
  226. public string PurchaseOrderNumbers { get; set; }
  227. [RequiredColumn]
  228. [EditorSequence(18)]
  229. public DateTime Cancelled { get; set; } = DateTime.MinValue;
  230. [RequiredColumn]
  231. [EditorSequence(19)]
  232. public DateTime Archived { get; set; } = DateTime.MinValue;
  233. [RequiredColumn]
  234. [Obsolete("Replaced with JobRequisitionItemPurchaseOrderItem")]
  235. [NullEditor]
  236. public DateTime Ordered { get; set; } = DateTime.MinValue;
  237. [RequiredColumn]
  238. [EditorSequence(20)]
  239. public DateTime OrderRequired { get; set; } = DateTime.MinValue;
  240. [NullEditor]
  241. public string Issues { get; set; }
  242. [NullEditor]
  243. public long Sequence { get; set; }
  244. static JobRequisitionItem()
  245. {
  246. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  247. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  248. LinkedProperties.Register<JobRequisitionItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  249. LinkedProperties.Register<JobRequisitionItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost,
  250. x => x.UnitCost);
  251. StockEntity.LinkStockDimensions<JobRequisitionItem>();
  252. }
  253. private bool bChanging;
  254. protected override void DoPropertyChanged(string name, object? before, object? after)
  255. {
  256. if (bChanging)
  257. return;
  258. try
  259. {
  260. bChanging = true;
  261. if (name.Equals(nameof(Qty)) && after is double qty)
  262. TotalCost = UnitCost * qty;
  263. else if (name.Equals(nameof(UnitCost)) && after is double cost)
  264. TotalCost = cost * Qty;
  265. else if (name.Equals(nameof(TotalCost)) && after is double total)
  266. {
  267. if (Qty == 0)
  268. Qty = 1;
  269. UnitCost = total / Qty;
  270. }
  271. }
  272. finally
  273. {
  274. bChanging = false;
  275. }
  276. base.DoPropertyChanged(name, before, after);
  277. }
  278. public static void UpdateCosts(IEnumerable<JobRequisitionItem> items, Dictionary<String,object?> changes)
  279. {
  280. void UpdateValue<TType>(JobRequisitionItem item,Expression<Func<JobRequisitionItem, TType>> property, TType value)
  281. {
  282. CoreUtils.MonitorChanges(
  283. item,
  284. () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),
  285. changes
  286. );
  287. }
  288. var productids = items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  289. MultiQuery query = new MultiQuery();
  290. query.Add(
  291. new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),
  292. Columns.None<SupplierProduct>().Add(x=>x.Product.ID)
  293. .Add(x=>x.SupplierLink.ID)
  294. .Add(x=>x.Style.ID)
  295. .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
  296. .Add(x=>x.Job.ID)
  297. .Add(x=>x.SupplierCode)
  298. .Add(x=>x.SupplierDescription)
  299. .Add(x=>x.CostPrice)
  300. );
  301. query.Add(
  302. new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),
  303. Columns.None<ProductInstance>().Add(x=>x.Product.ID)
  304. .Add(x => x.Style.ID)
  305. .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
  306. .Add(x => x.NettCost)
  307. );
  308. query.Query();
  309. var supplierProducts = query.Get<SupplierProduct>().ToArray<SupplierProduct>();
  310. var productInstances = query.Get<ProductInstance>().ToArray<ProductInstance>();
  311. foreach (var item in items)
  312. {
  313. //Check Supplier / Job Specific Pricing
  314. var supplierProduct = supplierProducts.FirstOrDefault(x =>
  315. x.Product.ID == item.Product.ID
  316. && x.SupplierLink.ID == item.Supplier.ID
  317. && x.Style.ID == item.Style.ID
  318. && x.Dimensions.Equals(item.Dimensions)
  319. && x.Job.ID == item.Job.ID);
  320. if (supplierProduct != null)
  321. {
  322. UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);
  323. continue;
  324. }
  325. // Check Supplier Pricing
  326. supplierProduct = supplierProducts.FirstOrDefault(x =>
  327. x.Product.ID == item.Product.ID
  328. && x.SupplierLink.ID == item.Supplier.ID
  329. && x.Style.ID == item.Style.ID
  330. && x.Dimensions.Equals(item.Dimensions)
  331. && x.Job.ID == Guid.Empty);
  332. if (supplierProduct != null)
  333. {
  334. UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);
  335. continue;
  336. }
  337. // Check Specific Product Instance
  338. var productInstance = productInstances.FirstOrDefault(x =>
  339. x.Product.ID == item.Product.ID
  340. && x.Style.ID == item.Style.ID
  341. && x.Dimensions.Equals(item.Dimensions));
  342. if (productInstance != null)
  343. UpdateValue<double>(item, x => x.UnitCost, productInstance.NettCost);
  344. else
  345. UpdateValue<double>(item, x => x.UnitCost, 0.0F);
  346. }
  347. }
  348. }
  349. }