ManufacturingItemGrid.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.Configuration;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. namespace PRSDesktop
  13. {
  14. public class ManufacturingItemGrid : DynamicGrid<ManufacturingItem>
  15. {
  16. private List<ManufacturingItem> _items;
  17. private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
  18. private readonly BitmapImage grouped = PRSDesktop.Resources.grouped.AsBitmapImage();
  19. private readonly FactorySetup settings = new GlobalConfiguration<FactorySetup>().Load();
  20. public ManufacturingItemGrid()
  21. {
  22. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows);
  23. //ActionColumns.Add(new DynamicActionColumn() { Action = BarcodeTypeClick, Image = BarcodeTypeImage });
  24. //HiddenColumns.Add(x => x.GroupedBarcode);
  25. SplitButton = AddButton("Split", PRSDesktop.Resources.split.AsBitmapImage(), SplitItems);
  26. }
  27. public Button SplitButton { get; }
  28. public List<ManufacturingItem> Items
  29. {
  30. get => _items;
  31. set
  32. {
  33. _items = value;
  34. Refresh(true, true);
  35. }
  36. }
  37. private bool SplitItems(Button sender, CoreRow[] rows)
  38. {
  39. if (rows.Length != 1)
  40. {
  41. MessageBox.Show("Please select an item to split!");
  42. return false;
  43. }
  44. var row = rows.First();
  45. var item = Items[row.Index];
  46. if (item.Quantity <= 1)
  47. {
  48. MessageBox.Show("Quantity must be >1 before splitting!");
  49. return false;
  50. }
  51. var Quantity = 1;
  52. if (NumberEdit.Execute("Quantity to Separate", 1, item.Quantity - 1, ref Quantity))
  53. {
  54. var newitem = new ManufacturingItem();
  55. CoreUtils.Clone(item, newitem);
  56. newitem.Quantity = newitem.Quantity - Quantity;
  57. item.Description = item.Description + " - copy";
  58. item.Quantity = Quantity;
  59. item.ID = Guid.NewGuid();
  60. Items.Insert(row.Index, newitem);
  61. return true;
  62. }
  63. return false;
  64. }
  65. protected override void SelectItems(CoreRow[] rows)
  66. {
  67. base.SelectItems(rows);
  68. var bQty = true;
  69. if (rows != null)
  70. foreach (var row in rows)
  71. if (row.Get<ManufacturingItem, int>(x => x.Quantity) <= 1)
  72. bQty = false;
  73. if (!bQty)
  74. SplitButton.IsEnabled = false;
  75. }
  76. //private bool ImportItems(Button arg1, CoreRow arg2)
  77. //{
  78. // Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
  79. // ofd.Filter = "Excel Files (*.xls,*.xlsx)|*.xlsx";
  80. // if (ofd.ShowDialog() == true)
  81. // {
  82. // try
  83. // {
  84. // using (FileStream file = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
  85. // {
  86. // IWorkbook xls = WorkbookFactory.Create(file);
  87. // ISheet sheet = xls.GetSheet("MASTERLIST");
  88. // IEnumerator rows = sheet.GetRowEnumerator();
  89. // // rows.MoveNext();
  90. // // rows.MoveNext();
  91. // while (rows.MoveNext())
  92. // // IRow headerrow = (IRow)rows.Current;
  93. // // int LocationColumn = GetColumn(headerrow, "LOCATION");
  94. // // int WindowColumn = GetColumn(headerrow, "WINDOW MARK");
  95. // // int SetoutColumn = GetColumn(headerrow, "FRAME - SETOUT");
  96. // // int IDColumn = GetColumn(headerrow, "MARK");
  97. // // int QtyColumn = GetColumn(headerrow, "QTY");
  98. // // int SpecificationColumn = GetColumn(headerrow, "GLASS SPECIFICATIONS");
  99. // // int HeightColumn = GetColumn(headerrow, "HEIGHT");
  100. // // int WidthColumn = GetColumn(headerrow, "WIDTH");
  101. // // int EdgeColumn = GetColumn(headerrow, "EDGEWORK");
  102. // // int SupplierColumn = GetColumn(headerrow, "SUPPLIER");
  103. // // while (rows.MoveNext())
  104. // // {
  105. // // IRow row = (IRow)rows.Current;
  106. // // String SetoutNumber = row.GetCell(SetoutColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue;
  107. // // if (!String.IsNullOrEmpty(SetoutNumber))
  108. // // {
  109. // // String ID = row.GetCell(IDColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue;
  110. // // if (String.IsNullOrWhiteSpace(ID))
  111. // // throw new Exception(String.Format("Row [{0}]: MARK Column is blank!", row.RowNum + 1));
  112. // // String Supplier = row.GetCell(SupplierColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue;
  113. // // if (String.IsNullOrWhiteSpace(Supplier))
  114. // // throw new Exception(String.Format("Row [{0}]: SUPPLIER Column is blank!", row.RowNum + 1));
  115. // // Progress.SetMessage(String.Format("Row [{0}]: Updating {1} ({2})", row.RowNum + 1, SetoutNumber, ID));
  116. // // Setout setout = new Client<Setout>().Load(new Filter<Setout>(x => x.Number).IsEqualTo(SetoutNumber)).FirstOrDefault();
  117. // // if (setout == null)
  118. // // {
  119. // // int JobNumber = int.Parse(SetoutNumber.Split('-')[0].Trim());
  120. // // Job job = new Client<Job>().Load(new Filter<Job>(x => x.Number).IsEqualTo(JobNumber)).FirstOrDefault();
  121. // // setout = new Setout();
  122. // // setout.Job = job ?? throw new Exception(String.Format("Row [{0}]: Job does not exist for setout [{1}]!", row.RowNum + 1, SetoutNumber));
  123. // // setout.Number = SetoutNumber;
  124. // // setout.Title = row.GetCell(WindowColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue;
  125. // // setout.WindowNumber = row.GetCell(LocationColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue;
  126. // // setout.Description = String.Format("Glasswork for {0}", SetoutNumber);
  127. // // }
  128. // // DataTable packets = new Client<ManufacturingPacket>().Query(
  129. // // new Filter<ManufacturingPacket>(x => x.SetoutID).IsEqualTo(setout.ID),
  130. // // new Columns<ManufacturingPacket>(x => x.ManufacturingItemID, x => x.Issued),
  131. // // null
  132. // // );
  133. // // String Template = (Supplier.Equals("INHOUSE") || Supplier.Equals("IN HOUSE")) ? "GMO" : "GPO";
  134. // // String ItemID = String.Format("{0}/{1}", Template, ID);
  135. // // ManufacturingItem item = setout.Manufacturing.FirstOrDefault(x => x.Code.Equals(ItemID));
  136. // // if (item == null)
  137. // // {
  138. // // item = new ManufacturingItem();
  139. // // setout.Manufacturing.Add(item);
  140. // // }
  141. // // DataRow pktrow = packets.Rows.FirstOrDefault(x => x.Get<ManufacturingPacket, Guid>(r => r.ManufacturingItemID).Equals(item.ID));
  142. // // bool bIssued = ((pktrow != null) && (!pktrow.Get<ManufacturingPacket, DateTime>(x => x.Issued).IsEmpty()));
  143. // // if (!item.Code.StartsWith(Template))
  144. // // {
  145. // // if (bIssued)
  146. // // throw new Exception(String.Format("Row [{0}]: Cannot Change Supplier / In House after packet has been issued!", row.RowNum + 1));
  147. // // FactoryTemplate template = settings.Templates.Where(x => x.Code.Equals(Template)).FirstOrDefault();
  148. // // if (template == null)
  149. // // throw new Exception(String.Format("Row [{0}]: {1} Manufacturing Template does not exist!", row.RowNum + 1, Template));
  150. // // item.TemplateID = template.ID;
  151. // // item.Stages.Clear();
  152. // // foreach (StageTemplate stage in template.Stages)
  153. // // {
  154. // // FactorySection section = settings.Sections.Where(x => x.ID.Equals(stage.SectionID)).FirstOrDefault();
  155. // // item.Stages.Add(new SetoutStage() { SectionID = stage.SectionID, Name = section != null ? section.Name : "", Minutes = stage.Minutes, Sequence = stage.Sequence });
  156. // // }
  157. // // foreach (var attr in template.Attributes)
  158. // // item.Attributes[attr.Name] = "";
  159. // // item.Group = template.Group;
  160. // // }
  161. // // item.Code = ItemID;
  162. // // item.Purchased = Template == "GPO";
  163. // // item.Description = String.Format("{0} ({1} x {2})",
  164. // // row.GetCell(SpecificationColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue,
  165. // // row.GetCell(WidthColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString(),
  166. // // row.GetCell(HeightColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString()
  167. // // );
  168. // // int Qty = (int)row.GetCell(QtyColumn, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue;
  169. // // item.Quantity = Qty;
  170. // // String[] keys = item.Attributes.Keys.ToArray();
  171. // // foreach (String key in keys)
  172. // // {
  173. // // try
  174. // // {
  175. // // int col = GetColumn(headerrow, key);
  176. // // item.Attributes[key] = row.GetCell(col, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  177. // // }
  178. // // catch
  179. // // {
  180. // // }
  181. // // }
  182. // // new Client<Setout>().Save(setout, "Imported from Master Glass Schedule");
  183. // // }
  184. // // }
  185. // // }
  186. // Progress.Close();
  187. // MessageBox.Show("Import Completed!");
  188. // Refresh();
  189. // }
  190. // catch (Exception e)
  191. // {
  192. // Progress.Close();
  193. // MessageBox.Show("Error while Importing File!\n\n" + e.Message);
  194. // }
  195. //}
  196. //}
  197. private BitmapImage BarcodeTypeImage(CoreRow row)
  198. {
  199. if (row == null)
  200. return barcode;
  201. return row.Get<ManufacturingItem, bool>(x => x.GroupedBarcode) ? grouped : null;
  202. }
  203. private bool BarcodeTypeClick(CoreRow row)
  204. {
  205. var item = _items[row.Index];
  206. item.GroupedBarcode = !item.GroupedBarcode;
  207. return true;
  208. }
  209. public override void ConfigureColumns(DynamicGridColumns columns /*, bool dolookups = true */)
  210. {
  211. var templates = new Dictionary<object, object>();
  212. foreach (var template in settings.Templates)
  213. templates[template.ID] = template.Name;
  214. columns.Clear();
  215. columns.AddRange(
  216. new[]
  217. {
  218. new() { ColumnName = "TemplateID", Caption = "Template", Lookups = templates, Width = 1, Alignment = Alignment.MiddleLeft },
  219. //new DynamicGridColumn(){ ColumnName = "Code", Caption="#", Width = 30, Alignment = DynamicGridColumnAlignment.MiddleCenter },
  220. new DynamicGridColumn { ColumnName = "Serial", Caption = "Serial", Width = 100, Alignment = Alignment.MiddleLeft },
  221. new DynamicGridColumn { ColumnName = "Description", Caption = "Description", Alignment = Alignment.MiddleLeft },
  222. new DynamicGridColumn { ColumnName = "Quantity", Width = 30, Caption = "Qty", Alignment = Alignment.MiddleCenter }
  223. }
  224. );
  225. }
  226. protected override void Reload(Filters<ManufacturingItem> criteria, Columns<ManufacturingItem> columns, ref SortOrder<ManufacturingItem> sort,
  227. Action<CoreTable, Exception> action)
  228. {
  229. var result = new CoreTable();
  230. result.Columns.Add(new CoreColumn { ColumnName = "TemplateID", DataType = typeof(Guid) });
  231. result.Columns.Add(new CoreColumn { ColumnName = "Serial", DataType = typeof(string) });
  232. result.Columns.Add(new CoreColumn { ColumnName = "Description", DataType = typeof(string) });
  233. result.Columns.Add(new CoreColumn { ColumnName = "Quantity", DataType = typeof(int) });
  234. result.LoadColumns(typeof(ManufacturingItem));
  235. result.LoadRows(Items);
  236. action.Invoke(result, null);
  237. }
  238. protected override ManufacturingItem LoadItem(CoreRow row)
  239. {
  240. return Items[row.Index];
  241. }
  242. protected override void SaveItem(ManufacturingItem item)
  243. {
  244. if (!Items.Contains(item))
  245. Items.Add(item);
  246. }
  247. protected override void DeleteItems(params CoreRow[] rows)
  248. {
  249. foreach (var index in rows.Select(x => x.Index).OrderByDescending(i => i))
  250. Items.RemoveAt(index);
  251. }
  252. public override bool EditItems(ManufacturingItem[] items, Func<Type, CoreTable> PageDataHandler, bool PreloadPages = false)
  253. {
  254. var templates = new Dictionary<Guid, Guid>();
  255. foreach (var item in items)
  256. templates[item.ID] = item.TemplateID;
  257. if (base.EditItems(items, PageDataHandler, PreloadPages))
  258. {
  259. foreach (var item in items)
  260. if (item.TemplateID != templates[item.ID])
  261. {
  262. item.Stages.Clear();
  263. var template = settings.Templates.Where(x => x.ID.Equals(item.TemplateID)).FirstOrDefault();
  264. if (template != null)
  265. {
  266. item.Group = template.Group;
  267. item.Code = template.Code;
  268. item.Stages.Clear();
  269. foreach (var stage in template.Stages)
  270. {
  271. var section = settings.Sections.Where(x => x.ID.Equals(stage.SectionID)).FirstOrDefault();
  272. item.Stages.Add(
  273. new SetoutStage
  274. {
  275. SectionID = stage.SectionID,
  276. Name = section != null ? section.Name : "",
  277. Minutes = stage.Minutes,
  278. Sequence = stage.Sequence,
  279. QualityChecks = stage.QualityChecks
  280. }
  281. );
  282. }
  283. }
  284. }
  285. return true;
  286. }
  287. return false;
  288. }
  289. }
  290. }