JobRequisitionsPanel.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. namespace PRSDesktop
  18. {
  19. public class JobRequisitionPanelSettings : BaseObject, IGlobalConfigurationSettings
  20. {
  21. [Caption("Default Style for Company", IncludePath = false)]
  22. public ProductStyleLink ProductStyle { get; set; }
  23. public JobRequisitionPanelSettings()
  24. {
  25. ProductStyle = new ProductStyleLink();
  26. }
  27. }
  28. /// <summary>
  29. /// Interaction logic for JobRequisitionsPanel.xaml
  30. /// </summary>
  31. public partial class JobRequisitionsPanel : UserControl, IPanel<JobRequisitionItem>
  32. {
  33. Guid JobID = Guid.Empty;
  34. private JobRequisitionPanelSettings _settings = null;
  35. public JobRequisitionsPanel()
  36. {
  37. InitializeComponent();
  38. JobRequiItems.Reconfigure(options =>
  39. {
  40. if(Mode == PanelMode.Reserve)
  41. {
  42. options.Remove(DynamicGridOption.MultiSelect);
  43. }
  44. else
  45. {
  46. options.Add(DynamicGridOption.MultiSelect);
  47. }
  48. });
  49. }
  50. enum PanelMode
  51. {
  52. Reserve,
  53. Purchase
  54. }
  55. private PanelMode mode;
  56. private PanelMode Mode
  57. {
  58. get => mode;
  59. set
  60. {
  61. mode = value;
  62. SetPanelDetail();
  63. }
  64. }
  65. private void SetPanelDetail()
  66. {
  67. JobRequiItems.Reconfigure();
  68. if (Mode == PanelMode.Reserve)
  69. {
  70. reserveCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
  71. purchaseCol.Width = new System.Windows.GridLength(0);
  72. reserveBtn.Background = ThemeManager.SelectedTabItemBackgroundBrush;
  73. reserveBtn.Foreground = ThemeManager.SelectedTabItemForegroundBrush;
  74. purchaseBtn.Background = new SolidColorBrush(Colors.WhiteSmoke);
  75. purchaseBtn.Foreground = new SolidColorBrush(Colors.Black);
  76. }
  77. else if (Mode == PanelMode.Purchase)
  78. {
  79. reserveCol.Width = new System.Windows.GridLength(0);
  80. purchaseCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
  81. reserveBtn.Background = new SolidColorBrush(Colors.WhiteSmoke);
  82. reserveBtn.Foreground = new SolidColorBrush(Colors.Black);
  83. purchaseBtn.Background = ThemeManager.SelectedTabItemBackgroundBrush;
  84. reserveBtn.Foreground = ThemeManager.SelectedTabItemForegroundBrush;
  85. if (JobRequiItems.SelectedRows.Any())
  86. {
  87. purchasing.JobRequiItems = CreateList(JobRequiItems.SelectedRows);
  88. purchasing.LoadFromRequiLine();
  89. }
  90. }
  91. }
  92. public bool IsReady { get; set; }
  93. public string SectionName => "Job Requisitions";
  94. public event DataModelUpdateEvent? OnUpdateDataModel;
  95. public void CreateToolbarButtons(IPanelHost host)
  96. {
  97. host.CreateSetupAction(new PanelAction() { Caption = "Product Configuration Settings", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
  98. }
  99. private void ConfigSettingsClick(PanelAction obj)
  100. {
  101. var pages = new DynamicEditorPages();
  102. var buttons = new DynamicEditorButtons();
  103. buttons.Add(
  104. "",
  105. PRSDesktop.Resources.help.AsBitmapImage(),
  106. _settings,
  107. (f, i) =>
  108. {
  109. Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + typeof(Equipment).Name.SplitCamelCase().Replace(" ", "_"))
  110. { UseShellExecute = true });
  111. }
  112. );
  113. var propertyEditor = new DynamicEditorForm(typeof(JobRequisitionPanelSettings), pages, buttons);
  114. propertyEditor.OnDefineLookups += sender =>
  115. {
  116. var editor = sender.EditorDefinition as ILookupEditor;
  117. var colname = sender.ColumnName;
  118. var values = editor.Values(colname, new[] { _settings });
  119. sender.LoadLookups(values);
  120. };
  121. propertyEditor.OnEditorValueChanged += (sender, name, value) =>
  122. {
  123. CoreUtils.SetPropertyValue(_settings, name, value);
  124. return new Dictionary<string, object?>();
  125. };
  126. propertyEditor.Items = new BaseObject[] { _settings };
  127. if (propertyEditor.ShowDialog() == true)
  128. {
  129. new GlobalConfiguration<JobRequisitionPanelSettings>().Save(_settings);
  130. holdings.CompanyDefaultStyle = _settings.ProductStyle;
  131. }
  132. }
  133. public DataModel DataModel(Selection selection)
  134. {
  135. var ids = JobRequiItems.ExtractValues(x => x.ID, selection).ToArray();
  136. return new BaseDataModel<JobRequisitionItem>(new Filter<JobRequisitionItem>(x => x.ID).InList(ids));
  137. }
  138. public void Heartbeat(TimeSpan time)
  139. {
  140. }
  141. public void Refresh()
  142. {
  143. JobRequiItems.Refresh(false, true);
  144. }
  145. public Dictionary<string, object[]> Selected()
  146. {
  147. var result = new Dictionary<string, object[]>();
  148. result[typeof(JobRequisitionItem).EntityName()] = JobRequiItems.SelectedRows;
  149. return result;
  150. }
  151. public void Setup()
  152. {
  153. Mode = PanelMode.Reserve;
  154. SetupJobRequiItemGrids();
  155. _settings = new GlobalConfiguration<JobRequisitionPanelSettings>().Load();
  156. holdings.CompanyDefaultStyle = _settings.ProductStyle;
  157. holdings.OnHoldingsReviewRefresh += Holdings_OnHoldingsReviewRefresh;
  158. purchasing.OnPurchaseOrderSaved += () => { RefreshJobRequiGrids(); };
  159. }
  160. private void Holdings_OnHoldingsReviewRefresh()
  161. {
  162. Refresh();
  163. RefreshJobRequiGrids();
  164. }
  165. private void RefreshJobRequiGrids()
  166. {
  167. JobRequiItems.bRefreshing = false;
  168. JobRequiItems.Refresh(false, true);
  169. }
  170. private void SetupJobRequiItemGrids()
  171. {
  172. JobRequiItems.OnGridRefresh += JobRequiItems_OnGridRefresh;
  173. JobRequiItems.OnJobRequiItemSelected += OnJobRequiItemSelected;
  174. JobRequiItems.Refresh(true, true);
  175. }
  176. private void OnJobRequiItemSelected(CoreRow[] rows)
  177. {
  178. holdings.Item = rows.FirstOrDefault()?.ToObject<JobRequisitionItem>();
  179. if (mode == PanelMode.Purchase)
  180. {
  181. purchasing.JobRequiItems = CreateList(rows);
  182. purchasing.LoadFromRequiLine();
  183. }
  184. }
  185. private List<JobRequisitionItem> CreateList(CoreRow[] rows)
  186. {
  187. List<JobRequisitionItem> list = new List<JobRequisitionItem>();
  188. foreach (var row in rows)
  189. list.Add(row.ToObject<JobRequisitionItem>());
  190. return list;
  191. }
  192. private void JobRequiItems_OnGridRefresh()
  193. {
  194. RefreshJobRequiGrids();
  195. }
  196. private void CheckSaved(CancelEventArgs cancel)
  197. {
  198. if (!purchasing.EditorChanged)
  199. {
  200. return;
  201. }
  202. var result = MessageBox.Show("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
  203. if (result == MessageBoxResult.Yes)
  204. {
  205. purchasing.Editor.SaveItem(cancel);
  206. if (!cancel.Cancel)
  207. {
  208. MessageBox.Show("Purchase Order saved.");
  209. }
  210. }
  211. else if (result == MessageBoxResult.Cancel)
  212. {
  213. cancel.Cancel = true;
  214. }
  215. }
  216. private bool CheckSaved()
  217. {
  218. var cancel = new CancelEventArgs();
  219. CheckSaved(cancel);
  220. return !cancel.Cancel;
  221. }
  222. public void Shutdown(CancelEventArgs? cancel)
  223. {
  224. if(cancel is not null && purchasing.EditorChanged)
  225. {
  226. CheckSaved(cancel);
  227. }
  228. }
  229. private void ReserveStock_Clicked(object sender, System.Windows.RoutedEventArgs e)
  230. {
  231. Mode = PanelMode.Reserve;
  232. }
  233. private void PurchaseStock_Clicked(object sender, System.Windows.RoutedEventArgs e)
  234. {
  235. Mode = PanelMode.Purchase;
  236. }
  237. private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  238. {
  239. if (e.LeftButton == MouseButtonState.Pressed
  240. && e.OriginalSource.GetType() != typeof(Border)
  241. && e.OriginalSource.GetType() != typeof(Thumb)
  242. )
  243. DragDrop.DoDragDrop(JobRequiItems, JobRequiItems.SelectedRows, DragDropEffects.Link);
  244. }
  245. private void Purchasing_Drop(object sender, DragEventArgs e)
  246. {
  247. purchasing.DropItems(JobRequiItems.SelectedRows);
  248. }
  249. }
  250. }