1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores;
- public class SupplierDiscountStore : BaseStore<SupplierDiscount>
- {
- protected override void BeforeSave(SupplierDiscount entity)
- {
- base.BeforeSave(entity);
-
- // Don't do anything for new entities
- if (entity.ID == Guid.Empty)
- return;
-
- // if the job has changed, zero out all discounts for the old job.id
- if (entity.Job.HasOriginalValue(x => x.ID))
- UpdateSupplierProducts(entity.Group.ID, entity.Job.GetOriginalValue(x => x.ID), entity.Group.Type, 0F);
- }
- protected override void AfterSave(SupplierDiscount entity)
- {
- base.AfterSave(entity);
- if (entity.Job.HasOriginalValue(x => x.ID) || entity.HasOriginalValue(x => x.Value))
- UpdateSupplierProducts(entity.Group.ID, entity.Job.ID, entity.Group.Type, entity.Value);
- }
- protected override void BeforeDelete(SupplierDiscount entity)
- {
- base.BeforeDelete(entity);
- var discounts = Provider.Query(
- new Filter<SupplierDiscount>(x=>x.ID).IsEqualTo(entity.ID),
- Columns.None<SupplierDiscount>()
- .Add(x=>x.Group.ID)
- .Add(x=>x.Job.ID)
- .Add(x=>x.Group.Type)
- ).ToArray<SupplierDiscount>();
- foreach (var discount in discounts)
- UpdateSupplierProducts(discount.Group.ID,discount.Job.ID, discount.Group.Type, 0F);
- }
- private void UpdateSupplierProducts(Guid groupid, Guid jobid, SupplierDiscountGroupType type, double value)
- {
- var products = Provider.Query(
- new Filter<SupplierProduct>(x => x.DiscountGroup.ID).IsEqualTo(groupid)
- .And(x=>x.Job.ID).IsEqualTo(jobid),
- Columns.Required<SupplierProduct>()
- .Add(x=>x.TradePrice)
- .Add(x=>x.Discount)
- .Add(x=>x.CostPrice)
- ).ToArray<SupplierProduct>();
- foreach (var product in products)
- {
- if (type == SupplierDiscountGroupType.Discount)
- product.Discount = value;
- else
- {
- product.TradePrice = value;
- product.Discount = 0.0;
- }
- }
-
- Provider.Save(products);
-
- }
- }
|