StoreRequiScannerPage.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Essentials;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. using Comal.Classes;
  10. using InABox.Core;
  11. using InABox.Clients;
  12. using System.Threading;
  13. using static comal.timesheets.RequiItems;
  14. using XF.Material.Forms.UI.Dialogs;
  15. using comal.timesheets.StoreRequis;
  16. using ZXing.PDF417.Internal;
  17. namespace comal.timesheets
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class StoreRequiScannerPage : ContentPage
  21. {
  22. #region Fields / Constructor
  23. public delegate bool OnScanEvent(object sender, string barcode);
  24. public event OnScanEvent OnScan;
  25. List<StoreRequiItemShell> shells = new List<StoreRequiItemShell>();
  26. List<StoreRequiItemShell> oldShells = new List<StoreRequiItemShell>();
  27. Requisition requisition = new Requisition();
  28. bool loading = false;
  29. Dictionary<StoreRequiItemShell, string> itemRowScannerRawResultPairs = new Dictionary<StoreRequiItemShell, string>();
  30. Dictionary<StoreRequiItemShell, string> itemRowScannerProcessedResultPairs = new Dictionary<StoreRequiItemShell, string>();
  31. bool firstLoad = true;
  32. bool containsNotes = false;
  33. public StoreRequiScannerPage(Guid requiID)
  34. {
  35. InitializeComponent();
  36. ConfigAll(requiID);
  37. }
  38. #endregion
  39. #region Config
  40. void ConfigAll(Guid requiID)
  41. {
  42. NavigationPage.SetHasBackButton(this, false);
  43. SetScannerOptions();
  44. if (requiID != Guid.Empty)
  45. {
  46. requisition.ID = requiID;
  47. LoadExistingRequi();
  48. }
  49. if (!HoldingsLoaded)
  50. {
  51. Timer t = null;
  52. t = new Timer(new TimerCallback((object o) =>
  53. {
  54. if (HoldingsLoaded)
  55. {
  56. Device.BeginInvokeOnMainThread(() =>
  57. {
  58. ConfigDisplay();
  59. });
  60. t.Dispose();
  61. }
  62. }), null, 0, 500);
  63. }
  64. else
  65. {
  66. loadingLbl.IsVisible = false;
  67. addBtn.IsEnabled = true;
  68. Task.Run(() =>
  69. {
  70. Thread.Sleep(1500);
  71. Device.BeginInvokeOnMainThread(() => { ConfigDisplay(); });
  72. });
  73. }
  74. }
  75. void SetScannerOptions()
  76. {
  77. var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
  78. {
  79. PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
  80. AutoRotate = false,
  81. TryInverted = true,
  82. TryHarder = true,
  83. };
  84. _scanView.Options = options;
  85. _scanView.IsAnalyzing = true;
  86. _scanView.IsScanning = true;
  87. _scanView.AutoFocus();
  88. _scanView.OnScanResult += ScanView_OnScanResult;
  89. }
  90. protected override void OnAppearing()
  91. {
  92. base.OnAppearing();
  93. if (!firstLoad)
  94. _scanView.IsAnalyzing = true;
  95. }
  96. protected override void OnDisappearing()
  97. {
  98. _scanView.IsAnalyzing = false;
  99. base.OnDisappearing();
  100. }
  101. async void LoadExistingRequi()
  102. {
  103. await Task.Run(() =>
  104. {
  105. requisition = new Client<Requisition>().Query(
  106. new Filter<Requisition>(x => x.ID).IsEqualTo(requisition.ID)
  107. ).Rows.FirstOrDefault().ToObject<Requisition>();
  108. if (!string.IsNullOrWhiteSpace(requisition.Request))
  109. {
  110. StoreRequiItemShell shell1 = new StoreRequiItemShell()
  111. {
  112. IsNotes = true,
  113. IsNotNotes = false,
  114. Summary = requisition.Request,
  115. BorderColor = Color.FromHex("#9f4576")
  116. };
  117. shells.Insert(0, shell1);
  118. containsNotes = true;
  119. }
  120. });
  121. await Task.Run(() =>
  122. {
  123. CoreTable table = new Client<RequisitionItem>().Query
  124. (
  125. new Filter<RequisitionItem>(x => x.RequisitionLink.ID).IsEqualTo(requisition.ID),
  126. new Columns<RequisitionItem>(
  127. x => x.ID,
  128. x => x.Product.ID,
  129. x => x.Product.Name,
  130. x => x.Product.Code,
  131. x => x.Quantity,
  132. x => x.Location.ID,
  133. x => x.Location.Description,
  134. x => x.Picked
  135. )
  136. );
  137. if (table.Rows.Any())
  138. {
  139. Device.BeginInvokeOnMainThread(() => { saveBtn.IsVisible = true; });
  140. foreach (CoreRow row in table.Rows)
  141. {
  142. StoreRequiItemShell shell = new StoreRequiItemShell()
  143. {
  144. ID = row.Get<RequisitionItem, Guid>(x => x.ID),
  145. ProductID = row.Get<RequisitionItem, Guid>(x => x.Product.ID),
  146. ProductName = row.Get<RequisitionItem, string>(x => x.Product.Name),
  147. ProductCode = row.Get<RequisitionItem, string>(x => x.Product.Code),
  148. Quantity = row.Get<RequisitionItem, double>(x => x.Quantity),
  149. LocationID = row.Get<RequisitionItem, Guid>(x => x.Location.ID),
  150. LocationName = row.Get<RequisitionItem, string>(x => x.Location.Description),
  151. Picked = row.Get<RequisitionItem, DateTime>(x => x.Picked),
  152. };
  153. shells.Add(shell);
  154. oldShells.Add(shell);
  155. }
  156. }
  157. Device.BeginInvokeOnMainThread(() =>
  158. {
  159. requiItemListView.ItemsSource = shells;
  160. if (containsNotes)
  161. {
  162. countLbl.Text = "Number of items: " + (shells.Count - 1);
  163. }
  164. else
  165. {
  166. countLbl.Text = "Number of items: " + shells.Count;
  167. }
  168. });
  169. });
  170. }
  171. void ConfigDisplay()
  172. {
  173. loadingLbl.IsVisible = false;
  174. addBtn.IsEnabled = true;
  175. double width = scannerGrid.Width / 6;
  176. double height = scannerGrid.Height / 6;
  177. lblv1.WidthRequest = height / 2;
  178. lblh1.IsVisible = true;
  179. lblv1.IsVisible = true;
  180. lblv2.WidthRequest = height / 2;
  181. lblh2.IsVisible = true;
  182. lblv2.IsVisible = true;
  183. lblv3.WidthRequest = height / 2;
  184. lblh3.IsVisible = true;
  185. lblv3.IsVisible = true;
  186. lblv4.WidthRequest = height / 2;
  187. lblh4.IsVisible = true;
  188. lblv4.IsVisible = true;
  189. scannerGrid.RaiseChild(lblv1);
  190. scannerGrid.RaiseChild(lblh1);
  191. scannerGrid.RaiseChild(lblv2);
  192. scannerGrid.RaiseChild(lblh2);
  193. scannerGrid.RaiseChild(lblv3);
  194. scannerGrid.RaiseChild(lblh3);
  195. scannerGrid.RaiseChild(lblv4);
  196. scannerGrid.RaiseChild(lblh4);
  197. firstLoad = false;
  198. }
  199. #endregion
  200. #region Scanning
  201. private void ScanView_OnScanResult(ZXing.Result result)
  202. {
  203. Device.BeginInvokeOnMainThread(async () =>
  204. {
  205. if (!loading)
  206. {
  207. loading = true;
  208. if (RequiItems.HoldingsLoaded)
  209. {
  210. bool bOK = true;
  211. if (OnScan != null)
  212. bOK = OnScan(this, result.Text);
  213. if (bOK)
  214. {
  215. if (!itemRowScannerRawResultPairs.Values.Contains(result.Text))
  216. {
  217. if (!itemRowScannerProcessedResultPairs.Values.Contains(result.Text))
  218. {
  219. Vibration.Vibrate();
  220. var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
  221. player.Load("requiitemadded.mp3");
  222. player.Play();
  223. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding"))
  224. {
  225. string rawResult = result.Text;
  226. Tuple<string, double> tuple = ProcessResult(result.Text);
  227. LoadProduct(tuple, rawResult);
  228. }
  229. }
  230. else
  231. {
  232. loading = false;
  233. }
  234. }
  235. else
  236. {
  237. loading = false;
  238. }
  239. }
  240. else
  241. {
  242. loading = false;
  243. }
  244. }
  245. else
  246. {
  247. loading = false;
  248. }
  249. }
  250. });
  251. }
  252. private Tuple<string, double> ProcessResult(string result)
  253. {
  254. double qty = 1;
  255. if (result.Contains("*"))
  256. {
  257. try
  258. {
  259. int i = result.IndexOf("*");
  260. string remainder = result.Substring(i);
  261. result = result.Remove(i);
  262. string s1 = remainder.Substring(1);
  263. qty = Convert.ToDouble(s1);
  264. }
  265. catch
  266. {
  267. loading = false;
  268. }
  269. }
  270. Tuple<string, double> tuple = new Tuple<string, double>(result, qty);
  271. return tuple;
  272. }
  273. private void LoadProduct(Tuple<string, double> processedResultQtyTuple, string rawResult)
  274. {
  275. Device.BeginInvokeOnMainThread(async () =>
  276. {
  277. //lookup product in productshells cache
  278. ProductShell product = GlobalVariables.ProductShells.Find(x => x.Code.Equals(processedResultQtyTuple.Item1));
  279. string itemLocation = "";
  280. Guid locationID = Guid.Empty;
  281. //lookup holding for product in holdings cache
  282. try
  283. {
  284. List<HoldingsCacheShell> list = holdingsCache.Where(x => x.ProductID.Equals(product.ID)).ToList();
  285. if (list.Count == 1) //one stockholding - auto choose holding
  286. AddShell(list.First(), product, rawResult, processedResultQtyTuple);
  287. else if (list.Count > 1) //more than one stockholding - user choose shelf
  288. {
  289. Dictionary<string, Guid> holdingLocationIDs = new Dictionary<string, Guid>();
  290. foreach (HoldingsCacheShell holding in list)
  291. {
  292. if (!holdingLocationIDs.ContainsKey(holding.LocationName))
  293. holdingLocationIDs.Add(holding.LocationName, holding.LocationID);
  294. }
  295. List<string> options = holdingLocationIDs.Keys.ToList();
  296. ListSelectionPage page = new ListSelectionPage(options);
  297. page.OnSimpleListTapped += (locationName) =>
  298. {
  299. AddShell(list.Find(x => x.LocationName == locationName),product, rawResult, processedResultQtyTuple);
  300. };
  301. Navigation.PushAsync(page);
  302. }
  303. else if (list.Count == 0)
  304. {
  305. DisplayAlert("No Holdings Found for Product", "", "OK");
  306. loading = false;
  307. return;
  308. }
  309. loading = false;
  310. }
  311. catch (Exception e)
  312. {
  313. DisplayAlert("Error", e.Message, "OK");
  314. loading = false;
  315. return;
  316. }
  317. });
  318. }
  319. private void AddShell(HoldingsCacheShell holding, ProductShell product, string rawResult, Tuple<string, double> processedResultQtyTuple)
  320. {
  321. StoreRequiItemShell shell = new StoreRequiItemShell
  322. {
  323. ProductID = product.ID,
  324. ProductName = product.Name,
  325. ProductCode = product.Code,
  326. Quantity = 1,
  327. LocationID = holding.LocationID,
  328. LocationName = holding.LocationName,
  329. StyleID = holding.StyleID,
  330. JobID = holding.StyleID
  331. };
  332. shell.Dimensions.Unit.ID = holding.Dimensions.Unit.ID;
  333. shell.Dimensions.Unit.HasQuantity = holding.Dimensions.Unit.HasQuantity;
  334. shell.Dimensions.Unit.HasLength = holding.Dimensions.Unit.HasLength;
  335. shell.Dimensions.Unit.HasHeight = holding.Dimensions.Unit.HasHeight;
  336. shell.Dimensions.Unit.HasWeight = holding.Dimensions.Unit.HasWeight;
  337. shell.Dimensions.Unit.HasWidth = holding.Dimensions.Unit.HasWidth;
  338. shell.Dimensions.Quantity = holding.Dimensions.Quantity;
  339. shell.Dimensions.Length = holding.Dimensions.Length;
  340. shell.Dimensions.Height = holding.Dimensions.Height;
  341. shell.Dimensions.Weight = holding.Dimensions.Weight;
  342. shell.Dimensions.Width = holding.Dimensions.Width;
  343. shell.Dimensions.Unit.Format = holding.Dimensions.Unit.Format;
  344. shell.Dimensions.Unit.Formula = holding.Dimensions.Unit.Formula;
  345. shell.Dimensions.UnitSize = holding.Dimensions.UnitSize;
  346. shells.Add(shell);
  347. itemRowScannerRawResultPairs.Add(shell, rawResult);
  348. itemRowScannerProcessedResultPairs.Add(shell, processedResultQtyTuple.Item1);
  349. requiItemListView.ItemsSource = null;
  350. requiItemListView.ItemsSource = shells;
  351. countLbl.IsVisible = true;
  352. if (containsNotes)
  353. {
  354. countLbl.Text = "Number of items: " + (shells.Count - 1);
  355. }
  356. else
  357. {
  358. countLbl.Text = "Number of items: " + shells.Count;
  359. }
  360. saveBtn.IsVisible = true;
  361. loading = false;
  362. }
  363. #endregion
  364. #region Button Presses
  365. private async void ExitBtn_Clicked(object sender, EventArgs e)
  366. {
  367. if (shells.Count > 0)
  368. {
  369. if (containsNotes && shells.Count > 1)
  370. {
  371. string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
  372. switch (chosenOption)
  373. {
  374. case "Cancel":
  375. return;
  376. case "Yes":
  377. Navigation.PopAsync();
  378. break;
  379. case "No":
  380. return;
  381. default:
  382. return;
  383. }
  384. }
  385. else if (containsNotes && shells.Count == 1)
  386. {
  387. Navigation.PopAsync();
  388. }
  389. else
  390. {
  391. string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
  392. switch (chosenOption)
  393. {
  394. case "Cancel":
  395. return;
  396. case "Yes":
  397. Navigation.PopAsync();
  398. break;
  399. case "No":
  400. return;
  401. default:
  402. return;
  403. }
  404. }
  405. }
  406. else
  407. Navigation.PopAsync();
  408. }
  409. void SaveBtn_Clicked(object sender, EventArgs e)
  410. {
  411. StoreRequiConfirmationPage page = new StoreRequiConfirmationPage(requisition, shells, oldShells);
  412. page.OnSaveSelected += () => { Navigation.PopAsync(); };
  413. Navigation.PushAsync(page);
  414. }
  415. private async void RequiItem_Tapped(object sender, EventArgs e)
  416. {
  417. var shell = requiItemListView.SelectedItem as StoreRequiItemShell;
  418. if (shell == null) return;
  419. await RequiItemTappedAsync(shell);
  420. }
  421. private async Task RequiItemTappedAsync(StoreRequiItemShell shell)
  422. {
  423. string pickstatus = shell.Picked == DateTime.MinValue ? "not picked" : "picked (" + shell.Picked.ToString("dd MMM yy") + ")";
  424. string options = shell.Picked == DateTime.MinValue ? ". Mark as Picked?" : ". Remove Picked status?";
  425. string chosenOption = await DisplayActionSheet("Line is " + pickstatus + options, "Cancel", null, "Yes", "No");
  426. if (chosenOption != "Yes")
  427. return;
  428. shell.Picked = shell.Picked == DateTime.MinValue ? DateTime.Today : DateTime.MinValue;
  429. shell.Colour = shell.Picked == DateTime.MinValue ? Color.Default : Color.FromHex("#8fbc8f");
  430. requiItemListView.ItemsSource = null;
  431. requiItemListView.ItemsSource = shells;
  432. }
  433. void ReduceQtyBtn_Clicked(object sender, EventArgs e)
  434. {
  435. var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
  436. if (shell == null) return;
  437. if (shell.Quantity <= 1)
  438. {
  439. shells.Remove(shell);
  440. itemRowScannerRawResultPairs.Remove(shell);
  441. itemRowScannerProcessedResultPairs.Remove(shell);
  442. if (containsNotes)
  443. {
  444. countLbl.Text = "Number of items: " + (shells.Count - 1);
  445. if (shells.Count == 1)
  446. {
  447. saveBtn.IsVisible = false;
  448. }
  449. }
  450. else
  451. {
  452. countLbl.Text = "Number of items: " + shells.Count;
  453. if (shells.Count == 0)
  454. {
  455. saveBtn.IsVisible = false;
  456. }
  457. }
  458. }
  459. else
  460. {
  461. shell.Quantity--;
  462. }
  463. requiItemListView.ItemsSource = null;
  464. requiItemListView.ItemsSource = shells;
  465. }
  466. void IncreaseQtyBtn_Clicked(object sender, EventArgs e)
  467. {
  468. var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
  469. if (shell == null)
  470. return;
  471. shell.Quantity++;
  472. requiItemListView.ItemsSource = null;
  473. requiItemListView.ItemsSource = shells;
  474. }
  475. void AddItem_Clicked(object sender, EventArgs e)
  476. {
  477. if (GlobalVariables.ProductsLoaded)
  478. {
  479. if (loading)
  480. return;
  481. loading = true;
  482. ProductList products = new ProductList(GlobalVariables.ProductShells, true);
  483. products.OnProductSelected += () =>
  484. {
  485. Tuple<string, double> tuple = new Tuple<string, double>(products.SelectedProduct.Code, 1);
  486. LoadProduct(new Tuple<string, double>(products.SelectedProduct.Code, 1), products.SelectedProduct.Code);
  487. };
  488. Navigation.PushAsync(products);
  489. }
  490. }
  491. void Qty_Changed(object sender, EventArgs e)
  492. {
  493. }
  494. #endregion
  495. }
  496. [DoNotPersist]
  497. public class StoreRequiItemShell : Entity
  498. {
  499. public Guid ProductID { get; set; }
  500. public string ProductCode { get; set; }
  501. public string ProductName { get; set; }
  502. public string LocationName { get; set; }
  503. public double Quantity { get; set; }
  504. public Guid LocationID { get; set; }
  505. public Color BorderColor { get; set; }
  506. public bool IsNotes { get; set; }
  507. public bool IsNotNotes { get; set; }
  508. public string Summary { get; set; }
  509. public DateTime Picked { get; set; }
  510. public Color Colour { get; set; }
  511. public Guid JobID { get; set; }
  512. public Guid StyleID { get; set; }
  513. public StockDimensions Dimensions { get; set; }
  514. public StoreRequiItemShell()
  515. {
  516. ProductID = Guid.Empty;
  517. ProductCode = "";
  518. ProductName = "";
  519. LocationName = "";
  520. Quantity = 0;
  521. ID = Guid.Empty;
  522. LocationID = Guid.Empty;
  523. BorderColor = Color.FromHex("#15C7C1");
  524. IsNotes = false;
  525. IsNotNotes = true;
  526. Picked = DateTime.MinValue;
  527. Colour = Color.Default;
  528. JobID = Guid.Empty;
  529. StyleID = Guid.Empty;
  530. Dimensions = new StockDimensions(() => this);
  531. }
  532. }
  533. }