StockWarehouseStore.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. using System.Linq;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. namespace Comal.Stores
  5. {
  6. public class StockWarehouseStore : BaseStore<StockWarehouse>
  7. {
  8. protected override void BeforeSave(StockWarehouse entity)
  9. {
  10. base.BeforeSave(entity);
  11. // Try to make sure there is one and one only default warehouse
  12. var otherdefaults = Provider.Query(
  13. new Filter<StockWarehouse>(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.Default).IsEqualTo(true),
  14. new Columns<StockWarehouse>(x => x.ID, x => x.Default)
  15. ).Rows.Select(x => x.ToObject<StockWarehouse>()).ToArray();
  16. if (!entity.Default)
  17. if (!otherdefaults.Any())
  18. entity.Default = true;
  19. if (entity.Default)
  20. {
  21. entity.Active = true;
  22. foreach (var otherdefault in otherdefaults)
  23. otherdefault.Default = false;
  24. Provider.Save(otherdefaults);
  25. }
  26. }
  27. }
  28. }