123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.Wpf;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- 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;
- using InABox.Configuration;
- using InABox.Clients;
- namespace PRSDesktop;
- public class StockForecastOrderScreenSettings : IGlobalConfigurationSettings
- {
- public StockForecastOrderingType OrderingType { get; set; } =
- StockForecastOrderingType.StockOrder;
- public StockForecastOrderingStrategy OrderingStrategy { get; set; } =
- StockForecastOrderingStrategy.LowestOverallPrice;
- }
- /// <summary>
- /// Interaction logic for StockForecastOrderScreen.xaml
- /// </summary>
- public partial class StockForecastOrderScreen : Window, INotifyPropertyChanged
- {
- private StockForecastOrderScreenSettings settings;
-
- private bool _canSave;
- public bool CanSave
- {
- get => _canSave;
- set
- {
- _canSave = true;
- OnPropertyChanged();
- }
- }
- public StockForecastOrderingType OrderType
- {
- get => Grid.OrderType;
- set => Grid.OrderType = value;
- }
- public StockForecastOrderingStrategy Strategy
- {
- get => Grid.OrderStrategy;
- set => Grid.OrderStrategy = value;
- }
- public IEnumerable<StockForecastOrderingResult> Results => Grid.Results;
- public StockForecastOrderScreen(List<StockForecastOrderData> items, bool allowstockOrder = true)
- {
- settings = new GlobalConfiguration<StockForecastOrderScreenSettings>().Load();
-
- InitializeComponent();
- if (allowstockOrder)
- {
- OrderType = settings.OrderingType;
- OrderTypeBox.ItemsSource = Enum.GetValues<StockForecastOrderingType>()
- .Select(x => new KeyValuePair<StockForecastOrderingType, string>(x, CoreUtils.Neatify(x.ToString())));
- OrderTypeBox.SelectedValuePath = "Key";
- OrderTypeBox.DisplayMemberPath = "Value";
- OrderTypeBox.VerticalContentAlignment = VerticalAlignment.Center;
- OrderTypeBox.Bind(ComboBox.SelectedValueProperty, this, x => x.OrderType);
- }
- else
- {
- OrderType = StockForecastOrderingType.Breakup;
- OrderTypeBox.Visibility = System.Windows.Visibility.Collapsed;
- OrderTypeLabel.Visibility = System.Windows.Visibility.Collapsed;
- }
- Strategy = settings.OrderingStrategy;
- OrderStrategyBox.ItemsSource = Enum.GetValues<StockForecastOrderingStrategy>()
- .Select(x => new KeyValuePair<StockForecastOrderingStrategy, string>(x, CoreUtils.Neatify(x.ToString())));
- OrderStrategyBox.SelectedValuePath = "Key";
- OrderStrategyBox.DisplayMemberPath = "Value";
- OrderStrategyBox.VerticalContentAlignment = VerticalAlignment.Center;
- OrderStrategyBox.Bind(ComboBox.SelectedValueProperty, this, x => x.Strategy);
- Grid.OrderData = items;
- Grid.Refresh(true, true);
- }
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- Close();
- }
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = true;
- Close();
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- private void Grid_OnChanged(object sender, EventArgs e)
- {
- CanSave = Grid.TotalQuantity > 0;
- }
- private void OrderTypeBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (Grid.OrderData == null)
- return;
- settings.OrderingType = (StockForecastOrderingType)OrderTypeBox.SelectedValue;
- new GlobalConfiguration<StockForecastOrderScreenSettings>().Save(settings);
- }
- private void OrderStrategyBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (Grid.OrderData == null)
- return;
- settings.OrderingStrategy = (StockForecastOrderingStrategy)OrderStrategyBox.SelectedValue;
- new GlobalConfiguration<StockForecastOrderScreenSettings>().Save(settings);
- }
- public void CreateOrders(string screenName)
- {
- var orders = new List<Tuple<PurchaseOrder, Dictionary<StockForecastOrderingResult,PurchaseOrderItem>>>();
- Progress.ShowModal("Creating Orders", progress =>
- {
- int iOrder = 1;
- foreach(var perSupplier in Results.GroupBy(x => x.Supplier.ID))
- {
- progress.Report($"Creating Orders ({iOrder++}/{Results.ToArray().Length})");
- var order = new PurchaseOrder();
- order.Description = $"Purchase Order created from {screenName} Screen";
- order.RaisedBy.ID = App.EmployeeID;
- LookupFactory.DoLookup<PurchaseOrder, Supplier, SupplierLink>(order, x => x.SupplierLink, perSupplier.Key);
- var orderItems = new Dictionary<StockForecastOrderingResult,PurchaseOrderItem>();
- var results = perSupplier.ToArray();
- foreach(var item in results)
- {
- var orderItem = new PurchaseOrderItem();
- orderItem.Product.ID = item.Item.Product.ID;
- orderItem.Style.ID = item.Item.Style.ID;
- orderItem.Job.ID = item.SupplierProduct.Job.ID;
- orderItems[item] = orderItem;
- }
- LookupFactory.DoLookups<PurchaseOrderItem, Job, JobLink>(
- orderItems.Values.Select(x => new Tuple<PurchaseOrderItem, Guid>(x, x.Job.ID)),
- x => x.Job);
- LookupFactory.DoLookups<PurchaseOrderItem, Product, ProductLink>(
- orderItems.Values.Select(x => new Tuple<PurchaseOrderItem, Guid>(x, x.Product.ID)),
- x => x.Product);
- LookupFactory.DoLookups<PurchaseOrderItem, ProductStyle, ProductStyleLink>(
- orderItems.Values.Select(x => new Tuple<PurchaseOrderItem, Guid>(x, x.Style.ID)),
- x => x.Style);
- LookupFactory.DoLookups<PurchaseOrderItem, TaxCode, TaxCodeLink>(
- orderItems.Values.Select(x => new Tuple<PurchaseOrderItem, Guid>(x, x.TaxCode.ID)),
- x => x.TaxCode);
- foreach(var item in results)
- {
- var orderItem = orderItems[item];
- orderItem.Dimensions.CopyFrom(item.SupplierProduct.Dimensions);
- orderItem.Qty = item.Quantity;
- orderItem.ForeignCurrencyCost = item.SupplierProduct.ForeignCurrencyPrice;
- orderItem.Cost = item.SupplierProduct.CostPrice;
- }
- orders.Add(new(order, orderItems));
- }
-
- progress.Report($"Saving {orders.Count} Orders");
- Client.Save(orders.Select(x => x.Item1), $"Created from {screenName} screen");
- foreach(var (order, orderItems) in orders)
- {
- foreach(var pair in orderItems)
- pair.Value.PurchaseOrderLink.ID = order.ID;
- }
- Client.Save(orders.SelectMany(x => x.Item2.Values), $"Created from {screenName} screen");
- var allocations = new List<PurchaseOrderItemAllocation>();
- progress.Report($"Processing Breakups");
- foreach(var (order, orderItems) in orders)
- {
- foreach(var (result, poi) in orderItems)
- {
- foreach (var breakup in result.Breakups)
- {
- // Skip the nominated job.
- if (breakup.JobID == poi.Job.ID && breakup.JobRequiItemID == Guid.Empty) continue;
- var alloc = new PurchaseOrderItemAllocation();
- alloc.Item.ID = poi.ID;
- alloc.Job.ID = breakup.JobID;
- alloc.JobRequisitionItem.ID = breakup.JobRequiItemID;
- alloc.Quantity = breakup.Quantity;
- allocations.Add(alloc);
- }
- }
- }
- if (allocations.Count != 0)
- Client.Save(allocations, $"Created from {screenName} Screen");
- });
- MessageWindow.ShowMessage($"The following orders were created:\n- {string.Join("\n- ",orders.Select(x=>x.Item1.PONumber))}", $"Created {orders.Count} orders");
- }
- }
|