StoreRequiScannerPage.xaml.cs 20 KB

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