using Comal.Classes; using InABox.Core; namespace Comal.Stores; public class ProductStore : BaseProductStore { 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? _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() && x.HasInterface()); var _uom = Provider.Query(new Filter(x => x.ID).IsEqualTo(entity.UnitOfMeasure.ID)) .ToObjects().FirstOrDefault() ?? new ProductDimensionUnit(); foreach (var _stockentitytype in _stockentitytypes) { var _children = Provider.Query( _stockentitytype, new Filter(x => x.Product.ID).IsEqualTo(entity.ID), Columns.None() .Add(x => x.ID) .AddSubColumns(x => x.Dimensions, null) ).ToArray(_stockentitytype).Cast().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); } }