Browse Source

Some fixes

Kenric Nugteren 1 year ago
parent
commit
9774a50ae0

+ 2 - 0
prs.desktop/Panels/PurchaseOrders/PurchaseOrderItemAllocationGrid.cs

@@ -19,6 +19,8 @@ public class PurchaseOrderItemAllocationGrid : DynamicOneToManyGrid<PurchaseOrde
     {
         base.Init();
 
+        HiddenColumns.Add(x => x.Nominated);
+
         ActionColumns.Add(new DynamicTickColumn<PurchaseOrderItemAllocation, bool>(x => x.Nominated, tick, tick, null, Nominated_Click));
     }
 

+ 23 - 18
prs.desktop/Panels/PurchaseOrders/SupplierPurchaseOrderItemOneToMany.cs

@@ -170,22 +170,22 @@ public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseO
         {
             var poi = LoadItem(row);
 
-            IList<Guid> _jriIDs;
+            IList<PurchaseOrderItemAllocation> _allocations;
             if(poi.ID == Guid.Empty)
             {
                 // If not saved yet, any allocations will be in the transient list.
-                _jriIDs = Allocations.Where(x => x.Item1 == poi).Select(x => x.Item2.JobRequisitionItem.ID).ToList();
+                _allocations = Allocations.Where(x => x.Item1 == poi).Select(x => x.Item2).ToList();
             }
             else
             {
                 // Otherwise, they'll all be in the database.
-                _jriIDs = Client.Query(
+                _allocations = Client.Query(
                     new Filter<PurchaseOrderItemAllocation>(x => x.Item.ID).IsEqualTo(poi.ID),
-                    Columns.None<PurchaseOrderItemAllocation>().Add(x => x.JobRequisitionItem.ID))
-                    .ExtractValues<PurchaseOrderItemAllocation, Guid>(x => x.JobRequisitionItem.ID);
+                    Columns.None<PurchaseOrderItemAllocation>().Add(x => x.Job.ID).Add(x => x.JobRequisitionItem.ID))
+                    .ToList<PurchaseOrderItemAllocation>();
             }
 
-            var newLine = new PurchaseOrderItem();
+            var newLine = CreateItem();
             newLine.BillLine.ID = poi.BillLine.ID;
             newLine.BillLine.Synchronise(poi.BillLine);
             newLine.StockLocation.ID = poi.StockLocation.ID;
@@ -225,11 +225,12 @@ public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseO
             poi.Qty = value;
             newLine.Qty = qty - value;
 
-            foreach(var jriID in _jriIDs)
+            foreach(var allocation in _allocations)
             {
                 // Add to a list to be saved later.
                 var jriPoi = new PurchaseOrderItemAllocation();
-                jriPoi.JobRequisitionItem.ID = jriID;
+                jriPoi.Job.CopyFrom(allocation.Job);
+                jriPoi.JobRequisitionItem.CopyFrom(allocation.JobRequisitionItem);
                 Allocations.Add(new(newLine, jriPoi));
             }
 
@@ -579,7 +580,6 @@ public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseO
                     poi.Product.CopyFrom(sp.Product);
                     poi.Style.CopyFrom(sp.Style);
                     poi.Dimensions.CopyFrom(sp.Dimensions);
-                    //poi.Job.CopyFrom(sp.Job);
                     poi.ForeignCurrencyCost = sp.ForeignCurrencyPrice;
                     poi.Cost = sp.CostPrice;
                     poi.CostCentre.CopyFrom(sp.Product.CostCentre);
@@ -587,11 +587,13 @@ public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseO
                     poi.Description = sp.Product.Name;
                     result.Add(poi);
                     
-                    var poia = new PurchaseOrderItemAllocation();
-                    poia.Job.CopyFrom(sp.Job);
-                    poia.Nominated = true;
-                    Allocations.Add(new(poi, poia));
-                    
+                    if(sp.Job.ID != Guid.Empty)
+                    {
+                        var poia = new PurchaseOrderItemAllocation();
+                        poia.Job.CopyFrom(sp.Job);
+                        poia.Nominated = true;
+                        Allocations.Add(new(poi, poia));
+                    }
                 }
                 else if (pi != null)
                 {
@@ -661,10 +663,13 @@ public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseO
                     poi.Description = sp.Product.Name;
                     result.Add(poi);
                     
-                    var poia = new PurchaseOrderItemAllocation();
-                    poia.Job.CopyFrom(sp.Job);
-                    poia.Nominated = true;
-                    Allocations.Add(new(poi, poia));
+                    if(sp.Job.ID != Guid.Empty)
+                    {
+                        var poia = new PurchaseOrderItemAllocation();
+                        poia.Job.CopyFrom(sp.Job);
+                        poia.Nominated = true;
+                        Allocations.Add(new(poi, poia));
+                    }
                 }
                 return result;
             });

