SupplierPanel.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. public class SupplierPanelSettings : IUserConfigurationSettings
  16. {
  17. public SupplierPanelSettings()
  18. {
  19. AnchorWidth = 800F;
  20. View = DynamicSplitPanelView.Combined;
  21. Anchor = DynamicSplitPanelAnchor.Detail;
  22. }
  23. public DynamicSplitPanelView View { get; set; }
  24. public DynamicSplitPanelAnchor Anchor { get; set; }
  25. public double AnchorWidth { get; set; }
  26. }
  27. /// <summary>
  28. /// Interaction logic for SupplierPanel.xaml
  29. /// </summary>
  30. public partial class SupplierPanel : UserControl, IPanel<Supplier>
  31. {
  32. private SupplierPanelSettings settings;
  33. private Supplier Supplier = new Supplier();
  34. public SupplierPanel()
  35. {
  36. InitializeComponent();
  37. Products.Visibility = Security.CanView<SupplierProduct>() ? Visibility.Visible : Visibility.Collapsed;
  38. if (Products.Visibility == Visibility.Visible)
  39. SupplierDetails.SelectedItem = Products;
  40. else
  41. SupplierDetails.SelectedItem = Contacts;
  42. }
  43. public event DataModelUpdateEvent? OnUpdateDataModel;
  44. public bool IsReady { get; set; }
  45. public void CreateToolbarButtons(IPanelHost host)
  46. {
  47. AccountsSetupActions.Standard(host);
  48. host.CreateSetupActionIfCanView<SupplierStatus>("Supplier Statuses", PRSDesktop.Resources.supplier,
  49. action =>
  50. {
  51. var list = new MasterList(typeof(SupplierStatus));
  52. list.ShowDialog();
  53. });
  54. host.CreateSetupActionIfCanView<SupplierCategory>("Supplier Categories", PRSDesktop.Resources.supplier, (action) =>
  55. {
  56. var list = new MasterList(typeof(SupplierCategory));
  57. list.ShowDialog();
  58. });
  59. host.CreateSetupSeparator();
  60. AccountsSetupActions.SupplierSpreadsheetTemplates(host);
  61. PostUtils.CreateToolbarButtons(
  62. host,
  63. () => (DataModel(Selection.Selected) as IDataModel<Supplier>)!,
  64. () => Suppliers.Refresh(false, true),
  65. true);
  66. }
  67. public string SectionName => "Suppliers";
  68. public DataModel DataModel(Selection selection)
  69. {
  70. var ids = Suppliers.ExtractValues(x => x.ID, selection).ToArray();
  71. return new BaseDataModel<Supplier>(new Filter<Supplier>(x => x.ID).InList(ids));
  72. }
  73. public void Heartbeat(TimeSpan time)
  74. {
  75. }
  76. public void Refresh()
  77. {
  78. Suppliers.Refresh(false, true);
  79. RefreshSubPage(SupplierDetails.SelectedContent as ISupplierGrid);
  80. }
  81. public Dictionary<string, object[]> Selected()
  82. {
  83. return new Dictionary<string, object[]> { { typeof(Supplier).EntityName(), Suppliers.SelectedRows } };
  84. }
  85. public void Setup()
  86. {
  87. settings = new UserConfiguration<SupplierPanelSettings>().Load();
  88. SplitPanel.View = settings.View;
  89. SplitPanel.Anchor = settings.Anchor;
  90. SplitPanel.AnchorWidth = settings.AnchorWidth;
  91. Suppliers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  92. Suppliers.Refresh(true, false);
  93. SupplierContacts.Refresh(true,false);
  94. SupplierProducts.Refresh(true,false);
  95. SupplierSpreadsheets.Refresh(true,false);
  96. }
  97. public void Shutdown(CancelEventArgs? cancel)
  98. {
  99. }
  100. private void RefreshSubPage(ISupplierGrid grid)
  101. {
  102. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("SupplierPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  103. if (grid == null)
  104. return;
  105. grid.Supplier = Supplier;
  106. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  107. timer.Tick += (o, e) =>
  108. {
  109. timer.Stop();
  110. grid.Refresh(!grid.IsReady, true);
  111. };
  112. timer.Start();
  113. }
  114. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  115. {
  116. settings.View = SplitPanel.View;
  117. settings.AnchorWidth = SplitPanel.AnchorWidth;
  118. new UserConfiguration<SupplierPanelSettings>().Save(settings);
  119. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  120. if (Suppliers.ColumnsTag != newTag)
  121. {
  122. Suppliers.ColumnsTag = newTag;
  123. Suppliers.Refresh(true, true);
  124. }
  125. }
  126. private void SupplierDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
  127. {
  128. if (e.AddedItems.Count > 0)
  129. {
  130. var t = e.AddedItems[0] as TabItem;
  131. if (t != null && t.Visibility == Visibility.Visible)
  132. RefreshSubPage(t.Content as ISupplierGrid);
  133. }
  134. }
  135. private void Suppliers_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  136. {
  137. var row = e.Rows?.FirstOrDefault();
  138. Supplier = row?.ToObject<Supplier>() ?? new Supplier();
  139. Dispatcher.Invoke(() => { RefreshSubPage(SupplierDetails.SelectedContent as ISupplierGrid); });
  140. }
  141. }