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.Configuration; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { public class ManufacturingItemGrid : DynamicGrid { private List _items; private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage(); private readonly BitmapImage grouped = PRSDesktop.Resources.grouped.AsBitmapImage(); private readonly FactorySetup settings = new GlobalConfiguration().Load(); public ManufacturingItemGrid() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows); //ActionColumns.Add(new DynamicActionColumn() { Action = BarcodeTypeClick, Image = BarcodeTypeImage }); //HiddenColumns.Add(x => x.GroupedBarcode); SplitButton = AddButton("Split", PRSDesktop.Resources.split.AsBitmapImage(), SplitItems); } public Button SplitButton { get; } public List Items { get => _items; set { _items = value; Refresh(true, true); } } private bool SplitItems(Button sender, CoreRow[] rows) { if (rows.Length != 1) { MessageBox.Show("Please select an item to split!"); return false; } var row = rows.First(); var item = Items[row.Index]; if (item.Quantity <= 1) { MessageBox.Show("Quantity must be >1 before splitting!"); return false; } var Quantity = 1; if (NumberEdit.Execute("Quantity to Separate", 1, item.Quantity - 1, ref Quantity)) { var newitem = new ManufacturingItem(); CoreUtils.Clone(item, newitem); newitem.Quantity = newitem.Quantity - Quantity; item.Description = item.Description + " - copy"; item.Quantity = Quantity; item.ID = Guid.NewGuid(); Items.Insert(row.Index, newitem); return true; } return false; } protected override void SelectItems(CoreRow[] rows) { base.SelectItems(rows); var bQty = true; if (rows != null) foreach (var row in rows) if (row.Get(x => x.Quantity) <= 1) bQty = false; if (!bQty) SplitButton.IsEnabled = false; } //private bool ImportItems(Button arg1, CoreRow arg2) //{ // Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); // ofd.Filter = "Excel Files (*.xls,*.xlsx)|*.xlsx"; // if (ofd.ShowDialog() == true) // { // try // { // using (FileStream file = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)) // { // IWorkbook xls = WorkbookFactory.Create(file); // ISheet sheet = xls.GetSheet("MASTERLIST"); // IEnumerator rows = sheet.GetRowEnumerator(); // // rows.MoveNext(); // // rows.MoveNext(); // while (rows.MoveNext()) // // IRow headerrow = (IRow)rows.Current; // // int LocationColumn = GetColumn(headerrow, "LOCATION"); // // int WindowColumn = GetColumn(headerrow, "WINDOW MARK"); // // int SetoutColumn = GetColumn(headerrow, "FRAME - SETOUT"); // // int IDColumn = GetColumn(headerrow, "MARK"); // // int QtyColumn = GetColumn(headerrow, "QTY"); // // int SpecificationColumn = GetColumn(headerrow, "GLASS SPECIFICATIONS"); // // int HeightColumn = GetColumn(headerrow, "HEIGHT"); // // int WidthColumn = GetColumn(headerrow, "WIDTH"); // // int EdgeColumn = GetColumn(headerrow, "EDGEWORK"); // // int SupplierColumn = GetColumn(headerrow, "SUPPLIER"); // // while (rows.MoveNext()) // // { // // IRow row = (IRow)rows.Current; // // String SetoutNumber = row.GetCell(SetoutColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue; // // if (!String.IsNullOrEmpty(SetoutNumber)) // // { // // String ID = row.GetCell(IDColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue; // // if (String.IsNullOrWhiteSpace(ID)) // // throw new Exception(String.Format("Row [{0}]: MARK Column is blank!", row.RowNum + 1)); // // String Supplier = row.GetCell(SupplierColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue; // // if (String.IsNullOrWhiteSpace(Supplier)) // // throw new Exception(String.Format("Row [{0}]: SUPPLIER Column is blank!", row.RowNum + 1)); // // Progress.SetMessage(String.Format("Row [{0}]: Updating {1} ({2})", row.RowNum + 1, SetoutNumber, ID)); // // Setout setout = new Client().Load(new Filter(x => x.Number).IsEqualTo(SetoutNumber)).FirstOrDefault(); // // if (setout == null) // // { // // int JobNumber = int.Parse(SetoutNumber.Split('-')[0].Trim()); // // Job job = new Client().Load(new Filter(x => x.Number).IsEqualTo(JobNumber)).FirstOrDefault(); // // setout = new Setout(); // // setout.Job = job ?? throw new Exception(String.Format("Row [{0}]: Job does not exist for setout [{1}]!", row.RowNum + 1, SetoutNumber)); // // setout.Number = SetoutNumber; // // setout.Title = row.GetCell(WindowColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue; // // setout.WindowNumber = row.GetCell(LocationColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue; // // setout.Description = String.Format("Glasswork for {0}", SetoutNumber); // // } // // DataTable packets = new Client().Query( // // new Filter(x => x.SetoutID).IsEqualTo(setout.ID), // // new Columns(x => x.ManufacturingItemID, x => x.Issued), // // null // // ); // // String Template = (Supplier.Equals("INHOUSE") || Supplier.Equals("IN HOUSE")) ? "GMO" : "GPO"; // // String ItemID = String.Format("{0}/{1}", Template, ID); // // ManufacturingItem item = setout.Manufacturing.FirstOrDefault(x => x.Code.Equals(ItemID)); // // if (item == null) // // { // // item = new ManufacturingItem(); // // setout.Manufacturing.Add(item); // // } // // DataRow pktrow = packets.Rows.FirstOrDefault(x => x.Get(r => r.ManufacturingItemID).Equals(item.ID)); // // bool bIssued = ((pktrow != null) && (!pktrow.Get(x => x.Issued).IsEmpty())); // // if (!item.Code.StartsWith(Template)) // // { // // if (bIssued) // // throw new Exception(String.Format("Row [{0}]: Cannot Change Supplier / In House after packet has been issued!", row.RowNum + 1)); // // FactoryTemplate template = settings.Templates.Where(x => x.Code.Equals(Template)).FirstOrDefault(); // // if (template == null) // // throw new Exception(String.Format("Row [{0}]: {1} Manufacturing Template does not exist!", row.RowNum + 1, Template)); // // item.TemplateID = template.ID; // // item.Stages.Clear(); // // foreach (StageTemplate stage in template.Stages) // // { // // FactorySection section = settings.Sections.Where(x => x.ID.Equals(stage.SectionID)).FirstOrDefault(); // // item.Stages.Add(new SetoutStage() { SectionID = stage.SectionID, Name = section != null ? section.Name : "", Minutes = stage.Minutes, Sequence = stage.Sequence }); // // } // // foreach (var attr in template.Attributes) // // item.Attributes[attr.Name] = ""; // // item.Group = template.Group; // // } // // item.Code = ItemID; // // item.Purchased = Template == "GPO"; // // item.Description = String.Format("{0} ({1} x {2})", // // row.GetCell(SpecificationColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue, // // row.GetCell(WidthColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString(), // // row.GetCell(HeightColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString() // // ); // // int Qty = (int)row.GetCell(QtyColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue; // // item.Quantity = Qty; // // String[] keys = item.Attributes.Keys.ToArray(); // // foreach (String key in keys) // // { // // try // // { // // int col = GetColumn(headerrow, key); // // item.Attributes[key] = row.GetCell(col, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString(); // // } // // catch // // { // // } // // } // // new Client().Save(setout, "Imported from Master Glass Schedule"); // // } // // } // // } // Progress.Close(); // MessageBox.Show("Import Completed!"); // Refresh(); // } // catch (Exception e) // { // Progress.Close(); // MessageBox.Show("Error while Importing File!\n\n" + e.Message); // } //} //} private BitmapImage BarcodeTypeImage(CoreRow row) { if (row == null) return barcode; return row.Get(x => x.GroupedBarcode) ? grouped : null; } private bool BarcodeTypeClick(CoreRow row) { var item = _items[row.Index]; item.GroupedBarcode = !item.GroupedBarcode; return true; } public override void ConfigureColumns(DynamicGridColumns columns /*, bool dolookups = true */) { var templates = new Dictionary(); foreach (var template in settings.Templates) templates[template.ID] = template.Name; columns.Clear(); columns.AddRange( new[] { new() { ColumnName = "TemplateID", Caption = "Template", Lookups = templates, Width = 1, Alignment = Alignment.MiddleLeft }, //new DynamicGridColumn(){ ColumnName = "Code", Caption="#", Width = 30, Alignment = DynamicGridColumnAlignment.MiddleCenter }, new DynamicGridColumn { ColumnName = "Serial", Caption = "Serial", Width = 100, Alignment = Alignment.MiddleLeft }, new DynamicGridColumn { ColumnName = "Description", Caption = "Description", Alignment = Alignment.MiddleLeft }, new DynamicGridColumn { ColumnName = "Quantity", Width = 30, Caption = "Qty", Alignment = Alignment.MiddleCenter } } ); } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { var result = new CoreTable(); result.Columns.Add(new CoreColumn { ColumnName = "TemplateID", DataType = typeof(Guid) }); result.Columns.Add(new CoreColumn { ColumnName = "Serial", DataType = typeof(string) }); result.Columns.Add(new CoreColumn { ColumnName = "Description", DataType = typeof(string) }); result.Columns.Add(new CoreColumn { ColumnName = "Quantity", DataType = typeof(int) }); result.LoadColumns(typeof(ManufacturingItem)); result.LoadRows(Items); action.Invoke(result, null); } protected override ManufacturingItem LoadItem(CoreRow row) { return Items[row.Index]; } protected override void SaveItem(ManufacturingItem item) { if (!Items.Contains(item)) Items.Add(item); } protected override void DeleteItems(params CoreRow[] rows) { foreach (var index in rows.Select(x => x.Index).OrderByDescending(i => i)) Items.RemoveAt(index); } public override bool EditItems(ManufacturingItem[] items, Func PageDataHandler, bool PreloadPages = false) { var templates = new Dictionary(); foreach (var item in items) templates[item.ID] = item.TemplateID; if (base.EditItems(items, PageDataHandler, PreloadPages)) { foreach (var item in items) if (item.TemplateID != templates[item.ID]) { item.Stages.Clear(); var template = settings.Templates.Where(x => x.ID.Equals(item.TemplateID)).FirstOrDefault(); if (template != null) { item.Group = template.Group; item.Code = template.Code; item.Stages.Clear(); foreach (var stage in template.Stages) { var section = settings.Sections.Where(x => x.ID.Equals(stage.SectionID)).FirstOrDefault(); item.Stages.Add( new SetoutStage { SectionID = stage.SectionID, Name = section != null ? section.Name : "", Minutes = stage.Minutes, Sequence = stage.Sequence, QualityChecks = stage.QualityChecks } ); } } } return true; } return false; } } }