ReservationManagementPanel.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using Comal.Classes;
  2. using InABox.Configuration;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using InABox.WPF.Themes;
  17. using NPOI.SS.Formula.Functions;
  18. using System.Collections.ObjectModel;
  19. using InABox.Clients;
  20. using InABox.Wpf;
  21. using InABox.Wpf.Reports;
  22. using Columns = InABox.Core.Columns;
  23. namespace PRSDesktop;
  24. /// <summary>
  25. /// Interaction logic for JobRequisitionsPanel.xaml
  26. /// </summary>
  27. public partial class ReservationManagementPanel : UserControl, IPanel<JobRequisitionItem>
  28. {
  29. private ReservationManagementGlobalSettings _globalSettings = null!; // Initialised in Setup()
  30. private DynamicSplitPanelView CurrentView;
  31. private List<PurchaseOrder> PurchaseOrders = new List<PurchaseOrder>();
  32. public ReservationManagementPanel()
  33. {
  34. InitializeComponent();
  35. CurrentView = SplitPanel.View;
  36. JobRequiItems.Reconfigure(options =>
  37. {
  38. var canMultiSelect = Mode != PanelMode.Reserve || SplitPanel.View == DynamicSplitPanelView.Master;
  39. if(canMultiSelect)
  40. {
  41. options.MultiSelect = true;
  42. }
  43. else
  44. {
  45. options.MultiSelect = false;
  46. }
  47. });
  48. }
  49. enum PanelMode
  50. {
  51. Reserve,
  52. Purchase
  53. }
  54. private PanelMode Mode { get; set; }
  55. private void SelectDetailButton(Button button)
  56. {
  57. reserveBtn.Background = new SolidColorBrush(Colors.WhiteSmoke);
  58. reserveBtn.Foreground = new SolidColorBrush(Colors.Black);
  59. foreach(var btn in PurchaseOrderButtons.Children.OfType<Button>())
  60. {
  61. btn.Background = new SolidColorBrush(Colors.WhiteSmoke);
  62. btn.Foreground = new SolidColorBrush(Colors.Black);
  63. }
  64. AddPOButton.Background = new SolidColorBrush(Colors.WhiteSmoke);
  65. AddPOButton.Foreground = new SolidColorBrush(Colors.Black);
  66. button.Background = ThemeManager.SelectedTabItemBackgroundBrush;
  67. button.Foreground = ThemeManager.SelectedTabItemForegroundBrush;
  68. }
  69. private void Reconfigure()
  70. {
  71. JobRequiItems.Reconfigure();
  72. OnUpdateDataModel?.Invoke(SectionName, DataModel(Selection.None));
  73. }
  74. private void SelectReserved()
  75. {
  76. SelectDetailButton(reserveBtn);
  77. reserveCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
  78. purchaseCol.Width = new System.Windows.GridLength(0);
  79. if(Mode != PanelMode.Reserve)
  80. {
  81. Mode = PanelMode.Reserve;
  82. Reconfigure();
  83. }
  84. }
  85. private void SelectNewPurchaseOrder()
  86. {
  87. SelectDetailButton(AddPOButton);
  88. reserveCol.Width = new System.Windows.GridLength(0);
  89. purchaseCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
  90. if (Mode != PanelMode.Purchase)
  91. {
  92. Mode = PanelMode.Purchase;
  93. Reconfigure();
  94. }
  95. purchasing.LoadFromRequiLine(Guid.Empty);
  96. }
  97. private void SelectPurchaseOrder(Button button, PurchaseOrder order)
  98. {
  99. SelectDetailButton(button);
  100. reserveCol.Width = new System.Windows.GridLength(0);
  101. purchaseCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
  102. if (Mode != PanelMode.Purchase)
  103. {
  104. Mode = PanelMode.Purchase;
  105. Reconfigure();
  106. }
  107. purchasing.LoadFromRequiLine(order.ID);
  108. }
  109. public bool IsReady { get; set; }
  110. public string SectionName => "Job Requisitions";
  111. public event DataModelUpdateEvent? OnUpdateDataModel;
  112. public void CreateToolbarButtons(IPanelHost host)
  113. {
  114. ProductSetupActions.Standard(host);
  115. host.CreateSetupAction(new PanelAction() { Caption = "Reservation Management Settings", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
  116. host.CreatePanelAction(new PanelAction("Treatment PO", PRSDesktop.Resources.purchase, TreatmentPO_Click));
  117. if(Mode == PanelMode.Purchase && SplitPanel.IsDetailVisible())
  118. {
  119. var sectionName = SupplierPurchaseOrderPanel.SectionName;
  120. var dataModel = SupplierPurchaseOrderPanel.DataModel(Array.Empty<Guid>());
  121. var reports = ReportUtils.LoadReports(sectionName, dataModel);
  122. foreach(var report in reports)
  123. {
  124. host.CreateReport(PanelHost.CreateReportAction(report, (selection) =>
  125. {
  126. Guid[] ids;
  127. if(selection == Selection.None)
  128. {
  129. ids = Array.Empty<Guid>();
  130. }
  131. else
  132. {
  133. ids = PurchaseOrders.Select(x => x.ID).ToArray();
  134. }
  135. return SupplierPurchaseOrderPanel.DataModel(ids);
  136. }));
  137. }
  138. }
  139. }
  140. private void ConfigSettingsClick(PanelAction obj)
  141. {
  142. var grid = new DynamicItemsListGrid<ReservationManagementGlobalSettings>();
  143. if(grid.EditItems(new ReservationManagementGlobalSettings[] { _globalSettings }))
  144. {
  145. new GlobalConfiguration<ReservationManagementGlobalSettings>().Save(_globalSettings);
  146. holdings.CompanyDefaultStyle = _globalSettings.ProductStyle;
  147. JobRequiItems.DueDateAlert = _globalSettings.DueDateAlert;
  148. JobRequiItems.DueDateWarning = _globalSettings.DueDateWarning;
  149. }
  150. }
  151. public DataModel DataModel(Selection selection)
  152. {
  153. var ids = JobRequiItems.ExtractValues(x => x.ID, selection).ToArray();
  154. return new BaseDataModel<JobRequisitionItem>(new Filter<JobRequisitionItem>(x => x.ID).InList(ids));
  155. }
  156. public void Heartbeat(TimeSpan time)
  157. {
  158. }
  159. public void Refresh()
  160. {
  161. JobRequiItems.Refresh(false, true);
  162. }
  163. public Dictionary<string, object[]> Selected()
  164. {
  165. return new Dictionary<string, object[]>
  166. {
  167. [typeof(JobRequisitionItem).EntityName()] = JobRequiItems.SelectedRows
  168. };
  169. }
  170. public void Setup()
  171. {
  172. SelectReserved();
  173. JobRequiItems.OnSelectItem += OnJobRequiItemSelected;
  174. JobRequiItems.Refresh(true, false);
  175. _globalSettings = new GlobalConfiguration<ReservationManagementGlobalSettings>().Load();
  176. JobRequiItems.DueDateAlert = _globalSettings.DueDateAlert;
  177. JobRequiItems.DueDateWarning = _globalSettings.DueDateWarning;
  178. holdings.CompanyDefaultStyle = _globalSettings.ProductStyle;
  179. holdings.OnHoldingsReviewRefresh += Holdings_OnHoldingsReviewRefresh;
  180. purchasing.OnPurchaseOrderSaved += Refresh;
  181. }
  182. private void OnJobRequiItemSelected(object sender, DynamicGridSelectionEventArgs e)
  183. {
  184. if (SplitPanel.IsDetailVisible())
  185. {
  186. holdings.Item = e.Rows?.FirstOrDefault()?.ToObject<JobRequisitionItem>();
  187. RefreshPurchaseOrderButtons();
  188. }
  189. }
  190. private void RefreshPurchaseOrderButtons()
  191. {
  192. var requiIDs = JobRequiItems.SelectedRows
  193. .Select(x => x.Get<JobRequisitionItem, Guid>(x => x.ID))
  194. .ToArray();
  195. var pos = Client.Query(
  196. new Filter<PurchaseOrder>(x => x.ID).InQuery(
  197. new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.JobRequisitionItem.ID)
  198. .InList(requiIDs),
  199. x => x.PurchaseOrderItem.PurchaseOrderLink.ID),
  200. Columns.None<PurchaseOrder>().Add(x => x.ID)
  201. .Add(x => x.PONumber))
  202. .ToObjects<PurchaseOrder>().ToList();
  203. PurchaseOrders = pos;
  204. PurchaseOrderButtons.Children.Clear();
  205. var buttons = new List<(Button btn, PurchaseOrder po)>();
  206. foreach (var po in pos)
  207. {
  208. var button = new Button
  209. {
  210. Content = po.PONumber,
  211. Height = 30,
  212. FontWeight = FontWeights.DemiBold,
  213. BorderBrush = new SolidColorBrush(Colors.DarkGray),
  214. BorderThickness = new Thickness(1.25),
  215. Margin = new Thickness(2, 0, 0, 2),
  216. Padding = new Thickness(13, 3, 13, 3)
  217. };
  218. button.Click += (o, e) =>
  219. {
  220. SelectPurchaseOrder(button, po);
  221. };
  222. buttons.Add((button, po));
  223. PurchaseOrderButtons.Children.Add(button);
  224. }
  225. if (Mode == PanelMode.Purchase)
  226. {
  227. if (buttons.Count == 0)
  228. {
  229. SelectNewPurchaseOrder();
  230. }
  231. else
  232. {
  233. var btn = buttons[0];
  234. SelectPurchaseOrder(btn.btn, btn.po);
  235. }
  236. }
  237. else if (Mode == PanelMode.Reserve)
  238. {
  239. SelectReserved();
  240. }
  241. }
  242. private void Holdings_OnHoldingsReviewRefresh()
  243. {
  244. Refresh();
  245. }
  246. private void CheckSaved(CancelEventArgs cancel)
  247. {
  248. if (!SplitPanel.IsDetailVisible() || !purchasing.EditorChanged)
  249. {
  250. return;
  251. }
  252. var result = MessageWindow.ShowYesNoCancel("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?");
  253. if (result == MessageWindowResult.Yes)
  254. {
  255. purchasing.Editor.SaveItem(cancel);
  256. if (!cancel.Cancel)
  257. {
  258. MessageWindow.ShowMessage("Purchase Order saved.", "Success");
  259. }
  260. }
  261. else if (result == MessageWindowResult.Cancel)
  262. {
  263. cancel.Cancel = true;
  264. }
  265. }
  266. public void Shutdown(CancelEventArgs? cancel)
  267. {
  268. if(cancel is not null && purchasing.EditorChanged)
  269. {
  270. CheckSaved(cancel);
  271. }
  272. }
  273. private void ReserveStock_Clicked(object sender, System.Windows.RoutedEventArgs e)
  274. {
  275. SelectReserved();
  276. }
  277. private void Purchasing_Drop(object sender, DragEventArgs e)
  278. {
  279. if(DynamicGridUtils.TryGetDropData(e, out var type, out var table))
  280. {
  281. if(type == typeof(JobRequisitionItem))
  282. {
  283. purchasing.DropItems(table.Rows);
  284. }
  285. }
  286. }
  287. private void AddPOButton_Click(object sender, RoutedEventArgs e)
  288. {
  289. SelectNewPurchaseOrder();
  290. }
  291. private void TreatmentPO_Click(PanelAction action)
  292. {
  293. var jris = JobRequiItems.SelectedRows.ToArray<JobRequisitionItem>();
  294. if(jris.Length == 0)
  295. {
  296. MessageWindow.ShowMessage("Please select at least one job requisition item.", "No items selected");
  297. return;
  298. }
  299. foreach(var jri in jris)
  300. {
  301. if(jri.TreatmentRequired - jri.TreatmentOnOrder <= 0)
  302. {
  303. MessageWindow.ShowMessage("Please select only items requiring treatment.", "Already treated");
  304. return;
  305. }
  306. }
  307. }
  308. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  309. {
  310. JobRequiItems.Reconfigure();
  311. if(CurrentView != e.View)
  312. {
  313. CurrentView = e.View;
  314. if (e.View != DynamicSplitPanelView.Master)
  315. {
  316. Refresh();
  317. }
  318. }
  319. }
  320. }