MainPage.xaml.cs 74 KB

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