using System; using System.Linq; using InABox.Core; namespace Comal.Classes { public class BillLineLink : EntityLink { [NullEditor] public override Guid ID { get; set; } } [UserTracking(typeof(Bill))] public class BillLine : Entity, IPersistent, IRemotable, IOneToMany, ITaxable, ILicense, IPostableFragment { [EntityRelationship(DeleteAction.Cascade)] [NullEditor] public BillLink BillLink { get; set; } private class PurchaseOrderItemLookup : LookupDefinitionGenerator { public override Filter DefineFilter(BillLine[] items) { if (!items.Any()) return new Filter().None(); var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault(); if(supplierID == Guid.Empty) return new Filter().None(); return new Filter(x => x.PurchaseOrderLink.SupplierLink.ID).IsEqualTo(supplierID) .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty); } public override Columns DefineFilterColumns() { return new Columns(x => x.BillLink.SupplierLink.ID); } } [LookupDefinition(typeof(PurchaseOrderItemLookup))] [EntityRelationship(DeleteAction.SetNull)] [EditorSequence(1)] public PurchaseOrderItemLink OrderItem { get; set; } private class ProductLookupGenerator : LookupDefinitionGenerator { public override Filter? DefineFilter(BillLine[] items) => new Filter(x => x.NonStock); } [EditorSequence(2)] [LookupDefinition(typeof(ProductLookupGenerator))] public ProductLink Product { get; set; } [MemoEditor] [EditorSequence(2)] public string Description { get; set; } [EditorSequence(3)] public PurchaseGLCodeLink PurchaseGL { get; set; } [EditorSequence(4)] public CostCentreLink CostCentre { get; set; } [CurrencyEditor(Summary = Summary.Sum)] [EditorSequence(5)] public double ExTax { get; set; } [RequiredColumn] [EditorSequence(6)] public TaxCodeLink TaxCode { get; set; } [DoubleEditor(Editable = Editable.Hidden)] public double TaxRate { get; set; } [CurrencyEditor(Summary = Summary.Sum)] public double Tax { get; set; } [CurrencyEditor(Summary = Summary.Sum)] [EditorSequence(7)] public double IncTax { get; set; } [EditorSequence(8)] public JobLink Job { get; set; } [NullEditor] public string PostedReference { get; set; } static BillLine() { LinkedProperties.Register(x => x.OrderItem, x => x.ExTax, x => x.ExTax); LinkedProperties.Register(x => x.OrderItem.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID); LinkedProperties.Register(x => x.OrderItem.CostCentre, x => x.ID, x => x.CostCentre.ID); LinkedProperties.Register(x => x.OrderItem.TaxCode, x => x.ID, x => x.TaxCode.ID); LinkedProperties.Register(x => x.OrderItem.TaxCode, x => x.Code, x => x.TaxCode.Code); LinkedProperties.Register(x => x.OrderItem.TaxCode, x => x.Description, x => x.TaxCode.Description); LinkedProperties.Register(x => x.OrderItem.TaxCode, x => x.Rate, x => x.TaxCode.Rate); LinkedProperties.Register(x => x.OrderItem, x => x.Tax, x => x.Tax); LinkedProperties.Register(x => x.OrderItem, x => x.IncTax, x => x.IncTax); LinkedProperties.Register(x => x.TaxCode, x => x.Rate, x => x.TaxRate); LinkedProperties.Register(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID); LinkedProperties.Register(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID); LinkedProperties.Register(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID); LinkedProperties.Register(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code); LinkedProperties.Register(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description); LinkedProperties.Register(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate); } private static readonly Column OrderItemColumn = new Column(x => x.OrderItem.ID); private static readonly Column ProductColumn = new Column(x => x.Product.ID); private static readonly Column JobColumn = new Column(x => x.Job.ID); protected override void DoPropertyChanged(string name, object? before, object? after) { base.DoPropertyChanged(name, before, after); if (OrderItemColumn.IsEqualTo(name) && after is Guid orderItemID && orderItemID != Guid.Empty) { Product.ID = Guid.Empty; Product.Clear(); Job.ID = Guid.Empty; Job.Clear(); } else if((ProductColumn.IsEqualTo(name) && after is Guid productID && productID != Guid.Empty) || (JobColumn.IsEqualTo(name) && after is Guid jobID && jobID != Guid.Empty)) { OrderItem.ID = Guid.Empty; OrderItem.Clear(); } } } }