BaseProductStore.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace Comal.Stores
  7. {
  8. public class BaseProductStore<T> : BaseStore<T> where T : Entity, new()
  9. {
  10. /// <summary>
  11. /// Update the ComponentCost and NettCost of any Product that contains this one
  12. /// as a component
  13. /// </summary>
  14. /// <param name="entity"></param>
  15. protected void UpdateProductComponentCost(Product entity)
  16. {
  17. // Extract all the Products that Contain this one as a component
  18. var products = Provider.Query(
  19. new Filter<ProductPriceComponent>(x => x.Component.ID).IsEqualTo(entity.ID),
  20. new Columns<ProductPriceComponent>(
  21. x => x.ID,
  22. x => x.Product.ID,
  23. x => x.Product.PricingStrategy,
  24. x => x.Product.BaseCost
  25. )
  26. );
  27. if (!products.Rows.Any())
  28. return;
  29. // All the info we need, we can extract from the product link, so we don't need to
  30. // go back to the database and incur another query penalty
  31. var infos = products.Rows.Select(x =>
  32. new ProdInfo
  33. {
  34. ProductID = x.Get<ProductPriceComponent, Guid>(c => c.Product.ID),
  35. Strategy = x.Get<ProductPriceComponent, ProductPricingStrategy>(c => c.Product.PricingStrategy),
  36. BaseCost = x.Get<ProductPriceComponent, double>(c => c.Product.BaseCost)
  37. }
  38. ).Distinct().ToArray();
  39. var ids = infos.Select(x => x.ProductID).ToArray();
  40. // Now get all the components that these products contain
  41. // We _could_ use the delta NettCost the for the entity (newvalue-oldvalue)
  42. // And skip this part, but then it will be hard to fix any errors that might creep in,
  43. // and we also will need to do this anyway for new products, etc
  44. var components = Provider.Query(
  45. new Filter<ProductPriceComponent>(x => x.Product.ID).InList(ids),
  46. new Columns<ProductPriceComponent>(
  47. x => x.Product.ID,
  48. x => x.NettCost
  49. )
  50. );
  51. // Now construct the required updates
  52. var updates = new List<Product>();
  53. foreach (var info in infos)
  54. {
  55. double compcost = 0.0F;
  56. components.Rows
  57. .Where(r => r.Get<ProductPriceComponent, Guid>(c => c.Product.ID).Equals(info.ProductID))
  58. .Select(r => r.Get<ProductPriceComponent, double>(c => c.NettCost))
  59. .ToList()
  60. .ForEach(x => compcost += x);
  61. if (info.Strategy != ProductPricingStrategy.Assembly)
  62. {
  63. var update = new Product
  64. {
  65. ID = info.ProductID,
  66. NettCost = Product.CalculateNettCost(info.Strategy, info.BaseCost, compcost)
  67. };
  68. updates.Add(update);
  69. }
  70. }
  71. Provider.Save(updates);
  72. }
  73. /// <summary>
  74. /// Update the Base Cost of any product that has this supplier price as its default
  75. /// </summary>
  76. /// <param name="entity"></param>
  77. protected void UpdateDefaultSupplierPricing(SupplierProduct entity)
  78. {
  79. var product = Provider.Query(
  80. new Filter<Product>(x => x.Supplier.ID).IsEqualTo(entity.ID).And(x => x.UseDefaultSupplierPricing).IsEqualTo(true),
  81. new Columns<Product>(
  82. x => x.ID,
  83. x => x.PricingStrategy,
  84. x => x.BaseCost,
  85. x => x.ComponentCost,
  86. x => x.NettCost
  87. )
  88. ).Rows.FirstOrDefault()?.ToObject<Product>();
  89. if (product != null)
  90. {
  91. product.BaseCost = entity.CostPrice;
  92. product.NettCost = Product.CalculateNettCost(product.PricingStrategy, product.BaseCost, product.ComponentCost);
  93. product.TaxCode.ID = entity.TaxCode.ID;
  94. if (product.IsChanged())
  95. {
  96. Provider.Save(product);
  97. UpdateProductComponentCost(product);
  98. }
  99. }
  100. }
  101. private struct ProdInfo
  102. {
  103. public Guid ProductID;
  104. public ProductPricingStrategy Strategy;
  105. public double BaseCost;
  106. }
  107. }
  108. }