JobBillOfMaterialsItem.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, JobLink, Guid>(x => x.Job, x => x.DefaultScope.ID, x => x.Scope.ID);
  81. StockEntity.LinkStockDimensions<JobBillOfMaterialsItem>();
  82. }
  83. private bool bChanging;
  84. protected override void DoPropertyChanged(string name, object? before, object? after)
  85. {
  86. if (bChanging)
  87. return;
  88. try
  89. {
  90. bChanging = true;
  91. if (name.Equals(nameof(Quantity)) && after is double qty)
  92. TotalCost = UnitCost * qty;
  93. else if (name.Equals(nameof(UnitCost)) && after is double cost)
  94. TotalCost = cost * Quantity;
  95. else if (name.Equals(nameof(TotalCost)) && after is double total)
  96. {
  97. if (Quantity == 0)
  98. Quantity = 1;
  99. UnitCost = total / Quantity;
  100. }
  101. }
  102. finally
  103. {
  104. bChanging = false;
  105. }
  106. base.DoPropertyChanged(name, before, after);
  107. }
  108. public static void UpdateCosts(IEnumerable<JobBillOfMaterialsItem> items, Dictionary<String,object?> changes)
  109. {
  110. void UpdateValue<TType>(JobBillOfMaterialsItem item,Expression<Func<JobBillOfMaterialsItem, TType>> property, TType value)
  111. {
  112. CoreUtils.MonitorChanges(
  113. item,
  114. () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),
  115. changes
  116. );
  117. }
  118. var productids = items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  119. MultiQuery query = new MultiQuery();
  120. query.Add(
  121. new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),
  122. Columns.None<SupplierProduct>().Add(x=>x.Product.ID)
  123. .Add(x=>x.SupplierLink.ID)
  124. .Add(x=>x.Style.ID)
  125. .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
  126. .Add(x=>x.Job.ID)
  127. .Add(x=>x.SupplierCode)
  128. .Add(x=>x.SupplierDescription)
  129. .Add(x=>x.CostPrice)
  130. );
  131. query.Add(
  132. new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),
  133. Columns.None<ProductInstance>().Add(x=>x.Product.ID)
  134. .Add(x => x.Style.ID)
  135. .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
  136. .Add(x => x.NettCost)
  137. );
  138. query.Query();
  139. var supplierProducts = query.Get<SupplierProduct>().ToArray<SupplierProduct>();
  140. var productInstances = query.Get<ProductInstance>().ToArray<ProductInstance>();
  141. foreach (var item in items)
  142. {
  143. //Check Supplier / Job Specific Pricing
  144. var supplierProduct = supplierProducts.FirstOrDefault(x =>
  145. x.Product.ID == item.Product.ID
  146. && x.SupplierLink.ID == item.Supplier.ID
  147. && x.Style.ID == item.Style.ID
  148. && x.Dimensions.Equals(item.Dimensions)
  149. && x.Job.ID == item.Job.ID);
  150. if (supplierProduct != null)
  151. {
  152. UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);
  153. continue;
  154. }
  155. // Check Supplier Pricing
  156. supplierProduct = supplierProducts.FirstOrDefault(x =>
  157. x.Product.ID == item.Product.ID
  158. && x.SupplierLink.ID == item.Supplier.ID
  159. && x.Style.ID == item.Style.ID
  160. && x.Dimensions.Equals(item.Dimensions)
  161. && x.Job.ID == Guid.Empty);
  162. if (supplierProduct != null)
  163. {
  164. UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);
  165. continue;
  166. }
  167. // Check Specific Product Instance
  168. var productInstance = productInstances.FirstOrDefault(x =>
  169. x.Product.ID == item.Product.ID
  170. && x.Style.ID == item.Style.ID
  171. && x.Dimensions.Equals(item.Dimensions));
  172. if (productInstance != null)
  173. UpdateValue<double>(item, x => x.UnitCost, productInstance.NettCost);
  174. else
  175. UpdateValue<double>(item, x => x.UnitCost, 0.0F);
  176. }
  177. }
  178. }
  179. }