| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using InABox.Core;using PRSClasses;namespace Comal.Classes{    [Caption("Materials")]    public class JobBillOfMaterialsItem : StockEntity, IRemotable, IPersistent, IOneToMany<Job>,         IOneToMany<JobBillOfMaterials>, ISequenceable, ILicense<ProjectManagementLicense>, IJobMaterial, IIssues,         IProblems<ManagedProblem>    {        [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))]        public override StockDimensions Dimensions { get; set; }                [EditorSequence(4)]        [RequiredColumn]        public JobScopeLink Scope { get; set; }                [EditorSequence(5)]        [DoubleEditor(Summary = Summary.Sum)]        public double Quantity { get; set; }                [EditorSequence(6)]        public double UnitCost { get; set; }        [EditorSequence(7)]        [CurrencyEditor(Summary = Summary.Sum)]        public double TotalCost { get; set; }                [EditorSequence(8)]        [TextBoxEditor]        public string Section { get; set; }        [EditorSequence(9)]        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()                => Columns.None<JobBillOfMaterialsItem>().Add(x => x.Job.ID);        }        [EditorSequence(10)]        [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; }                [NullEditor]        [Obsolete("Replaced with Problem", true)]        public string Issues { get; set; }                [EditorSequence("Issues", 1)]        public ManagedProblem Problem { 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);            LinkedProperties.Register<JobBillOfMaterialsItem, SupplierLink, Guid>(x => x.Product.Supplier.SupplierLink, x => x.ID, x => x.Supplier.ID);            LinkedProperties.Register<JobBillOfMaterialsItem, JobLink, Guid>(x => x.Job, x => x.DefaultScope.ID, x => x.Scope.ID);                        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 _items = items as JobBillOfMaterialsItem[] ?? items.ToArray();            var productids = _items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();            var jobids = _items.Select(x => x.Job.ID).ToList().Append(Guid.Empty).Distinct().ToArray();                        if(productids.Length == 0)                return;                        MultiQuery query = new MultiQuery();            query.Add(                new Filter<SupplierProduct>(x=>x.Product.ID).InList(productids),                Columns.None<SupplierProduct>().Add(x=>x.Product.ID)                    .Add(x=>x.SupplierLink.ID)                    .Add(x=>x.Style.ID)                    .AddDimensionsColumns(x => x.Dimensions)                    .Add(x=>x.Job.ID)                    .Add(x=>x.SupplierCode)                    .Add(x=>x.SupplierDescription)                    .Add(x=>x.TradePrice)                    .Add(x=>x.CostPrice)                    .Add(x=>x.DiscountGroup.ID)            );                        query.Add(                new Filter<SupplierDiscount>(x=>x.Job.ID).InList(jobids)                    .And(x => x.Group.ID).InQuery(                        new Filter<SupplierProduct>(x => x.Product.ID).InList(productids),                         x => x.DiscountGroup.ID                    ),                Columns.None<SupplierDiscount>()                    .Add(x=>x.Group.ID)                    .Add(x=>x.Job.ID)                    .Add(x=>x.Group.Type)                    .Add(x=>x.Value)            );                        query.Add(                new Filter<ProductInstance>(x=>x.Product.ID).InList(productids),                Columns.None<ProductInstance>().Add(x=>x.Product.ID)                    .Add(x => x.Style.ID)                    .AddDimensionsColumns(x => x.Dimensions)                    .Add(x => x.NettCost)            );                                                query.Query();            var supplierProducts = query.Get<SupplierProduct>().ToArray<SupplierProduct>();            var supplierDiscounts = query.Get<SupplierDiscount>().ToArray<SupplierDiscount>();            var productInstances = query.Get<ProductInstance>().ToArray<ProductInstance>();                            foreach (var item in _items)            {                //Check Supplier / Job Specific Pricing                var supplierProduct = supplierProducts.FirstOrDefault(x =>                    x.Product.ID == item.Product.ID                    && x.SupplierLink.ID == item.Supplier.ID                    && x.Style.ID == item.Style.ID                    && x.Dimensions.Equals(item.Dimensions)                    && x.Job.ID == item.Job.ID);                if (supplierProduct != null)                {                    UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);                    continue;                }                                // Check Supplier / General Pricing                supplierProduct = supplierProducts.FirstOrDefault(x =>                    x.Product.ID == item.Product.ID                    && x.SupplierLink.ID == item.Supplier.ID                    && x.Style.ID == item.Style.ID                    && x.Dimensions.Equals(item.Dimensions)                    && x.Job.ID == Guid.Empty);                if (supplierProduct != null)                {                    var discount = supplierDiscounts.FirstOrDefault(x=>x.Job.ID == item.Job.ID && x.Group.ID == supplierProduct.DiscountGroup.ID);                    if (discount == null)                        discount = supplierDiscounts.FirstOrDefault(x=>x.Job.ID == Guid.Empty && x.Group.ID == supplierProduct.DiscountGroup.ID);                    if (discount != null)                    {                        if (discount.Group.Type == SupplierDiscountGroupType.Discount)                        {                            var disc = (100.0-discount?.Value ?? 0.00)/100.0;                            UpdateValue<double>(item, x => x.UnitCost, supplierProduct.TradePrice * disc);                        }                        else                            UpdateValue<double>(item, x => x.UnitCost, discount.Value);                    }                    else                        UpdateValue<double>(item, x => x.UnitCost, supplierProduct.CostPrice);                    continue;                }                                // Check Specific Product Instance                var productInstance = productInstances.FirstOrDefault(x =>                    x.Product.ID == item.Product.ID                    && x.Style.ID == item.Style.ID                    && x.Dimensions.Equals(item.Dimensions));                if (productInstance != null)                    UpdateValue<double>(item, x => x.UnitCost, productInstance.NettCost);                else                    UpdateValue<double>(item, x => x.UnitCost, 0.0F);            }        }    }    }
 |