Browse Source

Fixing various bugs with ordering screen

Kenric Nugteren 1 year ago
parent
commit
d74eceff92

+ 23 - 30
prs.desktop/Panels/Reservation Management/Holdings/ReservationManagementHoldingsGrid.xaml.cs

@@ -1,4 +1,5 @@
-using Comal.Classes;
+using com.sun.org.apache.xml.@internal.dtm.@ref;
+using Comal.Classes;
 using InABox.Clients;
 using InABox.Core;
 using InABox.Wpf;
@@ -119,7 +120,7 @@ public partial class ReservationManagementHoldingsGrid
     {
         if(item is not null)
         {
-            var stockMovements = Client.Query(
+            var results = Client.Query(
                 new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(item.Product.ID)
                     .And(x => x.Dimensions).DimensionEquals(item.Dimensions)
                     .And(x => x.Location.ID).IsNotEqualTo(Guid.Empty),
@@ -137,37 +138,29 @@ public partial class ReservationManagementHoldingsGrid
                     x => x.JobRequisitionItem.ID,
                     x => x.Cost)
                     .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Data))
-                .ToObjects<StockMovement>()
-                .GroupBy(x => new { JobID = x.Job.ID, Allocated = x.JobRequisitionItem.ID != Guid.Empty })
-                .ToDictionary(
-                    x => x.Key,
-                    x => x.GroupBy(x => x.Style.ID)
-                        .Select(x =>
-                        {
-                            var mvts = x.ToList();
-                            return new
-                            {
-                                Style = x.Key,
-                                Movements = mvts
-                            };
-                        })
-                        .ToDictionary(
-                            x => x.Style,
-                            x => x.Movements));
+                .ToArray<StockMovement>();
+            var stockMovements = new Dictionary<(Guid job, bool allocated), Dictionary<Guid, List<StockMovement>>>();
+            foreach(var mvt in results)
+            {
+                var key = (mvt.Job.ID, mvt.JobRequisitionItem.ID != Guid.Empty);
+                var lineDict = stockMovements.GetValueOrAdd(key);
+                var mvts = lineDict.GetValueOrAdd(mvt.Style.ID);
+                mvts.Add(mvt);
+            }
 
             var jobs = Client.Query(
-                new Filter<Job>(x => x.ID).InList(stockMovements.Keys.Select(x => x.JobID).ToArray()),
+                new Filter<Job>(x => x.ID).InList(stockMovements.Keys.Select(x => x.job).ToArray()),
                 Columns.None<Job>().Add(x => x.ID).Add(x => x.JobNumber).Add(x => x.Name))
                 .ToObjects<Job>()
                 .ToDictionary(x => x.ID, x => x);
 
-            if(!stockMovements.ContainsKey(new { JobID = item.Job.ID, Allocated = false }))
+            if(!stockMovements.ContainsKey((item.Job.ID, false)))
             {
-                stockMovements.Add(new { JobID = item.Job.ID, Allocated = false }, new Dictionary<Guid, List<StockMovement>>());
+                stockMovements.Add((item.Job.ID, false), new Dictionary<Guid, List<StockMovement>>());
             }
-            if (!stockMovements.ContainsKey(new { JobID = Guid.Empty, Allocated = false }))
+            if (!stockMovements.ContainsKey((Guid.Empty, false)))
             {
-                stockMovements.Add(new { JobID = Guid.Empty, Allocated = false }, new Dictionary<Guid, List<StockMovement>>());
+                stockMovements.Add((Guid.Empty, false), new Dictionary<Guid, List<StockMovement>>());
             }
 
             greenList.Clear();
@@ -175,12 +168,12 @@ public partial class ReservationManagementHoldingsGrid
             redList.Clear();
             foreach (var (key, movements) in stockMovements)
             {
-                var job = jobs.GetValueOrDefault(key.JobID);
+                var job = jobs.GetValueOrDefault(key.job);
 
                 ReservationManagementHoldingsModel holding;
-                if (key.Allocated)
+                if (key.allocated)
                 {
-                    holding = new ReservationManagementHoldingsModel(key.JobID, job?.JobNumber ?? "", job?.Name ?? "")
+                    holding = new ReservationManagementHoldingsModel(key.job, job?.JobNumber ?? "", job?.Name ?? "")
                     {
                         AlreadyAllocated = true
                     };
@@ -190,13 +183,13 @@ public partial class ReservationManagementHoldingsGrid
                         redList.Add(holding);
                     }
                 }
-                else if (key.JobID == Guid.Empty)
+                else if (key.job == Guid.Empty)
                 {
                     holding = new ReservationManagementHoldingsModel(Guid.Empty, "Free Stock", "Free Stock");
                     DistributeMovements(movements, item.Style.ID, holding);
                     greenList.Add(holding);
                 }
-                else if(key.JobID == item.Job.ID)
+                else if(key.job == item.Job.ID)
                 {
                     holding = new ReservationManagementHoldingsModel(item.Job.ID, item.Job.JobNumber, item.Job.Name);
                     DistributeMovements(movements, item.Style.ID, holding);
@@ -204,7 +197,7 @@ public partial class ReservationManagementHoldingsGrid
                 }
                 else
                 {
-                    holding = new ReservationManagementHoldingsModel(key.JobID, job?.JobNumber ?? "", job?.Name ?? "");
+                    holding = new ReservationManagementHoldingsModel(key.job, job?.JobNumber ?? "", job?.Name ?? "");
                     DistributeMovements(movements, item.Style.ID, holding);
                     if (!holding.Empty)
                     {

+ 1 - 1
prs.desktop/Panels/Reservation Management/Holdings/ReservationManagementHoldingsWindow.xaml

@@ -9,7 +9,7 @@
         Title="Allocate Stock" 
         SizeToContent="Height"
         Width="600" 
-        MinHeight="250"
+        MinHeight="250" MaxHeight="700"
         WindowStartupLocation="CenterOwner">
     <Grid Margin="5">
         <local:ReservationManagementHoldingsGrid x:Name="holdings"/>

+ 5 - 0
prs.desktop/Panels/Reservation Management/Holdings/ReservationManagementHoldingsWindow.xaml.cs

@@ -5,6 +5,11 @@ namespace PRSDesktop;
 
 public partial class ReservationManagementHoldingsWindow : Window
 {
+    public event HoldingsReviewRefresh? OnHoldingsReviewRefresh
+    {
+        add => holdings.OnHoldingsReviewRefresh += value;
+        remove => holdings.OnHoldingsReviewRefresh -= value;
+    }
     
     public ReservationManagementHoldingsWindow(JobRequisitionItem item, ProductStyleLink defaultStyle)
     {

+ 16 - 5
prs.desktop/Panels/Reservation Management/ReservationManagementItemGrid.cs

@@ -362,7 +362,7 @@ public class ReservationManagementItemGrid : DynamicDataGrid<JobRequisitionItem>
                 jri.Style.CopyFrom(new ProductStyle());
             }
 
-            var qtyRequired = DimensionUtils.ConvertDimensions(jri.Dimensions, jri.Qty - jri.InStock, (f,c) => Client.Query(f,c));
+            var qtyRequired = DimensionUtils.ConvertDimensions(jri.Dimensions, Math.Max(jri.Qty - jri.InStock, 0.0), (f,c) => Client.Query(f,c));
 
             var item = items.FirstOrDefault(x =>
                 x.Product.ID == jri.Product.ID
@@ -635,12 +635,24 @@ public class ReservationManagementItemGrid : DynamicDataGrid<JobRequisitionItem>
 
     private void AllocateStock_Clicked(CoreRow? row)
     {
-        var jri = row?.ToObject<JobRequisitionItem>();
-        if (jri == null)
+        if (row is null)
             return;
+
+        var jri = row.ToObject<JobRequisitionItem>();
         var window = new ReservationManagementHoldingsWindow(jri, CompanyDefaultStyle) { Owner = Application.Current.MainWindow };
+        window.OnHoldingsReviewRefresh += () =>
+        {
+            var data = Client.Query(
+                new Filter<JobRequisitionItem>(x => x.ID).IsEqualTo(jri.ID),
+                DataColumns());
+            jri = data.ToObjects<JobRequisitionItem>().FirstOrDefault();
+            if(jri is not null)
+            {
+                UpdateRow(row, jri);
+                InvalidateRow(row);
+            }
+        };
         window.ShowDialog();
-        Refresh(false,true);
     }
     
     private void SubstituteItem_Clicked(CoreRow? row)
@@ -847,7 +859,6 @@ public class ReservationManagementItemGrid : DynamicDataGrid<JobRequisitionItem>
         menu.IsOpen = true;
     }
 
-
     private void ViewStockHoldings(CoreRow? row)
     {
         if (row is null) return;

+ 32 - 12
prs.desktop/Panels/Stock Forecast/OrderScreen/StockForecastOrderingGrid.cs

@@ -137,8 +137,6 @@ public class StockOrderingItem : BaseObject
     [EditorSequence(3)]
     public StockDimensions Dimensions { get; set; }
     
-    public List<StockForecastOrderData.QuantityBreakup> Breakups { get; } = [];
-    
     [EditorSequence(5)]
     [DoubleEditor]
     public double RequiredQuantity { get; set; }
@@ -457,11 +455,19 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
 
     private SupplierProduct? CalculateSupplierProduct(StockOrderingItem item, int supplierIdx)
     {
-        var supplierProduct = SelectSupplierProduct(SupplierProducts.Where(x => x.Product.ID == item.Product.ID && x.Style.ID == item.Style.ID && x.SupplierLink.ID == Suppliers[supplierIdx].ID), item);
+        var supplierProducts = string.IsNullOrWhiteSpace(item.Dimensions.Unit.Conversion)
+            ? SupplierProducts.Where(x => x.Dimensions.Equals(item.Dimensions))
+            : SupplierProducts;
+        var supplierProduct = SelectSupplierProduct(supplierProducts.Where(x => x.Product.ID == item.Product.ID && x.Style.ID == item.Style.ID && x.SupplierLink.ID == Suppliers[supplierIdx].ID), item);
 
         var qty = item.GetQuantity(supplierIdx);
         qty.SupplierProduct = supplierProduct;
         qty.OrderTotal = 0;
+        qty.Breakups.Clear();
+        foreach(var id in item.GetJobRequiredQuantities().Keys)
+        {
+            qty.Breakups[id] = 0;
+        }
 
         return supplierProduct;
     }
@@ -487,6 +493,14 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
             {
                 var qty = item.GetQuantity(supplierIdx);
                 qty.OrderTotal = GetRequiredQuantity(item, selectedSupplierProduct);
+                if(OrderType == StockForecastOrderingType.Breakup)
+                {
+                    qty.Breakups.Clear();
+                    foreach(var (id, q) in item.GetJobRequiredQuantities())
+                    {
+                        qty.Breakups[id] = q;
+                    }
+                }
             }
         }
     }
@@ -537,7 +551,9 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
 
     private double CalculateSupplierProductRequiredQuantity(StockOrderingItem item, SupplierProduct supplierProduct)
     {
-        var req = item.GetRequiredQuantity(OrderType);
+        var supplierIdx = Suppliers.WithIndex().FirstOrDefault(x => x.Value.ID == supplierProduct.ID).Key;
+        var qty = item.GetQuantity(supplierIdx);
+        var req = OrderType == StockForecastOrderingType.StockOrder ? item.RequiredQuantity : qty.Breakups.Sum(x => x.Value);
 
         var d = new StockDimensions();
         d.CopyFrom(supplierProduct.Dimensions);
@@ -676,8 +692,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
         {
             if(new Column<SupplierProduct>(x => x.SupplierLink.ID).IsEqualTo(column.ColumnName)
                 || new Column<SupplierProduct>(x => x.Product.ID).IsEqualTo(column.ColumnName)
-                || new Column<SupplierProduct>(x => x.Style.ID).IsEqualTo(column.ColumnName)
-                || new Column<SupplierProduct>(x => x.Job.ID).IsEqualTo(column.ColumnName))
+                || new Column<SupplierProduct>(x => x.Style.ID).IsEqualTo(column.ColumnName))
             {
                 editor.Editable = editor.Editable.Combine(Editable.Disabled);
             }
@@ -701,12 +716,12 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
             }
 
             var supplier = selection.Data().Rows.First().ToObject<Supplier>();
-            var productInstance = LoadItem(row);
+            var orderingItem = LoadItem(row);
 
             var supplierProduct = new SupplierProduct();
-            supplierProduct.Product.CopyFrom(productInstance.Product);
-            supplierProduct.Style.CopyFrom(productInstance.Style);
-            supplierProduct.Dimensions.CopyFrom(productInstance.Dimensions);
+            supplierProduct.Product.CopyFrom(orderingItem.Product);
+            supplierProduct.Style.CopyFrom(orderingItem.Style);
+            supplierProduct.Dimensions.CopyFrom(orderingItem.Dimensions);
             supplierProduct.SupplierLink.CopyFrom(supplier);
 
             if (DynamicGridUtils.EditEntity(supplierProduct, customiseGrid: EditSupplierProductGrid))
@@ -737,6 +752,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
                     }
                     else
                     {
+                        newQty.OrderTotal = 0;
                         foreach(var id in item.GetJobRequiredQuantities().Keys)
                         {
                             newQty.Breakups[id] = 0;
@@ -744,8 +760,12 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
                     }
                     item.SetQuantities(quantities);
                 }
-
                 Suppliers = newSuppliers;
+                foreach (var item in Items)
+                {
+                    CalculateSupplierProduct(item, newIdx);
+                }
+
                 _loadedColumns = false;
                 Refresh(true, true);
             }
@@ -992,7 +1012,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockOrderingItem>
                 : SupplierProducts;
 
             var items = supplierProducts.Where(x => x.SupplierLink.ID == Suppliers[idx].ID && x.Product.ID == instance.Product.ID)
-                .Select(x => new KeyValuePair<SupplierProduct?, string>(x, $"Job {x.Job.JobNumber}: {x.Dimensions.UnitSize}"));
+                .Select(x => new KeyValuePair<SupplierProduct?, string>(x, x.Job.ID == Guid.Empty ? x.Dimensions.UnitSize : $"Job {x.Job.JobNumber}: {x.Dimensions.UnitSize}"));
             if (items.Any())
                 items = items.Prepend(new KeyValuePair<SupplierProduct?, string>(null, ""));
             comboBox.SelectedValuePath = "Key";