123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using NPOI.OpenXmlFormats.Dml;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace PRSDesktop
- {
- public delegate void HoldingsReviewRefresh();
- /// <summary>
- /// Interaction logic for JobRequisitionHoldingsReview.xaml
- /// </summary>
- public partial class JobRequisitionHoldingsReview : UserControl
- {
- private ProductStyleLink companyDefaultStyle = new ProductStyleLink();
- public ProductStyleLink CompanyDefaultStyle
- {
- get => companyDefaultStyle;
- set
- {
- companyDefaultStyle = value;
- DefaultOrNoStyleText.Text = companyDefaultStyle.Description + @" /None";
- }
- }
- public event HoldingsReviewRefresh OnHoldingsReviewRefresh;
- List<Job> jobs = new List<Job>();
- private JobRequisitionItem item;
- public JobRequisitionItem Item
- {
- get
- {
- return item;
- }
- set
- {
- item = value;
- CalculateHoldings();
- SetStyle();
- SetProductName();
- }
- }
- private void SetProductName()
- {
- productLbl.Text = Item.Product.Name + " (" + Item.Product.Code + ")";
- }
- private void SetStyle()
- {
- if (Item.Style.ID != Guid.Empty)
- styleLbl.Text = Item.Style.Description + " (" + Item.Style.Code + ")";
- else
- styleLbl.Text = "No Style Selected";
- }
- public JobRequisitionHoldingsReview()
- {
- InitializeComponent();
- CoreTable table = new Client<Job>().Query(null, new Columns<Job>(x => x.ID, x => x.JobNumber, x => x.Name));
- foreach (var row in table.Rows)
- {
- jobs.Add(row.ToObject<Job>());
- }
- listViewGreen.ItemTemplate = (DataTemplate)Resources["template"];
- listViewYellow.ItemTemplate = (DataTemplate)Resources["template"];
- listViewRed.ItemTemplate = (DataTemplate)Resources["template"];
- greenList.CollectionChanged += (s, e) =>
- {
- if (greenList.Count == 0)
- availableTab.Visibility = Visibility.Collapsed;
- else
- availableTab.Visibility = Visibility.Visible;
- };
- yellowList.CollectionChanged += (s, e) =>
- {
- if (yellowList.Count == 0)
- reservedTab.Visibility = Visibility.Collapsed;
- else
- reservedTab.Visibility = Visibility.Visible;
- };
- redList.CollectionChanged += (s, e) =>
- {
- if (redList.Count == 0)
- requisitionedTab.Visibility = Visibility.Collapsed;
- else
- requisitionedTab.Visibility = Visibility.Visible;
- };
- }
- private ObservableCollection<JobRequiHoldingsReviewModel> greenList = new ObservableCollection<JobRequiHoldingsReviewModel>();
- private ObservableCollection<JobRequiHoldingsReviewModel> yellowList = new ObservableCollection<JobRequiHoldingsReviewModel>();
- private ObservableCollection<JobRequiHoldingsReviewModel> redList = new ObservableCollection<JobRequiHoldingsReviewModel>();
- private void CalculateHoldings()
- {
- CoreTable table = new Client<StockMovement>().Query(
- new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(item.Product.ID)
- .And(x => x.Location.ID).IsNotEqualTo(Guid.Empty),
- new Columns<StockMovement>(
- x => x.Style.ID,
- x => x.Style.Description,
- x => x.Style.Code,
- x => x.Received,
- x => x.Issued,
- x => x.Location.ID,
- x => x.Location.Code,
- x => x.Location.Description,
- x => x.Job.ID,
- x => x.Job.JobNumber,
- x => x.JobRequisitionItem.ID,
- x => x.Dimensions.Unit.ID,
- x => x.Dimensions.Unit.Formula,
- x => x.Dimensions.Unit.Format,
- x => x.Dimensions.Unit.HasHeight,
- x => x.Dimensions.Unit.HasWeight,
- x => x.Dimensions.Unit.HasLength,
- x => x.Dimensions.Unit.HasWidth,
- x => x.Dimensions.Unit.HasQuantity,
- x => x.Dimensions.Quantity,
- x => x.Dimensions.Height,
- x => x.Dimensions.Weight,
- x => x.Dimensions.Width,
- x => x.Dimensions.Length,
- x => x.Dimensions.UnitSize
- ));
- List<StockMovement> stockMovements = new List<StockMovement>();
- foreach (CoreRow row in table.Rows)
- stockMovements.Add(row.ToObject<StockMovement>());
- CalculateHoldingsForJob(stockMovements);
- CalculateHoldingsForFreeStock(stockMovements);
- listViewGreen.ItemsSource = greenList;
- CalculateNonRequiHoldingsForOtherJobs(stockMovements);
- listViewYellow.ItemsSource = yellowList;
- CalculateAlreadyRequiedHoldings(stockMovements);
- listViewRed.ItemsSource = redList;
- }
- private void CalculateHoldingsForJob(List<StockMovement> mvts)
- {
- greenList.Clear();
- var currentJobHoldings = new JobRequiHoldingsReviewModel(item.Job.ID, item.Job.JobNumber, item.Job.Name, mvts, CompanyDefaultStyle);
- currentJobHoldings.GetStock(item.Job.ID, item.Style.ID);
- currentJobHoldings.Background = new SolidColorBrush(Colors.LightGreen);
- greenList.Add(currentJobHoldings);
- }
- private void CalculateHoldingsForFreeStock(List<StockMovement> mvts)
- {
- var freeStock = new JobRequiHoldingsReviewModel(Guid.Empty, "Free Stock", "Free Stock", mvts, CompanyDefaultStyle);
- freeStock.GetStock(Guid.Empty, item.Style.ID);
- freeStock.Background = new SolidColorBrush(Colors.LightGreen);
- greenList.Add(freeStock);
- }
- private void CalculateNonRequiHoldingsForOtherJobs(List<StockMovement> mvts)
- {
- yellowList.Clear();
- foreach (var job in jobs)
- {
- if (job.ID == item.Job.ID)
- continue;
- var holdings = new JobRequiHoldingsReviewModel(job.ID, job.JobNumber, job.Name, mvts, CompanyDefaultStyle);
- holdings.GetStock(job.ID, item.Style.ID);
- if (holdings.StockOfCurrentStyle == 0 && holdings.StockOfNoStyle == 0 && holdings.StockOfOtherStyles == 0)
- continue;
- holdings.Background = new SolidColorBrush(Colors.Cornsilk);
- yellowList.Add(holdings);
- }
- }
- private void CalculateAlreadyRequiedHoldings(List<StockMovement> mvts)
- {
- redList.Clear();
- foreach (var job in jobs)
- {
- var holdings = new JobRequiHoldingsReviewModel(job.ID, job.JobNumber, job.Name, mvts, CompanyDefaultStyle);
- holdings.AlreadyRequisitioned = true;
- holdings.GetStock(job.ID, item.Style.ID);
- if (holdings.StockOfCurrentStyle == 0 && holdings.StockOfNoStyle == 0 && holdings.StockOfOtherStyles == 0)
- continue;
- holdings.Background = new SolidColorBrush(Colors.LightSalmon);
- redList.Add(holdings);
- }
- }
- private void Take_Click(object sender, RoutedEventArgs e)
- {
- LaunchStockSelectionPage((sender as Button).Name, (sender as Button).DataContext as JobRequiHoldingsReviewModel);
- }
- private void LaunchStockSelectionPage(string buttonName, JobRequiHoldingsReviewModel model)
- {
- var mvts = model.Movements.Where(x => x.Job.ID == model.JobID);
- switch (buttonName)
- {
- case "currentStyle":
- if (model.StockOfCurrentStyle == 0)
- return;
- mvts = mvts
- .Where(x => x.Style.ID == Item.Style.ID);
- break;
- case "noStyle":
- if (model.StockOfNoStyle == 0)
- return;
- mvts = mvts
- .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == CompanyDefaultStyle.ID);
- break;
- case "otherStyle":
- if (model.StockOfOtherStyles == 0)
- return;
- mvts = mvts
- .Where(x => x.Style.ID != Guid.Empty && x.Style.ID != Item.Style.ID && x.Style.ID != CompanyDefaultStyle.ID);
- break;
- }
- List<Guid> locationIDs = new List<Guid>();
- List<StockHolding> holdings = new List<StockHolding>();
- List<StockMovement> movements = new List<StockMovement>();
- foreach (var mvt in mvts)
- movements.Add(mvt);
- foreach (var mvt in movements)
- {
- if (!locationIDs.Contains(mvt.Location.ID))
- {
- var holding = new StockHolding();
- holding.Location.ID = mvt.Location.ID;
- holding.Location.Description = mvt.Location.Description;
- holding.Style.ID = mvt.Style.ID;
- holding.Style.Description = mvt.Style.Description;
- holding.Style.Code = mvt.Style.Code;
- holding.Dimensions.CopyFrom(mvt.Dimensions);
- if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyRequisitioned)
- {
- holding.Units = mvt.Received - mvt.Issued;
- holdings.Add(holding);
- locationIDs.Add(mvt.Location.ID);
- }
- else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyRequisitioned)
- {
- holding.Units = mvt.Received - mvt.Issued;
- holdings.Add(holding);
- locationIDs.Add(mvt.Location.ID);
- }
- }
- else
- {
- if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyRequisitioned)
- {
- var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
- holding.Units = holding.Units + mvt.Received - mvt.Issued;
- }
- else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyRequisitioned)
- {
- var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
- holding.Units = holding.Units + mvt.Received - mvt.Issued;
- }
- }
- }
- var filteredHoldings = holdings.Where(x => x.Units > 0);
- var page = new JobRequisitionStockSelectionPage(
- filteredHoldings,
- Item,
- new Job { ID = model.JobID, Name = model.JobName, JobNumber = model.JobNumber },
- model.AlreadyRequisitioned);
- if (page.ShowDialog() == true)
- OnHoldingsReviewRefresh?.Invoke();
- }
- }
- public class JobRequiHoldingsReviewModel
- {
- public string JobNumber { get; set; }
- public string JobName { get; set; }
- public double StockOfCurrentStyle { get; set; }
- public double StockOfNoStyle { get; set; }
- public double StockOfOtherStyles { get; set; }
- public List<StockMovement> Movements { get; set; }
- public Brush Background { get; set; }
- public bool AlreadyRequisitioned { get; set; }
- public Guid JobID { get; set; }
- public ProductStyleLink DefaultStyle { get; set; }
- public JobRequiHoldingsReviewModel(Guid jobid, string jobnumber, string jobname, List<StockMovement> movements, ProductStyleLink style)
- {
- JobNumber = jobnumber;
- JobID = jobid;
- Movements = movements;
- DefaultStyle = style;
- JobName = jobname;
-
- }
- public void GetStock(Guid jobid, Guid styleid)
- {
- GetStockOfCurrentStyle(jobid, styleid);
- GetStockOfNoStyle(jobid);
- GetStockOfOtherStyles(jobid, styleid);
- }
- private void GetStockOfCurrentStyle(Guid jobid, Guid styleid)
- {
- if (styleid == Guid.Empty)
- return;
- if (!AlreadyRequisitioned)
- {
- StockOfCurrentStyle = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID == styleid)
- )
- -
- CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID == styleid)
- .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
- }
- else
- StockOfCurrentStyle = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID == styleid)
- .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
- }
- private void GetStockOfOtherStyles(Guid jobid, Guid styleid)
- {
- if (!AlreadyRequisitioned)
- StockOfOtherStyles = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID != Guid.Empty)
- .Where(x => x.Style.ID != DefaultStyle.ID)
- .Where(x => x.Style.ID != styleid)
- .Where(x => x.JobRequisitionItem.ID == Guid.Empty));
- else
- StockOfOtherStyles = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID != Guid.Empty)
- .Where(x => x.Style.ID != DefaultStyle.ID)
- .Where(x => x.Style.ID != styleid)
- .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
- }
- private void GetStockOfNoStyle(Guid jobid)
- {
- if (!AlreadyRequisitioned)
- StockOfNoStyle = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == DefaultStyle.ID)
- .Where(x => x.JobRequisitionItem.ID == Guid.Empty));
- else
- StockOfNoStyle = CalculateTotal(Movements
- .Where(x => x.Job.ID == jobid)
- .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == DefaultStyle.ID)
- .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
- }
- private double CalculateTotal(IEnumerable<StockMovement> mvts)
- {
- double total = 0;
- double rec = 0;
- double issued = 0;
- foreach (var sm in mvts)
- {
- if (sm.Received != 0)
- rec = rec + sm.Received;
- else if (sm.Issued != 0)
- issued = issued + sm.Issued;
- }
- if (rec >= issued)
- total = rec - issued;
- return total;
- }
- }
- }
|