using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using InABox.Wpf; namespace PRSDesktop; public class SupplierBillPanelSettings : IUserConfigurationSettings { public SupplierBillPanelSettings() { AnchorWidth = 500F; ViewType = ScreenViewType.Combined; } public ScreenViewType ViewType { get; set; } public double AnchorWidth { get; set; } } public class SupplierBillPanelProperties : BaseObject, IGlobalConfigurationSettings { [IntegerEditor] [EditorSequence(1)] public int CurrencyDecimalPlaces { get; set; } = 2; [EditorSequence(2)] [CheckBoxEditor] public bool AllowBlankBillNumbers { get; set; } } public partial class SupplierBillPanel : UserControl, IPanel, IPropertiesPanel { private SupplierBillPanelSettings settings; public SupplierBillPanel() { InitializeComponent(); } public bool IsReady { get; set; } public event DataModelUpdateEvent? OnUpdateDataModel; public Dictionary Selected() { return new Dictionary { { typeof(Bill).EntityName(), Bills.SelectedRows } }; } public void CreateToolbarButtons(IPanelHost host) { AccountsSetupActions.Standard(host); PostUtils.CreateToolbarButtons(host, () => (DataModel(Selection.Selected) as IDataModel)!, () => Bills.Refresh(false, true), true); } public string SectionName => "Supplier Bills"; public SupplierBillPanelProperties Properties { get; set; } public DataModel DataModel(Selection selection) { var ids = Bills.ExtractValues(x => x.ID, selection).ToArray(); return new BaseDataModel(new Filter(x => x.ID).InList(ids)); } public void Refresh() { if (CheckSaved()) { Bills.Refresh(false, true); SetChanged(false); } } public void Setup() { settings = new UserConfiguration().Load(); SplitPanel.View = settings.ViewType == ScreenViewType.Register ? DynamicSplitPanelView.Master : settings.ViewType == ScreenViewType.Details ? DynamicSplitPanelView.Detail : DynamicSplitPanelView.Combined; SplitPanel.AnchorWidth = settings.AnchorWidth; Bill.SetLayoutType(); Bills.Refresh(true, false); } private void CheckSaved(CancelEventArgs cancel) { if (!bChanged) { return; } var result = MessageBox.Show("You have an unsaved Supplier Bill; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel); if (result == MessageBoxResult.Yes) { Bill.SaveItem(cancel); if (!cancel.Cancel) { MessageBox.Show("Purchase Order saved."); } } else if (result == MessageBoxResult.Cancel) { cancel.Cancel = true; } } private bool CheckSaved() { var cancel = new CancelEventArgs(); CheckSaved(cancel); return !cancel.Cancel; } public void Shutdown(CancelEventArgs? cancel) { if(cancel is not null) { CheckSaved(cancel); } } public void Heartbeat(TimeSpan time) { } public Dictionary DataEnvironment() { return new Dictionary { { typeof(Bill), Bills.Data } }; } private Bill[]? _bills = null; private CoreRow[]? _editRows = null; private void Bills_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e) { if(SplitPanel.View != DynamicSplitPanelView.Master) { ReloadBills(); } } private void ReloadBills() { if (Bills.SelectedRows.Length != 0) { _editRows = Bills.SelectedRows; _bills = Bills.LoadBills(_editRows); Bills.InitialiseEditorForm(Bill, _bills, null, true); Bill.Visibility = Visibility.Visible; } else { _bills = null; _editRows = null; Bill.Visibility = Visibility.Hidden; } } private void Bill_OnOnOK() { using (new WaitCursor()) { var cancel = new System.ComponentModel.CancelEventArgs(); Bill.SaveItem(cancel); if (!cancel.Cancel) { if(_editRows is not null && _bills is not null) { for(var i = 0; i < _editRows.Length; ++i) { Bills.UpdateRow(_editRows[i], _bills[i]); Bills.InvalidateRow(_editRows[i]); } } ReloadBills(); SetChanged(false); } } } private void Bill_OnOnCancel() { ReloadBills(); SetChanged(false); } private void SetChanged(bool changed) { bChanged = changed; Bills.IsEnabled = !changed; Bill.HideButtons = !changed; } private bool bChanged = false; private void Bill_OnOnChanged(object? sender, EventArgs e) { SetChanged(true); } private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e) { settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register : SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined; settings.AnchorWidth = SplitPanel.AnchorWidth; new UserConfiguration().Save(settings); } }