EquipmentPanel.xaml.cs 11 KB

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