InvoicePanel.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.Core.Postable;
  13. using InABox.DynamicGrid;
  14. using InABox.Wpf;
  15. namespace PRSDesktop;
  16. public enum InvoicingStrategy
  17. {
  18. InvoiceOnIssue,
  19. InvoiceOnPurchase
  20. }
  21. public class InvoicePanelSettings : BaseObject, IGlobalConfigurationSettings
  22. {
  23. [EditorSequence(1)]
  24. public InvoicingStrategy InvoicingStrategy { get; set; }
  25. }
  26. public class InvoicePanelUserSettings : BaseObject, IUserConfigurationSettings
  27. {
  28. public DynamicSplitPanelView View { get; set; } = DynamicSplitPanelView.Combined;
  29. public double AnchorWidth { get; set; } = 500;
  30. }
  31. /// <summary>
  32. /// Interaction logic for InvoiceGrid.xaml
  33. /// </summary>
  34. public partial class InvoicePanel : UserControl, IPanel<Invoice>, IMasterDetailControl<Job>
  35. {
  36. private InvoicePanelSettings _settings;
  37. private InvoicePanelUserSettings _userSettings;
  38. private InvoiceableGridSettings _gridSettings;
  39. public InvoicePanel()
  40. {
  41. InitializeComponent();
  42. Invoices.OnSelectItem += Invoices_OnSelectItem;
  43. _settings = new GlobalConfiguration<InvoicePanelSettings>().Load();
  44. _userSettings = UserConfiguration.Load<InvoicePanelUserSettings>();
  45. _gridSettings = UserConfiguration.Load<InvoiceableGridSettings>();
  46. Time.InvoiceGridSettings = _gridSettings;
  47. Parts.InvoiceGridSettings = _gridSettings;
  48. Bills.InvoiceGridSettings = _gridSettings;
  49. _gridSettings.PropertyChanged += GridSettings_PropertyChanged;
  50. SplitPanel.View = _userSettings.View;
  51. SplitPanel.AnchorWidth = _userSettings.AnchorWidth;
  52. }
  53. private void GridSettings_PropertyChanged(object? sender, PropertyChangedEventArgs e)
  54. {
  55. UserConfiguration.Save(_gridSettings);
  56. }
  57. public Job? Master
  58. {
  59. get => Invoices.Master;
  60. set
  61. {
  62. Invoices.Master = value;
  63. Lines.Job = value;
  64. }
  65. }
  66. public bool IsReady { get; set; }
  67. public event DataModelUpdateEvent? OnUpdateDataModel;
  68. public Dictionary<string, object[]> Selected()
  69. {
  70. return new Dictionary<string, object[]> { { typeof(Invoice).EntityName(), Invoices.SelectedRows } };
  71. }
  72. public void CreateToolbarButtons(IPanelHost host)
  73. {
  74. AccountsSetupActions.Standard(host);
  75. host.CreateSetupSeparator();
  76. host.CreateSetupAction(new PanelAction()
  77. {
  78. Caption = "Invoices Settings",
  79. Image = PRSDesktop.Resources.edit,
  80. OnExecute = action =>
  81. {
  82. if (DynamicGridUtils.EditObject(_settings))
  83. {
  84. new GlobalConfiguration<InvoicePanelSettings>().Save(_settings);
  85. Parts.InvoiceSettings = _settings;
  86. }
  87. }
  88. });
  89. PostUtils.CreateToolbarButtons(
  90. host,
  91. () => (DataModel(Selection.Selected) as IDataModel<Invoice>)!,
  92. () => Invoices.Refresh(false, true),
  93. true);
  94. }
  95. public void Setup()
  96. {
  97. Invoices.Refresh(true, false);
  98. Parts.Refresh(true, false);
  99. Time.Refresh(true, false);
  100. Lines.Refresh(true, false);
  101. Bills.Refresh(true, false);
  102. }
  103. public void Shutdown(CancelEventArgs? cancel)
  104. {
  105. }
  106. public void Refresh()
  107. {
  108. Invoices.Refresh(false, true);
  109. }
  110. public string SectionName => "Invoice Grid";
  111. public DataModel DataModel(Selection selection)
  112. {
  113. var ids = Invoices.ExtractValues(x => x.ID, selection).ToArray();
  114. return new InvoiceDataModel(Filter<Invoice>.Where(x => x.ID).InList(ids));
  115. }
  116. public void Heartbeat(TimeSpan time)
  117. {
  118. }
  119. private void Invoices_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  120. {
  121. var _invoice = e.Rows?.FirstOrDefault()?.ToObject<Invoice>();
  122. Parts.Invoice = _invoice;
  123. Parts.Refresh(false, true);
  124. Time.Invoice = _invoice;
  125. Time.Refresh(false, true);
  126. Lines.Invoice = _invoice;
  127. Lines.Refresh(false, true);
  128. Bills.Invoice = _invoice;
  129. Bills.Refresh(false, true);
  130. }
  131. private void DynamicSplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  132. {
  133. _userSettings.View = SplitPanel.View;
  134. _userSettings.AnchorWidth = SplitPanel.AnchorWidth;
  135. new UserConfiguration<InvoicePanelUserSettings>().Save(_userSettings);
  136. }
  137. }