JobBillOfMaterialsItemsGrid.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Controls;
  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. public class JobBillOfMaterialsItemGrid : DynamicDataGrid<JobBillOfMaterialsItem>, IMasterDetailControl<JobBillOfMaterials, JobBillOfMaterialsItem>
  15. {
  16. public Job? Job { get; set; }
  17. private JobBillOfMaterials? _master;
  18. public JobBillOfMaterials? Master
  19. {
  20. get => _master;
  21. set
  22. {
  23. _master = value;
  24. Reconfigure();
  25. CheckVisibility();
  26. }
  27. }
  28. public Filter<JobBillOfMaterialsItem> MasterDetailFilter => Master != null
  29. ? Master.ID != Guid.Empty
  30. ? new Filter<JobBillOfMaterialsItem>(x => x.BillOfMaterials.ID).IsEqualTo(Master.ID)
  31. : new Filter<JobBillOfMaterialsItem>().None()
  32. : Job is not null && Job.ID != Guid.Empty
  33. ? new Filter<JobBillOfMaterialsItem>(x => x.BillOfMaterials.Job.ID).IsEqualTo(Job.ID)
  34. : new Filter<JobBillOfMaterialsItem>().None();
  35. private Button CreateRequiButton;
  36. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  37. {
  38. base.DoReconfigure(options);
  39. options.AddRange(
  40. DynamicGridOption.RecordCount,
  41. DynamicGridOption.SelectColumns,
  42. DynamicGridOption.ExportData,
  43. DynamicGridOption.FilterRows,
  44. DynamicGridOption.MultiSelect,
  45. DynamicGridOption.EditRows,
  46. DynamicGridOption.DeleteRows
  47. );
  48. if(Master is not null)
  49. {
  50. options.Add(DynamicGridOption.AddRows);
  51. }
  52. else
  53. {
  54. options.Remove(DynamicGridOption.AddRows);
  55. }
  56. }
  57. private void CheckVisibility()
  58. {
  59. CreateRequiButton.Visibility = Master is not null && Security.CanEdit<JobRequisition>()
  60. ? Visibility.Visible
  61. : Visibility.Collapsed;
  62. }
  63. protected override void Init()
  64. {
  65. base.Init();
  66. foreach(var column in new Columns<JobBillOfMaterialsItem>()
  67. .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Data)
  68. .GetColumns())
  69. {
  70. HiddenColumns.Add(column);
  71. }
  72. HiddenColumns.Add(x => x.Product.ID);
  73. HiddenColumns.Add(x => x.Product.Code);
  74. HiddenColumns.Add(x => x.Product.Name);
  75. HiddenColumns.Add(x => x.Style.ID);
  76. HiddenColumns.Add(x => x.Style.Code);
  77. HiddenColumns.Add(x => x.Style.Description);
  78. HiddenColumns.Add(x => x.BillOfMaterials.ID);
  79. HiddenColumns.Add(x => x.BillOfMaterials.Description);
  80. HiddenColumns.Add(x => x.Job.ID);
  81. HiddenColumns.Add(x => x.PurchaseOrderItem.ID);
  82. HiddenColumns.Add(x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber);
  83. HiddenColumns.Add(x => x.PurchaseOrderItem.ReceivedDate);
  84. HiddenColumns.Add(x => x.Packet.ID);
  85. CreateRequiButton = AddButton("Create Requi", null, CreateRequi);
  86. AddButton("Create PO", null, CreatePO);
  87. CheckVisibility();
  88. }
  89. private bool CreateRequi(System.Windows.Controls.Button btn, CoreRow[] rows)
  90. {
  91. if (Master is null) return false;
  92. if (!rows.Any())
  93. {
  94. MessageBox.Show("Please select at least one row to add to Requi");
  95. return false;
  96. }
  97. var jobRequi = new JobRequisition();
  98. jobRequi.Job.ID = Master.Job.ID;
  99. jobRequi.Job.Synchronise(Master.Job);
  100. jobRequi.Description = "New Requisition";
  101. var page = new JobRequisitionGrid();
  102. page.OnAfterSave += (form, items) => { MessageBox.Show("Success - Job Requisition Raised."); };
  103. return page.EditItems(new[] { jobRequi }, LoadJobRequiItems, true);
  104. }
  105. private CoreTable? LoadJobRequiItems(Type arg)
  106. {
  107. if (Master is null || arg != typeof(JobRequisitionItem)) return null;
  108. var result = new CoreTable();
  109. result.LoadColumns(typeof(JobRequisitionItem));
  110. var requiItems = new List<JobRequisitionItem>();
  111. foreach (var BOMitem in SelectedRows.ToObjects<JobBillOfMaterialsItem>())
  112. {
  113. var requiItem = new JobRequisitionItem();
  114. requiItem.Qty = BOMitem.Quantity;
  115. requiItem.UnitCost = BOMitem.UnitCost;
  116. requiItem.Product.ID = BOMitem.Product.ID;
  117. requiItem.Product.Code = BOMitem.Product.Code;
  118. requiItem.Product.Name = BOMitem.Product.Name;
  119. requiItem.Style.ID = BOMitem.Style.ID;
  120. requiItem.Style.Code = BOMitem.Style.Code;
  121. requiItem.Style.Description = BOMitem.Style.Description;
  122. requiItem.Supplier.ID = BOMitem.Supplier.ID;
  123. requiItem.Supplier.Code = BOMitem.Supplier.Code;
  124. requiItem.Supplier.Name = BOMitem.Supplier.Name;
  125. requiItem.Dimensions.CopyFrom(BOMitem.Dimensions);
  126. requiItem.Job.ID = Master.Job.ID;
  127. requiItem.Job.Synchronise(Master.Job);
  128. requiItem.Supplier.ID = BOMitem.Supplier.ID;
  129. requiItem.Notes = "Create from BOM " + BOMitem.BillOfMaterials.Description + " on " + DateTime.Now.ToString("hh:mm t dd-MMM-yy") + " by " + App.EmployeeName;
  130. requiItems.Add(requiItem);
  131. }
  132. result.LoadRows(requiItems);
  133. return result;
  134. }
  135. public event DataModelUpdateEvent? OnUpdateDataModel;
  136. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  137. {
  138. if (Master is null || Master.ID == Guid.Empty || Master.ID == CoreUtils.FullGuid)
  139. MessageWindow.ShowMessage("Please select a bill of materials to cancel.", "Select BOM");
  140. else
  141. base.DoAdd();
  142. }
  143. protected override JobBillOfMaterialsItem CreateItem()
  144. {
  145. var result = base.CreateItem();
  146. if(Master is not null)
  147. {
  148. result.BillOfMaterials.ID = Master.ID;
  149. result.BillOfMaterials.Synchronise(Master);
  150. result.Job.ID = Master.Job.ID;
  151. result.Job.Synchronise(Master.Job);
  152. }
  153. result.Quantity = 1;
  154. return result;
  155. }
  156. protected override void Reload(Filters<JobBillOfMaterialsItem> criteria, Columns<JobBillOfMaterialsItem> columns,
  157. ref SortOrder<JobBillOfMaterialsItem>? sort, Action<CoreTable?, Exception?> action)
  158. {
  159. criteria.Add(MasterDetailFilter);
  160. base.Reload(criteria, columns, ref sort, action);
  161. }
  162. protected override void OnAfterEditorValueChanged(DynamicEditorGrid? grid, JobBillOfMaterialsItem[] items, AfterEditorValueChangedArgs args, Dictionary<string, object?> changes)
  163. {
  164. base.OnAfterEditorValueChanged(grid, items, args, changes);
  165. if (args.ColumnName.Equals("Product.ID") || args.ColumnName.Equals("Dimensions") || args.ColumnName.StartsWith("Dimensions.") || args.ColumnName.Equals("Style.ID") || args.ColumnName.Equals("Supplier.ID"))
  166. {
  167. JobBillOfMaterialsItem.UpdateCosts(
  168. items,
  169. changes
  170. );
  171. }
  172. }
  173. #region Create PO
  174. private bool CreatePO(System.Windows.Controls.Button btn, CoreRow[] rows)
  175. {
  176. if (!rows.Any())
  177. {
  178. MessageWindow.ShowMessage("Please select at least one row to add to PO.", "Select items");
  179. return false;
  180. }
  181. var purchaseOrder = new PurchaseOrder();
  182. purchaseOrder.Description = "Created from Job BOM Review Screen" + System.Environment.NewLine;
  183. purchaseOrder.RaisedBy.ID = App.EmployeeID;
  184. var page = new SupplierPurchaseOrders();
  185. page.OnAfterSave += (form, items) =>
  186. {
  187. PurchaseOrderOnSave(form, items.Cast<PurchaseOrder>().ToArray());
  188. MessageBox.Show("Success - New Purchase Order Created (" + purchaseOrder.PONumber + ")");
  189. };
  190. return page.EditItems(new[] { purchaseOrder }, LoadPurchaseOrderItems, true);
  191. }
  192. private void PurchaseOrderOnSave(IDynamicEditorForm form, PurchaseOrder[] items)
  193. {
  194. Progress.Show("Working");
  195. Guid POID = items[0].ID;
  196. CoreTable table = new Client<PurchaseOrderItem>().Query(new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID).IsEqualTo(POID),
  197. new Columns<PurchaseOrderItem>(x => x.ID, x => x.Product.ID, x => x.Qty, x => x.Dimensions.UnitSize, x => x.Dimensions.Value, x => x.Style.ID, x => x.DueDate, x => x.Job.ID));
  198. if (table.Rows.Any())
  199. {
  200. var poItems = AddPOItems(table, new List<PurchaseOrderItem>());
  201. var BOMItems = MatchBOMItems(poItems, new List<JobBillOfMaterialsItem>());
  202. if (BOMItems.Count > 0)
  203. SaveAndRefreshScreen(BOMItems);
  204. }
  205. Progress.Close();
  206. }
  207. private void SaveAndRefreshScreen(List<JobBillOfMaterialsItem> bomItems)
  208. {
  209. new Client<JobBillOfMaterialsItem>().Save(bomItems, "Updated on Create Purchase Order from BOM Dashboard");
  210. Dispatcher.BeginInvoke(() =>
  211. {
  212. Refresh(false, true);
  213. });
  214. }
  215. private List<PurchaseOrderItem> AddPOItems(CoreTable table, List<PurchaseOrderItem> poItems)
  216. {
  217. foreach (CoreRow row in table.Rows)
  218. {
  219. PurchaseOrderItem poItem = row.ToObject<PurchaseOrderItem>();
  220. poItems.Add(poItem);
  221. }
  222. return poItems;
  223. }
  224. private List<JobBillOfMaterialsItem> MatchBOMItems(List<PurchaseOrderItem> poItems, List<JobBillOfMaterialsItem> bomItems)
  225. {
  226. foreach (CoreRow row in SelectedRows)
  227. {
  228. JobBillOfMaterialsItem bomItem = row.ToObject<JobBillOfMaterialsItem>();
  229. foreach (var item in poItems)
  230. {
  231. if (MatchBOMItemToPOItem(bomItem, item))
  232. bomItems.Add(UpdateBOMItemWithPODetails(bomItem, item));
  233. }
  234. }
  235. return bomItems;
  236. }
  237. private JobBillOfMaterialsItem UpdateBOMItemWithPODetails(JobBillOfMaterialsItem bomItem, PurchaseOrderItem item)
  238. {
  239. bomItem.PurchaseOrderItem.ID = item.ID;
  240. bomItem.PurchaseOrderItem.DueDate = item.DueDate;
  241. return bomItem;
  242. }
  243. private bool MatchBOMItemToPOItem(JobBillOfMaterialsItem bomItem, PurchaseOrderItem item)
  244. {
  245. if (bomItem.Product.ID == item.Product.ID &&
  246. bomItem.Dimensions.UnitSize == item.Dimensions.UnitSize &&
  247. bomItem.Dimensions.Value == item.Dimensions.Value &&
  248. bomItem.Style.ID == item.Style.ID &&
  249. bomItem.Quantity == item.Qty &&
  250. bomItem.Job.ID == item.Job.ID)
  251. return true;
  252. else
  253. return false;
  254. }
  255. private CoreTable LoadPurchaseOrderItems(Type arg)
  256. {
  257. Progress.Show("Working");
  258. var result = new CoreTable();
  259. result.LoadColumns(typeof(PurchaseOrderItem));
  260. List<PurchaseOrderItem> items = new List<PurchaseOrderItem>();
  261. foreach (CoreRow row in SelectedRows)
  262. {
  263. JobBillOfMaterialsItem BOMItem = row.ToObject<JobBillOfMaterialsItem>();
  264. PurchaseOrderItem POItem = new PurchaseOrderItem();
  265. POItem.Product.ID = BOMItem.Product.ID;
  266. POItem.Product.Code = BOMItem.Product.Code;
  267. POItem.Product.Name = BOMItem.Product.Name;
  268. POItem.Qty = BOMItem.Quantity;
  269. POItem.Dimensions.CopyFrom(BOMItem.Dimensions);
  270. POItem.Dimensions.Value = BOMItem.Dimensions.Value;
  271. POItem.Style.ID = BOMItem.Style.ID;
  272. POItem.Style.Code = BOMItem.Style.Code;
  273. POItem.Style.Description = BOMItem.Style.Description;
  274. POItem.Job.ID = BOMItem.Job.ID;
  275. POItem.Dimensions.UnitSize = BOMItem.Dimensions.UnitSize;
  276. POItem.Description = BOMItem.Product.Name + " (" + BOMItem.Dimensions.ToString() + ")";
  277. items.Add(POItem);
  278. }
  279. result.LoadRows(items);
  280. Progress.Close();
  281. return result;
  282. }
  283. #endregion
  284. }