MainPage.xaml.cs 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Maps;
  9. using InABox.Core;
  10. using InABox.Configuration;
  11. using InABox.Clients;
  12. using InABox.Mobile;
  13. using Comal.Classes;
  14. using XF.Material.Forms.UI.Dialogs;
  15. using comal.timesheets.CustomControls;
  16. using comal.timesheets.StoreRequis;
  17. using PRSSecurity = InABox.Core.Security;
  18. using Plugin.LocalNotification;
  19. using comal.timesheets.Tasks;
  20. namespace comal.timesheets
  21. {
  22. public partial class MainPage : ContentPage
  23. {
  24. #region Fields
  25. List<ToolEntry> toolEntries = new List<ToolEntry>();
  26. private TimeSheet _timesheet = null;
  27. private Employee _employee = null;
  28. private CoreTable _jobs = null;
  29. public bool SettingsChanged { get; private set; }
  30. bool bUpdatingTimesheet = false;
  31. bool firstLoad = true;
  32. bool recentlyAskedToUpdate = true;
  33. int updateCounter;
  34. public static ConnectionSettings connectionSettings = null;
  35. bool midnightTimerOn = false;
  36. DateTime oneSecondBeforeMidnight = DateTime.Today.AddSeconds(864399);
  37. bool clockedOffInLast5Seconds = false;
  38. bool bRecentlyUpdatedTiles = false;
  39. bool bSharedDeviceFirstLoad = true;
  40. bool bSharedDevice = false;
  41. int NumberOfNotfications = 0;
  42. private Job _job = new Job();
  43. string deviceName = "";
  44. string matchedDeviceName = "";
  45. int notCount = 1;
  46. #endregion
  47. #region Constructor
  48. public MainPage()
  49. {
  50. InitializeComponent();
  51. try
  52. {
  53. App.GPS.OnLocationFound += LocationFound;
  54. App.GPS.OnLocationError += LocationError;
  55. App.Bluetooth.OnScanFinished += ScanFinished;
  56. App.Data.DataChanged += DataChanged;
  57. App.Data.DataRefreshed += DataRefreshed;
  58. GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
  59. GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
  60. MessagingCenter.Subscribe<App>(this, App.MessageOnResume,
  61. (o) =>
  62. {
  63. if (!App.GPS.RecentlyLocated)
  64. App.GPS.GetLocation();
  65. RefreshScreen();
  66. }
  67. );
  68. _timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  69. _employee = App.Data.Employee;
  70. _jobs = App.Data.Jobs;
  71. deviceName = MobileUtils.GetDeviceID();
  72. LoadCacheLists();
  73. InitToolEntryList();
  74. Timer t = new Timer(RecentlyAskedToUpdateTimer, null, 600000, 600000); //user is reminded to update when opening screen after timer of 10 minutes
  75. updateCounter = 1; //user is forced to update after 3rd reminder
  76. Timer t1 = new Timer(RecentlyUpdatedTilesTimer, null, 30000, 30000);
  77. //bluetooth data is allowed to upload once every minute, notifications refreshing is piggybacked to this too
  78. InitNotificationCentre();
  79. firstLoad = false;
  80. //if (GlobalVariables.EmpID == Guid.Parse("40f6ccd9-5272-4b1a-99bf-de7542205aac"))
  81. // RunCustomScript();
  82. NotifyChanges();
  83. }
  84. catch (Exception e)
  85. {
  86. }
  87. NavigationPage.SetHasBackButton(this, false);
  88. }
  89. private void RunCustomScript()
  90. {
  91. }
  92. private void NotifyChanges()
  93. {
  94. string latestChanges = "";
  95. List<string> changes = new List<string>
  96. {
  97. "- Upgrade to Setouts Module"
  98. };
  99. foreach (string s in changes)
  100. {
  101. latestChanges = s + System.Environment.NewLine + latestChanges;
  102. }
  103. Task.Run(() =>
  104. {
  105. if (App.Current.Properties.Count > 0)
  106. {
  107. foreach (string s in App.Current.Properties.Keys)
  108. {
  109. if (s.Contains("NotifiedOfChanges"))
  110. {
  111. if (!s.Equals("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber))
  112. {
  113. App.Current.Properties.Remove(s);
  114. }
  115. }
  116. }
  117. }
  118. if (!App.Current.Properties.ContainsKey("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber))
  119. {
  120. App.Current.Properties.Add("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber, "True");
  121. Thread.Sleep(7500);
  122. Device.BeginInvokeOnMainThread(() =>
  123. {
  124. DisplayAlert("Latest Changes", latestChanges
  125. , "OK");
  126. });
  127. }
  128. });
  129. }
  130. #endregion
  131. #region OnAppearing and Display
  132. protected override void OnAppearing()
  133. {
  134. if (!App.IsUserLoggedIn)
  135. {
  136. Navigation.PopAsync();
  137. return;
  138. }
  139. //if (Application.Current.Properties.ContainsKey("IsSharedDevice"))
  140. //{
  141. // if (Application.Current.Properties["IsSharedDevice"].Equals("True"))
  142. // {
  143. // bSharedDevice = true;
  144. // if (!bSharedDeviceFirstLoad)
  145. // {
  146. // App.LogoutUser();
  147. // Navigation.PopToRootAsync();
  148. // }
  149. // else
  150. // {
  151. // bSharedDeviceFirstLoad = false;
  152. // clockOnButton.IsEnabled = false;
  153. // clockOnButton.Text = "Shared Device";
  154. // clockOnButton.BackgroundColor = Color.CornflowerBlue;
  155. // CurrentLocation.IsVisible = false;
  156. // jobBtn.IsVisible = false;
  157. // addNoteBtn.IsVisible = false;
  158. // Grid.SetRowSpan(flexLayoutScrollView, 3);
  159. // Grid.SetRow(flexLayoutScrollView, 1);
  160. // Timer t = new Timer(AutoLogoutUser, null, 600000, Timeout.Infinite);
  161. // }
  162. // }
  163. // else
  164. // bSharedDevice = false;
  165. //}
  166. //getting strange results from .IsAllowed
  167. //if (!PRSSecurity.IsAllowed<CanBypassTimeBench>())
  168. //{
  169. // clockOnButton.IsVisible = false;
  170. // jobBtn.IsVisible = false;
  171. // addNoteBtn.IsVisible = false;
  172. // CurrentLocation.IsVisible = false;
  173. // row0.Height = 0;
  174. // row1.Height = 0;
  175. // row2.Height = 0;
  176. // ForceLayout();
  177. //}
  178. if (!firstLoad)
  179. RefreshScreen();
  180. Task.Run(async () =>
  181. {
  182. bool isLatest = true;
  183. try
  184. {
  185. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  186. }
  187. catch (Exception eLatest)
  188. {
  189. if (!recentlyAskedToUpdate)
  190. {
  191. Device.BeginInvokeOnMainThread(() =>
  192. {
  193. });
  194. recentlyAskedToUpdate = true;
  195. }
  196. string s = eLatest.Message;
  197. }
  198. if (!isLatest)
  199. {
  200. if (!recentlyAskedToUpdate)
  201. {
  202. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  203. if (updateCounter < 3)
  204. {
  205. Device.BeginInvokeOnMainThread(async () =>
  206. {
  207. string chosenOption = await DisplayActionSheet(String.Format("Version {0} Available. Update now?", latestVersionNumber), "You will be reminded again in 10 minutes.", null, "Yes", "No");
  208. switch (chosenOption)
  209. {
  210. case "No":
  211. break;
  212. case "Cancel":
  213. break;
  214. case "Yes":
  215. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  216. break;
  217. default:
  218. break;
  219. }
  220. });
  221. }
  222. else if (updateCounter >= 3)
  223. {
  224. Device.BeginInvokeOnMainThread(() =>
  225. {
  226. DisplayAlert(String.Format("Version {0} Available", latestVersionNumber), "Please update your software to the latest version.", "OK")
  227. .ContinueWith((Task task) =>
  228. {
  229. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  230. });
  231. });
  232. }
  233. recentlyAskedToUpdate = true;
  234. updateCounter++;
  235. }
  236. }
  237. });
  238. base.OnAppearing();
  239. }
  240. private void RecentlyAskedToUpdateTimer(object o)
  241. {
  242. recentlyAskedToUpdate = false;
  243. }
  244. private void AutoLogoutUser(object o)
  245. {
  246. App.LogoutUser();
  247. Navigation.PopToRootAsync();
  248. }
  249. private void RefreshScreen()
  250. {
  251. //if (bSharedDevice)
  252. // return;
  253. try
  254. {
  255. Device.BeginInvokeOnMainThread(() =>
  256. {
  257. homeScreenGrid.RaiseChild(CurrentLocation);
  258. bBusy = true;
  259. if (GlobalVariables.EmpID == Guid.Empty)
  260. {
  261. GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
  262. GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
  263. }
  264. clockOnButton.IsEnabled = false;
  265. bool PRSReady = (App.Data.Employee != null) && (App.Data.TimeSheets != null);
  266. bool GateReady = CheckLocation();
  267. CurrentLocation.Text = DisplayAddress();
  268. if (CurrentLocation.Text.Contains("ERROR"))
  269. CurrentLocation.Text = "Unknown Address";
  270. Title = null;
  271. Title = App.Data.Employee != null ? App.Data.Employee.Name : "";
  272. CoreRow timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault();
  273. clockOnButton.Text = PRSReady && GateReady ? timesheet == null ? "CLOCK ON" : "CLOCK OFF" : "PLEASE WAIT";
  274. clockOnButton.IsEnabled = PRSReady && GateReady;
  275. clockOnButton.BackgroundColor = PRSReady && GateReady ? timesheet == null ? Color.FromHex("#e6e6fa") : Color.FromHex("#15C7C1") : Color.Gainsboro;
  276. clockOnButton.BorderColor = PRSReady && GateReady ? timesheet == null ? Color.Black : Color.FromHex("#15C7C1") : Color.Gainsboro;
  277. if (clockOnButton.Text == "CLOCK OFF")
  278. {
  279. addNoteBtn.IsEnabled = true;
  280. if (GlobalVariables.JobsLoaded)
  281. jobBtn.IsEnabled = true;
  282. }
  283. else
  284. {
  285. addNoteBtn.IsEnabled = false;
  286. jobBtn.IsEnabled = false;
  287. }
  288. if (firstLoad)
  289. RefreshJobFromTimeSheet(timesheet);
  290. homeScreenGrid.RaiseChild(CurrentLocation);
  291. firstLoad = false;
  292. bBusy = false;
  293. });
  294. }
  295. catch (Exception e)
  296. {
  297. }
  298. //CurrentLocation.IsEnabled = PRSReady && GateReady;
  299. }
  300. private void RefreshJobFromTimeSheet(CoreRow timesheet)
  301. {
  302. Guid jobid = timesheet == null ? Guid.Empty : timesheet.Get<TimeSheet, Guid>(x => x.JobLink.ID);
  303. if (!jobid.Equals(Guid.Empty))
  304. {
  305. jobBtn.Text = String.Format("{0}: {1}", timesheet.Get<TimeSheet, String>(x => x.JobLink.JobNumber), timesheet.Get<TimeSheet, String>(x => x.JobLink.Name));
  306. _job.ID = timesheet == null ? Guid.Empty : timesheet.Get<TimeSheet, Guid>(x => x.JobLink.ID);
  307. _job.JobNumber = timesheet == null ? String.Empty : timesheet.Get<TimeSheet, String>(x => x.JobLink.JobNumber);
  308. _job.Name = timesheet == null ? String.Empty : timesheet.Get<TimeSheet, String>(x => x.JobLink.Name);
  309. }
  310. else
  311. {
  312. jobBtn.Text = "No Job Selected";
  313. _job = new Job();
  314. }
  315. }
  316. #endregion
  317. #region Clock on/off
  318. private void DataChanged(object sender, Type type, Exception e)
  319. {
  320. //if (bSharedDevice)
  321. // return;
  322. Device.BeginInvokeOnMainThread(() =>
  323. {
  324. if (e != null)
  325. {
  326. //DisplayAlert("Connection error with server - double check your connection", e.Message, "OK");
  327. }
  328. else
  329. RefreshScreen();
  330. });
  331. }
  332. private void DataRefreshed()
  333. {
  334. //if (bSharedDevice)
  335. // return;
  336. Device.BeginInvokeOnMainThread(() => { RefreshScreen(); });
  337. }
  338. bool bBusy = false;
  339. async void ClockOnOff_Clicked(object sender, System.EventArgs e)
  340. {
  341. if (bBusy)
  342. return;
  343. bBusy = true;
  344. string chosenOption = "Continue";
  345. if (clockOnButton.Text == "CLOCK OFF")
  346. {
  347. chosenOption = await DisplayActionSheet("Clock off?", "Cancel", null, "Continue", "Cancel");
  348. }
  349. switch (chosenOption)
  350. {
  351. case "Continue":
  352. break;
  353. case "Cancel":
  354. bBusy = false;
  355. return;
  356. break;
  357. default:
  358. bBusy = false;
  359. return;
  360. break;
  361. }
  362. if (clockOnButton.Text == "CLOCK ON" && clockedOffInLast5Seconds)
  363. {
  364. bBusy = false;
  365. return;
  366. }
  367. try
  368. {
  369. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  370. {
  371. InABox.Core.Location here = new InABox.Core.Location()
  372. {
  373. Latitude = App.GPS.Latitude,
  374. Longitude = App.GPS.Longitude,
  375. Timestamp = DateTime.Now
  376. };
  377. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  378. if (timesheet != null)
  379. {
  380. if (timesheet.ID != Guid.Empty)
  381. {
  382. if (ZeroLengthTimesheet())
  383. {
  384. bUpdatingTimesheet = true;
  385. new Client<TimeSheet>().Delete(timesheet, "Deleted due to zero duration timesheet");
  386. App.Data.TimeSheets.Rows.Clear();
  387. }
  388. else
  389. {
  390. FinishTimeSheet(timesheet, here);
  391. }
  392. }
  393. }
  394. else
  395. {
  396. Guid jobid = Guid.Empty;
  397. String jobnumber = "";
  398. String jobname = "";
  399. if (!App.Data.CanBypassGates)
  400. {
  401. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  402. if (row != null)
  403. {
  404. jobid = row.Get<JobTracker, Guid>(x => x.JobLink.ID);
  405. jobnumber = row.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  406. jobname = row.Get<JobTracker, String>(x => x.JobLink.Name);
  407. }
  408. CreateTimeSheet(jobid, jobnumber, jobname, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  409. }
  410. else
  411. {
  412. if ((!App.GPS.Latitude.Equals(0.0F)) && (!App.GPS.Longitude.Equals(0.0F)))
  413. {
  414. ChooseNearbyJob(here);
  415. }
  416. }
  417. }
  418. RefreshScreen();
  419. }
  420. }
  421. catch (Exception e2)
  422. {
  423. }
  424. bBusy = false;
  425. }
  426. #endregion
  427. #region Bluetooth
  428. private async void UploadTiles()
  429. {
  430. try
  431. {
  432. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  433. return;
  434. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  435. return;
  436. if (bRecentlyUpdatedTiles)
  437. return;
  438. bRecentlyUpdatedTiles = true;
  439. await Task.Run(() =>
  440. {
  441. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  442. curlocation.Timestamp = DateTime.Now;
  443. List<GPSTrackerLocation> trackersToUpdate = new List<GPSTrackerLocation>();
  444. foreach (String id in App.Bluetooth.DetectedBlueToothMACAddresses)
  445. {
  446. GPSTracker tracker = GlobalVariables.GPSTrackerCache.Find(x => x.DeviceID.Equals(id));
  447. bool stale = tracker.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
  448. bool moved = tracker.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  449. if (stale || moved)
  450. {
  451. GlobalVariables.GPSTrackerCache.Remove(tracker);
  452. tracker.Location = curlocation;
  453. GlobalVariables.GPSTrackerCache.Add(tracker);
  454. //cache is updated
  455. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  456. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  457. gpsTrackerLocation.Location.Timestamp = tracker.Location.Timestamp;
  458. gpsTrackerLocation.Location = curlocation;
  459. trackersToUpdate.Add(gpsTrackerLocation);
  460. }
  461. }
  462. if (trackersToUpdate.Any())
  463. {
  464. if (ClientFactory.UserGuid != Guid.Empty)
  465. new Client<GPSTrackerLocation>().Save(trackersToUpdate, "Updating Bluetooth Device Locations");
  466. }
  467. App.Bluetooth.DetectedBlueToothMACAddresses.Clear();
  468. }
  469. );
  470. }
  471. catch (Exception e)
  472. {
  473. }
  474. //if ((master != null) && (master.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0))))
  475. //{
  476. // GPSTrackerLocation device = new GPSTrackerLocation();
  477. // device.DeviceID = MobileUtils.GetDeviceID();
  478. // device.Location.Latitude = App.GPS.Latitude;
  479. // device.Location.Longitude = App.GPS.Longitude;
  480. // device.Location.Timestamp = DateTime.Now;
  481. // locations.Add(device);
  482. // //device.BatteryLevel = ((double)CrossBattery.Current.RemainingChargePercent);
  483. // //new Client<GPSTrackerLocation>().Save(device, "Updating Device Location"); //, SaveTrackerCallback);
  484. //}
  485. #region OLD
  486. //for (int i = 0; i < App.Bluetooth.Devices.Length; i++)
  487. //{
  488. // String id = App.Bluetooth.Devices[i];
  489. // int level = App.Bluetooth.BatteryLevels[i];
  490. // var btmaster = trackers.FirstOrDefault(x => x.DeviceID.Equals(id));
  491. // if ((btmaster != null) && (!locations.Any(x => x.DeviceID.Equals(btmaster.DeviceID))))
  492. // {
  493. // bool stale = btmaster.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
  494. // bool moved = btmaster.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  495. // if (stale || moved)
  496. // {
  497. // GPSTrackerLocation location = new GPSTrackerLocation();
  498. // location.DeviceID = id;
  499. // location.Location.Latitude = App.GPS.Latitude;
  500. // location.Location.Longitude = App.GPS.Longitude;
  501. // location.Location.Timestamp = DateTime.Now;
  502. // location.BatteryLevel = level;
  503. // locations.Add(location);
  504. // }
  505. // }
  506. // //new Client<GPSTrackerLocation>().Save(location, "Found Kontakt Device"); //, SaveTrackerCallback);
  507. //}
  508. //if (locations.Any())
  509. // new Client<GPSTrackerLocation>().Save(locations, "Updating Bluetooth Device Locations", (o, e) => { });
  510. #endregion
  511. }
  512. private void RecentlyUpdatedTilesTimer(object o)
  513. {
  514. bRecentlyUpdatedTiles = false;
  515. App.Data.Refresh(true);
  516. SearchForNewNotifications();
  517. }
  518. private void LocationFound(LocationServices sender)
  519. {
  520. //if (bSharedDevice)
  521. // return;
  522. if (App.Bluetooth.RecentlyScanned)
  523. UploadTiles();
  524. try
  525. {
  526. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  527. if (timesheet != null)
  528. {
  529. if (timesheet.StartLocation.Latitude.Equals(0.0F) && timesheet.StartLocation.Longitude.Equals(0.0F))
  530. {
  531. timesheet.StartLocation.Latitude = sender.Latitude;
  532. timesheet.StartLocation.Longitude = sender.Longitude;
  533. timesheet.StartLocation.Timestamp = sender.TimeStamp;
  534. timesheet.Address = sender.Address;
  535. new Client<TimeSheet>().Save(timesheet, "Updating Timesheet with GPS Coordinates", (o, e) => { });
  536. }
  537. }
  538. if (!string.IsNullOrWhiteSpace(matchedDeviceName))
  539. {
  540. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  541. curlocation.Timestamp = DateTime.Now;
  542. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  543. gpsTrackerLocation.DeviceID = matchedDeviceName;
  544. gpsTrackerLocation.Location.Timestamp = curlocation.Timestamp;
  545. gpsTrackerLocation.Location = curlocation;
  546. new Client<GPSTrackerLocation>().Save(gpsTrackerLocation, "Updated company device location from Timebench");
  547. }
  548. Device.BeginInvokeOnMainThread(() =>
  549. {
  550. RefreshScreen();
  551. });
  552. }
  553. catch { }
  554. }
  555. private void LocationError(LocationServices sebder, Exception error)
  556. {
  557. }
  558. private void ScanFinished(Bluetooth sender)
  559. {
  560. try
  561. {
  562. //if (bSharedDevice)
  563. // return;
  564. Device.BeginInvokeOnMainThread(() =>
  565. {
  566. RefreshScreen();
  567. //if (Button2.BackgroundColor == Color.WhiteSmoke)
  568. // Button2.BackgroundColor = Color.Red;
  569. //else
  570. // Button2.BackgroundColor = Color.WhiteSmoke;
  571. });
  572. if (App.GPS.RecentlyLocated)
  573. UploadTiles();
  574. }
  575. catch { }
  576. }
  577. #endregion
  578. #region Utilities
  579. private void InitNotificationCentre()
  580. {
  581. try
  582. {
  583. LocalNotificationCenter.Current.NotificationActionTapped += (Plugin.LocalNotification.EventArgs.NotificationActionEventArgs e) =>
  584. {
  585. string data = e.Request.ReturningData;
  586. int index = data.IndexOf("$");
  587. Guid ID = Guid.Parse(data.Remove(index));
  588. string type = data.Substring(index + 1);
  589. if (type == "Comal.Classes.Kanban")
  590. {
  591. Device.BeginInvokeOnMainThread(() =>
  592. {
  593. AddEditTask taskPage = new AddEditTask(ID);
  594. Navigation.PushAsync(taskPage);
  595. });
  596. }
  597. };
  598. }
  599. catch { }
  600. }
  601. private async void SearchForNewNotifications()
  602. {
  603. //notifications poll reliably in the background for Anroid, unreliable for iOS due to difficulty with cross-platform plugins for notifications
  604. try
  605. {
  606. await Task.Run(() =>
  607. {
  608. if (ClientFactory.UserGuid != Guid.Empty)
  609. {
  610. CoreTable table = new Client<Notification>().Query
  611. (new Filter<Notification>(x => x.Employee.UserLink.ID).IsEqualTo(ClientFactory.UserGuid).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  612. new Columns<Notification>(
  613. x => x.ID, //0
  614. x => x.Sender.Name, //1
  615. x => x.Title, //2
  616. x => x.Created, //3
  617. x => x.Description, //4
  618. x => x.EntityType, //5
  619. x => x.EntityID //6
  620. )
  621. );
  622. if (NumberOfNotfications == table.Rows.Count()) //no new notifications or none present at all
  623. return;
  624. else //new notifications or previous notifications have now been dismissed
  625. {
  626. NumberOfNotfications = table.Rows.Count();
  627. RefreshOnNotificationsChange();
  628. CheckNotificationsPushed(table);
  629. }
  630. }
  631. });
  632. }
  633. catch { }
  634. }
  635. private void RefreshOnNotificationsChange()
  636. {
  637. try
  638. {
  639. int index = toolEntries.FindIndex(x => x.Text.Equals("Notifications"));
  640. toolEntries.RemoveAt(index);
  641. string notificationsString = "";
  642. if (NumberOfNotfications != 0)
  643. {
  644. notificationsString = NumberOfNotfications.ToString();
  645. }
  646. ToolEntry Notifications = new ToolEntry(notificationsString)
  647. {
  648. Text = "Notifications",
  649. Image = "notifications"
  650. };
  651. Notifications.OnTapped += (async (object sender, EventArgs e) =>
  652. {
  653. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  654. {
  655. NotificationList notificationList = new NotificationList();
  656. notificationList.NotificationsClosed += (n) =>
  657. {
  658. NumberOfNotfications = n;
  659. RefreshOnNotificationsChange();
  660. };
  661. Navigation.PushAsync(notificationList);
  662. }
  663. });
  664. toolEntries.Insert(index, Notifications);
  665. Device.BeginInvokeOnMainThread(() =>
  666. {
  667. flexLayout.Children.RemoveAt(index);
  668. flexLayout.Children.Insert(index, toolEntries[index]);
  669. });
  670. }
  671. catch { }
  672. }
  673. private void CheckNotificationsPushed(CoreTable table)
  674. {
  675. try
  676. {
  677. if (!Application.Current.Properties.ContainsKey("LastPushedNotifications"))
  678. {
  679. Application.Current.Properties.Add("LastPushedNotifications", DateTime.Now);
  680. }
  681. DateTime lastPushed = DateTime.Parse(Application.Current.Properties["LastPushedNotifications"].ToString());
  682. List<NotificationShell> toNotify = new List<NotificationShell>();
  683. foreach (CoreRow row in table.Rows)
  684. {
  685. List<object> list = row.Values;
  686. DateTime created = DateTime.Parse(list[3].ToString());
  687. if (created > new DateTime(2022, 8, 22)) // prevent spam from buildup of old notifications before this is released
  688. {
  689. if (created > lastPushed)
  690. {
  691. if (list[1] == null) list[1] = "";
  692. if (list[2] == null) list[2] = "";
  693. if (list[3] == null) list[3] = DateTime.MinValue;
  694. if (list[4] == null) list[4] = "";
  695. if (list[5] == null) list[5] = "";
  696. if (list[6] == null) list[6] = Guid.Empty;
  697. NotificationShell shell = new NotificationShell
  698. {
  699. ID = Guid.Parse(list[0].ToString()),
  700. Sender = list[1].ToString(),
  701. Title = list[2].ToString(),
  702. Created = DateTime.Parse(list[3].ToString()),
  703. EntityType = list[5].ToString(),
  704. EntityID = Guid.Parse(list[6].ToString())
  705. };
  706. toNotify.Add(shell); //add notification to be pushed
  707. }
  708. }
  709. }
  710. if (toNotify.Count > 0)
  711. PushNotificationsAsync(toNotify);
  712. }
  713. catch { }
  714. }
  715. private async Task PushNotificationsAsync(List<NotificationShell> shells)
  716. {
  717. try
  718. {
  719. int count = 1;
  720. foreach (NotificationShell shell in shells)
  721. {
  722. var notification = new NotificationRequest
  723. {
  724. BadgeNumber = 1,
  725. Description = shell.Title,
  726. Title = "New PRS Notification: ",
  727. ReturningData = shell.EntityID.ToString() + "$" + shell.EntityType,
  728. NotificationId = count,
  729. };
  730. count++;
  731. NotificationImage img = new NotificationImage();
  732. img.ResourceName = "icon16.png";
  733. notification.Image = img;
  734. await LocalNotificationCenter.Current.Show(notification);
  735. //if (Device.RuntimePlatform.Equals(Device.iOS))
  736. //{
  737. // var content = new UNMutableNotificationContent();
  738. // content.Title = "New PRS Notification: ";
  739. // content.Subtitle = shell.Title;
  740. // content.Body = "";
  741. // content.Badge = 1;
  742. // var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
  743. // var requestID = "request";
  744. // var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
  745. // UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
  746. // {
  747. // if (err != null)
  748. // {
  749. // Do something with error...
  750. // }
  751. // });
  752. //}
  753. }
  754. Application.Current.Properties["LastPushedNotifications"] = DateTime.Now;
  755. }
  756. catch { }
  757. }
  758. private void StartMidnightTimeSheetTimer()
  759. {
  760. midnightTimerOn = true;
  761. int msUntilMidnight = (int)(oneSecondBeforeMidnight - DateTime.Now).TotalMilliseconds;
  762. Timer midnightTimer = new Timer(MidnightTimerCallback, null, msUntilMidnight, Timeout.Infinite);
  763. }
  764. private void MidnightTimerCallback(object o)
  765. {
  766. try
  767. {
  768. //if (bSharedDevice)
  769. // return;
  770. if (midnightTimerOn)
  771. {
  772. if (clockOnButton.Text == "CLOCK OFF")
  773. {
  774. InABox.Core.Location here = new InABox.Core.Location()
  775. {
  776. Latitude = App.GPS.Latitude,
  777. Longitude = App.GPS.Longitude,
  778. Timestamp = DateTime.Now
  779. };
  780. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  781. if (timesheet != null)
  782. {
  783. if (timesheet.ID != Guid.Empty)
  784. {
  785. if (ZeroLengthTimesheet())
  786. {
  787. bUpdatingTimesheet = true;
  788. new Client<TimeSheet>().Delete(timesheet, "Deleted due to zero duration timesheet");
  789. App.Data.TimeSheets.Rows.Clear();
  790. }
  791. else
  792. {
  793. timesheet.Finish = new TimeSpan(23, 59, 59);
  794. timesheet.FinishLocation = here;
  795. bUpdatingTimesheet = true;
  796. new Client<TimeSheet>().Save(timesheet, "Auto Close timesheet at Midnight");
  797. App.Data.TimeSheets.Rows.Clear();
  798. Guid jobid = Guid.Empty;
  799. String jobnumber = "";
  800. String jobname = "";
  801. if (!App.Data.CanBypassGates)
  802. {
  803. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  804. if (row != null)
  805. {
  806. jobid = row.Get<JobTracker, Guid>(x => x.JobLink.ID);
  807. jobnumber = row.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  808. jobname = row.Get<JobTracker, String>(x => x.JobLink.Name);
  809. }
  810. CreateTimeSheet(jobid, jobnumber, jobname, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  811. }
  812. else
  813. {
  814. if ((!App.GPS.Latitude.Equals(0.0F)) && (!App.GPS.Longitude.Equals(0.0F)))
  815. {
  816. ChooseNearbyJob(here);
  817. }
  818. }
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. catch { }
  826. }
  827. private void FinishTimeSheet(TimeSheet timesheet, InABox.Core.Location here)
  828. {
  829. try
  830. {
  831. TimeSpan tod = DateTime.Now - DateTime.Today;
  832. timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
  833. timesheet.FinishLocation = here;
  834. bUpdatingTimesheet = true;
  835. new Client<TimeSheet>().Save(timesheet, "Clocking Off");
  836. App.Data.TimeSheets.Rows.Clear();
  837. midnightTimerOn = false;
  838. Timer last60Seconds = new Timer(Last60SecondsTimerCallBack, null, 5000, Timeout.Infinite);
  839. clockedOffInLast5Seconds = true;
  840. }
  841. catch { }
  842. }
  843. private void Last60SecondsTimerCallBack(object o)
  844. {
  845. clockedOffInLast5Seconds = false;
  846. }
  847. private async void ChooseNearbyJob(InABox.Core.Location here)
  848. {
  849. try
  850. {
  851. JobShell selectedJob = new JobShell();
  852. Dictionary<string, JobShell> nearbyJobs = new Dictionary<string, JobShell>();
  853. foreach (CoreRow row in App.Data.Jobs.Rows)
  854. {
  855. InABox.Core.Location jobLocation = new InABox.Core.Location() { Latitude = row.Get<Job, double>(X => X.SiteAddress.Location.Latitude), Longitude = row.Get<Job, double>(X => X.SiteAddress.Location.Longitude) };
  856. double distance = here.DistanceTo(jobLocation, UnitOfLength.Kilometers);
  857. if (distance < 1.0F)
  858. {
  859. JobShell jobShell = new JobShell();
  860. jobShell.ID = row.Get<Job, Guid>(X => X.ID);
  861. jobShell.JobNumber = row.Get<Job, String>(x => x.JobNumber);
  862. jobShell.Name = row.Get<Job, String>(x => x.Name);
  863. jobShell.DisplayName = jobShell.JobNumber + " " + jobShell.Name;
  864. nearbyJobs.Add(jobShell.DisplayName, jobShell);
  865. }
  866. }
  867. if (nearbyJobs.Count > 1)
  868. {
  869. string[] array = nearbyJobs.Keys.ToArray();
  870. string chosenOption = await DisplayActionSheet("Choose job site", "Cancel", null, array);
  871. if (string.IsNullOrEmpty(chosenOption) || chosenOption.Equals("Cancel"))
  872. {
  873. CreateTimeSheet(selectedJob.ID, selectedJob.JobNumber, selectedJob.Name, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  874. return;
  875. }
  876. else
  877. {
  878. selectedJob = nearbyJobs[chosenOption];
  879. }
  880. }
  881. else if (nearbyJobs.Count == 1)
  882. {
  883. selectedJob = nearbyJobs.Values.First();
  884. }
  885. CreateTimeSheet(selectedJob.ID, selectedJob.JobNumber, selectedJob.Name, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  886. }
  887. catch { }
  888. }
  889. void AddNote_Tapped(System.Object sender, System.EventArgs e)
  890. {
  891. try
  892. {
  893. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  894. if (timesheet == null)
  895. return;
  896. var notepage = new NotePage(timesheet);
  897. Navigation.PushAsync(notepage);
  898. }
  899. catch { }
  900. }
  901. private void JobBtn_Tapped(object sender, EventArgs e)
  902. {
  903. try
  904. {
  905. JobSelectionPage jobSelectionPage = new JobSelectionPage();
  906. jobSelectionPage.OnItemSelected += (() =>
  907. {
  908. _job.ID = jobSelectionPage.Job.ID;
  909. _job.JobNumber = jobSelectionPage.Job.JobNumber;
  910. _job.Name = jobSelectionPage.Job.Name;
  911. JobPage_OnItemSelected(jobSelectionPage.Job);
  912. });
  913. Navigation.PushAsync(jobSelectionPage);
  914. }
  915. catch { }
  916. }
  917. private void JobPage_OnItemSelected(JobShell job)
  918. {
  919. try
  920. {
  921. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  922. if (timesheet == null)
  923. return;
  924. String auditmessage = String.Format("Changed Selected Job to: {0}: {1}", timesheet.JobLink.JobNumber, timesheet.JobLink.Name);
  925. if (ZeroLengthTimesheet())
  926. {
  927. timesheet.JobLink.ID = job.ID;
  928. timesheet.JobLink.JobNumber = job.JobNumber;
  929. timesheet.JobLink.Name = job.Name;
  930. bUpdatingTimesheet = true;
  931. new Client<TimeSheet>().Save(timesheet, auditmessage);
  932. Device.BeginInvokeOnMainThread(() =>
  933. {
  934. if (timesheet.JobLink.ID != Guid.Empty)
  935. {
  936. jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
  937. }
  938. else
  939. {
  940. jobBtn.Text = "No Job Selected";
  941. }
  942. });
  943. }
  944. else
  945. {
  946. Guid activityid = timesheet.ActivityLink.ID;
  947. String activitycode = timesheet.ActivityLink.Code;
  948. String activitydescription = timesheet.ActivityLink.Description;
  949. InABox.Core.Location here = new InABox.Core.Location()
  950. {
  951. Latitude = App.GPS.Latitude,
  952. Longitude = App.GPS.Longitude,
  953. Timestamp = DateTime.Now
  954. };
  955. TimeSpan tod = DateTime.Now - DateTime.Today;
  956. timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
  957. timesheet.FinishLocation = here;
  958. new Client<TimeSheet>().Save(timesheet, "Changing Job");
  959. CreateTimeSheet(
  960. job.ID,
  961. job.JobNumber,
  962. job.Name,
  963. activityid,
  964. activitycode,
  965. activitydescription,
  966. here,
  967. App.GPS.Address,
  968. auditmessage
  969. );
  970. }
  971. RefreshScreen();
  972. }
  973. catch { }
  974. }
  975. private bool CheckTimeSheetAgainstGates(TimeSheet timesheet)
  976. {
  977. DateTime now = DateTime.Now;
  978. //var timesheet = CurrentTimeSheet();
  979. //Can't confirm if there is no timesheet
  980. if (timesheet == null)
  981. return false;
  982. // Can't confirm if there are no devices
  983. if (App.Bluetooth.Devices.Length == 0)
  984. return false;
  985. if (App.Data.Gates == null)
  986. return false;
  987. long tsTicks = timesheet.Date.Add(timesheet.Start).Ticks;
  988. long btTicks = App.Bluetooth.TimeStamp.Ticks;
  989. if (Math.Abs(tsTicks - btTicks) > new TimeSpan(0, 2, 0).Ticks)
  990. return false;
  991. CoreRow firstgate = null;
  992. List<String> gates = new List<string>();
  993. // Scan every located d
  994. foreach (var device in App.Bluetooth.Devices)
  995. {
  996. CoreRow gate = App.Data.Gates?.Rows.FirstOrDefault(r => r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID) == device);
  997. if (gate != null)
  998. {
  999. if ((gate.Get<JobTracker, bool>(x => x.IsJobSite) == true) && (firstgate == null))
  1000. firstgate = gate;
  1001. gates.Add(gate.Get<JobTracker, String>(x => x.Gate));
  1002. }
  1003. }
  1004. if (gates.Any())
  1005. {
  1006. timesheet.Gate = String.Join(", ", gates.OrderBy(x => x));
  1007. if (firstgate != null)
  1008. {
  1009. timesheet.JobLink.ID = firstgate.Get<JobTracker, Guid>(x => x.JobLink.ID);
  1010. timesheet.JobLink.JobNumber = firstgate.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  1011. timesheet.JobLink.Name = firstgate.Get<JobTracker, String>(x => x.JobLink.Name);
  1012. }
  1013. return true;
  1014. //new Client<TimeSheet>().Save(timesheet, "Confirmed Gate Entry by Bluetooth Tracker", (o, e) => { });
  1015. }
  1016. return false;
  1017. }
  1018. private bool ZeroLengthTimesheet()
  1019. {
  1020. try
  1021. {
  1022. if (App.Data.TimeSheets == null)
  1023. return true;
  1024. CoreRow row = App.Data.TimeSheets.Rows.FirstOrDefault();
  1025. if (row == null)
  1026. return true;
  1027. String notes = row.Get<TimeSheet, String>(x => x.Notes);
  1028. if (!String.IsNullOrWhiteSpace(notes))
  1029. return false;
  1030. DateTime date = row.Get<TimeSheet, DateTime>(x => x.Date);
  1031. TimeSpan start = row.Get<TimeSheet, TimeSpan>(x => x.Start);
  1032. if (date.Equals(DateTime.Today))
  1033. {
  1034. TimeSpan tod = DateTime.Now - DateTime.Today;
  1035. var diff = (tod - start).TotalSeconds;
  1036. if (Math.Abs(diff) < 120.0F)
  1037. return true;
  1038. }
  1039. }
  1040. catch { }
  1041. return false;
  1042. }
  1043. private async void CreateTimeSheet(Guid jobid, string jobnumber, String jobname, Guid activityid, String activitycode, String activitydescription, InABox.Core.Location location, String address, String auditmessage)
  1044. {
  1045. try
  1046. {
  1047. var timesheet = new TimeSheet();
  1048. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  1049. {
  1050. timesheet.EmployeeID = App.Data.Employee.ID;
  1051. timesheet.EmployeeLink.ID = App.Data.Employee.ID;
  1052. timesheet.Date = DateTime.Today;
  1053. TimeSpan tod = DateTime.Now - DateTime.Today;
  1054. tod = new TimeSpan(tod.Hours, tod.Minutes, 0);
  1055. timesheet.Start = tod;
  1056. timesheet.StartLocation = location;
  1057. timesheet.JobLink.ID = jobid;
  1058. timesheet.JobLink.JobNumber = jobnumber;
  1059. timesheet.JobLink.Name = jobname;
  1060. timesheet.ActivityLink.ID = activityid;
  1061. timesheet.ActivityLink.Code = activitycode;
  1062. timesheet.ActivityLink.Description = activitydescription;
  1063. timesheet.Address = address;
  1064. timesheet.SoftwareVersion = MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString;
  1065. //if (ClientFactory.IsAllowed<AllowTimeSheetRollover>()) CheckTimeSheetAgainstGates(timesheet);
  1066. bUpdatingTimesheet = true;
  1067. new Client<TimeSheet>().Save(timesheet, auditmessage);
  1068. if (timesheet.ID == Guid.Empty)
  1069. {
  1070. DisplayAlert("Error creating new timesheet", "Please check your connection and try again", "OK");
  1071. return;
  1072. }
  1073. StartMidnightTimeSheetTimer();
  1074. // Don't Save Completed Timesheets!
  1075. App.Data.TimeSheets.Rows.Clear();
  1076. if (timesheet.Finish.Ticks == 0L)
  1077. {
  1078. CoreRow row = App.Data.TimeSheets.NewRow();
  1079. App.Data.TimeSheets.LoadRow(row, timesheet);
  1080. App.Data.TimeSheets.Rows.Add(row);
  1081. }
  1082. }
  1083. Device.BeginInvokeOnMainThread(() =>
  1084. {
  1085. if (timesheet.JobLink.ID != Guid.Empty)
  1086. {
  1087. jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
  1088. }
  1089. else
  1090. {
  1091. jobBtn.Text = "No Job Selected";
  1092. }
  1093. });
  1094. }
  1095. catch { }
  1096. }
  1097. private bool CheckLocation()
  1098. {
  1099. try
  1100. {
  1101. if (App.Data.CanBypassGates)
  1102. {
  1103. if (App.GPS.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 5, 0)))
  1104. return true;
  1105. else
  1106. return false;
  1107. }
  1108. if (App.Data.Gates == null)
  1109. return false;
  1110. if (App.Bluetooth.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 2, 0)))
  1111. return false;
  1112. if (!App.Bluetooth.Devices.Any())
  1113. return false;
  1114. return App.Data.Gates.Rows.Any(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  1115. }
  1116. catch
  1117. {
  1118. return true;
  1119. }
  1120. }
  1121. private String DisplayAddress()
  1122. {
  1123. try
  1124. {
  1125. bool PRSReady = App.Data.Employee != null; // && (TimeSheet != null); // && (Activities != null);
  1126. if (!PRSReady)
  1127. return "Retrieving Data";
  1128. if (App.Data.CanBypassGates)
  1129. {
  1130. if (App.GPS.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0)))
  1131. {
  1132. App.GPS.GetLocation(true);
  1133. return "Searching for GPS";
  1134. }
  1135. else
  1136. return App.GPS.Address;
  1137. }
  1138. else
  1139. {
  1140. // Hmm.. this can/should be simplified
  1141. // if in range of a gate
  1142. // Show Gate Description
  1143. // else
  1144. // "Looking for Gate"
  1145. if ((App.Data.Gates != null) && (App.Bluetooth.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 2, 0))))
  1146. {
  1147. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  1148. if (row != null)
  1149. return row.Get<JobTracker, String>(x => x.Gate);
  1150. //else if ((CurrentTimeSheet() != null) && (!GPS.TimeStamp.IsEmpty()))
  1151. // return GPS.Address;
  1152. else
  1153. return "Looking for Gate";
  1154. }
  1155. //else if ((CurrentTimeSheet() != null) && (GPS.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 2, 0))))
  1156. // return GPS.Address;
  1157. else
  1158. return "Looking for Gate";
  1159. }
  1160. }
  1161. catch
  1162. {
  1163. return "Address error";
  1164. }
  1165. }
  1166. #region Background Loading
  1167. private void LoadCacheLists()
  1168. {
  1169. GlobalVariables.ProductsLoaded = false;
  1170. GlobalVariables.JobsLoaded = false;
  1171. GlobalVariables.GetXamarinWidth();
  1172. LoadJobShells();
  1173. LoadEmployeeShells();
  1174. LoadProducts();
  1175. LoadCompanyDevices();
  1176. LoadBlueToothAddresses();
  1177. //LoadHRToDos(); to be uncommented when ready for roll out
  1178. }
  1179. private void LoadCompanyDevices()
  1180. {
  1181. Task.Run(() =>
  1182. {
  1183. if (!string.IsNullOrWhiteSpace(deviceName) && deviceName != "unknown")
  1184. {
  1185. List<Equipment> companyDevices = new List<Equipment>();
  1186. CoreTable table = new Client<Equipment>().Query
  1187. (
  1188. new Filter<Equipment>(x => x.GroupLink.Code).IsEqualTo("DEVICE"),
  1189. new Columns<Equipment>(
  1190. x => x.TrackerLink.DeviceID
  1191. )
  1192. );
  1193. if (table.Rows.Any())
  1194. {
  1195. foreach (CoreRow row in table.Rows)
  1196. {
  1197. List<object> list = row.Values;
  1198. if (list[0].ToString().Equals(deviceName))
  1199. {
  1200. matchedDeviceName = deviceName;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. });
  1206. }
  1207. private async void LoadEmployeeShells()
  1208. {
  1209. await Task.Run(() =>
  1210. {
  1211. List<EmployeeShell> employeeShells = new List<EmployeeShell>();
  1212. List<EmployeeShell> teamEmployeeShells = new List<EmployeeShell>();
  1213. MultiQuery query = new MultiQuery();
  1214. query.Add<Employee>(
  1215. LookupFactory.DefineFilter<Employee>(),
  1216. new Columns<Employee>(x => x.ID)
  1217. .Add(x => x.Code)
  1218. .Add(x => x.Name),
  1219. LookupFactory.DefineSort<Employee>()
  1220. );
  1221. query.Add<Team>(
  1222. LookupFactory.DefineFilter<Team>(),
  1223. new Columns<Team>(x => x.Name),
  1224. new SortOrder<Team>(x => x.Name)
  1225. );
  1226. query.Add<EmployeeTeam>(
  1227. LookupFactory.DefineFilter<EmployeeTeam>(),
  1228. new Columns<EmployeeTeam>(x => x.EmployeeLink.ID)
  1229. .Add(x => x.EmployeeLink.Code)
  1230. .Add(x => x.EmployeeLink.Name)
  1231. .Add(x => x.TeamLink.Name),
  1232. new SortOrder<EmployeeTeam>(x => x.EmployeeLink.Name)
  1233. );
  1234. query.Query();
  1235. CoreTable emps = query.Get<Employee>();
  1236. foreach (var row in emps.Rows)
  1237. {
  1238. employeeShells.Add(
  1239. new EmployeeShell()
  1240. {
  1241. ID = row.Get<Employee, Guid>(x => x.ID),
  1242. Code = row.Get<Employee, String>(x => x.Code),
  1243. Name = row.Get<Employee, String>(x => x.Name),
  1244. TeamName = "All Staff"
  1245. }
  1246. );
  1247. }
  1248. GlobalVariables.TeamNames = query.Get<Team>().Rows.Select(r => r.Get<Team, String>(c => c.Name)).ToList();
  1249. CoreTable members = query.Get<EmployeeTeam>();
  1250. foreach (var row in members.Rows)
  1251. {
  1252. teamEmployeeShells.Add(
  1253. new EmployeeShell()
  1254. {
  1255. ID = row.Get<EmployeeTeam, Guid>(x => x.EmployeeLink.ID),
  1256. Code = row.Get<EmployeeTeam, String>(x => x.EmployeeLink.Code),
  1257. Name = row.Get<EmployeeTeam, String>(x => x.EmployeeLink.Name),
  1258. TeamName = row.Get<EmployeeTeam, String>(x => x.TeamLink.Name)
  1259. }
  1260. );
  1261. }
  1262. GlobalVariables.EmployeeShells = employeeShells;
  1263. GlobalVariables.TeamEmployeeShells = teamEmployeeShells;
  1264. });
  1265. }
  1266. async void LoadJobShells()
  1267. {
  1268. try
  1269. {
  1270. await Task.Run(() =>
  1271. {
  1272. List<JobShell> jobShells = new List<JobShell>();
  1273. CoreTable table = new Client<Job>().Query(
  1274. new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true),
  1275. new Columns<Job>(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color),
  1276. new SortOrder<Job>(x => x.JobNumber)
  1277. );
  1278. foreach (CoreRow row in table.Rows)
  1279. {
  1280. List<object> list = row.Values;
  1281. if (list[0] == null) { list[0] = Guid.Empty; } //0
  1282. if (list[1] == null) { list[1] = ""; } //1
  1283. if (list[2] == null) { list[2] = ""; } //2
  1284. if (list[3] == null) { list[3] = ""; } //3
  1285. if (list[4] == null) { list[4] = ""; } //4
  1286. JobShell jobshell = new JobShell
  1287. {
  1288. ID = Guid.Parse(list[0].ToString()),
  1289. Name = list[1].ToString(),
  1290. JobNumber = list[2].ToString(),
  1291. JobStatusDescription = list[3].ToString(),
  1292. Color = Color.FromHex(list[4].ToString())
  1293. };
  1294. if (jobshell.JobStatusDescription.Equals("Active Projects"))
  1295. jobshell.JobStatusDescription = "Active";
  1296. else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View"))
  1297. jobshell.JobStatusDescription = "Hidden";
  1298. else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period"))
  1299. jobshell.JobStatusDescription = "Defect Liability";
  1300. jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name;
  1301. jobShells.Add(jobshell);
  1302. }
  1303. GlobalVariables.JobShells = jobShells;
  1304. GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" });
  1305. GlobalVariables.JobsLoaded = true;
  1306. });
  1307. }
  1308. catch { }
  1309. }
  1310. private async void LoadHRToDos()
  1311. {
  1312. try
  1313. {
  1314. await Task.Run(() =>
  1315. {
  1316. Thread.Sleep(10000);
  1317. if (GlobalVariables.UpdateHRItemsNeedingAttention())
  1318. {
  1319. string message = "You have HR Items needing attention. Open My HR now?";
  1320. Device.BeginInvokeOnMainThread(async () =>
  1321. {
  1322. string chosenOption = await DisplayActionSheet(message, "Cancel", null, "Yes", "No");
  1323. switch (chosenOption)
  1324. {
  1325. case "Cancel":
  1326. break;
  1327. case "No":
  1328. break;
  1329. default:
  1330. break;
  1331. case "Yes":
  1332. MyHRHome myHRHome = new MyHRHome();
  1333. Navigation.PushAsync(myHRHome);
  1334. break;
  1335. }
  1336. });
  1337. }
  1338. });
  1339. }
  1340. catch { }
  1341. }
  1342. private async void LoadProducts()
  1343. {
  1344. try
  1345. {
  1346. await Task.Run(() =>
  1347. {
  1348. ProductsLoader productsLoader = new ProductsLoader();
  1349. //if (ClientFactory.IsAllowed<CanViewStoresRequisitions>())
  1350. //{
  1351. //}
  1352. });
  1353. }
  1354. catch { }
  1355. }
  1356. private async void LoadBlueToothAddresses()
  1357. {
  1358. try
  1359. {
  1360. //if (bSharedDevice)
  1361. // return;
  1362. await Task.Run(() =>
  1363. {
  1364. GlobalVariables.GPSTrackerCache = new List<GPSTracker>();
  1365. CoreTable table = new Client<GPSTracker>().Query(new Filter<GPSTracker>(x => x.Type.Description).Contains("Kontakt"));
  1366. foreach (CoreRow row in table.Rows)
  1367. {
  1368. GPSTracker tracker = row.ToObject<GPSTracker>();
  1369. GlobalVariables.GPSTrackerCache.Add(tracker);
  1370. App.Bluetooth.KnownBlueToothMACAddresses.Add(tracker.DeviceID);
  1371. }
  1372. });
  1373. }
  1374. catch { }
  1375. }
  1376. #endregion
  1377. #endregion
  1378. #region Modules
  1379. public async void InitToolEntryList()
  1380. {
  1381. try
  1382. {
  1383. await Task.Run(() =>
  1384. {
  1385. //Assignments
  1386. ToolEntry Assignments = new ToolEntry
  1387. {
  1388. Text = "Assignments",
  1389. Image = "calendar"
  1390. };
  1391. Assignments.IsVisible = PRSSecurity.CanView<Assignment>();
  1392. Assignments.OnTapped += ((object sender, EventArgs e) =>
  1393. {
  1394. var assignment_form = new AssignmentList();
  1395. Navigation.PushAsync(assignment_form);
  1396. });
  1397. toolEntries.Add(Assignments);
  1398. //Deliveries
  1399. ToolEntry Deliveries = new ToolEntry
  1400. {
  1401. Text = "Deliveries",
  1402. Image = "deliveries"
  1403. };
  1404. Deliveries.IsVisible = PRSSecurity.CanView<Delivery>();
  1405. Deliveries.OnTapped += ((object sender, EventArgs e) =>
  1406. {
  1407. var delivery_form = new DeliveryList();
  1408. Navigation.PushAsync(delivery_form);
  1409. });
  1410. toolEntries.Add(Deliveries);
  1411. //Digital Forms
  1412. ToolEntry Forms = new ToolEntry
  1413. {
  1414. Text = "Forms",
  1415. Image = "forms"
  1416. };
  1417. Forms.IsVisible = PRSSecurity.CanView<DigitalForm>();
  1418. Forms.OnTapped += ((object sender, EventArgs e) =>
  1419. {
  1420. var qaFormPicker = new DigitalFormsPicker();
  1421. Navigation.PushAsync(qaFormPicker);
  1422. });
  1423. toolEntries.Add(Forms);
  1424. //Equipment
  1425. ToolEntry Equipment = new ToolEntry
  1426. {
  1427. Text = "Equipment",
  1428. Image = "digger"
  1429. };
  1430. Equipment.IsVisible = PRSSecurity.CanView<Equipment>();
  1431. Equipment.OnTapped += (async (object sender, EventArgs e) =>
  1432. {
  1433. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  1434. {
  1435. var equipment = new EquipmentModule();
  1436. Navigation.PushAsync(equipment);
  1437. }
  1438. });
  1439. toolEntries.Add(Equipment);
  1440. //InOut
  1441. ToolEntry InOut = new ToolEntry
  1442. {
  1443. Text = "In/Out",
  1444. Image = "inout"
  1445. };
  1446. InOut.IsVisible = PRSSecurity.IsAllowed<CanViewInOutBoard>();
  1447. InOut.OnTapped += ((object sender, EventArgs e) =>
  1448. {
  1449. var staff_form = new StaffStatusPage();
  1450. Navigation.PushAsync(staff_form);
  1451. });
  1452. toolEntries.Add(InOut);
  1453. //Manufacturing
  1454. ToolEntry Manufacturing = new ToolEntry
  1455. {
  1456. Text = "Manufacturing",
  1457. Image = "manufacturingg"
  1458. };
  1459. if (Device.RuntimePlatform.Equals(Device.iOS))
  1460. {
  1461. Manufacturing.Image = "Image";
  1462. }
  1463. Manufacturing.IsVisible = PRSSecurity.IsAllowed<CanViewManufacturingOnMobile>();
  1464. Manufacturing.OnTapped += ((object sender, EventArgs e) =>
  1465. {
  1466. ManufacturingScreen manufacturingScreen = new ManufacturingScreen();
  1467. Navigation.PushAsync(manufacturingScreen);
  1468. });
  1469. toolEntries.Add(Manufacturing);
  1470. //My HR
  1471. ToolEntry MyHR = new ToolEntry
  1472. {
  1473. Text = "My HR",
  1474. Image = "myhr"
  1475. };
  1476. MyHR.OnTapped += ((object sender, EventArgs e) =>
  1477. {
  1478. MyHRHome myHRHome = new MyHRHome();
  1479. Navigation.PushAsync(myHRHome);
  1480. });
  1481. toolEntries.Add(MyHR);
  1482. //Notifications
  1483. ToolEntry Notifications = new ToolEntry()
  1484. {
  1485. Text = "Notifications",
  1486. Image = "notifications"
  1487. };
  1488. Notifications.OnTapped += (object sender, EventArgs e) =>
  1489. {
  1490. NotificationList notificationList = new NotificationList();
  1491. notificationList.NotificationsClosed += (n) =>
  1492. {
  1493. NumberOfNotfications = n;
  1494. RefreshOnNotificationsChange();
  1495. };
  1496. Navigation.PushAsync(notificationList);
  1497. };
  1498. toolEntries.Add(Notifications);
  1499. ToolEntry Products = new ToolEntry()
  1500. {
  1501. Text = "Products",
  1502. Image = "products"
  1503. };
  1504. Products.OnTapped += ((object sender, EventArgs e) =>
  1505. {
  1506. if (GlobalVariables.ProductsLoaded)
  1507. {
  1508. ProductList products = new ProductList(GlobalVariables.ProductShells);
  1509. Navigation.PushAsync(products);
  1510. }
  1511. else
  1512. {
  1513. ProductList products = new ProductList();
  1514. Navigation.PushAsync(products);
  1515. }
  1516. });
  1517. toolEntries.Add(Products);
  1518. //Purchase Orders
  1519. ToolEntry PurchaseOrders = new ToolEntry()
  1520. {
  1521. Text = "Purchase Orders",
  1522. Image = "shoppingcart"
  1523. };
  1524. PurchaseOrders.IsVisible = PRSSecurity.CanView<PurchaseOrder>();
  1525. PurchaseOrders.OnTapped += ((object sender, EventArgs e) =>
  1526. {
  1527. PurchaseOrderModule page = new PurchaseOrderModule();
  1528. Navigation.PushAsync(page);
  1529. });
  1530. toolEntries.Add(PurchaseOrders);
  1531. //Scanner
  1532. ToolEntry Scanner = new ToolEntry
  1533. {
  1534. Text = "Scanner",
  1535. Image = "scanner"
  1536. };
  1537. Scanner.OnTapped += ((object sender, EventArgs e) =>
  1538. {
  1539. ScannerPage scannerPage = new ScannerPage();
  1540. Navigation.PushAsync(scannerPage);
  1541. });
  1542. toolEntries.Add(Scanner);
  1543. //Site
  1544. ToolEntry Site = new ToolEntry
  1545. {
  1546. Text = "Site",
  1547. Image = "construction"
  1548. };
  1549. Site.OnTapped += ((object sender, EventArgs e) =>
  1550. {
  1551. Site site = new Site(_job);
  1552. Navigation.PushAsync(site);
  1553. //if (_job.ID == Guid.Empty)
  1554. //{
  1555. // JobSelectionPage jobSelectionPage = new JobSelectionPage(true);
  1556. // jobSelectionPage.OnItemSelected += (async () =>
  1557. // {
  1558. // if (jobSelectionPage.Job.ID != Guid.Empty)
  1559. // {
  1560. // Job selectedJob = new Job();
  1561. // selectedJob.ID = jobSelectionPage.Job.ID;
  1562. // selectedJob.JobNumber = jobSelectionPage.Job.JobNumber;
  1563. // selectedJob.Name = jobSelectionPage.Job.Name;
  1564. // Site site = new Site(selectedJob);
  1565. // var previousPage = Navigation.NavigationStack.LastOrDefault();
  1566. // await Navigation.PushAsync(site);
  1567. // Navigation.RemovePage(previousPage);
  1568. // }
  1569. // });
  1570. // Navigation.PushAsync(jobSelectionPage);
  1571. //}
  1572. });
  1573. toolEntries.Add(Site);
  1574. //Store Requis
  1575. ToolEntry StoreRequis = new ToolEntry
  1576. {
  1577. Text = "Store Requis",
  1578. Image = "storerequis"
  1579. };
  1580. StoreRequis.IsVisible = PRSSecurity.CanView<Requisition>();
  1581. StoreRequis.OnTapped += ((object sender, EventArgs e) =>
  1582. {
  1583. var storeRequisList = new StoreRequiList();
  1584. Navigation.PushAsync(storeRequisList);
  1585. });
  1586. toolEntries.Add(StoreRequis);
  1587. //Tasks
  1588. ToolEntry Tasks = new ToolEntry
  1589. {
  1590. Text = "Tasks",
  1591. Image = "tasks"
  1592. };
  1593. Tasks.IsVisible = PRSSecurity.IsAllowed<CanViewTasks>();
  1594. Tasks.OnTapped += ((object sender, EventArgs e) =>
  1595. {
  1596. var tasksForm = new TasksList();
  1597. Navigation.PushAsync(tasksForm);
  1598. });
  1599. toolEntries.Add(Tasks);
  1600. //Warehousing
  1601. ToolEntry Warehousing = new ToolEntry
  1602. {
  1603. Text = "Warehousing",
  1604. Image = "newwarehousing"
  1605. };
  1606. Warehousing.IsVisible = PRSSecurity.CanView<StockWarehouse>();
  1607. Warehousing.OnTapped += ((object sender, EventArgs e) =>
  1608. {
  1609. Warehousing2 locations = new Warehousing2();
  1610. Navigation.PushAsync(locations);
  1611. });
  1612. toolEntries.Add(Warehousing);
  1613. AddChildren();
  1614. });
  1615. }
  1616. catch { }
  1617. }
  1618. private void AddChildren()
  1619. {
  1620. Device.BeginInvokeOnMainThread(() =>
  1621. {
  1622. foreach (ToolEntry toolEntry in toolEntries)
  1623. {
  1624. toolEntry.Margin = new Thickness(5, 0, 5, 0);
  1625. flexLayout.Children.Add(toolEntry);
  1626. }
  1627. SearchForNewNotifications();
  1628. AddBlanks();
  1629. });
  1630. }
  1631. private void AddBlanks()
  1632. {
  1633. for (int x = 0; x < 6; x++)
  1634. {
  1635. ToolEntry toolEntry = new ToolEntry(true);
  1636. toolEntry.Margin = new Thickness(5, 0, 5, 0);
  1637. flexLayout.Children.Add(toolEntry);
  1638. }
  1639. }
  1640. private void Settings_Tapped(object sender, EventArgs e)
  1641. {
  1642. try
  1643. {
  1644. Settings settingsform = new Settings();
  1645. settingsform.Disappearing += (object sender2, EventArgs e2) =>
  1646. {
  1647. settingsform = (Settings)sender2;
  1648. if (settingsform.SettingsChanged)
  1649. Navigation.PopModalAsync();
  1650. };
  1651. Navigation.PushAsync(settingsform);
  1652. }
  1653. catch { }
  1654. }
  1655. #endregion
  1656. }
  1657. }