ReservationManagementPanel.xaml.cs 11 KB

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