| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 | using System;using System.Linq;using InABox.Core;namespace Comal.Classes{    public class BillLineLink : EntityLink<BillLine>    {        [NullEditor]        public override Guid ID { get; set; }                [RequiredColumn]        public BillLink BillLink { get; set; }    }    [UserTracking(typeof(Bill))]    public class BillLine : Entity, IPersistent, IRemotable, IOneToMany<Bill>, ITaxable, ILicense<AccountsPayableLicense>, IPostableFragment<Bill>    {        [RequiredColumn]        [EntityRelationship(DeleteAction.Cascade)]        [NullEditor]        public BillLink BillLink { get; set; }        private class PurchaseOrderItemLookup : LookupDefinitionGenerator<PurchaseOrderItem, BillLine>        {            public override Filter<PurchaseOrderItem> DefineFilter(BillLine[] items)            {                if (!items.Any())                    return new Filter<PurchaseOrderItem>().None();                                var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();                if(supplierID == Guid.Empty)                    return new Filter<PurchaseOrderItem>().None();                                return new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.SupplierLink.ID).IsEqualTo(supplierID)                    .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);            }            public override Columns<BillLine> DefineFilterColumns()            {                return Columns.None<BillLine>().Add(x => x.BillLink.SupplierLink.ID);            }        }                [LookupDefinition(typeof(PurchaseOrderItemLookup))]        [EntityRelationship(DeleteAction.SetNull)]        [EditorSequence(1)]        public PurchaseOrderItemLink OrderItem { get; set; }                private class ConsignmentLookup : LookupDefinitionGenerator<Consignment, BillLine>        {            public override Filter<Consignment> DefineFilter(BillLine[] items)            {                if (!items.Any())                    return new Filter<Consignment>().None();                                var supplierID = items.Select(x => x.BillLink.SupplierLink.ID).Distinct().SingleOrDefault();                if(supplierID == Guid.Empty)                    return new Filter<Consignment>().None();                                return new Filter<Consignment>(x => x.Supplier.ID).IsEqualTo(supplierID)                    .And(x=>x.BillLine.ID).IsEqualTo(Guid.Empty);            }            public override Columns<BillLine> DefineFilterColumns()            {                return Columns.None<BillLine>().Add(x => x.BillLink.SupplierLink.ID);            }        }                [LookupDefinition(typeof(ConsignmentLookup))]        [EntityRelationship(DeleteAction.SetNull)]        [EditorSequence(2)]        public ConsignmentLink Consignment { get; set; }        private class ProductLookupGenerator : LookupDefinitionGenerator<Product, BillLine>        {            public override Filter<Product>? DefineFilter(BillLine[] items)                => new Filter<Product>(x => x.NonStock).IsEqualTo(true);        }        [EditorSequence(3)]        [LookupDefinition(typeof(ProductLookupGenerator))]        public ProductLink Product { get; set; }                [EditorSequence(4)]        public JobLink Job { get; set; }                [MemoEditor]        [EditorSequence(5)]        public string Description { get; set; }                [CurrencyEditor(Summary=Summary.Sum)]        [EditorSequence(6)]        public double ForeignCurrencyCost { get; set; }        [CurrencyEditor(Summary = Summary.Sum)]        [EditorSequence(7)]        public double ExTax { get; set; }        [RequiredColumn]        [EditorSequence(8)]        public TaxCodeLink TaxCode { get; set; }                [CurrencyEditor(Summary = Summary.Sum)]        [EditorSequence(9)]        public double Tax { get; set; }        [CurrencyEditor(Summary = Summary.Sum)]        [EditorSequence(10)]        public double IncTax { get; set; }        [EditorSequence(11)]        public PurchaseGLCodeLink PurchaseGL { get; set; }                [EditorSequence(12)]        public CostCentreLink CostCentre { get; set; }                [NullEditor]        public double TaxRate { get; set; }                [NullEditor]        public string PostedReference { get; set; }        static BillLine()        {            // If we select a product for this bill line            LinkedProperties.Register<BillLine, ProductLink, String>(x => x.Product, x => x.Name, x => x.Description);                        LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.ExTax,                x => x.ExTax);            LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.OrderItem.PurchaseGL, x => x.ID,                x => x.PurchaseGL.ID);            LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.OrderItem.CostCentre, x => x.ID,                x => x.CostCentre.ID);            LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.OrderItem.TaxCode, x => x.ID,                x => x.TaxCode.ID);                        LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Code,                x => x.TaxCode.Code);                        LinkedProperties.Register<BillLine, TaxCodeLink, String>(x => x.OrderItem.TaxCode, x => x.Description,                x => x.TaxCode.Description);                        LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.OrderItem.TaxCode, x => x.Rate,                x => x.TaxCode.Rate);                                    LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.Tax,                x => x.Tax);                        LinkedProperties.Register<BillLine, PurchaseOrderItemLink, double>(x => x.OrderItem, x => x.IncTax,                x => x.IncTax);            LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.TaxCode, x => x.Rate, x => x.TaxRate);            LinkedProperties.Register<BillLine, PurchaseGLCodeLink, Guid>(x => x.Product.PurchaseGL, x => x.ID, x => x.PurchaseGL.ID);            LinkedProperties.Register<BillLine, CostCentreLink, Guid>(x => x.Product.CostCentre, x => x.ID, x => x.CostCentre.ID);            LinkedProperties.Register<BillLine, TaxCodeLink, Guid>(x => x.Product.TaxCode, x => x.ID, x => x.TaxCode.ID);            LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Code, x => x.TaxCode.Code);            LinkedProperties.Register<BillLine, TaxCodeLink, string>(x => x.Product.TaxCode, x => x.Description, x => x.TaxCode.Description);            LinkedProperties.Register<BillLine, TaxCodeLink, double>(x => x.Product.TaxCode, x => x.Rate, x => x.TaxCode.Rate);        }        private static readonly Column<BillLine> OrderItemColumn = new Column<BillLine>(x => x.OrderItem.ID);        private static readonly Column<BillLine> ProductColumn = new Column<BillLine>(x => x.Product.ID);        private static readonly Column<BillLine> JobColumn = new Column<BillLine>(x => x.Job.ID);        private static readonly Column<BillLine> ForeignCurrencyColumn = new Column<BillLine>(x => x.ForeignCurrencyCost);        private static readonly Column<BillLine> ExTaxColumn = new Column<BillLine>(x => x.ExTax);        private static readonly Column<BillLine> IncTaxColumn = new Column<BillLine>(x => x.ExTax);        private bool bChanging = false;        protected override void DoPropertyChanged(string name, object? before, object? after)        {            if (bChanging)                return;            bChanging = true;            try            {                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();                }                else if (ForeignCurrencyColumn.IsEqualTo(name) && after is double fcc)                {                    ExTax = fcc / (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);                    Tax = ExTax * TaxRate / 100.0;                    IncTax = ExTax + Tax;                }                else if (ExTaxColumn.IsEqualTo(name) && after is double etc)                {                    ForeignCurrencyCost = etc * (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);                }                else if (IncTaxColumn.IsEqualTo(name) && after is double itc)                {                    ForeignCurrencyCost = ExTax * (BillLink.SupplierLink.Currency.ExchangeRate.IsEffectivelyEqual(0.0) ? 1.0 : BillLink.SupplierLink.Currency.ExchangeRate);                }                            }            finally            {                bChanging = false;            }                    }    }}
 |