JobBillOfMaterialsItem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. [Caption("Materials")]
  10. public class JobBillOfMaterialsItem : StockEntity, IRemotable, IPersistent, IOneToMany<Job>,
  11. IOneToMany<JobBillOfMaterials>, ISequenceable, ILicense<ProjectManagementLicense>, IJobMaterial,
  12. IProblems<ManagedProblem>
  13. {
  14. [EntityRelationship(DeleteAction.Cascade)]
  15. [Editable(Editable.Hidden)]
  16. public JobLink Job { get; set; }
  17. [EntityRelationship(DeleteAction.Cascade)]
  18. [Editable(Editable.Hidden)]
  19. public JobBillOfMaterialsLink BillOfMaterials { get; set; }
  20. [EditorSequence(1)]
  21. [EntityRelationship(DeleteAction.SetNull)]
  22. [RequiredColumn]
  23. public override ProductLink Product { get; set; }
  24. [EditorSequence(2)]
  25. [EntityRelationship(DeleteAction.SetNull)]
  26. public ProductStyleLink Style { get; set; }
  27. [EditorSequence(3)]
  28. [RequiredColumn]
  29. [DimensionsEditor(typeof(StockDimensions))]
  30. public override StockDimensions Dimensions { get; set; }
  31. [EditorSequence(4)]
  32. [RequiredColumn]
  33. public JobScopeLink Scope { get; set; }
  34. [EditorSequence(5)]
  35. [DecimalEditor(Summary = Summary.Sum)]
  36. public decimal Quantity { get; set; }
  37. [EditorSequence(6)]
  38. public decimal UnitCost { get; set; }
  39. [EditorSequence(7)]
  40. [CurrencyEditor(Summary = Summary.Sum)]
  41. public decimal TotalCost { get; set; }
  42. [EditorSequence(8)]
  43. [TextBoxEditor]
  44. public string Section { get; set; }
  45. [EditorSequence(9)]
  46. public SupplierLink Supplier { get; set; }
  47. private class JobITPLookup : LookupDefinitionGenerator<JobITP, JobBillOfMaterialsItem>
  48. {
  49. public override Filter<JobITP> DefineFilter(JobBillOfMaterialsItem[] items)
  50. {
  51. if (items.Length == 1)
  52. return Filter<JobITP>.Where(x => x.Job.ID).IsEqualTo(items.First().Job.ID);
  53. return LookupFactory.DefineFilter<JobITP>();
  54. }
  55. public override Columns<JobBillOfMaterialsItem> DefineFilterColumns()
  56. => Columns.None<JobBillOfMaterialsItem>().Add(x => x.Job.ID);
  57. }
  58. [EditorSequence(10)]
  59. [EntityRelationship(DeleteAction.SetNull)]
  60. [LookupDefinition(typeof(JobITPLookup))]
  61. public JobITPLink ITP { get; set; }
  62. [NullEditor]
  63. public long Sequence { get; set; }
  64. public PurchaseOrderItemLink PurchaseOrderItem { get; set; }
  65. public ManufacturingPacketLink Packet { get; set; }
  66. [EditorSequence("Issues", 1)]
  67. public ManagedProblem Problem { get; set; }
  68. static JobBillOfMaterialsItem()
  69. {
  70. LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);
  71. LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);
  72. LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);
  73. LinkedProperties.Register<JobBillOfMaterialsItem, ProductInstanceLink, decimal>(x => x.Product.DefaultInstance, x => x.NettCost, x => x.UnitCost);
  74. LinkedProperties.Register<JobBillOfMaterialsItem, SupplierLink, Guid>(x => x.Product.Supplier.Supplier, x => x.ID, x => x.Supplier.ID);
  75. LinkedProperties.Register<JobBillOfMaterialsItem, JobLink, Guid>(x => x.Job, x => x.DefaultScope.ID, x => x.Scope.ID);
  76. StockEntity.LinkStockDimensions<JobBillOfMaterialsItem>();
  77. }
  78. private bool bChanging;
  79. protected override void DoPropertyChanged(string name, object? before, object? after)
  80. {
  81. if (bChanging)
  82. return;
  83. try
  84. {
  85. bChanging = true;
  86. if (name.Equals(nameof(Quantity)) && after is decimal qty)
  87. TotalCost = UnitCost * qty;
  88. else if (name.Equals(nameof(UnitCost)) && after is decimal cost)
  89. TotalCost = cost * Quantity;
  90. else if (name.Equals(nameof(TotalCost)) && after is decimal total)
  91. {
  92. if (Quantity == 0)
  93. Quantity = 1;
  94. UnitCost = total / Quantity;
  95. }
  96. }
  97. finally
  98. {
  99. bChanging = false;
  100. }
  101. base.DoPropertyChanged(name, before, after);
  102. }
  103. public static void UpdateCosts(IEnumerable<JobBillOfMaterialsItem> items, Dictionary<String,object?> changes)
  104. {
  105. void UpdateValue<TType>(JobBillOfMaterialsItem item,Expression<Func<JobBillOfMaterialsItem, TType>> property, TType value)
  106. {
  107. CoreUtils.MonitorChanges(
  108. item,
  109. () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),
  110. changes
  111. );
  112. }
  113. var _items = items as JobBillOfMaterialsItem[] ?? items.ToArray();
  114. var productids = _items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  115. var jobids = _items.Select(x => x.Job.ID).ToList().Append(Guid.Empty).Distinct().ToArray();
  116. if(productids.Length == 0)
  117. return;
  118. MultiQuery query = new MultiQuery();
  119. query.Add(
  120. Filter<SupplierProduct>.Where(x =>x.Product.ID).InList(productids),
  121. Columns.None<SupplierProduct>().Add(x=>x.Product.ID)
  122. .Add(x=>x.Supplier.ID)
  123. .Add(x=>x.Style.ID)
  124. .AddDimensionsColumns(x => x.Dimensions)
  125. .Add(x=>x.Job.ID)
  126. .Add(x=>x.SupplierCode)
  127. .Add(x=>x.SupplierDescription)
  128. .Add(x=>x.TradePrice)
  129. .Add(x=>x.CostPrice)
  130. .Add(x=>x.DiscountGroup.ID)
  131. );
  132. query.Add(
  133. Filter<SupplierDiscount>.Where(x =>x.Job.ID).InList(jobids)
  134. .And(x => x.Group.ID).InQuery(
  135. Filter<SupplierProduct>.Where(x => x.Product.ID).InList(productids),
  136. x => x.DiscountGroup.ID
  137. ),
  138. Columns.None<SupplierDiscount>()
  139. .Add(x=>x.Group.ID)
  140. .Add(x=>x.Job.ID)
  141. .Add(x=>x.Group.Type)
  142. .Add(x=>x.Value)
  143. );
  144. query.Add(
  145. Filter<ProductInstance>.Where(x =>x.Product.ID).InList(productids),
  146. Columns.None<ProductInstance>().Add(x=>x.Product.ID)
  147. .Add(x => x.Style.ID)
  148. .AddDimensionsColumns(x => x.Dimensions)
  149. .Add(x => x.NettCost)
  150. );
  151. query.Query();
  152. var supplierProducts = query.Get<SupplierProduct>().ToArray<SupplierProduct>();
  153. var supplierDiscounts = query.Get<SupplierDiscount>().ToArray<SupplierDiscount>();
  154. var productInstances = query.Get<ProductInstance>().ToArray<ProductInstance>();
  155. foreach (var item in _items)
  156. {
  157. //Check Supplier / Job Specific Pricing
  158. var supplierProduct = supplierProducts.FirstOrDefault(x =>
  159. x.Product.ID == item.Product.ID
  160. && x.Supplier.ID == item.Supplier.ID
  161. && x.Style.ID == item.Style.ID
  162. && x.Dimensions.Equals(item.Dimensions)
  163. && x.Job.ID == item.Job.ID);
  164. if (supplierProduct != null)
  165. {
  166. UpdateValue<decimal>(item, x => x.UnitCost, supplierProduct.CostPrice);
  167. continue;
  168. }
  169. // Check Supplier / General Pricing
  170. supplierProduct = supplierProducts.FirstOrDefault(x =>
  171. x.Product.ID == item.Product.ID
  172. && x.Supplier.ID == item.Supplier.ID
  173. && x.Style.ID == item.Style.ID
  174. && x.Dimensions.Equals(item.Dimensions)
  175. && x.Job.ID == Guid.Empty);
  176. if (supplierProduct != null)
  177. {
  178. var discount = supplierDiscounts.FirstOrDefault(x=>x.Job.ID == item.Job.ID && x.Group.ID == supplierProduct.DiscountGroup.ID);
  179. if (discount == null)
  180. discount = supplierDiscounts.FirstOrDefault(x=>x.Job.ID == Guid.Empty && x.Group.ID == supplierProduct.DiscountGroup.ID);
  181. if (discount != null)
  182. {
  183. if (discount.Group.Type == SupplierDiscountGroupType.Discount)
  184. {
  185. var disc = (100 - discount?.Value ?? 0) / 100;
  186. UpdateValue<decimal>(item, x => x.UnitCost, supplierProduct.TradePrice * disc);
  187. }
  188. else
  189. UpdateValue<decimal>(item, x => x.UnitCost, discount.Value);
  190. }
  191. else
  192. UpdateValue<decimal>(item, x => x.UnitCost, supplierProduct.CostPrice);
  193. continue;
  194. }
  195. // Check Specific Product Instance
  196. var productInstance = productInstances.FirstOrDefault(x =>
  197. x.Product.ID == item.Product.ID
  198. && x.Style.ID == item.Style.ID
  199. && x.Dimensions.Equals(item.Dimensions));
  200. if (productInstance != null)
  201. UpdateValue<decimal>(item, x => x.UnitCost, productInstance.NettCost);
  202. else
  203. UpdateValue<decimal>(item, x => x.UnitCost, 0);
  204. }
  205. }
  206. }
  207. }