CustomerPanel.xaml.cs 4.7 KB

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