123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace PRSDesktop.Panels.Products.Locations;
- public class StockHoldingRelocationItem : INotifyPropertyChanged
- {
- public string ItemNumber { get; set; }
- public string Text { get; set; }
- public double Quantity { get; set; }
- private double _issued;
- public double Issued
- {
- get => _issued;
- set
- {
- _issued = value;
- OnPropertyChanged();
- }
- }
- public Guid ID { get; set; }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- /// <summary>
- /// Interaction logic for StockHoldingRelocationWindow.xaml
- /// </summary>
- public partial class StockHoldingRelocationWindow : Window, INotifyPropertyChanged
- {
- public ObservableCollection<StockHoldingRelocationItem> Items { get; set; } = new();
- private double _totalQuantity;
- public double TotalQuantity
- {
- get => _totalQuantity;
- set
- {
- _totalQuantity = value;
- OnPropertyChanged();
- }
- }
- private double _totalIssued;
- public double TotalIssued
- {
- get => _totalIssued;
- set
- {
- _totalIssued = value;
- OnPropertyChanged();
- OnPropertyChanged(nameof(CanSave));
- }
- }
- public StockHolding From { get; set; }
- private bool _isTargetEditable = true;
- public bool IsTargetEditable
- {
- get => _isTargetEditable;
- set
- {
- _isTargetEditable = value;
- OnPropertyChanged();
- }
- }
- private StockLocation? _to;
- public StockLocation? To
- {
- get => _to;
- set
- {
- _to = value;
- OnPropertyChanged();
- OnPropertyChanged(nameof(CanSave));
- OnPropertyChanged($"{nameof(To)}.{nameof(To.Code)}");
- OnPropertyChanged($"{nameof(To)}.{nameof(To.Description)}");
- }
- }
- public bool CanSave => (!IsTargetEditable || To is not null) && TotalIssued > 0;
- public StockHoldingRelocationWindow(StockHolding from, IEnumerable<JobRequisitionItem> items)
- {
- Client.EnsureColumns(from, new Columns<StockHolding>(x => x.Location.Code));
- From = from;
- InitializeComponent();
- SetRequisitionItems(items);
- Items.CollectionChanged += (o, e) => Recalculate();
- }
- private bool _observing = false;
- public void SetObserving(bool observing)
- {
- if(_observing != observing)
- {
- _observing = observing;
- if (_observing)
- {
- Recalculate();
- }
- }
- }
- public void SetRequisitionItems(IEnumerable<JobRequisitionItem> items)
- {
- SetObserving(false);
- var rItems = items.AsIList();
- var rIDs = rItems.Select(x => x.ID).Where(x => x != Guid.Empty).ToArray();
- var quantities = Client.Query(
- StockHolding.GetFilter(From)
- .Combine(new Filter<StockMovement>(x => x.JobRequisitionItem.ID).InList(rIDs)),
- new Columns<StockMovement>(x => x.Units).Add(x => x.JobRequisitionItem.ID))
- .ToObjects<StockMovement>().GroupBy(x => x.JobRequisitionItem.ID).ToDictionary(x => x.Key, x => x.Sum(x => x.Units));
- var requidItems = new List<StockHoldingRelocationItem>();
- foreach(var item in rItems)
- {
- var qty = item.ID != Guid.Empty ? quantities.GetValueOrDefault(item.ID) : item.Qty;
- var newItem = new StockHoldingRelocationItem
- {
- Issued = 0,
- Quantity = qty,
- Text = item.ID == Guid.Empty
- ? "Unrequisitioned Items"
- : $"{item.Job.JobNumber}:{item.Requisition.Number} {item.Requisition.Description} ({qty})",
- ID = item.ID
- };
- newItem.PropertyChanged += (o, e) => Recalculate();
- if(item.ID == Guid.Empty)
- {
- Items.Add(newItem);
- }
- else
- {
- requidItems.Add(newItem);
- }
- }
- int i = 1;
- foreach(var item in requidItems)
- {
- item.ItemNumber = $"{i}. ";
- Items.Add(item);
- ++i;
- }
- SetObserving(true);
- }
- public Dictionary<Guid, double> GetQuantities()
- {
- return Items.ToDictionary(x => x.ID, x => x.Issued);
- }
- public StockLocation GetTargetLocation()
- {
- return To ?? new StockLocation();
- }
- private void Recalculate()
- {
- if (!_observing) return;
- TotalQuantity = Items.Sum(x => x.Quantity);
- TotalIssued = Items.Sum(x => x.Issued);
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- private void Minus_Click(object sender, RoutedEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
- item.Issued = Math.Max(0, item.Issued - 1);
- }
- private void Plus_Click(object sender, RoutedEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
- item.Issued = Math.Min(item.Issued + 1, item.Quantity);
- }
- private void None_Click(object sender, RoutedEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
- item.Issued = 0;
- }
- private void All_Click(object sender, RoutedEventArgs e)
- {
- if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
- item.Issued = item.Quantity;
- }
- private bool DoLookup(string? column, string? value)
- {
- var grid = new MultiSelectDialog<StockLocation>(
- LookupFactory.DefineFilter<StockLocation>(),
- new Columns<StockLocation>(x => x.ID).Add(x => x.Code).Add(x => x.Description),
- multiselect: false);
- if (grid.ShowDialog(column, value))
- {
- To = grid.Data().Rows.First().ToObject<StockLocation>();
- return true;
- }
- else
- {
- return false;
- }
- }
- private void ToButton_Click(object sender, RoutedEventArgs e)
- {
- DoLookup(null, null);
- }
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = true;
- Close();
- }
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- Close();
- }
- private void ToBox_LostFocus(object sender, RoutedEventArgs e)
- {
- if (ToBox.Text.IsNullOrWhiteSpace() || ToBox.Text == To?.Code) return;
- var location = Client.Query(
- new Filter<StockLocation>(x => x.Code).IsEqualTo(ToBox.Text),
- new Columns<StockLocation>(x => x.ID).Add(x => x.Code).Add(x => x.Description))
- .ToObjects<StockLocation>().FirstOrDefault();
- if(location is not null)
- {
- To = location;
- }
- else
- {
- if(!DoLookup(nameof(To.Code), ToBox.Text))
- {
- To = null;
- }
- }
- }
- }
|