JobBillOfMaterialsItem.cs 10 KB

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