123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- 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.DynamicGrid;
- using InABox.Wpf;
- using javax.swing;
- namespace PRSDesktop;
- public class StockSelectionItem : JobRequisitionItemSelectionItem
- {
- public string Area => Holding.Location.Area.Code;
- public string Requisition => JRI is null
- ? "Unrequisitioned Items"
- : $"{JRI.Job.JobNumber}: #{JRI.Requisition.Number} ({JRI.Requisition.Description})";
- public string Location => Holding.Location.Code;
- public string Style => Holding.Style.Code;
- public StockHolding Holding { get; set; }
- }
- public class StockSelectionGrid : JobRequisitionItemSelectionGrid<StockSelectionItem>
- {
- public bool ShowRequisition { get; set; } = false;
- public override DynamicGridColumns GenerateColumns()
- {
- var columns = new DynamicGridColumns();
- if (ShowRequisition)
- {
- columns.Add<StockSelectionItem, string>(x => x.Area, 120, "Area", "", Alignment.MiddleCenter);
- columns.Add<StockSelectionItem, string>(x => x.Location, 120, "Location", "", Alignment.MiddleCenter);
- columns.Add<StockSelectionItem, string>(x => x.Style, 120, "Style", "", Alignment.MiddleCenter);
- columns.Add<StockSelectionItem, string>(x => x.Requisition, 0, "Requisition", "", Alignment.MiddleLeft);
- }
- else
- {
- columns.Add<StockSelectionItem, string>(x => x.Area, 0, "Area", "", Alignment.MiddleCenter);
- columns.Add<StockSelectionItem, string>(x => x.Location, 0, "Location", "", Alignment.MiddleCenter);
- columns.Add<StockSelectionItem, string>(x => x.Style, 0, "Style", "", Alignment.MiddleCenter);
- }
- return columns;
- }
- protected override void Reload(Filters<StockSelectionItem> criteria, Columns<StockSelectionItem> columns, ref SortOrder<StockSelectionItem>? sort, Action<CoreTable?, Exception?> action)
- {
- foreach(var item in Items)
- {
- item.Issued = 0;
- item.Quantity = item.Holding.Units;
- item.MaxValue = item.Quantity;
- }
- base.Reload(criteria, columns, ref sort, action);
- }
- }
- /// <summary>
- /// Interaction logic for JobRequisitionStockSelectionPage.xaml
- /// </summary>
- public partial class StockSelectionPage : ThemableWindow, INotifyPropertyChanged
- {
- public class Holding(StockHolding holding, Guid jobRequisitionItemID)
- {
- public StockHolding StockHolding { get; set; } = holding;
- public Guid JobRequisitionItemID { get; set; } = jobRequisitionItemID;
- }
- public bool CanSave => Grid.TotalIssued > 0;
- public JobRequisitionItem Item { get; set; }
- public Guid IssuingJobID { get; set; }
- private bool Allocated = false;
- private bool _canEdit = true;
- public bool CanEdit
- {
- get => _canEdit;
- private set
- {
- _canEdit = value;
- UpdateItems();
- }
- }
- private void UpdateItems()
- {
- foreach(var item in Grid.Items)
- {
- item.Editable = _canEdit && item.Quantity.IsEffectivelyGreaterThan(0.0);
- }
- }
- public bool ShowRequisition
- {
- get => Grid.ShowRequisition;
- set => Grid.ShowRequisition = value;
- }
- public StockSelectionPage(IEnumerable<Holding> holdings, JobRequisitionItem item, Job issuingJob, bool allocated = false)
- {
- InitializeComponent();
- Item = item;
- var jris = Client.Query(
- new Filter<JobRequisitionItem>(x => x.ID).InList(holdings.Select(x => x.JobRequisitionItemID).Where(x => x != Guid.Empty).ToArray()),
- Columns.None<JobRequisitionItem>()
- .Add(x => x.ID)
- .Add(x => x.Job.JobNumber)
- .Add(x => x.Requisition.Number)
- .Add(x => x.Requisition.Description))
- .ToObjects<JobRequisitionItem>().ToDictionary(x => x.ID);
- Grid.Items = holdings.Select(x => new StockSelectionItem
- {
- JRI = jris.GetValueOrDefault(x.JobRequisitionItemID)
- ?? new(),
- Holding = x.StockHolding
- }).ToList();
- IssuingJobID = issuingJob.ID;
- jobLbl.Text = $"Taking stock from Job: {issuingJob.Name} ({issuingJob.JobNumber})";
- Allocated = allocated;
- if (allocated)
- {
- CanEdit = Security.IsAllowed<CanEditAllocatedJobRequisitions>();
- ShowRequisition = true;
- // availableUnitsLbl.Text = "Allocated";
- if (CanEdit)
- {
- // allocateLabel.Text = "Take";
- }
- }
- //UpdateItems();
- Grid.Refresh(true, true);
- }
- private void Grid_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if(e.PropertyName == nameof(Grid.TotalIssued))
- {
- OnPropertyChanged(nameof(CanSave));
- }
- }
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- if(Grid.TotalIssued <= 0)
- {
- MessageWindow.ShowMessage("Please select from at least one holding to reserve.", "Error", image: MessageWindow.WarningImage);
- return;
- }
- if (Allocated)
- {
- var result = MessageWindow.ShowYesNoCancel("You are attempting to take reserved stock that has already been allocated to job requisition items. " +
- "Are you sure you wish to proceed? (This will decrease the allocation for the job requisition item being taken from.)",
- "Confirm re-allocation");
- if(result != MessageWindowResult.Yes)
- {
- return;
- }
- }
- foreach (var item in Grid.Items.Where(x => x.Issued > 0))
- {
- CreateStockMovements(item);
- }
- DialogResult = true;
- }
- private void CreateStockMovements(StockSelectionItem item)
- {
- 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(item, batch.ID, item.JRI);
- issuing.Job.ID = IssuingJobID;
- issuing.Issued = item.Issued;
- issuing.JobRequisitionItem.ID = item.JRI?.ID ?? Guid.Empty;
- issuing.Type = StockMovementType.TransferOut;
- var receiving = CreateBaseMovement(item, batch.ID, item.JRI);
- receiving.Job.ID = Item.Job.ID;
- receiving.Received = item.Issued;
- 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(StockSelectionItem item, Guid batchid, JobRequisitionItem? fromJRI)
- {
- var mvt = item.Holding.CreateMovement();
- mvt.Batch.ID = batchid;
- mvt.Employee.ID = App.EmployeeID;
- mvt.Date = DateTime.Now;
- mvt.Product.ID = Item.Product.ID;
- if(fromJRI is null)
- {
- mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line";
- }
- else
- {
- mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line from " +
- $"requisition '{fromJRI.Requisition.Number} - {fromJRI.Requisition.Description}' for Job {fromJRI.Job.JobNumber}";
- }
- return mvt;
- }
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- private void Grid_OnAfterRefresh(object sender, AfterRefreshEventArgs args)
- {
- UpdateItems();
- }
- }
|