+ 23 - 9
prs.desktop/Panels/Stock Forecast/OrderScreen/StockForecastOrderingGrid.cs

@@ -28,7 +28,7 @@ namespace PRSDesktop;
 
 public class StockForecastBreakupKey(Guid jobID, Guid requiID)
 {
-    public Guid JobID { get; set; } = jobID != Guid.Empty ? jobID : throw new ArgumentException("Job ID cannot be Empty!", "Job ID");
+    public Guid JobID { get; set; } = jobID;
 
     public Guid RequiID { get; set; } = requiID;
 
@@ -55,7 +55,7 @@ public class StockForecastOrderData(ProductLink product, ProductStyleLink style,
 
     public class QuantityBreakup(Guid jobID, Guid requiID, string description, double qty)
     {
-        public Guid JobID { get; set; } = jobID != Guid.Empty ? jobID : throw new ArgumentException("jobID cannot be Guid.Empty!", "jobID");
+        public Guid JobID { get; set; } = jobID;
 
         public Guid JobRequiItemID { get; set; } = requiID;
 
@@ -117,6 +117,11 @@ public class StockForecastOrderingItemQuantity
         Changed?.Invoke();
     }
 
+    public double JobTotal => Breakups.Values.Sum();
+
+    public double GetTotal(StockForecastOrderingType type) => type == StockForecastOrderingType.StockOrder
+        ? OrderTotal
+        : JobTotal;
 }
 
 public class StockForecastOrderingItem : BaseObject
@@ -157,7 +162,9 @@ public class StockForecastOrderingItem : BaseObject
 
     public StockForecastOrderingItemQuantity GetQuantity(int i) => Quantities[i];
 
-    public double GetTotalQuantity(StockForecastOrderingType type) => Quantities.Sum(x => x.OrderTotal);
+    public double GetTotalQuantity(StockForecastOrderingType type) => type == StockForecastOrderingType.StockOrder
+        ? Quantities.Sum(x => x.OrderTotal)
+        : Quantities.Sum(x => x.JobTotal);
 
     public void SetQuantities(StockForecastOrderingItemQuantity[] quantities)
     {
@@ -288,7 +295,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
                     {
                         yield return new(supplier, new(), item, qty.OrderTotal, qty.SupplierProduct);
                     }
-                    else
+                    else if(qty.OrderTotal > 0)
                     {
                         var breakups = new List<StockForecastOrderData.QuantityBreakup>();
                         foreach(var (key, q) in qty.Breakups)
@@ -340,7 +347,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
 
         public void UpdateOrderType(StockForecastOrderingType type)
         {
-            DataGrid.FrozenColumnCount = type == StockForecastOrderingType.StockOrder ? 8 : 9;
+            DataGrid.FrozenColumnCount = 8;
         }
 
         protected override Brush? GetCellSelectionBackgroundBrush()
@@ -474,11 +481,18 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
             if(supplierIdx != -1)
             {
                 var qty = item.GetQuantity(supplierIdx);
-                qty.OrderTotal = GetRequiredQuantity(item, selectedSupplierProduct, item.RequiredQuantity);
-                qty.Breakups.Clear();
-                foreach(var (id, q) in item.GetJobRequiredQuantities())
+                if(OrderType == StockForecastOrderingType.StockOrder)
                 {
-                    qty.Breakups[id] = q;
+                    qty.OrderTotal = GetRequiredQuantity(item, selectedSupplierProduct, item.RequiredQuantity);
+                }
+                else
+                {
+                    qty.Breakups.Clear();
+                    qty.OrderTotal = GetRequiredQuantity(item, selectedSupplierProduct, item.RequiredQuantity);
+                    foreach(var (id, q) in item.GetJobRequiredQuantities())
+                    {
+                        qty.Breakups[id] = q / item.RequiredQuantity * qty.OrderTotal;
+                    }
                 }
             }
         }

+ 35 - 9
prs.desktop/Panels/Stock Forecast/StockForecastGrid.cs

@@ -696,8 +696,6 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
 
     protected override void Reload(Filters<StockForecastItem> criteria, Columns<StockForecastItem> columns, ref SortOrder<StockForecastItem>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
     {
-        Items.Clear();
-
         // Need to query ProductInstances, StockHoldings, Job BOM and PO.
         KeyedQueryDef<T> GetQuery<T>(Filter<T>? filter = null, Columns<T>? columns = null) where T : Entity, IJobMaterial, IRemotable, IPersistent, new()
         {
@@ -741,6 +739,17 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
                     .Add(x => x.MinimumStockLevel)),
             GetQuery<StockHolding>(
                 columns: Columns.None<StockHolding>().Add(x => x.Units)),
+            new KeyedQueryDef<PurchaseOrderItem>(
+                new Filter<PurchaseOrderItem>(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue)
+                    .And(x => x.Product.Group.ID).InList(GroupIDs),
+                Columns.None<PurchaseOrderItem>()
+                    .Add(x => x.ID)
+                    .Add(x => x.Qty)
+                    .Add(x => x.Product.ID)
+                    .Add(x => x.Style.ID)
+                    .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local)
+                    .Add(x => x.Dimensions.UnitSize)
+                    .Add(x => x.Dimensions.Value)),
             new KeyedQueryDef<PurchaseOrderItemAllocation>(
                 new Filter<PurchaseOrderItemAllocation>(x => x.Item.ReceivedDate).IsEqualTo(DateTime.MinValue)
                     .And(x => x.Item.Product.Group.ID).InList(GroupIDs)
@@ -749,6 +758,7 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
                 Columns.None<PurchaseOrderItemAllocation>()
                     .Add(x => x.Quantity)
                     .Add(x => x.Job.ID)
+                    .Add(x => x.Item.ID)
                     .Add(x => x.Item.Product.ID)
                     .Add(x => x.Item.Style.ID)
                     .AddDimensionsColumns(x => x.Item.Dimensions, Dimensions.ColumnsType.Local)
@@ -816,6 +826,9 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
                     }
                 }
 
+                var purchaseOrderItems = results.GetObjects<PurchaseOrderItem>()
+                    .ToDictionary(x => x.ID);
+
                 var allocations = results.Get<PurchaseOrderItemAllocation>();
                 foreach(var allocation in allocations.Rows)
                 {
@@ -827,17 +840,26 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
                     var jobID = allocation.Get<PurchaseOrderItemAllocation, Guid>(x => x.Job.ID);
 
                     var qty = allocation.Get<PurchaseOrderItemAllocation, double>(x => x.Quantity);
-                    if(jobID == Guid.Empty)
-                    {
-                        item.GenPO += qty;
-                    }
-                    else
+                    item.JobPO += qty;
+                    item.AddJobPO(jobID, qty);
+
+                    if(purchaseOrderItems.TryGetValue(allocation.Get<PurchaseOrderItemAllocation, Guid>(x => x.Item.ID), out var poi))
                     {
-                        item.JobPO += qty;
-                        item.AddJobPO(jobID, qty);
+                        poi.Qty -= qty;
                     }
                 }
 
+                foreach(var poi in purchaseOrderItems.Values)
+                {
+                    var key = new ItemKey(
+                        poi.Product.ID,
+                        poi.Style.ID,
+                        poi.Dimensions);
+                    var item = GetItem(key);
+
+                    item.GenPO += poi.Qty;
+                }
+
                 var jobBOMItems = results.Get<JobBillOfMaterialsItem>();
                 foreach(var bomItem in jobBOMItems.Rows)
                 {
@@ -977,6 +999,10 @@ public class StockForecastGrid : DynamicItemsListGrid<StockForecastItem>, IDataM
         {
             var item = new StockForecastOrderData(forecastItem.Product, forecastItem.Style, forecastItem.Dimensions);
             item.RequiredQuantity = Optimise ? forecastItem.Optimised : forecastItem.Required;
+            if(forecastItem.StockRequired > 0)
+            {
+                item.SetRequiredQuantity(Guid.Empty, Guid.Empty, "", forecastItem.StockRequired);
+            }
             
             foreach(var (id, jobInfo) in forecastItem.JobInfo)
             {