StoreRequiScannerPage.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Xamarin.Essentials;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class StoreRequiScannerPage
  17. {
  18. #region Fields / Constructor
  19. private StockHoldingModel _holdings = new StockHoldingModel(
  20. App.Data,
  21. () => new Filter<StockHolding>(x => x.Units).IsNotEqualTo(0.0F)
  22. );
  23. private Task _holdingsTask = null;
  24. List<StoreRequiItemShell> shells = new List<StoreRequiItemShell>();
  25. List<StoreRequiItemShell> oldShells = new List<StoreRequiItemShell>();
  26. Requisition requisition = new Requisition();
  27. Dictionary<StoreRequiItemShell, string> itemRowScannerRawResultPairs = new Dictionary<StoreRequiItemShell, string>();
  28. Dictionary<StoreRequiItemShell, string> itemRowScannerProcessedResultPairs = new Dictionary<StoreRequiItemShell, string>();
  29. private bool loading = true;
  30. bool containsNotes = false;
  31. public StoreRequiScannerPage(Guid requiID)
  32. {
  33. _holdingsTask = Task.Run(() => _holdings.Refresh(false));
  34. InitializeComponent();
  35. ConfigAll(requiID);
  36. }
  37. #endregion
  38. #region Config
  39. void ConfigAll(Guid requiID)
  40. {
  41. _scanView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions()
  42. {
  43. PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
  44. AutoRotate = false,
  45. TryInverted = true,
  46. TryHarder = true,
  47. };
  48. ConfigDisplay();
  49. if (requiID != Guid.Empty)
  50. {
  51. requisition.ID = requiID;
  52. LoadExistingRequi();
  53. }
  54. }
  55. protected override void OnAppearing()
  56. {
  57. base.OnAppearing();
  58. _scanView.IsAnalyzing = true;
  59. _scanView.IsScanning = true;
  60. _scanView.AutoFocus(); }
  61. protected override void OnDisappearing()
  62. {
  63. _scanView.IsScanning = false;
  64. _scanView.IsAnalyzing = false;
  65. base.OnDisappearing();
  66. }
  67. async void LoadExistingRequi()
  68. {
  69. await Task.Run(() =>
  70. {
  71. CoreTable table = QueryRequi();
  72. while(table == null)
  73. table = QueryRequi();
  74. requisition = table.Rows.FirstOrDefault().ToObject<Requisition>();
  75. string notes = CheckNotes(requisition.Notes);
  76. if (!string.IsNullOrWhiteSpace(requisition.Request) || !string.IsNullOrWhiteSpace(notes))
  77. {
  78. StoreRequiItemShell shell1 = new StoreRequiItemShell()
  79. {
  80. IsNotes = true,
  81. IsNotNotes = false,
  82. BorderColor = Color.FromHex("#9f4576")
  83. };
  84. shell1.Summary = !string.IsNullOrWhiteSpace(requisition.Request) ? "REQUEST: " + requisition.Request + System.Environment.NewLine : "";
  85. shell1.Summary = shell1.Summary + notes;
  86. shells.Insert(0, shell1);
  87. containsNotes = true;
  88. }
  89. });
  90. await Task.Run(() =>
  91. {
  92. CoreTable table = QueryRequiItems();
  93. while(table == null)
  94. table = QueryRequiItems();
  95. if (table.Rows.Any())
  96. {
  97. Device.BeginInvokeOnMainThread(() => { saveBtn.IsVisible = true; });
  98. foreach (CoreRow row in table.Rows)
  99. {
  100. StoreRequiItemShell shell = new StoreRequiItemShell()
  101. {
  102. ID = row.Get<RequisitionItem, Guid>(x => x.ID),
  103. ProductID = row.Get<RequisitionItem, Guid>(x => x.Product.ID),
  104. ProductName = row.Get<RequisitionItem, string>(x => x.Product.Name),
  105. ProductCode = row.Get<RequisitionItem, string>(x => x.Product.Code),
  106. Quantity = row.Get<RequisitionItem, double>(x => x.Quantity),
  107. LocationID = row.Get<RequisitionItem, Guid>(x => x.Location.ID),
  108. LocationName = row.Get<RequisitionItem, string>(x => x.Location.Description),
  109. Picked = row.Get<RequisitionItem, DateTime>(x => x.Picked),
  110. };
  111. shells.Add(shell);
  112. oldShells.Add(shell);
  113. }
  114. }
  115. Device.BeginInvokeOnMainThread(() =>
  116. {
  117. requiItemListView.ItemsSource = shells;
  118. if (containsNotes)
  119. {
  120. countLbl.Text = "Number of items: " + (shells.Count - 1);
  121. }
  122. else
  123. {
  124. countLbl.Text = "Number of items: " + shells.Count;
  125. }
  126. });
  127. });
  128. }
  129. private CoreTable QueryRequi()
  130. {
  131. try
  132. {
  133. return new Client<Requisition>().Query(
  134. new Filter<Requisition>(x => x.ID).IsEqualTo(requisition.ID)
  135. );
  136. }
  137. catch (Exception ex)
  138. {
  139. InABox.Mobile.MobileLogging.Log(ex);
  140. return null;
  141. }
  142. }
  143. private CoreTable QueryRequiItems()
  144. {
  145. try
  146. {
  147. return new Client<RequisitionItem>().Query
  148. (
  149. new Filter<RequisitionItem>(x => x.RequisitionLink.ID).IsEqualTo(requisition.ID),
  150. new Columns<RequisitionItem>(ColumnTypeFlags.None).Add(
  151. x => x.ID,
  152. x => x.Product.ID,
  153. x => x.Product.Name,
  154. x => x.Product.Code,
  155. x => x.Quantity,
  156. x => x.Location.ID,
  157. x => x.Location.Description,
  158. x => x.Picked
  159. )
  160. );
  161. }
  162. catch (Exception ex)
  163. {
  164. InABox.Mobile.MobileLogging.Log(ex);
  165. return null;
  166. }
  167. }
  168. private string CheckNotes(string[] notes)
  169. {
  170. string combinednotes = "----------" + System.Environment.NewLine + "NOTES: " + System.Environment.NewLine;
  171. if (notes.Count() > 0)
  172. {
  173. foreach (var note in notes)
  174. {
  175. combinednotes = combinednotes + note + System.Environment.NewLine;
  176. }
  177. }
  178. return combinednotes;
  179. }
  180. void ConfigDisplay()
  181. {
  182. scannerGrid.RaiseChild(topleft);
  183. scannerGrid.RaiseChild(topright);
  184. scannerGrid.RaiseChild(bottomright);
  185. scannerGrid.RaiseChild(bottomleft);
  186. }
  187. #endregion
  188. #region Scanning
  189. private async void ScanView_OnScanResult(ZXing.Result result)
  190. {
  191. if (!loading)
  192. {
  193. loading = true;
  194. Vibration.Vibrate();
  195. try
  196. {
  197. _holdingsTask.Wait();
  198. if (!itemRowScannerRawResultPairs.Values.Contains(result.Text))
  199. {
  200. if (!itemRowScannerProcessedResultPairs.Values.Contains(result.Text))
  201. {
  202. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding"))
  203. {
  204. string rawResult = result.Text;
  205. Tuple<string, double> tuple = ProcessResult(result.Text);
  206. var status = await LoadProduct(tuple, rawResult);
  207. if (status == LoadProductStatus.InvalidCode)
  208. await DisplayAlert("Error", "Invalid Product Code", "OK");
  209. else if (status == LoadProductStatus.NoHoldings)
  210. await DisplayAlert("Error", "No Holdings Found for Product", "OK");
  211. }
  212. }
  213. }
  214. }
  215. catch (Exception e)
  216. {
  217. MobileLogging.Log(e, "StoreRequiScanner");
  218. }
  219. loading = false;
  220. }
  221. }
  222. private Tuple<string, double> ProcessResult(string result)
  223. {
  224. double qty = 1;
  225. if (result.Contains("*"))
  226. {
  227. try
  228. {
  229. int i = result.IndexOf("*");
  230. string remainder = result.Substring(i);
  231. result = result.Remove(i);
  232. string s1 = remainder.Substring(1);
  233. qty = Convert.ToDouble(s1);
  234. }
  235. catch
  236. {
  237. loading = false;
  238. }
  239. }
  240. Tuple<string, double> tuple = new Tuple<string, double>(result, qty);
  241. return tuple;
  242. }
  243. private enum LoadProductStatus
  244. {
  245. Success,
  246. InvalidCode,
  247. NoHoldings
  248. }
  249. private async Task<LoadProductStatus> LoadProduct(Tuple<string, double> processedResultQtyTuple, string rawResult)
  250. {
  251. ProductShell? product = App.Data.Products.FirstOrDefault(x => x.Code.Equals(processedResultQtyTuple.Item1));
  252. if (product == null)
  253. return LoadProductStatus.InvalidCode;
  254. var holdings = _holdings.Where(x => x.ProductID == product.ID).ToArray();
  255. if (holdings.Length == 0)
  256. return LoadProductStatus.NoHoldings;
  257. else if (holdings.Length == 1)
  258. AddStoreRequiItemShell(holdings.First(), product, rawResult, processedResultQtyTuple);
  259. else
  260. UserSelectFromList(holdings, product, rawResult, processedResultQtyTuple);
  261. return LoadProductStatus.Success;
  262. }
  263. private void UserSelectFromList(StockHoldingShell[] list, ProductShell product, string rawResult, Tuple<string, double> processedResultQtyTuple)
  264. {
  265. StockHoldingSelectionPage page = new StockHoldingSelectionPage(_holdings, (shell) =>
  266. {
  267. AddStoreRequiItemShell(shell, product, rawResult, processedResultQtyTuple);
  268. });
  269. Navigation.PushAsync(page);
  270. }
  271. private void AddStoreRequiItemShell(StockHoldingShell holding, ProductShell product, string rawResult, Tuple<string, double> processedResultQtyTuple)
  272. {
  273. StoreRequiItemShell shell = new StoreRequiItemShell
  274. {
  275. ProductID = product.ID,
  276. ProductName = product.Name,
  277. ProductCode = product.Code,
  278. Quantity = 1,
  279. LocationID = holding.LocationID,
  280. LocationName = holding.LocationDescription,
  281. StyleID = holding.StyleID,
  282. JobID = holding.StyleID
  283. };
  284. shell.Dimensions.Unit.ID = holding.DimensionsUnitID;
  285. shell.Dimensions.Unit.HasQuantity = holding.DimensionsHasQuantity;
  286. shell.Dimensions.Unit.HasLength = holding.DimensionsHasLength;
  287. shell.Dimensions.Unit.HasHeight = holding.DimensionsHasHeight;
  288. shell.Dimensions.Unit.HasWeight = holding.DimensionsHasWeight;
  289. shell.Dimensions.Unit.HasWidth = holding.DimensionsHasWidth;
  290. shell.Dimensions.Quantity = holding.DimensionsQuantity;
  291. shell.Dimensions.Length = holding.DimensionsLength;
  292. shell.Dimensions.Height = holding.DimensionsHeight;
  293. shell.Dimensions.Weight = holding.DimensionsWeight;
  294. shell.Dimensions.Width = holding.DimensionsWidth;
  295. shell.Dimensions.Unit.Format = holding.DimensionsUnitFormat;
  296. shell.Dimensions.Unit.Formula = holding.DimensionsUnitFormula;
  297. shell.Dimensions.UnitSize = holding.DimensionsUnitSize;
  298. shells.Add(shell);
  299. itemRowScannerRawResultPairs.Add(shell, rawResult);
  300. itemRowScannerProcessedResultPairs.Add(shell, processedResultQtyTuple.Item1);
  301. requiItemListView.ItemsSource = null;
  302. requiItemListView.ItemsSource = shells;
  303. countLbl.IsVisible = true;
  304. if (containsNotes)
  305. {
  306. countLbl.Text = "Number of items: " + (shells.Count - 1);
  307. }
  308. else
  309. {
  310. countLbl.Text = "Number of items: " + shells.Count;
  311. }
  312. saveBtn.IsVisible = true;
  313. loading = false;
  314. }
  315. #endregion
  316. #region Button Presses
  317. private async void ExitBtn_Clicked(object sender, EventArgs e)
  318. {
  319. if (shells.Count > 0)
  320. {
  321. if (containsNotes && shells.Count > 1)
  322. {
  323. string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
  324. switch (chosenOption)
  325. {
  326. case "Cancel":
  327. return;
  328. case "Yes":
  329. Navigation.PopAsync();
  330. break;
  331. case "No":
  332. return;
  333. default:
  334. return;
  335. }
  336. }
  337. else if (containsNotes && shells.Count == 1)
  338. {
  339. Navigation.PopAsync();
  340. }
  341. else
  342. {
  343. string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
  344. switch (chosenOption)
  345. {
  346. case "Cancel":
  347. return;
  348. case "Yes":
  349. Navigation.PopAsync();
  350. break;
  351. case "No":
  352. return;
  353. default:
  354. return;
  355. }
  356. }
  357. }
  358. else
  359. Navigation.PopAsync();
  360. }
  361. void SaveBtn_Clicked(object sender, EventArgs e)
  362. {
  363. StoreRequiConfirmationPage page = new StoreRequiConfirmationPage(requisition, shells, oldShells);
  364. page.OnSaveSelected += () => { Navigation.PopAsync(); };
  365. Navigation.PushAsync(page);
  366. }
  367. private async void RequiItem_Tapped(object sender, EventArgs e)
  368. {
  369. var shell = requiItemListView.SelectedItem as StoreRequiItemShell;
  370. if (shell == null) return;
  371. await RequiItemTappedAsync(shell);
  372. }
  373. private async Task RequiItemTappedAsync(StoreRequiItemShell shell)
  374. {
  375. string pickstatus = shell.Picked == DateTime.MinValue ? "not picked" : "picked (" + shell.Picked.ToString("dd MMM yy") + ")";
  376. string options = shell.Picked == DateTime.MinValue ? ". Mark as Picked?" : ". Remove Picked status?";
  377. string chosenOption = await DisplayActionSheet("Line is " + pickstatus + options, "Cancel", null, "Yes", "No");
  378. if (chosenOption != "Yes")
  379. return;
  380. shell.Picked = shell.Picked == DateTime.MinValue ? DateTime.Today : DateTime.MinValue;
  381. shell.Colour = shell.Picked == DateTime.MinValue ? Color.Default : Color.FromHex("#8fbc8f");
  382. requiItemListView.ItemsSource = null;
  383. requiItemListView.ItemsSource = shells;
  384. }
  385. void ReduceQtyBtn_Clicked(object sender, EventArgs e)
  386. {
  387. var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
  388. if (shell == null) return;
  389. if (shell.Quantity <= 1)
  390. {
  391. shells.Remove(shell);
  392. itemRowScannerRawResultPairs.Remove(shell);
  393. itemRowScannerProcessedResultPairs.Remove(shell);
  394. if (containsNotes)
  395. {
  396. countLbl.Text = "Number of items: " + (shells.Count - 1);
  397. if (shells.Count == 1)
  398. {
  399. saveBtn.IsVisible = false;
  400. }
  401. }
  402. else
  403. {
  404. countLbl.Text = "Number of items: " + shells.Count;
  405. if (shells.Count == 0)
  406. {
  407. saveBtn.IsVisible = false;
  408. }
  409. }
  410. }
  411. else
  412. {
  413. shell.Quantity--;
  414. }
  415. requiItemListView.ItemsSource = null;
  416. requiItemListView.ItemsSource = shells;
  417. }
  418. void IncreaseQtyBtn_Clicked(object sender, EventArgs e)
  419. {
  420. var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
  421. if (shell == null)
  422. return;
  423. shell.Quantity++;
  424. requiItemListView.ItemsSource = null;
  425. requiItemListView.ItemsSource = shells;
  426. }
  427. void AddItem_Clicked(object sender, EventArgs e)
  428. {
  429. if (App.Data.Products.Loaded)
  430. {
  431. if (loading)
  432. return;
  433. loading = true;
  434. var products = new ProductSelectionPage(
  435. (product) =>
  436. {
  437. Tuple<string, double> tuple = new Tuple<string, double>(product.Code, 1);
  438. LoadProduct(new Tuple<string, double>(product.Code, 1), product.Code);
  439. }
  440. );
  441. Navigation.PushAsync(products);
  442. }
  443. }
  444. void Qty_Changed(object sender, EventArgs e)
  445. {
  446. }
  447. #endregion
  448. }
  449. }