123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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))]
- 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()
- => Columns.None<JobBillOfMaterialsItem>().Add(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),
- Columns.None<SupplierProduct>().Add(x=>x.Product.ID)
- .Add(x=>x.SupplierLink.ID)
- .Add(x=>x.Style.ID)
- .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
- .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),
- Columns.None<ProductInstance>().Add(x=>x.Product.ID)
- .Add(x => x.Style.ID)
- .AddDimensionsColumns(x => x.Dimensions, Classes.Dimensions.ColumnsType.Local)
- .Add(x => x.NettCost)
- );
- query.Query();
- var supplierProducts = query.Get<SupplierProduct>().ToArray<SupplierProduct>();
- 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 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)
- {
- 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);
- }
- }
- }
-
- }
|