JobRequisitionsPanel.xaml.cs 7.8 KB

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