RecTrans.xaml.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. using comal.timesheets.CustomControls;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using Plugin.Media;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using Xamarin.CommunityToolkit.Extensions;
  15. using Xamarin.CommunityToolkit.UI.Views;
  16. using Xamarin.Essentials;
  17. using Xamarin.Forms;
  18. using Xamarin.Forms.Xaml;
  19. using XF.Material.Forms.UI.Dialogs;
  20. namespace comal.timesheets
  21. {
  22. [XamlCompilation(XamlCompilationOptions.Compile)]
  23. public partial class RecTrans : ContentPage
  24. {
  25. #region Fields + Constructor + Loading
  26. List<StockHoldingShell> issuingHoldings = new List<StockHoldingShell>();
  27. List<StockHoldingShell> receivingHoldings = new List<StockHoldingShell>();
  28. List<StockHoldingShell> originalHoldings = new List<StockHoldingShell>();
  29. StockLocation ReceivingStockLocation = new StockLocation();
  30. StockLocation IssuingStockLocation = new StockLocation();
  31. ProductStyle DefaultStyle = new ProductStyle();
  32. ProductStyle PreviousStyle = new ProductStyle();
  33. Filter<StockHolding> issuingFilter = new Filter<StockHolding>();
  34. Job ReceivingJob = new Job();
  35. bool bTapping = false;
  36. double frameHeight = 80;
  37. List<StockLocation> favourites = new List<StockLocation>();
  38. StockMovementBatchType BatchType = new StockMovementBatchType();
  39. string DeviceType = "";
  40. public RecTrans(StockMovementBatchType batchType, string issuingGuidString = "")
  41. {
  42. InitializeComponent();
  43. NavigationPage.SetHasBackButton(this, false);
  44. BatchType = batchType;
  45. var idiom = DeviceInfo.Idiom;
  46. if (idiom.Equals(DeviceIdiom.Tablet))
  47. {
  48. DeviceType = "Tablet";
  49. }
  50. //DisplayAlert("Instructions", "1. Select Issuing Location or use default (incoming stores)." + System.Environment.NewLine
  51. // + System.Environment.NewLine
  52. // + "2. Select Receiving Location or Job to Issue to." + System.Environment.NewLine
  53. // + System.Environment.NewLine
  54. // + "3. Tap items to transfer." + System.Environment.NewLine
  55. // + System.Environment.NewLine
  56. // + "4. Save with photos + notes." + System.Environment.NewLine
  57. // + System.Environment.NewLine
  58. // + "Note: Exiting loses unsaved work", "OK");
  59. IssuingStockLocation.PropertyChanged += IssuingStockLocation_PropertyChanged;
  60. ReceivingStockLocation.PropertyChanged += ReceivingStockLocation_PropertyChanged;
  61. LoadFavourites();
  62. ShowHideButtonsBasedOnType();
  63. LoadFromProductSearch(issuingGuidString);
  64. }
  65. private void LoadFromProductSearch(string issuingGuidString)
  66. {
  67. if (!string.IsNullOrWhiteSpace(issuingGuidString))
  68. {
  69. try
  70. {
  71. Guid issuingLocationID = Guid.Parse(issuingGuidString);
  72. CoreTable table = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.ID).IsEqualTo(issuingLocationID),
  73. new Columns<StockLocation>(x => x.ID, x => x.Code, x => x.Description)
  74. );
  75. if (table.Rows.Any())
  76. {
  77. List<object> list = table.Rows.First().Values;
  78. IssuingStockLocation.ID = Guid.Parse(list[0].ToString());
  79. IssuingStockLocation.Code = list[1].ToString();
  80. IssuingStockLocation.Description = list[2].ToString();
  81. }
  82. Filter<StockHolding> filter = new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(issuingLocationID);
  83. LoadIncomingListData(filter);
  84. }
  85. catch { }
  86. }
  87. }
  88. private void ShowHideButtonsBasedOnType()
  89. {
  90. if (BatchType == StockMovementBatchType.Issue)
  91. {
  92. receivingFavouriteBtn1.IsEnabled = false;
  93. receivingFavouriteBtn2.IsEnabled = false;
  94. receivingFavouriteBtn3.IsEnabled = false;
  95. chooseReceivingLocationBtn.IsVisible = false;
  96. receivingLocationLbl.Text = "Choose Job";
  97. titleLbl.Text = "Issue to Job";
  98. }
  99. else if (BatchType == StockMovementBatchType.Transfer)
  100. {
  101. chooseJobBtn.IsVisible = false;
  102. }
  103. }
  104. #endregion
  105. #region Issuing Button Presses
  106. private async void ChooseIssuingLocationBtn_Clicked(object sender, EventArgs e)
  107. {
  108. CollapseExpanderOnButtonPress();
  109. if (receivingHoldings.Count > 0)
  110. {
  111. PromptResetScreen();
  112. }
  113. else
  114. {
  115. ChooseIssuingLocation();
  116. }
  117. }
  118. private void IssuingFavourite_Clicked(object sender, EventArgs e)
  119. {
  120. string chosenFavourite = (sender as Button).Text;
  121. CollapseExpanderOnButtonPress();
  122. if (receivingHoldings.Count > 0)
  123. {
  124. PromptResetScreen(chosenFavourite);
  125. }
  126. else
  127. {
  128. IssuingFavouriteChosen(chosenFavourite);
  129. }
  130. }
  131. #endregion
  132. #region Issuing Location Methods
  133. private void ChooseIssuingLocation()
  134. {
  135. StockLocationSelectionPage page = new StockLocationSelectionPage();
  136. page.OnLocationSelected += (s) =>
  137. {
  138. if (ReceivingStockLocation.ID == s.ID)
  139. return;
  140. IssuingStockLocation.ID = s.ID;
  141. IssuingStockLocation.Code = s.Code;
  142. IssuingStockLocation.Description = s.Description;
  143. Filter<StockHolding> newIssuingFilter = new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(IssuingStockLocation.ID);
  144. issuingHoldings.Clear();
  145. receivingHoldings.Clear();
  146. LoadIncomingListData(newIssuingFilter);
  147. Device.BeginInvokeOnMainThread(() =>
  148. {
  149. });
  150. };
  151. Navigation.PushAsync(page);
  152. }
  153. private async void IssuingFavouriteChosen(string chosenFavourite)
  154. {
  155. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  156. {
  157. ClearLists();
  158. StockLocation location = favourites.Find(x => x.Code.Equals(chosenFavourite));
  159. IssuingStockLocation.ID = location.ID;
  160. IssuingStockLocation.Code = location.Code;
  161. IssuingStockLocation.Description = location.Description;
  162. Filter<StockHolding> filter = new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(IssuingStockLocation.ID);
  163. LoadIncomingListData(filter);
  164. }
  165. }
  166. #endregion
  167. #region Receiving Button Presses
  168. private async void ChooseReceivingLocationBtn_Clicked(object sender, EventArgs e)
  169. {
  170. string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, "New Location", "Existing Location");
  171. switch (chosenOption)
  172. {
  173. case "Cancel":
  174. return;
  175. break;
  176. case "New Location":
  177. ChooseNewLocation();
  178. CollapseExpanderOnButtonPress();
  179. break;
  180. case "Existing Location":
  181. ChooseReceivingLocation();
  182. CollapseExpanderOnButtonPress();
  183. break;
  184. default:
  185. return;
  186. break;
  187. }
  188. }
  189. private void JobBtn_Clicked(object sender, EventArgs e)
  190. {
  191. ChooseJob();
  192. CollapseExpanderOnButtonPress();
  193. }
  194. private void ReceivingFavourite_Clicked(object sender, EventArgs e)
  195. {
  196. CollapseExpanderOnButtonPress();
  197. string chosenFavourite = (sender as Button).Text;
  198. ReceivingFavouriteChosen(chosenFavourite);
  199. }
  200. #endregion
  201. #region Receiving Location Methods
  202. private void ReceivingFavouriteChosen(string chosenFavourite)
  203. {
  204. StockLocation location = favourites.Find(x => x.Code.Equals(chosenFavourite));
  205. ReceivingStockLocation.ID = location.ID;
  206. ReceivingStockLocation.Code = location.Code;
  207. ReceivingStockLocation.Description = location.Description;
  208. receivingLocationLbl.Text = ReceivingStockLocation.Description;
  209. }
  210. private void ChooseReceivingLocation()
  211. {
  212. StockLocationSelectionPage page = new StockLocationSelectionPage();
  213. page.OnLocationSelected += (s) =>
  214. {
  215. if (s.ID == IssuingStockLocation.ID)
  216. return;
  217. ReceivingStockLocation.ID = s.ID;
  218. ReceivingStockLocation.Code = s.Code;
  219. ReceivingStockLocation.Description = s.Description;
  220. ReceivingJob = new Job();
  221. LoadDefaultStyle();
  222. Device.BeginInvokeOnMainThread(() =>
  223. {
  224. titleLbl.Text = "Transfer Stock";
  225. receivingLocationLbl.Text = ReceivingStockLocation.Description;
  226. });
  227. };
  228. Navigation.PushAsync(page);
  229. }
  230. private void ChooseNewLocation()
  231. {
  232. try
  233. {
  234. StockLocation location = new StockLocation();
  235. location.Active = true;
  236. location.Warehouse.ID = Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd"); //EXTRUSIONS
  237. location.Warehouse.Description = "Extrusions";
  238. location.Warehouse.Code = "EXTRUSIONS";
  239. location.Area.Warehouse.ID = Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd"); //EXTRUSIONS
  240. location.Area.Warehouse.Description = "Extrusions";
  241. location.Area.ID = Guid.Parse("fa02ecd8-e642-49aa-98b5-c04d7ea0f4eb"); //Rack FLOOR
  242. location.Area.Description = "Rack FLOOR";
  243. location.Area.Code = "FLOOR";
  244. LocationDetailsPage locationDetailsPage = new LocationDetailsPage(location);
  245. locationDetailsPage.OnSave += (o, loc) =>
  246. {
  247. if (String.IsNullOrWhiteSpace(loc.Code))
  248. {
  249. MaterialDialog.Instance.AlertAsync(message: "Code may not be blank!");
  250. return false;
  251. }
  252. if (String.IsNullOrWhiteSpace(loc.Description))
  253. {
  254. MaterialDialog.Instance.AlertAsync(message: "Description may not be blank!");
  255. return false;
  256. }
  257. if (loc.Area.ID == Guid.Empty)
  258. {
  259. MaterialDialog.Instance.AlertAsync(message: "Area may not be blank!");
  260. return false;
  261. }
  262. CoreTable others = new Client<StockLocation>().Query(
  263. new Filter<StockLocation>(x => x.Code).IsEqualTo(loc.Code).And(x => x.ID).IsNotEqualTo(loc.ID),
  264. new Columns<StockLocation>(x => x.ID)
  265. );
  266. if (others.Rows.Any())
  267. {
  268. MaterialDialog.Instance.AlertAsync(message: "Location Code already exists!");
  269. return false;
  270. }
  271. try
  272. {
  273. new Client<StockLocation>().Save(loc, "Created Location");
  274. ReceivingStockLocation.ID = loc.ID;
  275. ReceivingStockLocation.Code = loc.Code;
  276. ReceivingStockLocation.Description = loc.Description;
  277. LoadDefaultStyle();
  278. Device.BeginInvokeOnMainThread(() =>
  279. {
  280. titleLbl.Text = "Transfer Stock";
  281. receivingLocationLbl.Text = ReceivingStockLocation.Description;
  282. });
  283. }
  284. catch (Exception err)
  285. {
  286. MaterialDialog.Instance.AlertAsync(message: "Unable to save Location\n" + err.Message);
  287. return false;
  288. }
  289. return true;
  290. };
  291. Navigation.PushAsync(locationDetailsPage);
  292. }
  293. catch { }
  294. }
  295. private void ChooseJob()
  296. {
  297. JobSelectionPage jobSelectionPage = new JobSelectionPage();
  298. jobSelectionPage.OnItemSelected += (() =>
  299. {
  300. ReceivingStockLocation = new StockLocation();
  301. ReceivingJob.ID = jobSelectionPage.Job.ID;
  302. ReceivingJob.Name = jobSelectionPage.Job.Name;
  303. ReceivingJob.JobNumber = jobSelectionPage.Job.JobNumber;
  304. Device.BeginInvokeOnMainThread(() =>
  305. {
  306. titleLbl.Text = "Issue to Job";
  307. receivingLocationLbl.Text = ReceivingJob.JobNumber;
  308. });
  309. });
  310. Navigation.PushAsync(jobSelectionPage);
  311. }
  312. #endregion
  313. #region Lists Tapped
  314. private void IssuingListView_Tapped(object sender, EventArgs e)
  315. {
  316. if (ReceivingStockLocation.ID == Guid.Empty && ReceivingJob.ID == Guid.Empty)
  317. return;
  318. if (bTapping)
  319. return;
  320. bTapping = true;
  321. StockHoldingShell shell = issuingListView.SelectedItem as StockHoldingShell;
  322. StockHoldingShell originalShell = DuplicateShell(shell);
  323. Job job = new Job();
  324. if (ReceivingStockLocation.Job.ID != Guid.Empty)
  325. {
  326. job.ID = ReceivingStockLocation.Job.ID;
  327. job.JobNumber = ReceivingStockLocation.Job.JobNumber;
  328. job.Name = ReceivingStockLocation.Job.Name;
  329. }
  330. RecTransferPopup popup = new RecTransferPopup(shell, PreviousStyle, job, ReceivingJob);
  331. popup.OnRecTransferItemAccepted += (() =>
  332. {
  333. if (popup.Shell.Units == originalShell.Units)
  334. {
  335. issuingHoldings.Remove(shell);
  336. receivingHoldings.Add(popup.Shell);
  337. RefreshLists();
  338. }
  339. else if (originalShell.Units > popup.Shell.Units)
  340. {
  341. int index = issuingHoldings.FindIndex(x => x.ID.Equals(originalShell.ID));
  342. issuingHoldings.Remove(shell);
  343. originalShell.Units = originalShell.Units - popup.Shell.Units;
  344. originalShell.DisplayUnits = "Units: " + originalShell.Units;
  345. issuingHoldings.Insert(index, originalShell);
  346. //original shell is updated to remove qty taken away
  347. receivingHoldings.Add(popup.Shell);
  348. //popup shell with new properties is added to receiving list
  349. RefreshLists();
  350. }
  351. originalHoldings.Add(originalShell);
  352. PreviousStyle.ID = popup.Shell.StyleID;
  353. PreviousStyle.Code = popup.Shell.StyleCode;
  354. PreviousStyle.Description = popup.Shell.Finish;
  355. bTapping = false;
  356. });
  357. popup.OnRecTransferPopupBackButtonPressed += (() =>
  358. {
  359. int index = issuingHoldings.FindIndex(x => x.ID.Equals(originalShell.ID));
  360. issuingHoldings.Remove(shell);
  361. issuingHoldings.Insert(index, originalShell);
  362. bTapping = false;
  363. RefreshLists();
  364. });
  365. Navigation.PushAsync(popup);
  366. }
  367. private void ReceivingListView_Tapped(object sender, EventArgs e)
  368. {
  369. StockHoldingShell shell = receivingListView.SelectedItem as StockHoldingShell;
  370. RemoveHoldingFromBatch(shell);
  371. }
  372. private void RemoveHoldingFromBatch(StockHoldingShell shell)
  373. {
  374. receivingHoldings.Remove(shell);
  375. if (issuingHoldings.Find(x => x.ID.Equals(shell.ID)) != null)
  376. {
  377. StockHoldingShell existingHolding = issuingHoldings.Find(x => x.ID.Equals(shell.ID));
  378. int index = issuingHoldings.FindIndex(x => x.ID.Equals(existingHolding.ID));
  379. issuingHoldings.Remove(existingHolding);
  380. existingHolding.Units = existingHolding.Units + shell.Units;
  381. existingHolding.DisplayUnits = "Units: " + existingHolding.Units;
  382. issuingHoldings.Insert(index, existingHolding);
  383. }
  384. else
  385. {
  386. issuingHoldings.Add(shell);
  387. }
  388. RefreshLists();
  389. }
  390. #endregion
  391. #region Refresh Reset Screen
  392. private async void PromptResetScreen(string chosenFavourite = "")
  393. {
  394. string chosenOption = await DisplayActionSheet("This will remove items in current batch", "Cancel", null, "Confirm");
  395. switch (chosenOption)
  396. {
  397. case "Confirm":
  398. ClearLists();
  399. if (!string.IsNullOrWhiteSpace(chosenFavourite))
  400. IssuingFavouriteChosen(chosenFavourite);
  401. else
  402. ChooseIssuingLocation();
  403. break;
  404. default:
  405. return;
  406. }
  407. }
  408. private void ClearLists()
  409. {
  410. issuingHoldings.Clear();
  411. receivingHoldings.Clear();
  412. RefreshLists();
  413. }
  414. private void RefreshLists()
  415. {
  416. Device.BeginInvokeOnMainThread(() =>
  417. {
  418. issuingListView.ItemsSource = null;
  419. issuingListView.ItemsSource = issuingHoldings;
  420. receivingListView.ItemsSource = null;
  421. receivingListView.ItemsSource = receivingHoldings;
  422. issuingLocationCountLbl.Text = "Items: " + issuingHoldings.Count();
  423. receivingCountLbl.Text = "Items in Batch: " + receivingHoldings.Count();
  424. searchEnt.Text = "";
  425. if (receivingHoldings.Count > 0)
  426. {
  427. saveBatchBtn.IsVisible = true;
  428. }
  429. else
  430. {
  431. saveBatchBtn.IsVisible = false;
  432. }
  433. });
  434. }
  435. #endregion
  436. #region Search
  437. private void SearchEnt_Changed(object sender, EventArgs e)
  438. {
  439. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  440. {
  441. issuingListView.ItemsSource = issuingHoldings;
  442. }
  443. else
  444. {
  445. RunSearch();
  446. }
  447. }
  448. private void RunSearch()
  449. {
  450. issuingListView.ItemsSource = issuingHoldings.Where
  451. (x => x.Code.Contains(searchEnt.Text) || x.Code.Contains(UpperCaseFirst(searchEnt.Text)) || x.Code.Contains(searchEnt.Text.ToLower()) || x.Code.Contains(searchEnt.Text.ToUpper())
  452. || x.Name.Contains(searchEnt.Text) || x.Name.Contains(UpperCaseFirst(searchEnt.Text)) || x.Name.Contains(searchEnt.Text.ToLower()) || x.Name.Contains(searchEnt.Text.ToUpper())
  453. || x.Finish.Contains(searchEnt.Text) || x.Finish.Contains(UpperCaseFirst(searchEnt.Text)) || x.Finish.Contains(searchEnt.Text.ToLower()) || x.Finish.Contains(searchEnt.Text.ToUpper())
  454. );
  455. }
  456. static String UpperCaseFirst(string s)
  457. {
  458. char[] a = s.ToCharArray();
  459. a[0] = char.ToUpper(a[0]);
  460. return new string(a);
  461. }
  462. #endregion
  463. #region Utils
  464. private async void LoadIncomingListData(Filter<StockHolding> _filter)
  465. {
  466. await Task.Run(() =>
  467. {
  468. CoreTable table = new Client<StockHolding>().Query
  469. (
  470. _filter,
  471. new Columns<StockHolding>
  472. (
  473. x => x.ID, //0
  474. x => x.Product.Code, //1
  475. x => x.Product.Name, //2
  476. x => x.Style.Description, //3
  477. x => x.Dimensions.UnitSize, //4
  478. x => x.Units, //5
  479. x => x.Location.ID, //6
  480. x => x.Job.ID, //7
  481. x => x.Job.Name, //8
  482. x => x.Job.JobNumber, //9
  483. x => x.Style.ID, //10
  484. x => x.Style.Code, //11
  485. x => x.Product.ID, //12
  486. x => x.Product.Image.ID, //13
  487. x => x.Dimensions.Unit.ID, //14
  488. x => x.Dimensions.Quantity, //15
  489. x => x.Dimensions.Length, //16
  490. x => x.Dimensions.Width, //17
  491. x => x.Dimensions.Height, //18
  492. x => x.Dimensions.Weight, //19
  493. x => x.Dimensions.Value //20
  494. ),
  495. null
  496. );
  497. if (table.Rows.Any())
  498. {
  499. foreach (CoreRow row in table.Rows)
  500. {
  501. List<object> list = row.Values;
  502. if (list[0] == null) { list[0] = Guid.Empty; } //0
  503. if (list[1] == null) { list[1] = ""; } //1
  504. if (list[2] == null) { list[2] = ""; } //2
  505. if (list[3] == null) { list[3] = ""; } //3
  506. if (list[4] == null) { list[4] = ""; } //4
  507. if (list[5] == null) { list[5] = 0.0; } //5
  508. if (list[6] == null) { list[6] = Guid.Empty; } //6
  509. if (list[7] == null) { list[7] = Guid.Empty; } //7
  510. if (list[8] == null) { list[8] = ""; } //8
  511. if (list[9] == null) { list[9] = ""; } //9
  512. if (list[10] == null) { list[10] = Guid.Empty; } //10
  513. if (list[11] == null) { list[11] = ""; } //11
  514. if (list[12] == null) { list[12] = Guid.Empty; } //12
  515. if (list[13] == null) { list[13] = Guid.Empty; } //13
  516. if (list[14] == null) { list[14] = Guid.Empty; } //14
  517. if (list[15] == null) { list[15] = 0.0; } //15
  518. if (list[16] == null) { list[16] = 0.0; } //16
  519. if (list[17] == null) { list[17] = 0.0; } //17
  520. if (list[18] == null) { list[18] = 0.0; } //18
  521. if (list[19] == null) { list[19] = 0.0; } //19
  522. if (list[20] == null) { list[20] = 0.0; } //20
  523. StockHoldingShell shell = new StockHoldingShell();
  524. shell.ID = Guid.Parse(list[0].ToString());
  525. shell.Code = list[1].ToString();
  526. shell.Name = list[2].ToString();
  527. shell.Finish = list[3].ToString();
  528. shell.DisplayFinish = "Finish: " + list[3].ToString();
  529. shell.DimensionsUnitSize = list[4].ToString();
  530. shell.Units = double.Parse(list[5].ToString());
  531. shell.DisplaySize = "Size: " + list[4].ToString();
  532. shell.DisplayUnits = "Units: " + shell.Units;
  533. shell.JobID = Guid.Parse(list[7].ToString());
  534. shell.JobName = list[8].ToString();
  535. shell.JobNumber = list[9].ToString();
  536. shell.DisplayJob = "Job: " + shell.JobNumber;
  537. shell.StyleID = Guid.Parse(list[10].ToString());
  538. shell.StyleCode = list[11].ToString();
  539. shell.ProductID = Guid.Parse(list[12].ToString());
  540. shell.ImageID = Guid.Parse(list[13].ToString());
  541. shell.DimensionsUnitID = Guid.Parse(list[14].ToString());
  542. shell.DimensionsQuantity = double.Parse(list[15].ToString());
  543. shell.DimensionsLength = double.Parse(list[16].ToString());
  544. shell.DimensionsWidth = double.Parse(list[17].ToString());
  545. shell.DimensionsHeight = double.Parse(list[18].ToString());
  546. shell.DimensionsWeight = double.Parse(list[19].ToString());
  547. shell.DimensionsValue = double.Parse(list[20].ToString());
  548. double units = 0.0;
  549. if (!shell.Code.Contains("FREIGHT"))
  550. {
  551. if (double.TryParse(list[5].ToString(), out units))
  552. {
  553. if (units > 0)
  554. {
  555. shell.DisplayUnits = "Units: " + shell.Units;
  556. issuingHoldings.Add(shell);
  557. }
  558. }
  559. }
  560. }
  561. }
  562. RefreshLists();
  563. LoadImages();
  564. });
  565. }
  566. private void LoadImages()
  567. {
  568. Task.Run(() =>
  569. {
  570. foreach (StockHoldingShell shell in issuingHoldings)
  571. {
  572. if (shell.ImageID != Guid.Empty)
  573. {
  574. CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.ImageID));
  575. if (table.Rows.Any())
  576. {
  577. CoreRow docrow = table.Rows.FirstOrDefault();
  578. if (docrow != null)
  579. {
  580. byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  581. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  582. if (src != null)
  583. {
  584. shell.ImageSource = src;
  585. shell.ImageVisible = true;
  586. if (DeviceType == "Tablet")
  587. {
  588. shell.LastRowHeight = 300;
  589. }
  590. else
  591. {
  592. shell.LastRowHeight = 150;
  593. }
  594. Device.BeginInvokeOnMainThread(() =>
  595. {
  596. issuingListView.ItemsSource = null;
  597. issuingListView.ItemsSource = issuingHoldings;
  598. });
  599. }
  600. }
  601. }
  602. }
  603. }
  604. });
  605. }
  606. private void CollapseExpanderOnButtonPress()
  607. {
  608. if (issuingExpander.State.Equals(ExpandState.Expanding) || issuingExpander.State.Equals(ExpandState.Expanded))
  609. {
  610. issuingExpander.IsExpanded = false;
  611. issuingFrame.HeightRequest = frameHeight;
  612. ForceLayout();
  613. }
  614. if (receivingExpander.State.Equals(ExpandState.Expanding) || receivingExpander.State.Equals(ExpandState.Expanded))
  615. {
  616. receivingExpander.IsExpanded = false;
  617. receivingFrame.HeightRequest = frameHeight;
  618. ForceLayout();
  619. }
  620. }
  621. //TODO
  622. private StockHoldingShell DuplicateShell(StockHoldingShell shell)
  623. {
  624. StockHoldingShell NewShell = new StockHoldingShell()
  625. {
  626. ID = shell.ID,
  627. Code = shell.Code,
  628. Name = shell.Name,
  629. Finish = shell.Finish,
  630. Units = shell.Units,
  631. DisplayUnits = shell.DisplayUnits,
  632. JobID = shell.JobID,
  633. JobName = shell.JobName,
  634. JobNumber = shell.JobNumber,
  635. DisplayJob = shell.DisplayJob,
  636. DisplayFinish = shell.DisplayFinish,
  637. StyleID = shell.StyleID,
  638. StyleCode = shell.StyleCode,
  639. ProductID = shell.ProductID,
  640. DisplaySize = shell.DisplaySize,
  641. LastRowHeight = shell.LastRowHeight,
  642. ImageID = shell.ImageID,
  643. ImageSource = shell.ImageSource,
  644. ImageVisible = shell.ImageVisible,
  645. DimensionsUnitID = shell.DimensionsUnitID,
  646. DimensionsQuantity = shell.DimensionsQuantity,
  647. DimensionsLength = shell.DimensionsLength,
  648. DimensionsWidth = shell.DimensionsWidth,
  649. DimensionsHeight = shell.DimensionsHeight,
  650. DimensionsWeight = shell.DimensionsWeight,
  651. DimensionsValue = shell.DimensionsValue,
  652. DimensionsUnitSize = shell.DimensionsUnitSize,
  653. };
  654. return NewShell;
  655. }
  656. private void IssuingExpander_Tapped(object sender, EventArgs e)
  657. {
  658. if (issuingExpander.State.Equals(ExpandState.Expanding) || issuingExpander.State.Equals(ExpandState.Expanded))
  659. {
  660. }
  661. else if (issuingExpander.State.Equals(ExpandState.Collapsing) || issuingExpander.State.Equals(ExpandState.Collapsed))
  662. {
  663. issuingFrame.HeightRequest = frameHeight;
  664. ForceLayout();
  665. }
  666. }
  667. private void ReceivingExpander_Tapped(object sender, EventArgs e)
  668. {
  669. if (receivingExpander.State.Equals(ExpandState.Expanding) || receivingExpander.State.Equals(ExpandState.Expanded))
  670. {
  671. }
  672. else if (receivingExpander.State.Equals(ExpandState.Collapsing) || receivingExpander.State.Equals(ExpandState.Collapsed))
  673. {
  674. receivingFrame.HeightRequest = frameHeight;
  675. ForceLayout();
  676. }
  677. }
  678. private void ReceivingStockLocation_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  679. {
  680. Device.BeginInvokeOnMainThread(() =>
  681. {
  682. receivingLocationLbl.Text = ReceivingStockLocation.Description;
  683. });
  684. }
  685. private void IssuingStockLocation_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  686. {
  687. Device.BeginInvokeOnMainThread(() =>
  688. {
  689. issuingLocationLbl.Text = IssuingStockLocation.Description;
  690. });
  691. }
  692. private async void LoadDefaultStyle()
  693. {
  694. try
  695. {
  696. await Task.Run(() =>
  697. {
  698. CoreTable table = new Client<Job>().Query(
  699. new Filter<Job>(x => x.ID).IsEqualTo(ReceivingStockLocation.Job.ID),
  700. new Columns<Job>(x => x.Style.ID, x => x.Style.Code, x => x.Style.Description)
  701. );
  702. if (table.Rows.Any())
  703. {
  704. List<object> list = table.Rows.First().Values;
  705. if (list[0] == null) { list[0] = Guid.Empty; } //0
  706. if (list[1] == null) { list[1] = ""; } //1
  707. if (list[2] == null) { list[2] = ""; } //2
  708. DefaultStyle.ID = Guid.Parse(list[0].ToString());
  709. DefaultStyle.Code = list[1].ToString();
  710. DefaultStyle.Description = list[2].ToString();
  711. }
  712. });
  713. }
  714. catch { }
  715. }
  716. private void LoadFavourites()
  717. {
  718. try
  719. {
  720. CoreTable table = new Client<StockLocation>().Query
  721. (
  722. new Filter<StockLocation>(x => x.Favourite).IsEqualTo(true),
  723. new Columns<StockLocation>(x => x.ID, x => x.Code, x => x.Description)
  724. );
  725. if (table.Rows.Any())
  726. {
  727. foreach (CoreRow row in table.Rows)
  728. {
  729. if (favourites.Count < 3)
  730. {
  731. List<object> list = row.Values;
  732. if (list[0] == null) list[0] = Guid.Empty;
  733. if (list[1] == null) list[1] = "";
  734. if (list[2] == null) list[2] = "";
  735. StockLocation location = new StockLocation()
  736. {
  737. ID = Guid.Parse(list[0].ToString()),
  738. Code = list[1].ToString(),
  739. Description = list[2].ToString(),
  740. };
  741. favourites.Add(location);
  742. }
  743. }
  744. Device.BeginInvokeOnMainThread(() =>
  745. {
  746. if (favourites.Count == 0)
  747. {
  748. DisableButtonOne();
  749. DisableButtonTwo();
  750. DisableButtonThree();
  751. }
  752. if (favourites.Count == 1)
  753. {
  754. issuingFavouriteBtn1.Text = favourites[0].Code;
  755. receivingFavouriteBtn1.Text = favourites[0].Code;
  756. DisableButtonTwo();
  757. DisableButtonThree();
  758. }
  759. if (favourites.Count == 2)
  760. {
  761. issuingFavouriteBtn1.Text = favourites[0].Code;
  762. receivingFavouriteBtn1.Text = favourites[0].Code;
  763. issuingFavouriteBtn2.Text = favourites[1].Code;
  764. receivingFavouriteBtn2.Text = favourites[1].Code;
  765. DisableButtonThree();
  766. }
  767. if (favourites.Count == 3)
  768. {
  769. issuingFavouriteBtn1.Text = favourites[0].Code;
  770. receivingFavouriteBtn1.Text = favourites[0].Code;
  771. issuingFavouriteBtn2.Text = favourites[1].Code;
  772. receivingFavouriteBtn2.Text = favourites[1].Code;
  773. issuingFavouriteBtn3.Text = favourites[2].Code;
  774. receivingFavouriteBtn3.Text = favourites[2].Code;
  775. }
  776. });
  777. }
  778. }
  779. catch
  780. {
  781. }
  782. }
  783. void DisableButtonOne()
  784. {
  785. issuingFavouriteBtn1.Text = "Not set";
  786. issuingFavouriteBtn1.IsEnabled = false;
  787. receivingFavouriteBtn1.Text = "Not set";
  788. receivingFavouriteBtn1.IsEnabled = false;
  789. }
  790. void DisableButtonTwo()
  791. {
  792. issuingFavouriteBtn2.Text = "Not set";
  793. issuingFavouriteBtn2.IsEnabled = false;
  794. receivingFavouriteBtn2.Text = "Not set";
  795. receivingFavouriteBtn2.IsEnabled = false;
  796. }
  797. void DisableButtonThree()
  798. {
  799. issuingFavouriteBtn3.Text = "Not set";
  800. issuingFavouriteBtn3.IsEnabled = false;
  801. receivingFavouriteBtn3.Text = "Not set";
  802. receivingFavouriteBtn3.IsEnabled = false;
  803. }
  804. #endregion
  805. #region Page Navigation / Saving
  806. private async void ExitWithoutSaving(object sender, EventArgs e)
  807. {
  808. if (receivingHoldings.Count > 0)
  809. {
  810. string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
  811. switch (chosenOption)
  812. {
  813. case "Cancel":
  814. return;
  815. case "Yes":
  816. Navigation.PopAsync();
  817. break;
  818. case "No":
  819. return;
  820. default:
  821. return;
  822. }
  823. }
  824. else
  825. Navigation.PopAsync();
  826. }
  827. private void SaveBatch_Clicked(object sender, EventArgs e)
  828. {
  829. if (receivingHoldings.Count == 0)
  830. return;
  831. if (ReceivingStockLocation.ID == Guid.Empty && ReceivingJob.ID == Guid.Empty)
  832. return;
  833. RecTransCompletion completionPage = new RecTransCompletion(receivingHoldings, originalHoldings, IssuingStockLocation, ReceivingStockLocation, ReceivingJob);
  834. completionPage.OnRecTransCompleted += (() =>
  835. {
  836. ReceivingStockLocation = new StockLocation();
  837. ReceivingJob = new Job();
  838. receivingLocationLbl.Text = "Receiving Location:";
  839. receivingHoldings.Clear();
  840. originalHoldings.Clear();
  841. issuingListView.ItemsSource = null;
  842. receivingListView.ItemsSource = null;
  843. PreviousStyle = new ProductStyle();
  844. RefreshLists();
  845. });
  846. Navigation.PushAsync(completionPage);
  847. }
  848. #endregion
  849. }
  850. public class StockHoldingShell
  851. {
  852. public Guid ID { get; set; }
  853. public string Code { get; set; }
  854. public string Name { get; set; }
  855. public string DisplayFinish { get; set; }
  856. public string DisplayUnits { get; set; }
  857. public string Finish { get; set; } //style.description
  858. public double Units { get; set; }
  859. public Guid JobID { get; set; }
  860. public string JobName { get; set; }
  861. public string JobNumber { get; set; }
  862. public string DisplayJob { get; set; }
  863. public Guid StyleID { get; set; }
  864. public string StyleCode { get; set; }
  865. public Guid ProductID { get; set; }
  866. public string DisplaySize { get; set; }
  867. public double LastRowHeight { get; set; }
  868. public bool ImageVisible { get; set; }
  869. public ImageSource ImageSource { get; set; }
  870. public Guid ImageID { get; set; }
  871. public Color Color { get; set; }
  872. public Guid DimensionsUnitID { get; set; }
  873. public double DimensionsQuantity { get; set; }
  874. public double DimensionsLength { get; set; }
  875. public double DimensionsWidth { get; set; }
  876. public double DimensionsHeight { get; set; }
  877. public double DimensionsWeight { get; set; }
  878. public double DimensionsValue { get; set; }
  879. public string DimensionsUnitSize { get; set; }
  880. public StockHoldingShell()
  881. {
  882. ID = Guid.Empty;
  883. Code = "";
  884. Name = "";
  885. DisplayFinish = "";
  886. DisplayUnits = "";
  887. Finish = "";
  888. Units = 0.0;
  889. JobID = Guid.Empty;
  890. JobName = "";
  891. JobNumber = "";
  892. DisplayJob = "";
  893. StyleID = Guid.Empty;
  894. StyleCode = "";
  895. ProductID = Guid.Empty;
  896. DisplaySize = "";
  897. LastRowHeight = 0;
  898. ImageVisible = false;
  899. ImageSource = "";
  900. ImageID = Guid.Empty;
  901. Color = Color.Default;
  902. DimensionsUnitID = Guid.Empty;
  903. DimensionsQuantity = 0;
  904. DimensionsLength = 0;
  905. DimensionsWidth = 0;
  906. DimensionsHeight = 0;
  907. DimensionsWeight = 0;
  908. DimensionsValue = 0;
  909. DimensionsUnitSize = "";
  910. }
  911. }
  912. }