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;
namespace PRSDesktop;
///
/// Interaction logic for JobRequisitionsPanel.xaml
///
public partial class ReservationManagementPanel : UserControl, IPanel
{
private ReservationManagementGlobalSettings _globalSettings = null!; // Initialised in Setup()
private DynamicSplitPanelView CurrentView;
private List PurchaseOrders = new List();
public ReservationManagementPanel()
{
InitializeComponent();
CurrentView = SplitPanel.View;
JobRequiItems.Reconfigure(options =>
{
var canMultiSelect = Mode != PanelMode.Reserve || SplitPanel.View == DynamicSplitPanelView.Master;
if(canMultiSelect)
{
options.Add(DynamicGridOption.MultiSelect);
}
else
{
options.Remove(DynamicGridOption.MultiSelect);
}
});
}
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())
{
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 });
if(Mode == PanelMode.Purchase && SplitPanel.IsDetailVisible())
{
var sectionName = SupplierPurchaseOrderPanel.SectionName;
var dataModel = SupplierPurchaseOrderPanel.DataModel(Array.Empty());
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();
}
else
{
ids = PurchaseOrders.Select(x => x.ID).ToArray();
}
return SupplierPurchaseOrderPanel.DataModel(ids);
}));
}
}
}
private void ConfigSettingsClick(PanelAction obj)
{
var grid = new DynamicItemsListGrid();
if(grid.EditItems(new ReservationManagementGlobalSettings[] { _globalSettings }))
{
new GlobalConfiguration().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(new Filter(x => x.ID).InList(ids));
}
public void Heartbeat(TimeSpan time)
{
}
public void Refresh()
{
JobRequiItems.Refresh(false, true);
}
public Dictionary Selected()
{
return new Dictionary
{
[typeof(JobRequisitionItem).EntityName()] = JobRequiItems.SelectedRows
};
}
public void Setup()
{
SelectReserved();
JobRequiItems.OnSelectItem += OnJobRequiItemSelected;
JobRequiItems.Refresh(true, false);
_globalSettings = new GlobalConfiguration().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();
RefreshPurchaseOrderButtons();
}
}
private void RefreshPurchaseOrderButtons()
{
var requiIDs = JobRequiItems.SelectedRows
.Select(x => x.Get(x => x.ID))
.ToArray();
var pos = Client.Query(
new Filter(x => x.ID).InQuery(
new Filter(x => x.JobRequisitionItem.ID)
.InList(requiIDs),
x => x.PurchaseOrderItem.PurchaseOrderLink.ID),
new Columns(x => x.ID)
.Add(x => x.PONumber))
.ToObjects().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 SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
{
JobRequiItems.Reconfigure();
if(CurrentView != e.View)
{
CurrentView = e.View;
if (e.View != DynamicSplitPanelView.Master)
{
Refresh();
}
}
}
}