JobRequisitionHoldingsReview.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using NPOI.OpenXmlFormats.Dml;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Collections.Specialized;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. namespace PRSDesktop
  15. {
  16. public delegate void HoldingsReviewRefresh();
  17. /// <summary>
  18. /// Interaction logic for JobRequisitionHoldingsReview.xaml
  19. /// </summary>
  20. public partial class JobRequisitionHoldingsReview : UserControl
  21. {
  22. private ProductStyleLink companyDefaultStyle = new ProductStyleLink();
  23. public ProductStyleLink CompanyDefaultStyle
  24. {
  25. get => companyDefaultStyle;
  26. set
  27. {
  28. companyDefaultStyle = value;
  29. DefaultOrNoStyleText.Text = companyDefaultStyle.Description + @" /None";
  30. }
  31. }
  32. public event HoldingsReviewRefresh OnHoldingsReviewRefresh;
  33. List<Job> jobs = new List<Job>();
  34. private JobRequisitionItem item;
  35. public JobRequisitionItem Item
  36. {
  37. get
  38. {
  39. return item;
  40. }
  41. set
  42. {
  43. item = value;
  44. CalculateHoldings();
  45. SetStyle();
  46. SetProductName();
  47. }
  48. }
  49. private void SetProductName()
  50. {
  51. productLbl.Text = Item.Product.Name + " (" + Item.Product.Code + ")";
  52. }
  53. private void SetStyle()
  54. {
  55. if (Item.Style.ID != Guid.Empty)
  56. styleLbl.Text = Item.Style.Description + " (" + Item.Style.Code + ")";
  57. else
  58. styleLbl.Text = "No Style Selected";
  59. }
  60. public JobRequisitionHoldingsReview()
  61. {
  62. InitializeComponent();
  63. CoreTable table = new Client<Job>().Query(null, new Columns<Job>(x => x.ID, x => x.JobNumber, x => x.Name));
  64. foreach (var row in table.Rows)
  65. {
  66. jobs.Add(row.ToObject<Job>());
  67. }
  68. listViewGreen.ItemTemplate = (DataTemplate)Resources["template"];
  69. listViewYellow.ItemTemplate = (DataTemplate)Resources["template"];
  70. listViewRed.ItemTemplate = (DataTemplate)Resources["template"];
  71. greenList.CollectionChanged += (s, e) =>
  72. {
  73. if (greenList.Count == 0)
  74. availableTab.Visibility = Visibility.Collapsed;
  75. else
  76. availableTab.Visibility = Visibility.Visible;
  77. };
  78. yellowList.CollectionChanged += (s, e) =>
  79. {
  80. if (yellowList.Count == 0)
  81. reservedTab.Visibility = Visibility.Collapsed;
  82. else
  83. reservedTab.Visibility = Visibility.Visible;
  84. };
  85. redList.CollectionChanged += (s, e) =>
  86. {
  87. if (redList.Count == 0)
  88. requisitionedTab.Visibility = Visibility.Collapsed;
  89. else
  90. requisitionedTab.Visibility = Visibility.Visible;
  91. };
  92. }
  93. private ObservableCollection<JobRequiHoldingsReviewModel> greenList = new ObservableCollection<JobRequiHoldingsReviewModel>();
  94. private ObservableCollection<JobRequiHoldingsReviewModel> yellowList = new ObservableCollection<JobRequiHoldingsReviewModel>();
  95. private ObservableCollection<JobRequiHoldingsReviewModel> redList = new ObservableCollection<JobRequiHoldingsReviewModel>();
  96. private void CalculateHoldings()
  97. {
  98. CoreTable table = new Client<StockMovement>().Query(
  99. new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(item.Product.ID)
  100. .And(x => x.Location.ID).IsNotEqualTo(Guid.Empty),
  101. new Columns<StockMovement>(
  102. x => x.Style.ID,
  103. x => x.Style.Description,
  104. x => x.Style.Code,
  105. x => x.Received,
  106. x => x.Issued,
  107. x => x.Location.ID,
  108. x => x.Location.Code,
  109. x => x.Location.Description,
  110. x => x.Job.ID,
  111. x => x.Job.JobNumber,
  112. x => x.JobRequisitionItem.ID,
  113. x => x.Dimensions.Unit.ID,
  114. x => x.Dimensions.Unit.Formula,
  115. x => x.Dimensions.Unit.Format,
  116. x => x.Dimensions.Unit.HasHeight,
  117. x => x.Dimensions.Unit.HasWeight,
  118. x => x.Dimensions.Unit.HasLength,
  119. x => x.Dimensions.Unit.HasWidth,
  120. x => x.Dimensions.Unit.HasQuantity,
  121. x => x.Dimensions.Quantity,
  122. x => x.Dimensions.Height,
  123. x => x.Dimensions.Weight,
  124. x => x.Dimensions.Width,
  125. x => x.Dimensions.Length,
  126. x => x.Dimensions.UnitSize
  127. ));
  128. List<StockMovement> stockMovements = new List<StockMovement>();
  129. foreach (CoreRow row in table.Rows)
  130. stockMovements.Add(row.ToObject<StockMovement>());
  131. CalculateHoldingsForJob(stockMovements);
  132. CalculateHoldingsForFreeStock(stockMovements);
  133. listViewGreen.ItemsSource = greenList;
  134. CalculateNonRequiHoldingsForOtherJobs(stockMovements);
  135. listViewYellow.ItemsSource = yellowList;
  136. CalculateAlreadyRequiedHoldings(stockMovements);
  137. listViewRed.ItemsSource = redList;
  138. }
  139. private void CalculateHoldingsForJob(List<StockMovement> mvts)
  140. {
  141. greenList.Clear();
  142. var currentJobHoldings = new JobRequiHoldingsReviewModel(item.Job.ID, item.Job.JobNumber, item.Job.Name, mvts, CompanyDefaultStyle);
  143. currentJobHoldings.GetStock(item.Job.ID, item.Style.ID);
  144. currentJobHoldings.Background = new SolidColorBrush(Colors.LightGreen);
  145. greenList.Add(currentJobHoldings);
  146. }
  147. private void CalculateHoldingsForFreeStock(List<StockMovement> mvts)
  148. {
  149. var freeStock = new JobRequiHoldingsReviewModel(Guid.Empty, "Free Stock", "Free Stock", mvts, CompanyDefaultStyle);
  150. freeStock.GetStock(Guid.Empty, item.Style.ID);
  151. freeStock.Background = new SolidColorBrush(Colors.LightGreen);
  152. greenList.Add(freeStock);
  153. }
  154. private void CalculateNonRequiHoldingsForOtherJobs(List<StockMovement> mvts)
  155. {
  156. yellowList.Clear();
  157. foreach (var job in jobs)
  158. {
  159. if (job.ID == item.Job.ID)
  160. continue;
  161. var holdings = new JobRequiHoldingsReviewModel(job.ID, job.JobNumber, job.Name, mvts, CompanyDefaultStyle);
  162. holdings.GetStock(job.ID, item.Style.ID);
  163. if (holdings.StockOfCurrentStyle == 0 && holdings.StockOfNoStyle == 0 && holdings.StockOfOtherStyles == 0)
  164. continue;
  165. holdings.Background = new SolidColorBrush(Colors.Cornsilk);
  166. yellowList.Add(holdings);
  167. }
  168. }
  169. private void CalculateAlreadyRequiedHoldings(List<StockMovement> mvts)
  170. {
  171. redList.Clear();
  172. foreach (var job in jobs)
  173. {
  174. var holdings = new JobRequiHoldingsReviewModel(job.ID, job.JobNumber, job.Name, mvts, CompanyDefaultStyle);
  175. holdings.AlreadyRequisitioned = true;
  176. holdings.GetStock(job.ID, item.Style.ID);
  177. if (holdings.StockOfCurrentStyle == 0 && holdings.StockOfNoStyle == 0 && holdings.StockOfOtherStyles == 0)
  178. continue;
  179. holdings.Background = new SolidColorBrush(Colors.LightSalmon);
  180. redList.Add(holdings);
  181. }
  182. }
  183. private void Take_Click(object sender, RoutedEventArgs e)
  184. {
  185. LaunchStockSelectionPage((sender as Button).Name, (sender as Button).DataContext as JobRequiHoldingsReviewModel);
  186. }
  187. private void LaunchStockSelectionPage(string buttonName, JobRequiHoldingsReviewModel model)
  188. {
  189. var mvts = model.Movements.Where(x => x.Job.ID == model.JobID);
  190. switch (buttonName)
  191. {
  192. case "currentStyle":
  193. if (model.StockOfCurrentStyle == 0)
  194. return;
  195. mvts = mvts
  196. .Where(x => x.Style.ID == Item.Style.ID);
  197. break;
  198. case "noStyle":
  199. if (model.StockOfNoStyle == 0)
  200. return;
  201. mvts = mvts
  202. .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == CompanyDefaultStyle.ID);
  203. break;
  204. case "otherStyle":
  205. if (model.StockOfOtherStyles == 0)
  206. return;
  207. mvts = mvts
  208. .Where(x => x.Style.ID != Guid.Empty && x.Style.ID != Item.Style.ID && x.Style.ID != CompanyDefaultStyle.ID);
  209. break;
  210. }
  211. List<Guid> locationIDs = new List<Guid>();
  212. List<StockHolding> holdings = new List<StockHolding>();
  213. List<StockMovement> movements = new List<StockMovement>();
  214. foreach (var mvt in mvts)
  215. movements.Add(mvt);
  216. foreach (var mvt in movements)
  217. {
  218. if (!locationIDs.Contains(mvt.Location.ID))
  219. {
  220. var holding = new StockHolding();
  221. holding.Location.ID = mvt.Location.ID;
  222. holding.Location.Description = mvt.Location.Description;
  223. holding.Style.ID = mvt.Style.ID;
  224. holding.Style.Description = mvt.Style.Description;
  225. holding.Style.Code = mvt.Style.Code;
  226. holding.Dimensions.CopyFrom(mvt.Dimensions);
  227. if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyRequisitioned)
  228. {
  229. holding.Units = mvt.Received - mvt.Issued;
  230. holdings.Add(holding);
  231. locationIDs.Add(mvt.Location.ID);
  232. }
  233. else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyRequisitioned)
  234. {
  235. holding.Units = mvt.Received - mvt.Issued;
  236. holdings.Add(holding);
  237. locationIDs.Add(mvt.Location.ID);
  238. }
  239. }
  240. else
  241. {
  242. if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyRequisitioned)
  243. {
  244. var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
  245. holding.Units = holding.Units + mvt.Received - mvt.Issued;
  246. }
  247. else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyRequisitioned)
  248. {
  249. var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
  250. holding.Units = holding.Units + mvt.Received - mvt.Issued;
  251. }
  252. }
  253. }
  254. var filteredHoldings = holdings.Where(x => x.Units > 0);
  255. var page = new JobRequisitionStockSelectionPage(
  256. filteredHoldings,
  257. Item,
  258. new Job { ID = model.JobID, Name = model.JobName, JobNumber = model.JobNumber },
  259. model.AlreadyRequisitioned);
  260. if (page.ShowDialog() == true)
  261. OnHoldingsReviewRefresh?.Invoke();
  262. }
  263. }
  264. public class JobRequiHoldingsReviewModel
  265. {
  266. public string JobNumber { get; set; }
  267. public string JobName { get; set; }
  268. public double StockOfCurrentStyle { get; set; }
  269. public double StockOfNoStyle { get; set; }
  270. public double StockOfOtherStyles { get; set; }
  271. public List<StockMovement> Movements { get; set; }
  272. public Brush Background { get; set; }
  273. public bool AlreadyRequisitioned { get; set; }
  274. public Guid JobID { get; set; }
  275. public ProductStyleLink DefaultStyle { get; set; }
  276. public JobRequiHoldingsReviewModel(Guid jobid, string jobnumber, string jobname, List<StockMovement> movements, ProductStyleLink style)
  277. {
  278. JobNumber = jobnumber;
  279. JobID = jobid;
  280. Movements = movements;
  281. DefaultStyle = style;
  282. JobName = jobname;
  283. }
  284. public void GetStock(Guid jobid, Guid styleid)
  285. {
  286. GetStockOfCurrentStyle(jobid, styleid);
  287. GetStockOfNoStyle(jobid);
  288. GetStockOfOtherStyles(jobid, styleid);
  289. }
  290. private void GetStockOfCurrentStyle(Guid jobid, Guid styleid)
  291. {
  292. if (styleid == Guid.Empty)
  293. return;
  294. if (!AlreadyRequisitioned)
  295. {
  296. StockOfCurrentStyle = CalculateTotal(Movements
  297. .Where(x => x.Job.ID == jobid)
  298. .Where(x => x.Style.ID == styleid)
  299. )
  300. -
  301. CalculateTotal(Movements
  302. .Where(x => x.Job.ID == jobid)
  303. .Where(x => x.Style.ID == styleid)
  304. .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
  305. }
  306. else
  307. StockOfCurrentStyle = CalculateTotal(Movements
  308. .Where(x => x.Job.ID == jobid)
  309. .Where(x => x.Style.ID == styleid)
  310. .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
  311. }
  312. private void GetStockOfOtherStyles(Guid jobid, Guid styleid)
  313. {
  314. if (!AlreadyRequisitioned)
  315. StockOfOtherStyles = CalculateTotal(Movements
  316. .Where(x => x.Job.ID == jobid)
  317. .Where(x => x.Style.ID != Guid.Empty)
  318. .Where(x => x.Style.ID != DefaultStyle.ID)
  319. .Where(x => x.Style.ID != styleid)
  320. .Where(x => x.JobRequisitionItem.ID == Guid.Empty));
  321. else
  322. StockOfOtherStyles = CalculateTotal(Movements
  323. .Where(x => x.Job.ID == jobid)
  324. .Where(x => x.Style.ID != Guid.Empty)
  325. .Where(x => x.Style.ID != DefaultStyle.ID)
  326. .Where(x => x.Style.ID != styleid)
  327. .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
  328. }
  329. private void GetStockOfNoStyle(Guid jobid)
  330. {
  331. if (!AlreadyRequisitioned)
  332. StockOfNoStyle = CalculateTotal(Movements
  333. .Where(x => x.Job.ID == jobid)
  334. .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == DefaultStyle.ID)
  335. .Where(x => x.JobRequisitionItem.ID == Guid.Empty));
  336. else
  337. StockOfNoStyle = CalculateTotal(Movements
  338. .Where(x => x.Job.ID == jobid)
  339. .Where(x => x.Style.ID == Guid.Empty || x.Style.ID == DefaultStyle.ID)
  340. .Where(x => x.JobRequisitionItem.ID != Guid.Empty));
  341. }
  342. private double CalculateTotal(IEnumerable<StockMovement> mvts)
  343. {
  344. double total = 0;
  345. double rec = 0;
  346. double issued = 0;
  347. foreach (var sm in mvts)
  348. {
  349. if (sm.Received != 0)
  350. rec = rec + sm.Received;
  351. else if (sm.Issued != 0)
  352. issued = issued + sm.Issued;
  353. }
  354. if (rec >= issued)
  355. total = rec - issued;
  356. return total;
  357. }
  358. }
  359. }