ProductStore.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Comal.Classes;
  2. using InABox.Core;
  3. namespace Comal.Stores;
  4. public class ProductStore : BaseProductStore<Product>
  5. {
  6. protected override void AfterSave(Product entity)
  7. {
  8. base.AfterSave(entity);
  9. if (entity.HasOriginalValue("NettCost"))
  10. UpdateProductComponentCost(entity);
  11. if (entity.UnitOfMeasure.HasOriginalValue(nameof(ProductDimensionUnitLink.ID)))
  12. UpdateProductUOMs(entity);
  13. }
  14. private static List<Type>? _stockentitytypes = null;
  15. private void UpdateProductUOMs(Product entity)
  16. {
  17. // If this is a new Product (ie original value is Guid.Empty)
  18. if (entity.GetOriginalValue(x => x.ID, entity.ID) == Guid.Empty)
  19. return;
  20. _stockentitytypes ??= CoreUtils.TypeList(x =>
  21. x.IsSubclassOf(typeof(StockEntity))
  22. && !x.HasAttribute<AutoEntity>()
  23. && x.HasInterface<IPersistent>());
  24. var _uom = Provider.Query(new Filter<ProductDimensionUnit>(x => x.ID).IsEqualTo(entity.UnitOfMeasure.ID))
  25. .ToObjects<ProductDimensionUnit>().FirstOrDefault() ?? new ProductDimensionUnit();
  26. foreach (var _stockentitytype in _stockentitytypes)
  27. {
  28. var _children = Provider.Query(
  29. _stockentitytype,
  30. new Filter<StockEntity>(x => x.Product.ID).IsEqualTo(entity.ID),
  31. Columns.None<StockEntity>()
  32. .Add(x => x.ID)
  33. .AddSubColumns(x => x.Dimensions, null)
  34. ).ToArray(_stockentitytype).Cast<StockEntity>().ToArray();
  35. foreach (var _child in _children)
  36. _child.Dimensions.Unit.CopyFrom(_uom);
  37. Provider.Save(_stockentitytype, _children.Where(x => x.IsChanged()));
  38. }
  39. }
  40. protected override void AfterDelete(Product entity)
  41. {
  42. base.AfterDelete(entity);
  43. UpdateProductComponentCost(entity);
  44. }
  45. }