|
@@ -2,12 +2,15 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Windows;
|
|
|
+using System.Windows.Controls;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using Comal.Classes;
|
|
|
+using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
using InABox.DynamicGrid;
|
|
|
using InABox.Wpf;
|
|
|
using InABox.WPF;
|
|
|
+using PRSDimensionUtils;
|
|
|
|
|
|
namespace PRSDesktop.Grids;
|
|
|
|
|
@@ -15,6 +18,7 @@ public class ProductDimensionUnitGrid : DynamicDataGrid<ProductDimensionUnit>
|
|
|
{
|
|
|
|
|
|
private static readonly BitmapImage CONVERT = PRSDesktop.Resources.specifications.AsBitmapImage();
|
|
|
+ private Button ConvertButton = null!;
|
|
|
|
|
|
protected override void Init()
|
|
|
{
|
|
@@ -35,6 +39,25 @@ public class ProductDimensionUnitGrid : DynamicDataGrid<ProductDimensionUnit>
|
|
|
UpdateExpressions(rows.ToArray<ProductDimensionUnit>());
|
|
|
return false;
|
|
|
});
|
|
|
+ ConvertButton = AddButton("Convert Movements", null, (button, rows) =>
|
|
|
+ {
|
|
|
+ UpdateConversion(rows.ToArray<ProductDimensionUnit>());
|
|
|
+ return false;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void SelectItems(CoreRow[]? rows)
|
|
|
+ {
|
|
|
+ base.SelectItems(rows);
|
|
|
+
|
|
|
+ if(!(rows?.FirstOrDefault()?.Get<ProductDimensionUnit, string>(x => x.Conversion)).IsNullOrWhiteSpace())
|
|
|
+ {
|
|
|
+ ConvertButton.Visibility = Visibility.Visible;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ConvertButton.Visibility = Visibility.Collapsed;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private BitmapImage? ConvertImage(CoreRow? row)
|
|
@@ -53,7 +76,94 @@ public class ProductDimensionUnitGrid : DynamicDataGrid<ProductDimensionUnit>
|
|
|
item.Validate(errors);
|
|
|
}
|
|
|
|
|
|
- private void UpdateExpressions(ProductDimensionUnit[] items)
|
|
|
+ private static void UpdateConversion(ProductDimensionUnit[] items)
|
|
|
+ {
|
|
|
+ int? nMovements = null;
|
|
|
+ Exception? exception = null;
|
|
|
+
|
|
|
+ var filter = new Filter<StockMovement>(x => x.Dimensions.Unit.ID).InList(items.ToArray(x => x.ID));
|
|
|
+ var columns = Columns.Required<StockMovement>()
|
|
|
+ .Add(x => x.Units)
|
|
|
+ .Add(x => x.Received)
|
|
|
+ .Add(x => x.Issued)
|
|
|
+ .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local);
|
|
|
+ var nTotal = Client.Query(filter, Columns.None<StockMovement>().Add(x => x.ID)).Rows.Count;
|
|
|
+
|
|
|
+ if(!MessageWindow.ShowYesNo($"This will potentially update the dimensions of {nTotal} stock movements, and this cannot be reversed. Are you sure you wish to proceed?", "Confirm"))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Progress.ShowModal("Updating Dimensions", progress =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ progress.Report($"Updating Stock Movements");
|
|
|
+
|
|
|
+ var nProcessed = 0;
|
|
|
+ var nResult = 0;
|
|
|
+ var done = false;
|
|
|
+
|
|
|
+ var percentStep = Math.Max(nTotal / 100, 1);
|
|
|
+ var range = CoreRange.Database(1000);
|
|
|
+
|
|
|
+ while(nProcessed < nTotal && !done)
|
|
|
+ {
|
|
|
+ var rows = Client.Query(filter, columns, range: range).Rows;
|
|
|
+ if (rows.Count == 0) break;
|
|
|
+ if(rows.Count < 1000)
|
|
|
+ {
|
|
|
+ done = true;
|
|
|
+ }
|
|
|
+ range.Next();
|
|
|
+
|
|
|
+ var results = new List<StockMovement>(rows.Count);
|
|
|
+ for(int i = 0; i < rows.Count; ++i)
|
|
|
+ {
|
|
|
+ if(nProcessed % percentStep == 0)
|
|
|
+ {
|
|
|
+ progress.Report($"Updating Stock Movements: {(double)nProcessed / (double)nTotal * 100:F0}%");
|
|
|
+ }
|
|
|
+ var mvt = rows[i].ToObject<StockMovement>();
|
|
|
+ var qty = DimensionUtils.ConvertDimensions(mvt.Dimensions, mvt.Units, Client<ProductDimensionUnit>.Provider);
|
|
|
+ if(qty >= 0)
|
|
|
+ {
|
|
|
+ mvt.Received = qty;
|
|
|
+ mvt.Issued = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ mvt.Received = 0;
|
|
|
+ mvt.Issued = -qty;
|
|
|
+ }
|
|
|
+ if (mvt.IsChanged())
|
|
|
+ {
|
|
|
+ results.Add(mvt);
|
|
|
+ nResult++;
|
|
|
+ }
|
|
|
+ nProcessed++;
|
|
|
+ }
|
|
|
+ Client.Save(results, "Updated conversion script.");
|
|
|
+ }
|
|
|
+
|
|
|
+ nMovements = nResult;
|
|
|
+ }
|
|
|
+ catch(Exception e)
|
|
|
+ {
|
|
|
+ exception = e;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(nMovements is not null)
|
|
|
+ {
|
|
|
+ MessageWindow.ShowMessage($"Update successful: {nMovements.Value} movements updated.", "Success");
|
|
|
+ }
|
|
|
+ else if(exception is not null)
|
|
|
+ {
|
|
|
+ MessageWindow.ShowError("Error while updating dimensions", exception);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void UpdateExpressions(ProductDimensionUnit[] items)
|
|
|
{
|
|
|
Dictionary<Type, int>? results = null;
|
|
|
Exception? exception = null;
|