CustomerPanel.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Threading;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.Wpf;
  14. namespace PRSDesktop
  15. {
  16. public class CustomerPanelSettings : IUserConfigurationSettings
  17. {
  18. public CustomerPanelSettings()
  19. {
  20. AnchorWidth = 500F;
  21. View = DynamicSplitPanelView.Combined;
  22. }
  23. public DynamicSplitPanelView View { get; set; }
  24. public double AnchorWidth { get; set; }
  25. }
  26. public partial class CustomerPanel : UserControl, IPanel<Customer>
  27. {
  28. private CustomerPanelSettings settings;
  29. private Customer _customer;
  30. private enum PageIndex
  31. {
  32. Contacts,
  33. Spreadsheets
  34. }
  35. public CustomerPanel()
  36. {
  37. InitializeComponent();
  38. }
  39. public bool IsReady { get; set; }
  40. public event DataModelUpdateEvent? OnUpdateDataModel;
  41. public Dictionary<string, object[]> Selected()
  42. {
  43. return new Dictionary<string, object[]> { { typeof(Customer).EntityName(), Customers.SelectedRows } };
  44. }
  45. public void Setup()
  46. {
  47. settings = new UserConfiguration<CustomerPanelSettings>().Load();
  48. SplitPanel.View = settings.View;
  49. SplitPanel.AnchorWidth = settings.AnchorWidth;
  50. Customers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  51. Customers.Refresh(true, false);
  52. CustomerContacts.Refresh(true, false);
  53. CustomerSpreadsheets.Refresh(true,false);
  54. CustomerActivities.Refresh(true,false);
  55. CustomerProducts.Refresh(true,false);
  56. }
  57. public void Shutdown(CancelEventArgs? cancel)
  58. {
  59. }
  60. public void CreateToolbarButtons(IPanelHost host)
  61. {
  62. AccountsSetupActions.Standard(host);
  63. host.CreateSetupActionIfCanView<CustomerStatus>("Customer Statuses", PRSDesktop.Resources.customer,
  64. action =>
  65. {
  66. var list = new MasterList(typeof(CustomerStatus));
  67. list.ShowDialog();
  68. });
  69. host.CreateSetupSeparator();
  70. AccountsSetupActions.CustomerSpreadsheetTemplates(host);
  71. //host.CreateToolbarButton(new PanelAction() { Caption = "Select Columns", OnExecute = DoSelectColumns, Image = PRSDesktop.Resources.shared });
  72. PostUtils.CreateToolbarButtons(
  73. host,
  74. () => (DataModel(Selection.Selected) as IDataModel<Customer>)!,
  75. () => Customers.Refresh(false, true),
  76. true);
  77. }
  78. public void Refresh()
  79. {
  80. Customers.Refresh(true, true);
  81. }
  82. public string SectionName => "Customers";
  83. public DataModel DataModel(Selection selection)
  84. {
  85. var ids = Customers.ExtractValues(x => x.ID, selection).ToArray();
  86. return new BaseDataModel<Customer>(new Filter<Customer>(x => x.ID).InList(ids));
  87. }
  88. public void Heartbeat(TimeSpan time)
  89. {
  90. }
  91. public Type DataType()
  92. {
  93. return typeof(Customer);
  94. }
  95. private void RefreshSubPage(ICustomerGrid grid)
  96. {
  97. if (grid == null)
  98. return;
  99. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("CustomerPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  100. grid.Customer = _customer;
  101. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  102. timer.Tick += (o, e) =>
  103. {
  104. timer.Stop();
  105. grid.Refresh(!grid.IsReady, true);
  106. };
  107. timer.Start();
  108. }
  109. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  110. {
  111. settings.View = SplitPanel.View;
  112. settings.AnchorWidth = SplitPanel.AnchorWidth;
  113. new UserConfiguration<CustomerPanelSettings>().Save(settings);
  114. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  115. if (Customers.ColumnsTag != newTag)
  116. {
  117. Customers.ColumnsTag = newTag;
  118. Customers.Refresh(true, true);
  119. }
  120. }
  121. private void CustomerDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
  122. {
  123. if (e.AddedItems.Count > 0)
  124. {
  125. var t = e.AddedItems[0] as TabItem;
  126. if (t != null && t.Visibility == Visibility.Visible)
  127. RefreshSubPage(t.Content as ICustomerGrid);
  128. }
  129. }
  130. private void Customers_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  131. {
  132. var row = e.Rows?.FirstOrDefault();
  133. _customer = row?.ToObject<Customer>() ?? new Customer();
  134. Dispatcher.Invoke(() => { RefreshSubPage(CustomerDetails.SelectedContent as ICustomerGrid); });
  135. }
  136. }
  137. }