ProductDimensionUnitGrid.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.Wpf;
  9. using InABox.WPF;
  10. namespace PRSDesktop.Grids;
  11. public class ProductDimensionUnitGrid : DynamicDataGrid<ProductDimensionUnit>
  12. {
  13. protected override void Init()
  14. {
  15. base.Init();
  16. HiddenColumns.Add(x => x.Code);
  17. HiddenColumns.Add(x => x.HasQuantity);
  18. HiddenColumns.Add(x => x.HasLength);
  19. HiddenColumns.Add(x => x.HasWeight);
  20. HiddenColumns.Add(x => x.HasWidth);
  21. HiddenColumns.Add(x => x.HasHeight);
  22. AddButton("Update Expressions", null, (button, rows) =>
  23. {
  24. UpdateExpressions(rows.ToArray<ProductDimensionUnit>());
  25. return false;
  26. });
  27. }
  28. protected override void DoValidate(ProductDimensionUnit[] items, List<string> errors)
  29. {
  30. base.DoValidate(items, errors);
  31. foreach (var item in items)
  32. item.Validate(errors);
  33. }
  34. private void UpdateExpressions(ProductDimensionUnit[] items)
  35. {
  36. Dictionary<Type, int>? results = null;
  37. Exception? exception = null;
  38. Progress.ShowModal("Updating Dimensions", progress =>
  39. {
  40. try
  41. {
  42. results = DimensionUnitUtils.UpdateExpressions<ProductDimensionUnit, ProductDimensionUnitLink>(items, progress);
  43. }
  44. catch(Exception e)
  45. {
  46. exception = e;
  47. }
  48. });
  49. if(results is not null)
  50. {
  51. MessageWindow.ShowMessage($"Update successful:\n{string.Join("\n", results.Select(x => $"- {x.Key.Name}: {x.Value} items updated"))}", "Success");
  52. }
  53. else if(exception is not null)
  54. {
  55. MessageWindow.ShowError("Error while updating dimensions", exception);
  56. }
  57. }
  58. private bool ShouldUpdateExpressions = false;
  59. protected override void DoBeforeSave(IDynamicEditorForm editor, ProductDimensionUnit[] items)
  60. {
  61. base.DoBeforeSave(editor, items);
  62. ShouldUpdateExpressions = false;
  63. if(items.Any(x => x.HasOriginalValue(x => x.Format) || x.HasOriginalValue(x => x.Formula)))
  64. {
  65. if(MessageWindow.ShowYesNo("The format and/or formula has been changed; do you wish to update the UnitSize/Value of every item that uses dimension to match the new expression? (This may take a while)", "Update expressions?"))
  66. {
  67. ShouldUpdateExpressions = true;
  68. }
  69. }
  70. }
  71. protected override void DoAfterSave(IDynamicEditorForm editor, ProductDimensionUnit[] items)
  72. {
  73. base.DoAfterSave(editor, items);
  74. if (ShouldUpdateExpressions)
  75. {
  76. UpdateExpressions(items);
  77. }
  78. }
  79. protected override bool DoMerge(CoreRow[] rows)
  80. {
  81. var columns = new Column<ProductDimensionUnit>[]
  82. {
  83. new(x => x.HasLength),
  84. new(x => x.HasQuantity),
  85. new(x => x.HasHeight),
  86. new(x => x.HasWeight),
  87. new(x => x.HasWidth),
  88. };
  89. var target = rows[^1].ToObject<ProductDimensionUnit>();
  90. if(columns.Any(c => rows.Select(r => r[c.Property]).Distinct().Skip(1).Any()))
  91. {
  92. MessageWindow.ShowMessage("These dimension units are incompatible, and cannot be merged.\n\n(Dimension units can only be merged if they have the same [HasQuantity], [HasLength], [HasWidth], [HasHeight] and [HasWeight] values).", "Incompatible units", image: MessageWindow.WarningImage);
  93. return false;
  94. }
  95. if (base.DoMerge(rows))
  96. {
  97. if(MessageWindow.ShowYesNo(
  98. $"Do you wish to update the UnitSize/Value for every item that uses {target.Code}? (This may take a while)",
  99. "Update Expressions?"))
  100. {
  101. UpdateExpressions([target]);
  102. }
  103. return true;
  104. }
  105. return false;
  106. }
  107. protected override void CustomiseEditor(ProductDimensionUnit[] items, DynamicGridColumn column, BaseEditor editor)
  108. {
  109. base.CustomiseEditor(items, column, editor);
  110. if (column.ColumnName == nameof(ProductDimensionUnit.Conversion) && editor is ScriptEditor scriptEditor)
  111. {
  112. scriptEditor.Type = ScriptEditorType.TemplateEditor;
  113. scriptEditor.OnEditorClicked += () =>
  114. {
  115. var script = items.FirstOrDefault()?.Conversion.NotWhiteSpaceOr()
  116. ?? DimensionUnit.DefaultConvertDimensionsScript();
  117. var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
  118. if (editor.ShowDialog() == true)
  119. {
  120. foreach (var item in items)
  121. SetEditorValue(item, column.ColumnName, editor.Script);
  122. }
  123. };
  124. }
  125. }
  126. }