| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using InABox.Core;using PRSClasses;namespace Comal.Classes{    public class JobBillOfMaterialsItem : StockEntity, IRemotable, IPersistent, IOneToMany<Job>,         IOneToMany<JobBillOfMaterials>, ISequenceable, ILicense<ProjectManagementLicense>, IJobMaterial    {        [EntityRelationship(DeleteAction.Cascade)]        [Editable(Editable.Hidden)]        public JobLink Job { get; set; }                [EntityRelationship(DeleteAction.Cascade)]        [Editable(Editable.Hidden)]        public JobBillOfMaterialsLink BillOfMaterials { get; set; }                [EditorSequence(1)]        [EntityRelationship(DeleteAction.SetNull)]        [RequiredColumn]        public override ProductLink Product { get; set; }        [EditorSequence(2)]        [EntityRelationship(DeleteAction.SetNull)]        public ProductStyleLink Style { get; set; }        [NullEditor]        [Obsolete("Replaced by Dimensions",true)]        public double UnitSize { get; set; }        [EditorSequence(3)]        [RequiredColumn]        [DimensionsEditor(typeof(StockDimensions), AllowEditingUnit = false)]        public override StockDimensions Dimensions { get; set; }                [EditorSequence(4)]        [DoubleEditor(Summary = Summary.Sum)]        public double Quantity { get; set; }                [EditorSequence(5)]        public double UnitCost { get; set; }        [EditorSequence(6)]        [CurrencyEditor(Summary = Summary.Sum)]        public double TotalCost { get; set; }                [EditorSequence(7)]        [TextBoxEditor]        public string Section { get; set; }        [EditorSequence(8)]        public SupplierLink Supplier { get; set; }                private class JobITPLookup : LookupDefinitionGenerator<JobITP, JobBillOfMaterialsItem>        {            public override Filter<JobITP> DefineFilter(JobBillOfMaterialsItem[] items)            {                if (items.Length == 1)                    return new Filter<JobITP>(x => x.Job.ID).IsEqualTo(items.First().Job.ID);                return LookupFactory.DefineFilter<JobITP>();            }            public override Columns<JobBillOfMaterialsItem> DefineFilterColumns()                => new Columns<JobBillOfMaterialsItem>(x => x.Job.ID);        }        [EditorSequence(9)]        [EntityRelationship(DeleteAction.SetNull)]        [LookupDefinition(typeof(JobITPLookup))]        public JobITPLink ITP { get; set; }                [NullEditor]        public long Sequence { get; set; }        public PurchaseOrderItemLink PurchaseOrderItem { get; set; }        public ManufacturingPacketLink Packet { get; set; }        static JobBillOfMaterialsItem()        {            LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, Guid>(x => x.Product.DefaultInstance.Style, x => x.ID, x => x.Style.ID);            LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Code, x => x.Style.Code);            LinkedProperties.Register<JobBillOfMaterialsItem, ProductStyleLink, String>(x => x.Product.DefaultInstance.Style, x => x.Description, x => x.Style.Description);                        LinkedProperties.Register<JobBillOfMaterialsItem, ProductInstanceLink, double>(x => x.Product.DefaultInstance, x => x.NettCost, x => x.UnitCost);            StockEntity.LinkStockDimensions<JobBillOfMaterialsItem>();        }                private bool bChanging;                protected override void DoPropertyChanged(string name, object? before, object? after)        {            if (bChanging)                return;            try            {                bChanging = true;                if (name.Equals(nameof(Quantity)) && after is double qty)                    TotalCost = UnitCost * qty;                                else if (name.Equals(nameof(UnitCost)) && after is double cost)                    TotalCost = cost * Quantity;                else if (name.Equals(nameof(TotalCost)) && after is double total)                {                    if (Quantity == 0)                        Quantity = 1;                    UnitCost = total / Quantity;                }            }            finally            {                bChanging = false;            }            base.DoPropertyChanged(name, before, after);        }                public static void UpdateCosts(IEnumerable<JobBillOfMaterialsItem> items, Dictionary<String,object?> changes)        {            void UpdateValue<TType>(JobBillOfMaterialsItem item,Expression<Func<JobBillOfMaterialsItem, TType>> property, TType value)            {                CoreUtils.MonitorChanges(                    item,                     () => CoreUtils.SetPropertyValue(item, CoreUtils.GetFullPropertyName(property, "."), value),                    changes                );            }                        var productids = items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();            MultiQuery query = new MultiQuery();            query.Add(                new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),                new Columns<SupplierProduct>(x=>x.Product.ID)                    .Add(x=>x.SupplierLink.ID)                    .Add(x=>x.Style.ID)                    .Add(x=>x.Dimensions.UnitSize)                    .Add(x=>x.Job.ID)                    .Add(x=>x.SupplierCode)                    .Add(x=>x.SupplierDescription)                    .Add(x=>x.CostPrice)            );            query.Add(                new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),                new Columns<ProductInstance>(x=>x.Product.ID)                    .Add(x => x.Style.ID)                    .Add(x => x.Dimensions.UnitSize)                    .Add(x => x.NettCost)            );            query.Query();                            foreach (var item in items)            {                                //Check Supplier / Job Specific Pricing                CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>                    (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)                    && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)                    && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)                    && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))                    && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)                );                if (row != null)                {                    UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));                    continue;                }                                // Check Supplier Pricing                row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>                    (r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)                    && (r.Get<SupplierProduct, Guid>(c => c.SupplierLink.ID) == item.Supplier.ID)                    && (r.Get<SupplierProduct, Guid>(c => c.Style.ID) == item.Style.ID)                    && (string.Equals(r.Get<SupplierProduct,String>(c=>c.Dimensions.UnitSize),item.Dimensions.UnitSize))                    && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)                );                if (row != null)                {                    UpdateValue<double>(item, x => x.UnitCost, row.Get<SupplierProduct, double>(c => c.CostPrice));                    continue;                }                                // Check Specific Product Instance                row = query.Get<ProductInstance>()?.Rows.FirstOrDefault(r =>                    (r.Get<ProductInstance, Guid>(c => c.Product.ID) == item.Product.ID)                    && (r.Get<ProductInstance, Guid>(c => c.Style.ID) == item.Style.ID)                    && (string.Equals(r.Get<ProductInstance,string>(c => c.Dimensions.UnitSize),item.Dimensions.UnitSize))                );                if (row != null)                    UpdateValue<double>(item, x => x.UnitCost, row.Get<ProductInstance, double>(c => c.NettCost));                else                    UpdateValue<double>(item, x => x.UnitCost, 0.0F);            }        }    }    }
 |