JobBillOfMaterialsItem.cs 8.5 KB

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