浏览代码

Setting the Order Strategy combobox to null if anything custom happens.

Kenric Nugteren 11 月之前
父节点
当前提交
c9a5f9329b
共有 1 个文件被更改,包括 38 次插入28 次删除
  1. 38 28
      prs.desktop/Panels/Stock Forecast/OrderScreen/StockForecastOrderingGrid.cs

+ 38 - 28
prs.desktop/Panels/Stock Forecast/OrderScreen/StockForecastOrderingGrid.cs

@@ -110,6 +110,8 @@ public class StockForecastOrderingItem : BaseObject
     [EnumLookupEditor(typeof(SupplierProductOrderStrategy))]
     [EnumLookupEditor(typeof(SupplierProductOrderStrategy))]
     public SupplierProductOrderStrategy OrderStrategy { get; set; }
     public SupplierProductOrderStrategy OrderStrategy { get; set; }
 
 
+    public bool CustomStrategy { get; set; } = false;
+
     public StockForecastOrderingItemQuantity[] Quantities = [];
     public StockForecastOrderingItemQuantity[] Quantities = [];
 
 
     public StockForecastOrderingItemQuantity GetQuantity(int i) => Quantities[i];
     public StockForecastOrderingItemQuantity GetQuantity(int i) => Quantities[i];
@@ -210,6 +212,7 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
                     StockForecastOrderingStrategy.RoundUp => SupplierProductOrderStrategy.RoundUp,
                     StockForecastOrderingStrategy.RoundUp => SupplierProductOrderStrategy.RoundUp,
                     StockForecastOrderingStrategy.PerProduct or _ => item.Product.OrderStrategy
                     StockForecastOrderingStrategy.PerProduct or _ => item.Product.OrderStrategy
                 };
                 };
+                item.CustomStrategy = false;
             }
             }
 
 
             CalculateQuantities(false);
             CalculateQuantities(false);
@@ -382,17 +385,22 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
         return qty;
         return qty;
     }
     }
 
 
