123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using System;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class StockLocationStore : BaseStore<StockLocation>
- {
- protected override void BeforeSave(StockLocation entity)
- {
- base.BeforeSave(entity);
- // If the Warehouse is not set, use the default one
- if (!entity.Warehouse.IsValid())
- {
- var warehouseid = Provider.Query(
- new Filter<StockWarehouse>(x => x.Default).IsEqualTo(true),
- new Columns<StockWarehouse>(x => x.ID)
- ).Rows.Select(r => r.Get<StockWarehouse, Guid>(c => c.ID)).FirstOrDefault();
- entity.Warehouse.ID = warehouseid;
- }
- // Try to make sure that there is one and one only default location for the warehouse
- var otherdefaults = Provider.Query(
- new Filter<StockLocation>(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.Warehouse.ID).IsEqualTo(entity.Warehouse.ID)
- .And(x => x.Default)
- .IsEqualTo(true),
- new Columns<StockLocation>(x => x.ID, x => x.Default)
- ).Rows.Select(x => x.ToObject<StockLocation>()).ToArray();
- if (!entity.Default)
- if (!otherdefaults.Any())
- entity.Default = true;
- if (entity.Default)
- {
- entity.Active = true;
- foreach (var otherdefault in otherdefaults)
- otherdefault.Default = false;
- Provider.Save(otherdefaults);
- }
- }
- }
- }
|