123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- using Comal.Classes;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- using InABox.WPF.Themes;
- using NPOI.SS.Formula.Functions;
- using System.Collections.ObjectModel;
- using InABox.Clients;
- using InABox.Wpf;
- using InABox.Wpf.Reports;
- using Columns = InABox.Core.Columns;
- namespace PRSDesktop;
- /// <summary>
- /// Interaction logic for JobRequisitionsPanel.xaml
- /// </summary>
- public partial class ReservationManagementPanel : UserControl, IPanel<JobRequisitionItem>
- {
- private ReservationManagementGlobalSettings _globalSettings = null!; // Initialised in Setup()
- private DynamicSplitPanelView CurrentView;
- private List<PurchaseOrder> PurchaseOrders = new List<PurchaseOrder>();
- public ReservationManagementPanel()
- {
- InitializeComponent();
- CurrentView = SplitPanel.View;
- JobRequiItems.Reconfigure(options =>
- {
- var canMultiSelect = Mode != PanelMode.Reserve || SplitPanel.View == DynamicSplitPanelView.Master;
- if(canMultiSelect)
- {
- options.MultiSelect = true;
- }
- else
- {
- options.MultiSelect = false;
- }
- });
- }
- enum PanelMode
- {
- Reserve,
- Purchase
- }
- private PanelMode Mode { get; set; }
- private void SelectDetailButton(Button button)
- {
- reserveBtn.Background = new SolidColorBrush(Colors.WhiteSmoke);
- reserveBtn.Foreground = new SolidColorBrush(Colors.Black);
- foreach(var btn in PurchaseOrderButtons.Children.OfType<Button>())
- {
- btn.Background = new SolidColorBrush(Colors.WhiteSmoke);
- btn.Foreground = new SolidColorBrush(Colors.Black);
- }
- AddPOButton.Background = new SolidColorBrush(Colors.WhiteSmoke);
- AddPOButton.Foreground = new SolidColorBrush(Colors.Black);
- button.Background = ThemeManager.SelectedTabItemBackgroundBrush;
- button.Foreground = ThemeManager.SelectedTabItemForegroundBrush;
- }
- private void Reconfigure()
- {
- JobRequiItems.Reconfigure();
- OnUpdateDataModel?.Invoke(SectionName, DataModel(Selection.None));
- }
- private void SelectReserved()
- {
- SelectDetailButton(reserveBtn);
- reserveCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
- purchaseCol.Width = new System.Windows.GridLength(0);
- if(Mode != PanelMode.Reserve)
- {
- Mode = PanelMode.Reserve;
- Reconfigure();
- }
- }
- private void SelectNewPurchaseOrder()
- {
- SelectDetailButton(AddPOButton);
- reserveCol.Width = new System.Windows.GridLength(0);
- purchaseCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
- if (Mode != PanelMode.Purchase)
- {
- Mode = PanelMode.Purchase;
- Reconfigure();
- }
- purchasing.LoadFromRequiLine(Guid.Empty);
- }
- private void SelectPurchaseOrder(Button button, PurchaseOrder order)
- {
- SelectDetailButton(button);
- reserveCol.Width = new System.Windows.GridLength(0);
- purchaseCol.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
- if (Mode != PanelMode.Purchase)
- {
- Mode = PanelMode.Purchase;
- Reconfigure();
- }
- purchasing.LoadFromRequiLine(order.ID);
- }
- public bool IsReady { get; set; }
- public string SectionName => "Job Requisitions";
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- ProductSetupActions.Standard(host);
- host.CreateSetupAction(new PanelAction() { Caption = "Reservation Management Settings", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
- host.CreatePanelAction(new PanelAction("Treatment PO", PRSDesktop.Resources.purchase, TreatmentPO_Click));
- if(Mode == PanelMode.Purchase && SplitPanel.IsDetailVisible())
- {
- var sectionName = SupplierPurchaseOrderPanel.SectionName;
- var dataModel = SupplierPurchaseOrderPanel.DataModel(Array.Empty<Guid>());
- var reports = ReportUtils.LoadReports(sectionName, dataModel);
- foreach(var report in reports)
- {
- host.CreateReport(PanelHost.CreateReportAction(report, (selection) =>
- {
- Guid[] ids;
- if(selection == Selection.None)
- {
- ids = Array.Empty<Guid>();
- }
- else
- {
- ids = PurchaseOrders.Select(x => x.ID).ToArray();
- }
- return SupplierPurchaseOrderPanel.DataModel(ids);
- }));
- }
- }
- }
- private void ConfigSettingsClick(PanelAction obj)
- {
- var grid = new DynamicItemsListGrid<ReservationManagementGlobalSettings>();
- if(grid.EditItems(new ReservationManagementGlobalSettings[] { _globalSettings }))
- {
- new GlobalConfiguration<ReservationManagementGlobalSettings>().Save(_globalSettings);
- holdings.CompanyDefaultStyle = _globalSettings.ProductStyle;
- JobRequiItems.DueDateAlert = _globalSettings.DueDateAlert;
- JobRequiItems.DueDateWarning = _globalSettings.DueDateWarning;
- }
- }
- public DataModel DataModel(Selection selection)
- {
- var ids = JobRequiItems.ExtractValues(x => x.ID, selection).ToArray();
- return new BaseDataModel<JobRequisitionItem>(new Filter<JobRequisitionItem>(x => x.ID).InList(ids));
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Refresh()
- {
- JobRequiItems.Refresh(false, true);
- }
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]>
- {
- [typeof(JobRequisitionItem).EntityName()] = JobRequiItems.SelectedRows
- };
- }
- public void Setup()
- {
- SelectReserved();
- JobRequiItems.OnSelectItem += OnJobRequiItemSelected;
- JobRequiItems.Refresh(true, false);
- _globalSettings = new GlobalConfiguration<ReservationManagementGlobalSettings>().Load();
- JobRequiItems.DueDateAlert = _globalSettings.DueDateAlert;
- JobRequiItems.DueDateWarning = _globalSettings.DueDateWarning;
-
- holdings.CompanyDefaultStyle = _globalSettings.ProductStyle;
- holdings.OnHoldingsReviewRefresh += Holdings_OnHoldingsReviewRefresh;
- purchasing.OnPurchaseOrderSaved += Refresh;
- }
- private void OnJobRequiItemSelected(object sender, DynamicGridSelectionEventArgs e)
- {
- if (SplitPanel.IsDetailVisible())
- {
- holdings.Item = e.Rows?.FirstOrDefault()?.ToObject<JobRequisitionItem>();
- RefreshPurchaseOrderButtons();
- }
- }
- private void RefreshPurchaseOrderButtons()
- {
- var requiIDs = JobRequiItems.SelectedRows
- .Select(x => x.Get<JobRequisitionItem, Guid>(x => x.ID))
- .ToArray();
- var pos = Client.Query(
- new Filter<PurchaseOrder>(x => x.ID).InQuery(
- new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.JobRequisitionItem.ID)
- .InList(requiIDs),
- x => x.PurchaseOrderItem.PurchaseOrderLink.ID),
- Columns.None<PurchaseOrder>().Add(x => x.ID)
- .Add(x => x.PONumber))
- .ToObjects<PurchaseOrder>().ToList();
- PurchaseOrders = pos;
- PurchaseOrderButtons.Children.Clear();
- var buttons = new List<(Button btn, PurchaseOrder po)>();
- foreach (var po in pos)
- {
- var button = new Button
- {
- Content = po.PONumber,
- Height = 30,
- FontWeight = FontWeights.DemiBold,
- BorderBrush = new SolidColorBrush(Colors.DarkGray),
- BorderThickness = new Thickness(1.25),
- Margin = new Thickness(2, 0, 0, 2),
- Padding = new Thickness(13, 3, 13, 3)
- };
- button.Click += (o, e) =>
- {
- SelectPurchaseOrder(button, po);
- };
- buttons.Add((button, po));
- PurchaseOrderButtons.Children.Add(button);
- }
- if (Mode == PanelMode.Purchase)
- {
- if (buttons.Count == 0)
- {
- SelectNewPurchaseOrder();
- }
- else
- {
- var btn = buttons[0];
- SelectPurchaseOrder(btn.btn, btn.po);
- }
- }
- else if (Mode == PanelMode.Reserve)
- {
- SelectReserved();
- }
- }
- private void Holdings_OnHoldingsReviewRefresh()
- {
- Refresh();
- }
- private void CheckSaved(CancelEventArgs cancel)
- {
- if (!SplitPanel.IsDetailVisible() || !purchasing.EditorChanged)
- {
- return;
- }
- var result = MessageWindow.ShowYesNoCancel("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?");
- if (result == MessageWindowResult.Yes)
- {
- purchasing.Editor.SaveItem(cancel);
- if (!cancel.Cancel)
- {
- MessageWindow.ShowMessage("Purchase Order saved.", "Success");
- }
- }
- else if (result == MessageWindowResult.Cancel)
- {
- cancel.Cancel = true;
- }
- }
- public void Shutdown(CancelEventArgs? cancel)
- {
- if(cancel is not null && purchasing.EditorChanged)
- {
- CheckSaved(cancel);
- }
- }
- private void ReserveStock_Clicked(object sender, System.Windows.RoutedEventArgs e)
- {
- SelectReserved();
- }
- private void Purchasing_Drop(object sender, DragEventArgs e)
- {
- if(DynamicGridUtils.TryGetDropData(e, out var type, out var table))
- {
- if(type == typeof(JobRequisitionItem))
- {
- purchasing.DropItems(table.Rows);
- }
- }
- }
- private void AddPOButton_Click(object sender, RoutedEventArgs e)
- {
- SelectNewPurchaseOrder();
- }
- private void TreatmentPO_Click(PanelAction action)
- {
- var jris = JobRequiItems.SelectedRows.ToArray<JobRequisitionItem>();
- if(jris.Length == 0)
- {
- MessageWindow.ShowMessage("Please select at least one job requisition item.", "No items selected");
- return;
- }
- foreach(var jri in jris)
- {
- if(jri.TreatmentRequired - jri.TreatmentOnOrder <= 0)
- {
- MessageWindow.ShowMessage("Please select only items requiring treatment.", "Already treated");
- return;
- }
- }
- }
- private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
- {
- JobRequiItems.Reconfigure();
- if(CurrentView != e.View)
- {
- CurrentView = e.View;
- if (e.View != DynamicSplitPanelView.Master)
- {
- Refresh();
- }
- }
- }
- }
|