+    private SupplierProduct CalculateSupplierProduct(StockForecastOrderingItem 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 qty = item.GetQuantity(supplierIdx);
+        qty.SupplierProduct = supplierProduct;
+        qty.Total = 0;
+        return supplierProduct;
+    }
+
     private void CalculateSupplierProduct(StockForecastOrderingItem item)
     private void CalculateSupplierProduct(StockForecastOrderingItem item)
     {
     {
         var selectedSupplierProducts = new List<SupplierProduct>();
         var selectedSupplierProducts = new List<SupplierProduct>();
         for(int i = 0; i < Suppliers.Length; ++i)
         for(int i = 0; i < Suppliers.Length; ++i)
         {
         {
-            var supplierProduct = SelectSupplierProduct(SupplierProducts.Where(x => x.Product.ID == item.Product.ID && x.Style.ID == item.Style.ID && x.SupplierLink.ID == Suppliers[i].ID), item);
-
-            var qty = item.GetQuantity(i);
-            qty.SupplierProduct = supplierProduct;
-            qty.Total = 0;
-
+            var supplierProduct = CalculateSupplierProduct(item, i);
             if(supplierProduct is not null)
             if(supplierProduct is not null)
             {
             {
                 selectedSupplierProducts.Add(supplierProduct);
                 selectedSupplierProducts.Add(supplierProduct);
@@ -512,8 +520,8 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
         var columns = new DynamicGridColumns();
         var columns = new DynamicGridColumns();
         columns.Add<StockForecastOrderingItem, string>(x => x.Product.Code, 120, "Product Code", "", Alignment.MiddleCenter);
         columns.Add<StockForecastOrderingItem, string>(x => x.Product.Code, 120, "Product Code", "", Alignment.MiddleCenter);
         columns.Add<StockForecastOrderingItem, string>(x => x.Product.Name, 200, "Product Name", "", Alignment.MiddleLeft);
         columns.Add<StockForecastOrderingItem, string>(x => x.Product.Name, 200, "Product Name", "", Alignment.MiddleLeft);
-        columns.Add<StockForecastOrderingItem, string>(x => x.Style.Code, 80, "Style", "", Alignment.MiddleCenter);
         columns.Add<StockForecastOrderingItem, string>(x => x.Dimensions.UnitSize, 80, "Size", "", Alignment.MiddleCenter);
         columns.Add<StockForecastOrderingItem, string>(x => x.Dimensions.UnitSize, 80, "Size", "", Alignment.MiddleCenter);
+        columns.Add<StockForecastOrderingItem, string>(x => x.Style.Code, 80, "Style", "", Alignment.MiddleCenter);
         if(OrderType == StockForecastOrderingType.JobOrder)
         if(OrderType == StockForecastOrderingType.JobOrder)
         {
         {
             columns.Add<StockForecastOrderingItem, string>(x => x.Job.JobNumber, 80, "Job No.", "", Alignment.MiddleCenter);
             columns.Add<StockForecastOrderingItem, string>(x => x.Job.JobNumber, 80, "Job No.", "", Alignment.MiddleCenter);
@@ -536,10 +544,12 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
                     .Select(x => new KeyValuePair<SupplierProductOrderStrategy, string>(x, CoreUtils.Neatify(x.ToString())));
                     .Select(x => new KeyValuePair<SupplierProductOrderStrategy, string>(x, CoreUtils.Neatify(x.ToString())));
                 box.DisplayMemberPath = "Value";
                 box.DisplayMemberPath = "Value";
                 box.SelectedValuePath = "Key";
                 box.SelectedValuePath = "Key";
-                box.SelectedValue = item.OrderStrategy;
+                box.SelectedValue = item.CustomStrategy ? null : item.OrderStrategy;
                 box.SelectionChanged += (o, e) =>
                 box.SelectionChanged += (o, e) =>
                 {
                 {
-                    item.OrderStrategy = (SupplierProductOrderStrategy)box.SelectedValue;
+                    if (box.SelectedValue is not SupplierProductOrderStrategy strategy) return;
+                    item.OrderStrategy = strategy;
+                    item.CustomStrategy = false;
                     CalculateSupplierProduct(item);
                     CalculateSupplierProduct(item);
                     InvalidateRow(row);
                     InvalidateRow(row);
                 };
                 };
@@ -735,11 +745,6 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
             if (row is null) return null;
             if (row is null) return null;
 
 
             var item = LoadItem(row);
             var item = LoadItem(row);
-            var supplierProduct = GetSupplierProduct(item, Suppliers[idx].ID);
-            if (supplierProduct is not null)
-            {
-                return null;
-            }
 
 
             var menu = new ContextMenu();
             var menu = new ContextMenu();
             menu.AddItem("Create Supplier Product", null, new Tuple<StockForecastOrderingItem, int>(item, idx), CreateSupplierProduct_Click);
             menu.AddItem("Create Supplier Product", null, new Tuple<StockForecastOrderingItem, int>(item, idx), CreateSupplierProduct_Click);
@@ -753,20 +758,31 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
 
 
             var comboBox = new ComboBox();
             var comboBox = new ComboBox();
 
 
-            comboBox.ItemsSource =
-                SupplierProducts.Where(x => x.SupplierLink.ID == Suppliers[idx].ID && x.Product.ID == instance.Product.ID)
+            var items = SupplierProducts.Where(x => x.SupplierLink.ID == Suppliers[idx].ID && x.Product.ID == instance.Product.ID)
                 .Select(x => new KeyValuePair<SupplierProduct?, string>(
                 .Select(x => new KeyValuePair<SupplierProduct?, string>(
                     x,
                     x,
-                    x.Style.ID != Guid.Empty ? $"{x.Dimensions.UnitSize}/{x.Style.Code}" : $"{x.Dimensions.UnitSize}"));
+                    x.Style.ID != Guid.Empty ? $"{x.Dimensions.UnitSize}/{x.Style.Code}" : $"{x.Dimensions.UnitSize}"))
+                .ToArray();
             comboBox.SelectedValuePath = "Key";
             comboBox.SelectedValuePath = "Key";
+
+            comboBox.ItemsSource = items;
             comboBox.DisplayMemberPath = "Value";
             comboBox.DisplayMemberPath = "Value";
 
 
             var qty = instance.GetQuantity(idx);
             var qty = instance.GetQuantity(idx);
 
 
             comboBox.Bind(ComboBox.SelectedValueProperty, qty, x => x.SupplierProduct);
             comboBox.Bind(ComboBox.SelectedValueProperty, qty, x => x.SupplierProduct);
+            comboBox.SelectionChanged += (o, e) =>
+            {
+                instance.CustomStrategy = true;
+                InvalidateRow(row);
+            };
 
 
             comboBox.VerticalContentAlignment = VerticalAlignment.Center;
             comboBox.VerticalContentAlignment = VerticalAlignment.Center;
             comboBox.Margin = new Thickness(2);
             comboBox.Margin = new Thickness(2);
+            if(items.Length == 0)
+            {
+                comboBox.IsEnabled = false;
+            }
 
 
             return comboBox;
             return comboBox;
         })
         })
@@ -837,21 +853,15 @@ public class StockForecastOrderingGrid : DynamicItemsListGrid<StockForecastOrder
         if (DynamicGridUtils.EditEntity(supplierProduct, customiseGrid: EditSupplierProductGrid))
         if (DynamicGridUtils.EditEntity(supplierProduct, customiseGrid: EditSupplierProductGrid))
         {
         {
             SupplierProducts.Add(supplierProduct);
             SupplierProducts.Add(supplierProduct);
+            var qty = item.GetQuantity(supplierIdx);
+            if(qty.SupplierProduct is null)
+            {
+                CalculateSupplierProduct(item, supplierIdx);
+            }
             InvalidateGrid();
             InvalidateGrid();
         }
         }
     }
     }
 
 
-    private static bool Matches(StockForecastOrderingItem item, SupplierProduct supplierProduct)
-    {
-        return item.Product.ID == supplierProduct.Product.ID
-            && item.Style.ID == supplierProduct.Style.ID
-            && item.Dimensions.Equals(supplierProduct.Dimensions);
-    }
-    private SupplierProduct? GetSupplierProduct(StockForecastOrderingItem item, Guid supplierID)
-    {
-        return SupplierProducts.FirstOrDefault(x => x.SupplierLink.ID == supplierID && Matches(item, x));
-    }
-
     private class CostAggregate : ISummaryAggregate
     private class CostAggregate : ISummaryAggregate
     {
     {
         public double Sum { get; private set; }
         public double Sum { get; private set; }