StoreRequiScannerPage.xaml.cs 20 KB

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