using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using com.sun.tools.javac.util; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.Wpf; using javax.swing; namespace PRSDesktop; /// /// Interaction logic for JobRequisitionStockSelectionPage.xaml /// public partial class StockSelectionPage : ThemableWindow, INotifyPropertyChanged { public ObservableCollection ViewModels { get; } = new ObservableCollection(); private readonly JobRequisitionItem Item; public Guid IssuingJobID { get; set; } public bool Allocated = false; public double TotalChosen => ViewModels.Sum(x => x.ChosenUnits); public StockSelectionPage(IEnumerable holdings, JobRequisitionItem item, Job issuingJob, bool requisitioned = false) { Item = item; foreach (var holding in holdings) { ViewModels.Add(new StockSelectionViewModel(holding)); } InitializeComponent(); IssuingJobID = issuingJob.ID; jobLbl.Text = $"Taking stock from Job: {issuingJob.Name} ({issuingJob.JobNumber})"; if (requisitioned) { availableUnitsLbl.Text = "Allocated"; listView.IsEnabled = false; okButton.IsEnabled = false; } } private void SaveButton_Click(object sender, RoutedEventArgs e) { if(TotalChosen <= 0) { MessageWindow.ShowMessage("Please select at least from at least one holding to reserve.", "Error", image: MessageWindow.WarningImage); return; } var models = ViewModels.Where(x => x.ChosenUnits > 0); foreach (var model in models) { CreateStockMovements(model); } DialogResult = true; } private void CreateStockMovements(StockSelectionViewModel model) { var batch = new StockMovementBatch { TimeStamp = DateTime.Now, Type = StockMovementBatchType.Transfer, Notes = $"Allocated to Job {Item.Job.JobNumber}" }; Client.Save(batch, "Created for requisitioning stock"); var issuing = CreateBaseMovement(model, batch.ID); issuing.Job.ID = IssuingJobID; issuing.Issued = model.ChosenUnits; issuing.Type = StockMovementType.TransferOut; var receiving = CreateBaseMovement(model, batch.ID); receiving.Job.ID = Item.Job.ID; receiving.Received = model.ChosenUnits; receiving.JobRequisitionItem.ID = Item.ID; receiving.Type = StockMovementType.TransferIn; receiving.Transaction = issuing.Transaction; Client.Save(new StockMovement[] { issuing, receiving }, "Created from Reservation Management Screen"); } private StockMovement CreateBaseMovement(StockSelectionViewModel model, Guid batchid) { var mvt = new StockMovement(); mvt.Style.ID = model.Holding.Style.ID; mvt.Location.ID = model.Holding.Location.ID; mvt.Dimensions.CopyFrom(model.Holding.Dimensions); mvt.Batch.ID = batchid; mvt.Employee.ID = App.EmployeeID; mvt.Date = DateTime.Now; mvt.Product.ID = Item.Product.ID; mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line"; return mvt; } private void Cancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; } private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { if (sender is not TextBox box || box.Tag is not StockSelectionViewModel model) return; if (box.Text.IsNullOrWhiteSpace()) return; if (double.TryParse(box.Text, out double value)) { model.ChosenUnits = value; OnPropertyChanged(nameof(TotalChosen)); } else { // This'll call the event handler again. box.Text = "0"; } } private void All_Clicked(object sender, RoutedEventArgs e) { if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return; model.ChosenUnits = model.Holding.Units; OnPropertyChanged(nameof(TotalChosen)); } private void Minus_Click(object sender, RoutedEventArgs e) { if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return; model.ChosenUnits--; OnPropertyChanged(nameof(TotalChosen)); } private void Plus_Click(object sender, RoutedEventArgs e) { if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return; model.ChosenUnits++; OnPropertyChanged(nameof(TotalChosen)); } public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); if(propertyName == nameof(TotalChosen)) { okButton.IsEnabled = TotalChosen > 0; } } } public class StockSelectionViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; public string Location => Holding.Location.Description; public string Area => Holding.Location.Area.Description; public string Style => Holding.Style.Description; public double Units => Holding.Units; private double _chosenUnits; public double ChosenUnits { get => _chosenUnits; set { _chosenUnits = Math.Clamp(value, 0, Holding.Units); OnPropertyChanged(); } } public StockHolding Holding { get; set; } public StockSelectionViewModel(StockHolding holding) { Holding = holding; ChosenUnits = 0; } protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }