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; namespace PRSDesktop { public class OptimisationItem : BaseObject { public OptimisationItem() { Packets = new List(); } public string Specification { get; set; } public string Setout { get; set; } public double Area { get; set; } public int Quantity { get; set; } public List Packets { get; } public bool Selected { get; set; } } public class OptimisationGrid : DynamicGrid { private readonly BitmapImage disabled = PRSDesktop.Resources.disabled.AsBitmapImage(); private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage(); public OptimisationGrid() { Items = new List(); ActionColumns.Add(new DynamicTickColumn(x => x.Selected, tick, tick, disabled, CheckClick)); HiddenColumns.Add(x => x.Selected); } public List Items { get; } protected override void DeleteItems(params CoreRow[] row) { } protected override void SaveItem(OptimisationItem item) { } protected override OptimisationItem LoadItem(CoreRow row) { return Items[row.Index]; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { var table = new CoreTable(); table.LoadColumns(typeof(OptimisationItem)); table.LoadRows(Items); action.Invoke(table, null); } private bool CheckClick(CoreRow row) { var item = Items[row.Index]; item.Selected = !item.Selected; return true; } protected override DynamicGridColumns LoadColumns() { var columns = new DynamicGridColumns(); columns.Add(new DynamicGridColumn { ColumnName = "Setout", Caption = "Setout", Width = 0, Alignment = Alignment.MiddleLeft }); columns.Add(new DynamicGridColumn { ColumnName = "Area", Caption = "m2", Format = "N4", Width = 70, Alignment = Alignment.MiddleCenter }); columns.Add(new DynamicGridColumn { ColumnName = "Quantity", Caption = "#", Width = 50, Alignment = Alignment.MiddleCenter }); return columns; } } /// /// Interaction logic for Optimisation.xaml /// public partial class Optimisation : ThemableWindow { private readonly OptimisationGrid grid = new(); private readonly List Items = new(); private string SelectedSpecification = ""; public Optimisation() { InitializeComponent(); grid.SetValue(Grid.RowProperty, 1); grid.SetValue(Grid.ColumnProperty, 0); grid.SetValue(Grid.ColumnSpanProperty, 4); grid.Margin = new Thickness(5); layout.Children.Add(grid); LoadData(); grid.Refresh(true, true); } private T GetAttribute(ManufacturingItem item, string name) { if (item.Attributes.ContainsKey(name)) return (T)Convert.ChangeType(item.Attributes[name], typeof(T)); return default; } private void LoadData() { Specifications.Items.Clear(); Specifications.Items.Add(""); grid.Items.Clear(); var setouts = new Dictionary(); var mcli = new Client(); var packets = mcli.Query( new Filter(x => x.StageLink).NotLinkValid(), new Columns(x => x.ID, x => x.ManufacturingItemID, x => x.SetoutLink.ID, x => x.ManufacturingTemplateLink.Code) ); foreach (var row in packets.Rows) { var packetid = row.Get(r => r.ID); var setoutid = row.Get(r => r.SetoutLink.ID); var code = row.Get(x => x.ManufacturingTemplateLink.Code); if (code.StartsWith("GMO")) { Setout setout = null; if (setouts.ContainsKey(setoutid)) { setout = setouts[setoutid]; } else { var scli = new Client(); setout = scli.Load(new Filter(x => x.ID).IsEqualTo(setoutid)).FirstOrDefault(); } if (setout != null) { setouts[setoutid] = setout; ManufacturingItem item = null; //setout.Manufacturing.FirstOrDefault(x => x.Code.Equals(code)); if (item != null) { var spec = GetAttribute(item, "Glass Specifications"); if (!string.IsNullOrEmpty(spec)) { if (!Specifications.Items.Contains(spec)) Specifications.Items.Add(spec); var height = GetAttribute(item, "Height"); var width = GetAttribute(item, "Width"); var edge = GetAttribute(item, "Edgework"); var opt = Items.FirstOrDefault(x => !string.IsNullOrEmpty(x.Specification) && x.Specification.Equals(spec) && x.Setout.Equals(setout.Number)); if (opt == null) { opt = new OptimisationItem { Specification = spec, Setout = setout.Number }; Items.Add(opt); } opt.Area += height / 1000F * (width / 1000F); opt.Quantity += item.Quantity; opt.Packets.Add(packetid); } } } } } Specifications.SelectedValue = ""; } private void Specifications_SelectionChanged(object sender, SelectionChangedEventArgs e) { SelectedSpecification = Specifications.SelectedValue.ToString(); grid.Items.Clear(); grid.Items.AddRange(Items.Where(x => x.Specification.Equals(SelectedSpecification))); grid.Items.ForEach(opt => opt.Selected = true); grid.Refresh(false, true); } private void OKButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Not Implemented!"); // Prompt for location of new MO sheet // Copy blank into location // Open sheet / find "Data" page // Iterate over OptimisationItems // Set Fields // Open for User to perform followup work Close(); } private void CancelButton_Click(object sender, RoutedEventArgs e) { Close(); } } }