using System.Linq; using Comal.Classes; using InABox.Core; using System; namespace Comal.Stores { public class StockLocationStore : BaseStore { 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(x => x.Default).IsEqualTo(true), Columns.None().Add(x => x.ID) ).Rows.Select(r => r.Get(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(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.Warehouse.ID).IsEqualTo(entity.Warehouse.ID) .And(x => x.Default) .IsEqualTo(true), Columns.None().Add(x => x.ID, x => x.Default) ).Rows.Select(x => x.ToObject()).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); } } } }