123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using NPOI.Util;
- namespace PRSDesktop;
- public class SupplierBillsSettings : IUserConfigurationSettings
- {
- public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
- }
- public class SupplierBills : DynamicDataGrid<Bill>
- {
- private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private static readonly BitmapImage? data = PRSDesktop.Resources.pencil.AsBitmapImage();
- private static readonly BitmapImage? check = PRSDesktop.Resources.checklist.AsBitmapImage();
- private SupplierBillPanelProperties _settings = null;
- private SupplierBillsSettings _userSettings;
-
- public SupplierBills()
- {
- _settings = new GlobalConfiguration<SupplierBillPanelProperties>().Load();
- _userSettings = new UserConfiguration<SupplierBillsSettings>().Load();
- FilterComponent.BuiltInFilters.Add(new("My Unapproved Bills", GetMyUnapprovedBillsFilter));
- FilterComponent.SetSettings(_userSettings.Filters, false);
- FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
- HiddenColumns.Add(x => x.IsApproved);
- HiddenColumns.Add(x => x.DataEntered);
- HiddenColumns.Add(x => x.Checked);
- HiddenColumns.Add(x => x.ApprovalSet.ID);
- ActionColumns.Add(new DynamicImageColumn(DataEntered_Image, null) { ToolTip = DataEntered_ToolTip });
- ActionColumns.Add(new DynamicImageColumn(Checked_Image, null) { ToolTip = Checked_ToolTip });
- ActionColumns.Add(new DynamicImageColumn(Approved_Image, null) { ToolTip = Approved_ToolTip });
- PostUtils.AddPostColumn(this);
- ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
- }
- private Filter<Bill> GetMyUnapprovedBillsFilter()
- {
- return new Filter<Bill>(x => x.ID)
- .InQuery(
- new Filter<BillApproval>(x => x.Employee.ID).IsEqualTo(App.EmployeeID)
- .And(x => x.Approved).IsEqualTo(DateTime.MinValue),
- x => x.Bill.ID);
- }
- private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
- {
- if (row is null) return;
- var menu = column.GetMenu();
- var approvalSetItem = menu.AddItem("Approval Set", null, null);
- approvalSetItem.AddItem("Loading...", null, null, enabled: false);
- var id = row.Get<Bill, Guid>(x => x.ID);
- var selectedApproval = row.Get<Bill, Guid>(x => x.ApprovalSet.ID);
- Task.Run(() =>
- {
- return Client.Query<BillApprovalSet>(
- null,
- Columns.None<BillApprovalSet>().Add(x => x.ID).Add(x => x.Code).Add(x => x.Description))
- .ToArray<BillApprovalSet>();
- }).ContinueWith(approvals =>
- {
- approvalSetItem.Items.Clear();
- foreach(var approval in approvals.Result)
- {
- var item = approvalSetItem.AddItem($"{approval.Code}: {approval.Description}", null, (row, approval), ApprovalSet_Click);
- item.IsChecked = approval.ID == selectedApproval;
- }
- }, TaskScheduler.FromCurrentSynchronizationContext());
- }
- private void ApprovalSet_Click((CoreRow row, BillApprovalSet approvalSet) item)
- {
- var bill = item.row.ToObject<Bill>();
- if (bill.ApprovalSet.ID == item.approvalSet.ID) return;
- BillApprovalSetEmployee[] oldEmployees;
- if(bill.ApprovalSet.ID != Guid.Empty)
- {
- oldEmployees = Client.Query(
- new Filter<BillApprovalSetEmployee>(x => x.ApprovalSet.ID).IsEqualTo(bill.ApprovalSet.ID),
- Columns.None<BillApprovalSetEmployee>().Add(x => x.Employee.ID),
- new SortOrder<BillApprovalSetEmployee>(x => x.Sequence))
- .ToArray<BillApprovalSetEmployee>();
- }
- else
- {
- oldEmployees = [];
- }
- var newEmployees = Client.Query(
- new Filter<BillApprovalSetEmployee>(x => x.ApprovalSet.ID).IsEqualTo(item.approvalSet.ID),
- Columns.None<BillApprovalSetEmployee>().Add(x => x.Employee.ID),
- new SortOrder<BillApprovalSetEmployee>(x => x.Sequence))
- .ToArray<BillApprovalSetEmployee>();
- var approvals = Client.Query(
- new Filter<BillApproval>(x => x.Bill.ID).IsEqualTo(bill.ID),
- Columns.None<BillApproval>()
- .Add(x => x.ID)
- .Add(x => x.Approved)
- .Add(x => x.Employee.ID)
- .Add(x => x.Sequence),
- new SortOrder<BillApproval>(x => x.Sequence))
- .ToArray<BillApproval>();
- var toDelete = new List<BillApproval>();
- var toSave = new List<BillApproval>();
- foreach(var employee in newEmployees)
- {
- if(!approvals.Any(x => x.Employee.ID == employee.Employee.ID))
- {
- var newApproval = new BillApproval();
- newApproval.Employee.CopyFrom(employee.Employee);
- newApproval.Bill.CopyFrom(bill);
- toSave.Add(newApproval);
- }
- }
- foreach(var approval in approvals)
- {
- if(approval.Approved == DateTime.MinValue
- && oldEmployees.Any(x => x.Employee.ID == approval.Employee.ID)
- && !newEmployees.Any(x => x.Employee.ID == approval.Employee.ID))
- {
- toDelete.Add(approval);
- }
- else
- {
- toSave.Add(approval);
- }
- }
- foreach(var (i, approval) in toSave.WithIndex())
- {
- approval.Sequence = i;
- }
- Client.Delete(toDelete, "Deleted by changing approval set.");
- Client.Save(toSave, "Updated when changing approval set.");
- bill.ApprovalSet.CopyFrom(item.approvalSet);
- SaveItem(bill);
- Refresh(false, true);
- }
- public override DynamicEditorPages LoadEditorPages(Bill item)
- {
- // Need to re-order the pages, since we don't want approvals before lines or documents.
- var pages = base.LoadEditorPages(item);
- if(pages.TryGetPage<SupplierBillLineGrid>(out var billLineGrid))
- {
- billLineGrid.Order = 0;
- }
- if(pages.TryGetPage<DynamicDocumentGrid<BillDocument, Bill, BillLink>>(out var docGrid))
- {
- docGrid.Order = 1;
- }
- if(pages.TryGetPage<DynamicOneToManyGrid<Bill, BillApproval>>(out var approvalGrid))
- {
- approvalGrid.Order = 2;
- }
-
- return pages;
- }
- private FrameworkElement? Approved_ToolTip(DynamicActionColumn column, CoreRow? row)
- {
- return row is null ? column.TextToolTip("Has this bill been approved?")
- : !row.Get<Bill, bool>(x => x.IsApproved) ? column.TextToolTip("Not yet approved")
- : column.TextToolTip("Approved");
- }
- private FrameworkElement? Checked_ToolTip(DynamicActionColumn column, CoreRow? row)
- {
- return row is null ? column.TextToolTip("Has this bill been checked?")
- : row.Get<Bill, DateTime>(x => x.Checked).IsEmpty() ? column.TextToolTip("Not yet checked")
- : column.TextToolTip("Checked");
- }
- private FrameworkElement? DataEntered_ToolTip(DynamicActionColumn column, CoreRow? row)
- {
- return row is null ? column.TextToolTip("Has this bill been entered via Data Entry?")
- : row.Get<Bill, DateTime>(x => x.DataEntered).IsEmpty() ? column.TextToolTip("Data not entered.")
- : column.TextToolTip("Data Entered");
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.FilterRows = true;
- options.SelectColumns = true;
- options.MultiSelect = true;
- options.RecordCount = true;
- options.ShowHelp = true;
- }
- private BitmapImage? DataEntered_Image(CoreRow? row)
- {
- return (row != null) && row.Get<Bill, DateTime>(x => x.DataEntered).IsEmpty()
- ? null
- : data;
- }
- private BitmapImage? Checked_Image(CoreRow? row)
- {
- return (row != null) && row.Get<Bill, DateTime>(x => x.Checked).IsEmpty()
- ? null
- : check;
- }
- private BitmapImage? Approved_Image(CoreRow? row)
- {
- return (row != null) && !row.Get<Bill, bool>(x => x.IsApproved)
- ? null
- : tick;
- }
- public Bill[] LoadBills(CoreRow[] rows)
- {
- return LoadItems(rows);
- }
- protected override void DoValidate(Bill[] items, List<string> errors)
- {
- base.DoValidate(items, errors);
- if(items.Any(x => x.Number.IsNullOrWhiteSpace()) && !_settings.AllowBlankBillNumbers)
- {
- errors.Add($"[{nameof(Bill.Number)}] may not be blank!");
- }
- if(items.Any(x => x.SupplierLink.ID == Guid.Empty))
- {
- errors.Add($"[{nameof(Bill.SupplierLink)}] may not be blank!");
- }
- }
-
- private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
- {
- _userSettings.Filters = settings;
- new UserConfiguration<SupplierBillsSettings>().Save(_userSettings);
- }
- }
|