using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop; public class FactoryComponentsGrid : DynamicDataGrid { private ManufacturingPacket? _currentPacket; public ManufacturingPacket? CurrentPacket { get => _currentPacket; set { _currentPacket = value; Refresh(false,true); } } protected override void Init() { base.Init(); HiddenColumns.Add(x=>x.Consumed); ActionColumns.Add( new DynamicTickColumn( x=>x.Consumed, PRSDesktop.Resources.tick.AsBitmapImage(), PRSDesktop.Resources.tick.AsBitmapImage(), null, ConsumeItem) { Position = DynamicActionColumnPosition.Start }); } private bool ConsumeItem(CoreRow? row) { var rows = row == null ? Data.Rows : [row]; Dictionary map = new Dictionary( rows.Select(r => new KeyValuePair(r.ToObject(), r))); if (map.Keys.Any(x => x.Consumed == 0)) foreach (var packet in map.Keys) packet.Consumed = packet.Quantity; else foreach (var packet in map.Keys) packet.Consumed = 0; foreach (var key in map.Keys.Where(x => x.IsChanged())) { map[key].Set(x => x.Consumed, key.Consumed); InvalidateRow(map[key]); } Client.Save(map.Keys.Where(x => x.IsChanged()), "Updated from Factory Floor Screen", (o, e) => { }); return false; } public override DynamicGridColumns GenerateColumns() { var result = new DynamicGridColumns(); result.Add(x=>x.Product.Code, 100); result.Add(x => x.Product.Name, 0); result.Add(x => x.Dimensions.UnitSize, 70); result.Add(x => x.Quantity, 60); return result; } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.Clear(); options.SelectColumns = true; options.FilterRows = true; options.HideDatabaseFilters = true; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { var filter = _currentPacket == null ? new Filter().None() : new Filter(x => x.Packet.ID).IsEqualTo(_currentPacket.ID); criteria.Add(filter); base.Reload(criteria, columns, ref sort, token, action); } }