Optimisation.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.Wpf;
  12. using InABox.WPF;
  13. namespace PRSDesktop
  14. {
  15. public class OptimisationItem : BaseObject
  16. {
  17. public OptimisationItem()
  18. {
  19. Packets = new List<Guid>();
  20. }
  21. public string Specification { get; set; }
  22. public string Setout { get; set; }
  23. public double Area { get; set; }
  24. public int Quantity { get; set; }
  25. public List<Guid> Packets { get; }
  26. public bool Selected { get; set; }
  27. }
  28. public class OptimisationGrid : DynamicGrid<OptimisationItem>
  29. {
  30. private readonly BitmapImage disabled = PRSDesktop.Resources.disabled.AsBitmapImage();
  31. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  32. public OptimisationGrid()
  33. {
  34. Items = new List<OptimisationItem>();
  35. ActionColumns.Add(new DynamicTickColumn<OptimisationItem, bool>(x => x.Selected, tick, tick, disabled, CheckClick));
  36. HiddenColumns.Add(x => x.Selected);
  37. }
  38. public List<OptimisationItem> Items { get; }
  39. protected override void DeleteItems(params CoreRow[] row)
  40. {
  41. }
  42. protected override void SaveItem(OptimisationItem item)
  43. {
  44. }
  45. protected override OptimisationItem LoadItem(CoreRow row)
  46. {
  47. return Items[row.Index];
  48. }
  49. protected override void Reload(Filters<OptimisationItem> criteria, Columns<OptimisationItem> columns, ref SortOrder<OptimisationItem> sort,
  50. Action<CoreTable, Exception> action)
  51. {
  52. var table = new CoreTable();
  53. table.LoadColumns(typeof(OptimisationItem));
  54. table.LoadRows(Items);
  55. action.Invoke(table, null);
  56. }
  57. private bool CheckClick(CoreRow row)
  58. {
  59. var item = Items[row.Index];
  60. item.Selected = !item.Selected;
  61. return true;
  62. }
  63. protected override DynamicGridColumns LoadColumns()
  64. {
  65. var columns = new DynamicGridColumns();
  66. columns.Add(new DynamicGridColumn { ColumnName = "Setout", Caption = "Setout", Width = 0, Alignment = Alignment.MiddleLeft });
  67. columns.Add(new DynamicGridColumn { ColumnName = "Area", Caption = "m2", Format = "N4", Width = 70, Alignment = Alignment.MiddleCenter });
  68. columns.Add(new DynamicGridColumn { ColumnName = "Quantity", Caption = "#", Width = 50, Alignment = Alignment.MiddleCenter });
  69. return columns;
  70. }
  71. }
  72. /// <summary>
  73. /// Interaction logic for Optimisation.xaml
  74. /// </summary>
  75. public partial class Optimisation : ThemableWindow
  76. {
  77. private readonly OptimisationGrid grid = new();
  78. private readonly List<OptimisationItem> Items = new();
  79. private string SelectedSpecification = "";
  80. public Optimisation()
  81. {
  82. InitializeComponent();
  83. grid.SetValue(Grid.RowProperty, 1);
  84. grid.SetValue(Grid.ColumnProperty, 0);
  85. grid.SetValue(Grid.ColumnSpanProperty, 4);
  86. grid.Margin = new Thickness(5);
  87. layout.Children.Add(grid);
  88. LoadData();
  89. grid.Refresh(true, true);
  90. }
  91. private T GetAttribute<T>(ManufacturingItem item, string name)
  92. {
  93. if (item.Attributes.ContainsKey(name))
  94. return (T)Convert.ChangeType(item.Attributes[name], typeof(T));
  95. return default;
  96. }
  97. private void LoadData()
  98. {
  99. Specifications.Items.Clear();
  100. Specifications.Items.Add("");
  101. grid.Items.Clear();
  102. var setouts = new Dictionary<Guid, Setout>();
  103. var mcli = new Client<ManufacturingPacket>();
  104. var packets = mcli.Query(
  105. new Filter<ManufacturingPacket>(x => x.StageLink).NotLinkValid(),
  106. new Columns<ManufacturingPacket>(x => x.ID, x => x.ManufacturingItemID, x => x.SetoutLink.ID, x => x.ManufacturingTemplateLink.Code)
  107. );
  108. foreach (var row in packets.Rows)
  109. {
  110. var packetid = row.Get<ManufacturingPacket, Guid>(r => r.ID);
  111. var setoutid = row.Get<ManufacturingPacket, Guid>(r => r.SetoutLink.ID);
  112. var code = row.Get<ManufacturingPacket, string>(x => x.ManufacturingTemplateLink.Code);
  113. if (code.StartsWith("GMO"))
  114. {
  115. Setout setout = null;
  116. if (setouts.ContainsKey(setoutid))
  117. {
  118. setout = setouts[setoutid];
  119. }
  120. else
  121. {
  122. var scli = new Client<Setout>();
  123. setout = scli.Load(new Filter<Setout>(x => x.ID).IsEqualTo(setoutid)).FirstOrDefault();
  124. }
  125. if (setout != null)
  126. {
  127. setouts[setoutid] = setout;
  128. ManufacturingItem item = null; //setout.Manufacturing.FirstOrDefault(x => x.Code.Equals(code));
  129. if (item != null)
  130. {
  131. var spec = GetAttribute<string>(item, "Glass Specifications");
  132. if (!string.IsNullOrEmpty(spec))
  133. {
  134. if (!Specifications.Items.Contains(spec))
  135. Specifications.Items.Add(spec);
  136. var height = GetAttribute<double>(item, "Height");
  137. var width = GetAttribute<double>(item, "Width");
  138. var edge = GetAttribute<string>(item, "Edgework");
  139. var opt = Items.FirstOrDefault(x =>
  140. !string.IsNullOrEmpty(x.Specification) && x.Specification.Equals(spec) && x.Setout.Equals(setout.Number));
  141. if (opt == null)
  142. {
  143. opt = new OptimisationItem { Specification = spec, Setout = setout.Number };
  144. Items.Add(opt);
  145. }
  146. opt.Area += height / 1000F * (width / 1000F);
  147. opt.Quantity += item.Quantity;
  148. opt.Packets.Add(packetid);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. Specifications.SelectedValue = "";
  155. }
  156. private void Specifications_SelectionChanged(object sender, SelectionChangedEventArgs e)
  157. {
  158. SelectedSpecification = Specifications.SelectedValue.ToString();
  159. grid.Items.Clear();
  160. grid.Items.AddRange(Items.Where(x => x.Specification.Equals(SelectedSpecification)));
  161. grid.Items.ForEach(opt => opt.Selected = true);
  162. grid.Refresh(false, true);
  163. }
  164. private void OKButton_Click(object sender, RoutedEventArgs e)
  165. {
  166. MessageBox.Show("Not Implemented!");
  167. // Prompt for location of new MO sheet
  168. // Copy blank into location
  169. // Open sheet / find "Data" page
  170. // Iterate over OptimisationItems
  171. // Set Fields
  172. // Open for User to perform followup work
  173. Close();
  174. }
  175. private void CancelButton_Click(object sender, RoutedEventArgs e)
  176. {
  177. Close();
  178. }
  179. }
  180. }