EquipmentPanel.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. using Comal.Classes;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.DynamicGrid;
  16. using InABox.WPF;
  17. using InABox.Wpf;
  18. namespace PRSDesktop
  19. {
  20. public class EquipmentPanelSettings : BaseObject, IGlobalConfigurationSettings
  21. {
  22. public bool CustomerFilterEnabled { get; set; }
  23. public EquipmentPanelSettings()
  24. {
  25. CustomerFilterEnabled = false;
  26. }
  27. }
  28. /// <summary>
  29. /// Interaction logic for EquipmentPanel.xaml
  30. /// </summary>
  31. public partial class EquipmentPanel : UserControl, IPanel<Equipment>
  32. {
  33. private EquipmentPanelSettings _settings = null;
  34. private String _searchfilter = "";
  35. public ObservableCollection<Tuple<string, Guid, BitmapImage>> GroupList { get; private set; }
  36. public bool IsReady { get; set; }
  37. private String _customercode = "";
  38. private List<Tuple<Guid, String, String>> _customers;
  39. public EquipmentPanel()
  40. {
  41. GroupList = new ObservableCollection<Tuple<string, Guid, BitmapImage>>();
  42. InitializeComponent();
  43. }
  44. public Type DataType()
  45. {
  46. return typeof(Equipment);
  47. }
  48. public event DataModelUpdateEvent? OnUpdateDataModel;
  49. public Dictionary<string, object[]> Selected()
  50. {
  51. return new Dictionary<string, object[]> { { typeof(Equipment).EntityName(), _Equipment.SelectedRows } };
  52. }
  53. public void Setup()
  54. {
  55. _Equipment.CustomerID = Guid.Empty;
  56. _Equipment.GroupID = CoreUtils.FullGuid;
  57. _settings = new GlobalConfiguration<EquipmentPanelSettings>().Load();
  58. ReconfigurePanel();
  59. _Equipment.Refresh(true, false);
  60. GroupList.Add(new Tuple<string, Guid, BitmapImage>("All Items", CoreUtils.FullGuid, PRSDesktop.Resources.edit.AsBitmapImage()));
  61. var groups = new Client<EquipmentGroup>().Query(
  62. null,
  63. new Columns<EquipmentGroup>(x=>x.ID)
  64. .Add(x=>x.Description)
  65. .Add(x=>x.Thumbnail.ID),
  66. new SortOrder<EquipmentGroup>(x => x.Code)
  67. );
  68. var ids = groups.ExtractValues<EquipmentGroup, Guid>(c => c.Thumbnail.ID).Distinct().Where(x => x != Guid.Empty).ToArray();
  69. var Images = ids.Any()
  70. ? new Client<Document>().Load(new Filter<Document>(x=>x.ID).InList(ids))
  71. : new Document[] { };
  72. foreach (var row in groups.Rows)
  73. {
  74. var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)));
  75. var img = ImageUtils.LoadImage(image?.Data) ?? PRSDesktop.Resources.specifications.AsBitmapImage();
  76. GroupList.Add(
  77. new Tuple<string, Guid, BitmapImage>(
  78. row.Get<EquipmentGroup,String>(c=>c.Description),
  79. row.Get<EquipmentGroup,Guid>(c=>c.ID),
  80. img
  81. )
  82. );
  83. }
  84. Groups.Visibility = groups.Rows.Any() ? Visibility.Visible : Visibility.Collapsed;
  85. Groups.ItemsSource = GroupList;
  86. Groups.SelectedIndex = 0;
  87. }
  88. public void Shutdown(CancelEventArgs? cancel)
  89. {
  90. }
  91. public void CreateToolbarButtons(IPanelHost host)
  92. {
  93. EquipmentSetupActions.TrackerTypes(host);
  94. EquipmentSetupActions.WebStickers(host);
  95. EquipmentSetupActions.DigitalKeys(host);
  96. EquipmentSetupActions.EquipmentGroups(host);
  97. host.CreateSetupSeparator();
  98. host.CreateSetupAction(new PanelAction() { Caption = "Equipment Settings", Image = PRSDesktop.Resources.specifications, OnExecute = EquipmentSettingsClick });
  99. }
  100. private void EquipmentSettingsClick(PanelAction obj)
  101. {
  102. var pages = new DynamicEditorPages();
  103. var buttons = new DynamicEditorButtons();
  104. buttons.Add(
  105. "",
  106. PRSDesktop.Resources.help.AsBitmapImage(),
  107. _settings,
  108. (f, i) =>
  109. {
  110. Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + typeof(Equipment).Name.SplitCamelCase().Replace(" ", "_"))
  111. { UseShellExecute = true });
  112. }
  113. );
  114. var propertyEditor = new DynamicEditorForm(typeof(EquipmentPanelSettings), pages, buttons);
  115. propertyEditor.Items = new BaseObject[] { _settings };
  116. if (propertyEditor.ShowDialog() == true)
  117. {
  118. new GlobalConfiguration<EquipmentPanelSettings>().Save(_settings);
  119. ReconfigurePanel();
  120. }
  121. }
  122. private void ReconfigurePanel()
  123. {
  124. CustomerLabel.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
  125. Customer.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
  126. CustomerSearch.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
  127. CustomerName.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
  128. if (!_settings.CustomerFilterEnabled)
  129. {
  130. _Equipment.CustomerID = CoreUtils.FullGuid;
  131. _customercode = "";
  132. Customer.Text = "";
  133. CustomerName.Text = "";
  134. }
  135. else if (_Equipment.CustomerID == CoreUtils.FullGuid)
  136. _Equipment.CustomerID = Guid.Empty;
  137. if (IsReady)
  138. _Equipment.Refresh(false,true);
  139. }
  140. public void Refresh()
  141. {
  142. _Equipment.Refresh(false, true);
  143. }
  144. public string SectionName => "Equipment";
  145. public DataModel DataModel(Selection selection)
  146. {
  147. var ids = _Equipment?.ExtractValues(x => x.ID, selection)?.ToArray() ?? new Guid[] { };
  148. return new BaseDataModel<Equipment>(new Filter<Equipment>(x => x.ID).InList(ids));
  149. }
  150. public void Heartbeat(TimeSpan time)
  151. {
  152. }
  153. private bool _Equipment_OnFilterRecord(CoreRow row)
  154. {
  155. if (!string.IsNullOrWhiteSpace(_searchfilter))
  156. {
  157. for (int i=0; i<row.Table.Columns.Count; i++)
  158. {
  159. if ((row.Table.Columns[i].DataType == typeof(String)))
  160. {
  161. var value = row.Values[i] as string;
  162. if (!string.IsNullOrEmpty(value) && value.ToUpper().Contains(_searchfilter.ToUpper()))
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. return true;
  169. }
  170. private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
  171. {
  172. var sCur = _Equipment.GroupID;
  173. if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
  174. {
  175. _Equipment.GroupID = CoreUtils.FullGuid;
  176. }
  177. else
  178. {
  179. var selected = (Tuple<string, Guid, BitmapImage>)e.AddedItems[0];
  180. _Equipment.GroupID = selected.Item2;
  181. }
  182. if (sCur != _Equipment.GroupID)
  183. _Equipment.Refresh(false, true);
  184. }
  185. private void Search_OnTextChanged(object sender, TextChangedEventArgs e)
  186. {
  187. if (string.IsNullOrEmpty(Search.Text) && !string.Equals(Search.Text, _searchfilter))
  188. {
  189. _searchfilter = "";
  190. _Equipment.Refresh(false, false);
  191. }
  192. }
  193. private void Search_OnKeyUp(object sender, KeyEventArgs e)
  194. {
  195. if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key) && !String.Equals(Search.Text, _searchfilter))
  196. {
  197. _searchfilter = Search.Text;
  198. _Equipment.Refresh(false, false);
  199. }
  200. }
  201. private void CheckCustomerCache()
  202. {
  203. if (_customers == null)
  204. {
  205. _customers = new Client<Customer>().Query(
  206. null,
  207. new Columns<Customer>(x => x.ID).Add(x => x.Code).Add(x => x.Name),
  208. new SortOrder<Customer>(x => x.Name)
  209. ).ToTuples<Customer,Guid,String,String>(x =>x.ID,x=>x.Code,x=>x.Name).ToList();
  210. }
  211. }
  212. private void DoSearchCustomers()
  213. {
  214. var dlg = new MultiSelectDialog<Customer>(null, new Columns<Customer>(x => x.ID).Add(x => x.Code).Add(x => x.Name), false);
  215. if (dlg.ShowDialog("Code",Customer.Text))
  216. {
  217. var customer = dlg.Items().First();
  218. _customercode = customer.Code;
  219. Customer.Text = customer.Code;
  220. CustomerName.Text = customer.Name;
  221. _Equipment.CustomerID = customer.ID;
  222. }
  223. else
  224. {
  225. _customercode = "";
  226. Customer.Text = "";
  227. CustomerName.Text = "";
  228. _Equipment.CustomerID = Guid.Empty;
  229. }
  230. _Equipment.Refresh(false, true);
  231. }
  232. private void CustomerSearch_OnClick(object sender, RoutedEventArgs e)
  233. {
  234. DoSearchCustomers();
  235. }
  236. private void Customer_OnKeyUp(object sender, KeyEventArgs e)
  237. {
  238. if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key))
  239. {
  240. if (!String.Equals(Customer.Text, _customercode))
  241. {
  242. CheckCustomerCache();
  243. var lookup = _customers.FirstOrDefault(x => String.Equals(x.Item2, Customer.Text));
  244. if (lookup != null)
  245. {
  246. _customercode = lookup.Item2;
  247. CustomerName.Text = lookup.Item3;
  248. _Equipment.CustomerID = lookup.Item1;
  249. _Equipment.Refresh(false, true);
  250. }
  251. else
  252. DoSearchCustomers();
  253. }
  254. }
  255. }
  256. private void Customer_OnTextChanged(object sender, TextChangedEventArgs e)
  257. {
  258. if (String.IsNullOrEmpty(Customer.Text) && !String.Equals(Customer.Text, _customercode))
  259. {
  260. _Equipment.CustomerID = Guid.Empty;
  261. _customercode = "";
  262. CustomerName.Text = "";
  263. _Equipment.Refresh(false, true);
  264. }
  265. }
  266. }
  267. }