1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores;
- public class ProductStore : BaseProductStore<Product>
- {
- protected override void AfterSave(Product entity)
- {
- base.AfterSave(entity);
- if (entity.HasOriginalValue("NettCost"))
- UpdateProductComponentCost(entity);
-
- if (entity.UnitOfMeasure.HasOriginalValue(nameof(ProductDimensionUnitLink.ID)))
- UpdateProductUOMs(entity);
- }
- private static List<Type>? _stockentitytypes = null;
-
- private void UpdateProductUOMs(Product entity)
- {
- // If this is a new Product (ie original value is Guid.Empty)
- if (entity.GetOriginalValue(x => x.ID, entity.ID) == Guid.Empty)
- return;
-
- _stockentitytypes ??= CoreUtils.TypeList(x =>
- x.IsSubclassOf(typeof(StockEntity))
- && !x.HasAttribute<AutoEntity>()
- && x.HasInterface<IPersistent>());
- var _uom = Provider.Query(new Filter<ProductDimensionUnit>(x => x.ID).IsEqualTo(entity.UnitOfMeasure.ID))
- .ToObjects<ProductDimensionUnit>().FirstOrDefault() ?? new ProductDimensionUnit();
- foreach (var _stockentitytype in _stockentitytypes)
- {
- var _children = Provider.Query(
- _stockentitytype,
- new Filter<StockEntity>(x => x.Product.ID).IsEqualTo(entity.ID),
- Columns.None<StockEntity>()
- .Add(x => x.ID)
- .AddSubColumns(x => x.Dimensions, null)
- ).ToArray(_stockentitytype).Cast<StockEntity>().ToArray();
- foreach (var _child in _children)
- _child.Dimensions.Unit.CopyFrom(_uom);
- Provider.Save(_stockentitytype, _children.Where(x => x.IsChanged()));
- }
- }
- protected override void AfterDelete(Product entity)
- {
- base.AfterDelete(entity);
- UpdateProductComponentCost(entity);
- }
- }
|