123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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;
- /// <summary>
- /// Interaction logic for JobRequisitionStockSelectionPage.xaml
- /// </summary>
- public partial class StockSelectionPage : ThemableWindow, INotifyPropertyChanged
- {
- public ObservableCollection<StockSelectionViewModel> ViewModels { get; } = new ObservableCollection<StockSelectionViewModel>();
- private readonly JobRequisitionItem Item;
- public Guid IssuingJobID { get; set; }
- public bool Allocated = false;
- public double TotalChosen => ViewModels.Sum(x => x.ChosenUnits);
- public StockSelectionPage(IEnumerable<StockHolding> 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));
- }
- }
|