JobBillOfMaterialsItem.cs 7.8 KB

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