SupplierBillPanel.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.WPF;
  13. using InABox.Wpf;
  14. namespace PRSDesktop;
  15. public class SupplierBillPanelSettings : IUserConfigurationSettings
  16. {
  17. public SupplierBillPanelSettings()
  18. {
  19. AnchorWidth = 500F;
  20. ViewType = ScreenViewType.Combined;
  21. }
  22. public ScreenViewType ViewType { get; set; }
  23. public double AnchorWidth { get; set; }
  24. }
  25. public class SupplierBillPanelProperties : BaseObject, IGlobalConfigurationSettings
  26. {
  27. [IntegerEditor]
  28. [EditorSequence(1)]
  29. public int CurrencyDecimalPlaces { get; set; } = 2;
  30. [EditorSequence(2)]
  31. [CheckBoxEditor]
  32. public bool AllowBlankBillNumbers { get; set; }
  33. }
  34. public partial class SupplierBillPanel : UserControl, IPanel<Bill>, IPropertiesPanel<SupplierBillPanelProperties, CanConfigureAccountsPanels>
  35. {
  36. private SupplierBillPanelSettings settings;
  37. public SupplierBillPanel()
  38. {
  39. InitializeComponent();
  40. }
  41. public bool IsReady { get; set; }
  42. public event DataModelUpdateEvent? OnUpdateDataModel;
  43. public Dictionary<string, object[]> Selected()
  44. {
  45. return new Dictionary<string, object[]> { { typeof(Bill).EntityName(), Bills.SelectedRows } };
  46. }
  47. public void CreateToolbarButtons(IPanelHost host)
  48. {
  49. AccountsSetupActions.Standard(host);
  50. PostUtils.CreateToolbarButtons(host,
  51. () => (DataModel(Selection.Selected) as IDataModel<Bill>)!,
  52. () => Bills.Refresh(false, true),
  53. true);
  54. }
  55. public string SectionName => "Supplier Bills";
  56. public SupplierBillPanelProperties Properties { get; set; }
  57. public DataModel DataModel(Selection selection)
  58. {
  59. var ids = Bills.ExtractValues(x => x.ID, selection).ToArray();
  60. return new BaseDataModel<Bill>(new Filter<Bill>(x => x.ID).InList(ids));
  61. }
  62. public void Refresh()
  63. {
  64. if (CheckSaved())
  65. {
  66. Bills.Refresh(false, true);
  67. SetChanged(false);
  68. }
  69. }
  70. public void Setup()
  71. {
  72. settings = new UserConfiguration<SupplierBillPanelSettings>().Load();
  73. SplitPanel.View = settings.ViewType == ScreenViewType.Register ? DynamicSplitPanelView.Master :
  74. settings.ViewType == ScreenViewType.Details ? DynamicSplitPanelView.Detail : DynamicSplitPanelView.Combined;
  75. SplitPanel.AnchorWidth = settings.AnchorWidth;
  76. Bill.SetLayoutType<SupplierBillEditLayout>();
  77. Bills.Refresh(true, false);
  78. }
  79. private void CheckSaved(CancelEventArgs cancel)
  80. {
  81. if (!bChanged)
  82. {
  83. return;
  84. }
  85. var result = MessageBox.Show("You have an unsaved Supplier Bill; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
  86. if (result == MessageBoxResult.Yes)
  87. {
  88. Bill.SaveItem(cancel);
  89. if (!cancel.Cancel)
  90. {
  91. MessageBox.Show("Purchase Order saved.");
  92. }
  93. }
  94. else if (result == MessageBoxResult.Cancel)
  95. {
  96. cancel.Cancel = true;
  97. }
  98. }
  99. private bool CheckSaved()
  100. {
  101. var cancel = new CancelEventArgs();
  102. CheckSaved(cancel);
  103. return !cancel.Cancel;
  104. }
  105. public void Shutdown(CancelEventArgs? cancel)
  106. {
  107. if(cancel is not null)
  108. {
  109. CheckSaved(cancel);
  110. }
  111. }
  112. public void Heartbeat(TimeSpan time)
  113. {
  114. }
  115. public Dictionary<Type, CoreTable> DataEnvironment()
  116. {
  117. return new Dictionary<Type, CoreTable>
  118. {
  119. { typeof(Bill), Bills.Data }
  120. };
  121. }
  122. private Bill[]? _bills = null;
  123. private void Bills_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  124. {
  125. if(SplitPanel.View != DynamicSplitPanelView.Master)
  126. {
  127. ReloadBills();
  128. }
  129. }
  130. private void ReloadBills()
  131. {
  132. if (Bills.SelectedRows?.Any() == true)
  133. {
  134. _bills = Bills.LoadBills(Bills.SelectedRows);
  135. Bills.InitialiseEditorForm(Bill, _bills, null, true);
  136. Bill.Visibility = Visibility.Visible;
  137. }
  138. else
  139. {
  140. _bills = null;
  141. Bill.Visibility = Visibility.Hidden;
  142. }
  143. }
  144. private void Bill_OnOnOK()
  145. {
  146. using (new WaitCursor())
  147. {
  148. var cancel = new System.ComponentModel.CancelEventArgs();
  149. Bill.SaveItem(cancel);
  150. if (!cancel.Cancel)
  151. {
  152. ReloadBills();
  153. SetChanged(false);
  154. }
  155. }
  156. }
  157. private void Bill_OnOnCancel()
  158. {
  159. ReloadBills();
  160. SetChanged(false);
  161. }
  162. private void SetChanged(bool changed)
  163. {
  164. bChanged = changed;
  165. Bills.IsEnabled = !changed;
  166. Bill.HideButtons = !changed;
  167. }
  168. private bool bChanged = false;
  169. private void Bill_OnOnChanged(object? sender, EventArgs e)
  170. {
  171. SetChanged(true);
  172. }
  173. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  174. {
  175. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
  176. SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
  177. settings.AnchorWidth = SplitPanel.AnchorWidth;
  178. new UserConfiguration<SupplierBillPanelSettings>().Save(settings);
  179. }
  180. }