SupplierBillPanel.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 CoreRow[]? _editRows = null;
  124. private void Bills_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  125. {
  126. if(SplitPanel.View != DynamicSplitPanelView.Master)
  127. {
  128. ReloadBills();
  129. }
  130. }
  131. private void ReloadBills()
  132. {
  133. if (Bills.SelectedRows.Length != 0)
  134. {
  135. _editRows = Bills.SelectedRows;
  136. _bills = Bills.LoadBills(_editRows);
  137. Bills.InitialiseEditorForm(Bill, _bills, null, true);
  138. Bill.Visibility = Visibility.Visible;
  139. }
  140. else
  141. {
  142. _bills = null;
  143. _editRows = null;
  144. Bill.Visibility = Visibility.Hidden;
  145. }
  146. }
  147. private void Bill_OnOnOK()
  148. {
  149. using (new WaitCursor())
  150. {
  151. var cancel = new System.ComponentModel.CancelEventArgs();
  152. Bill.SaveItem(cancel);
  153. if (!cancel.Cancel)
  154. {
  155. if(_editRows is not null && _bills is not null)
  156. {
  157. for(var i = 0; i < _editRows.Length; ++i)
  158. {
  159. Bills.UpdateRow(_editRows[i], _bills[i]);
  160. Bills.InvalidateRow(_editRows[i]);
  161. }
  162. }
  163. ReloadBills();
  164. SetChanged(false);
  165. }
  166. }
  167. }
  168. private void Bill_OnOnCancel()
  169. {
  170. ReloadBills();
  171. SetChanged(false);
  172. }
  173. private void SetChanged(bool changed)
  174. {
  175. bChanged = changed;
  176. Bills.IsEnabled = !changed;
  177. Bill.HideButtons = !changed;
  178. }
  179. private bool bChanged = false;
  180. private void Bill_OnOnChanged(object? sender, EventArgs e)
  181. {
  182. SetChanged(true);
  183. }
  184. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  185. {
  186. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
  187. SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
  188. settings.AnchorWidth = SplitPanel.AnchorWidth;
  189. new UserConfiguration<SupplierBillPanelSettings>().Save(settings);
  190. }
  191. }