DataModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using InABox.Rpc;
  10. namespace comal.timesheets
  11. {
  12. public class GPSEventArgs : EventArgs
  13. {
  14. }
  15. public delegate void GPSLocationUpdatedEvent(GPSEventArgs args);
  16. public class BluetoothEventArgs : EventArgs
  17. {
  18. }
  19. public delegate void BluetoothScanFinishedEvent(BluetoothEventArgs args);
  20. public class TransportConnectedEventArgs : EventArgs
  21. {
  22. }
  23. public delegate void TransportConnectedEvent(TransportConnectedEventArgs args);
  24. public class TransportDisconnectedEventArgs : EventArgs
  25. {
  26. }
  27. public delegate void TransportDisconnectedEvent(TransportDisconnectedEventArgs args);
  28. public class DataModel : IModelHost
  29. {
  30. public event GPSLocationUpdatedEvent GPSLocationUpdated;
  31. public event BluetoothScanFinishedEvent BluetoothScanFinished;
  32. /// <summary>
  33. /// All Active Employees of the Company
  34. /// </summary>
  35. public EmployeeModel Employees { get; private set; }
  36. /// <summary>
  37. /// EmployeeDetails for Currently Logged in User
  38. /// </summary>
  39. public EmployeeDetailModel Me { get; private set; }
  40. /// <summary>
  41. /// All Employee Teams for the Company
  42. /// Should this be just teams I am a part of, or all available teams
  43. /// Need to implement a Security Token to differentiate
  44. /// </summary>
  45. public EmployeeTeamModel EmployeeTeams { get; private set; }
  46. /// <summary>
  47. /// All Available Contacts - used in Deliveries
  48. /// </summary>
  49. public ContactModel Contacts { get; private set; }
  50. /// <summary>
  51. /// All Defined Bluetooth-enabled Gates
  52. /// </summary>
  53. public BluetoothGateModel BluetoothGates { get; private set; }
  54. /// <summary>
  55. /// Unprocessed Timesheets for Currently Logged in User
  56. /// </summary>
  57. public TimeSheetModel TimeSheets { get; private set; }
  58. /// <summary>
  59. /// List of All active Jobs
  60. /// </summary>
  61. public JobLookupModel JobLookups { get; private set; }
  62. /// <summary>
  63. /// List of Jobs that Currently Logged in User has access To
  64. /// </summary>
  65. public JobModel Jobs { get; private set; }
  66. /// <summary>
  67. /// List of Assignments for ??? need to specify this.
  68. /// Can I see other people's assignments? (Security Token)
  69. /// How far back / forward can I look.
  70. /// This smells Transient, or maybe split into lookups (for all)
  71. /// and shells (for mine)
  72. /// Possibly need a back/forward window as well?
  73. /// </summary>
  74. public AssignmentModel Assignments { get; private set; }
  75. /// <summary>
  76. /// Master Product Catalogue for the company
  77. /// This should be split into Lookups and Shells
  78. /// </summary>
  79. public ProductModel Products { get; private set; }
  80. /// <summary>
  81. /// Master Product Groups List (Lookup?)
  82. /// </summary>
  83. public ProductGroupLookupModel ProductGroupsLookup { get; private set; }
  84. /// <summary>
  85. /// All GPS Tracker Devices registered by the company
  86. /// </summary>
  87. public GPSTrackerModel GPSTrackers { get; private set; }
  88. /// <summary>
  89. /// Master Equipment Register
  90. /// </summary>
  91. public EquipmentModel Equipment { get; private set; }
  92. /// <summary>
  93. /// Master List of Equipment Groups
  94. /// </summary>
  95. public EquipmentGroupModel EquipmentGroups { get; private set; }
  96. /// <summary>
  97. /// List of all Deliveries made in the last 90 days
  98. /// hmmm.. should be configurable?
  99. /// </summary>
  100. public DeliveryModel Deliveries { get; private set; }
  101. /// <summary>
  102. /// My Notifications (open only?)
  103. /// </summary>
  104. public NotificationModel Notifications { get; private set; }
  105. /// <summary>
  106. /// All Open Kanbans
  107. /// </summary>
  108. public KanbanLookupModel KanbanLookups { get; private set; }
  109. /// <summary>
  110. /// All Tasks I am subscribed to
  111. /// </summary>
  112. public KanbanModel Kanbans { get; private set; }
  113. /// <summary>
  114. /// Available Form Library - this includes all "Applies To" values
  115. /// You can filter to a specific type by using the "Search" function
  116. /// </summary>
  117. public DigitalFormModel DigitalForms { get; private set; }
  118. // The list of open task-kased forms for the current user
  119. // Specifically for us in the "Forms" module
  120. // This overlaps the "Task" module a fair amount, but it provides
  121. // an alternative paradigm for tasks
  122. public KanbanFormModel KanbanForms { get; private set; }
  123. /// <summary>
  124. /// List of Employees, along with Clock on / off status
  125. /// </summary>
  126. public InOutModel InOut { get; private set; }
  127. /// <summary>
  128. /// List of all available Document Set Tags
  129. /// </summary>
  130. public JobDocumentSetTagModel DocumentSetTags { get; private set; }
  131. /// <summary>
  132. /// List of available Delivery Barcodes
  133. /// </summary>
  134. public DeliveryItemLookupModel DeliveryItems { get; private set; }
  135. public DataModel()
  136. {
  137. Me = new EmployeeDetailModel(this,
  138. () => new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)
  139. );
  140. Employees = new EmployeeModel(this,
  141. () => LookupFactory.DefineFilter<Employee>());
  142. EmployeeTeams = new EmployeeTeamModel(this,
  143. () => new Filter<EmployeeTeam>(x => x.EmployeeLink.ID)
  144. .InQuery(LookupFactory.DefineFilter<Employee>(), x => x.ID)
  145. );
  146. InOut = new InOutModel(this,
  147. () => LookupFactory.DefineFilter<Employee>()
  148. .And(x=>x.ID).IsNotEqualTo(App.Data.Me.ID));
  149. Jobs = new JobModel(this, MyJobsFilter);
  150. JobLookups = new JobLookupModel(this,
  151. () => LookupFactory.DefineFilter<Job>());
  152. Products = new ProductModel(this,
  153. () => LookupFactory.DefineFilter<Product>());
  154. ProductGroupsLookup = new ProductGroupLookupModel(this,
  155. () => LookupFactory.DefineFilter<ProductGroup>());
  156. GPSTrackers = new GPSTrackerModel(this,
  157. () => LookupFactory.DefineFilter<GPSTracker>());
  158. Equipment = new EquipmentModel(this,
  159. () =>
  160. {
  161. var filter = LookupFactory.DefineFilter<Equipment>();
  162. var privfilter = new Filter<Equipment>(x => x.Private).IsEqualTo(false);
  163. if (InABox.Core.Security.IsAllowed<CanViewPrivateEquipment>())
  164. return filter;
  165. return filter != null
  166. ? filter.And(privfilter)
  167. : privfilter;
  168. },
  169. "equipment.index");
  170. EquipmentGroups = new EquipmentGroupModel(this,
  171. () => LookupFactory.DefineFilter<EquipmentGroup>());
  172. Deliveries = new DeliveryModel(this,
  173. () => new Filter<Delivery>(x => x.Completed).IsEqualTo(DateTime.MinValue)
  174. .Or(x => x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90))
  175. );
  176. DeliveryItems = new DeliveryItemLookupModel(this,
  177. () => LookupFactory.DefineFilter<DeliveryItem>()
  178. );
  179. Notifications = new NotificationModel(this,
  180. () => new Filter<Notification>(x=>x.Closed).IsEqualTo(DateTime.MinValue)
  181. .And(x=>x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  182. );
  183. TimeSheets = new TimeSheetModel(this,
  184. () => new Filter<TimeSheet>(x=>x.Processed).IsEqualTo(DateTime.MinValue)
  185. .And(x=>x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID));
  186. Assignments = new AssignmentModel(this,
  187. () => new Filter<Assignment>(x => x.Date).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-90))
  188. .And(x => x.Date).IsLessThanOrEqualTo(DateTime.Today.AddDays(90))
  189. );
  190. Contacts = new ContactModel(this,
  191. () => LookupFactory.DefineFilter<Contact>());
  192. Kanbans = new KanbanModel(this,
  193. () => new Filter<KanbanSubscriber>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID));
  194. KanbanLookups = new KanbanLookupModel(this,
  195. () => new Filter<Kanban>(x => x.Completed).IsEqualTo(DateTime.MinValue));
  196. BluetoothGates = new BluetoothGateModel(this,
  197. () => new Filter<JobTracker>(x => x.Active).IsEqualTo(true)
  198. );
  199. DigitalForms = new DigitalFormModel(this,
  200. () => new Filter<EmployeeDigitalForm>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  201. .And(x => x.Form.Active).IsEqualTo(true)
  202. .And(x => x.Form.Secure).IsEqualTo(false)
  203. );
  204. KanbanForms = new KanbanFormModel(this,
  205. () => new Filter<KanbanForm>(x=>x.Parent.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID)
  206. .And(x=>x.FormCompleted).IsEqualTo(DateTime.MinValue)
  207. );
  208. DocumentSetTags = new JobDocumentSetTagModel(this,
  209. () => null
  210. );
  211. }
  212. private static Filter<Job> MyJobsFilter()
  213. {
  214. return InABox.Core.Security.IsAllowed<CanViewAllJobs>()
  215. ? new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  216. .And(x => x.JobStatus.Active).IsEqualTo(true)
  217. : new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue)
  218. .And(x => x.JobStatus.Active).IsEqualTo(true)
  219. .And(x => x.ID).InQuery(
  220. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID),
  221. x => x.JobLink.ID
  222. );
  223. }
  224. public void Setup()
  225. {
  226. _connected = true;
  227. InABox.Core.Security.CheckTokens();
  228. Me.Refresh(true);
  229. Task[] _setuptasks = new Task[]
  230. {
  231. Task.Run(() => TimeSheets.Refresh(true)),
  232. Task.Run(() => GPSTrackers.Refresh(true)),
  233. Task.Run(() => BluetoothGates.Refresh(true)),
  234. Task.Run(() => Notifications.Refresh(true)),
  235. Task.Run(() => Kanbans.Refresh(true))
  236. };
  237. Task.WaitAll(_setuptasks);
  238. App.Bluetooth.OnScanFinished += OnBluetoothScanFinished;
  239. App.GPS.OnLocationFound += OnGPSLocationFound;
  240. App.Transport.OnOpen += OnTransportConnected;
  241. App.Transport.OnClose += OnTransportDisconnected;
  242. }
  243. public bool IsConnected() => _connected;
  244. private bool _connected;
  245. public event TransportDisconnectedEvent TransportDisconnected;
  246. public event TransportConnectedEvent TransportConnected;
  247. private void OnTransportDisconnected(IRpcTransport transport, RpcTransportCloseArgs e)
  248. {
  249. _connected = false;
  250. TransportDisconnected?.Invoke(new TransportDisconnectedEventArgs());
  251. Task.Run(() =>
  252. {
  253. while (!_connected)
  254. App.Transport.Connect();
  255. var status = ClientFactory.Validate(ClientFactory.SessionID);
  256. });
  257. }
  258. private void OnTransportConnected(IRpcTransport transport, RpcTransportOpenArgs e)
  259. {
  260. _connected = true;
  261. TransportConnected?.Invoke(new TransportConnectedEventArgs());
  262. }
  263. private GPSTrackerLocation GetGPSTrackerUpdate(string deviceid, InABox.Core.Location location,
  264. TimeSpan threshold, double distance)
  265. {
  266. GPSTrackerShell tracker = GPSTrackers.FirstOrDefault(x => x.DeviceID.Equals(deviceid));
  267. if (tracker != null)
  268. {
  269. if ((tracker.Timestamp < location.Timestamp.Subtract(threshold)) || (tracker.Location.DistanceTo(location, UnitOfLength.Kilometers) > distance))
  270. {
  271. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  272. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  273. gpsTrackerLocation.Tracker.ID = tracker.ID;
  274. gpsTrackerLocation.Location = location;
  275. return gpsTrackerLocation;
  276. }
  277. }
  278. return null;
  279. }
  280. private InABox.Core.Location _lastgpslocation = new InABox.Core.Location();
  281. private void OnGPSLocationFound(LocationServices sender)
  282. {
  283. if (_lastgpslocation.Timestamp < DateTime.Now.Subtract(TimeSpan.FromMinutes(2)))
  284. {
  285. var devicelocation = new InABox.Core.Location()
  286. {
  287. Latitude = App.GPS.Latitude,
  288. Longitude = App.GPS.Longitude,
  289. Timestamp = App.GPS.TimeStamp,
  290. Address = App.GPS.Address
  291. };
  292. GPSTrackers.Refresh(false);
  293. var update = GetGPSTrackerUpdate(MobileUtils.GetDeviceID(), devicelocation, TimeSpan.FromMinutes(2), 0.1);
  294. if (update != null)
  295. new Client<GPSTrackerLocation>().Save(update, "Updated via Mobile Device", (o, e) => { });
  296. }
  297. GPSLocationUpdated?.Invoke(new GPSEventArgs());
  298. }
  299. private void OnBluetoothScanFinished(Bluetooth sender)
  300. {
  301. UploadTiles();
  302. BluetoothScanFinished?.Invoke(new BluetoothEventArgs());
  303. }
  304. private void UploadTiles()
  305. {
  306. try
  307. {
  308. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  309. return;
  310. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  311. return;
  312. var devicelocation = new InABox.Core.Location()
  313. {
  314. Latitude = App.GPS.Latitude,
  315. Longitude = App.GPS.Longitude,
  316. Timestamp = App.GPS.TimeStamp,
  317. Address = App.GPS.Address
  318. };
  319. GPSTrackers.Refresh(true);
  320. List<GPSTrackerLocation> updates = new List<GPSTrackerLocation>();
  321. foreach (String deviceid in App.Bluetooth.DetectedBlueToothMACAddresses)
  322. {
  323. var update = GetGPSTrackerUpdate(deviceid, devicelocation, TimeSpan.FromMinutes(2), 0.1);
  324. if (update != null)
  325. updates.Add(update);
  326. }
  327. if (updates.Any())
  328. new Client<GPSTrackerLocation>().Save(updates, $"Updated by Mobile {MobileUtils.GetDeviceID()}",
  329. (o, e) => { });
  330. }
  331. catch (Exception ex)
  332. {
  333. MobileLogging.Log($"UploadTiles() {ex.Message} \n {ex.StackTrace}");
  334. }
  335. }
  336. public int EmployeeFormsToDo { get; set; }
  337. public int QualificationsNeedingAttention { get; set; }
  338. public bool UpdateHRItemsNeedingAttention()
  339. {
  340. try
  341. {
  342. EmployeeFormsToDo = 0;
  343. QualificationsNeedingAttention = 0;
  344. CoreTable table = new Client<EmployeeQualification>().Query(
  345. new Filter<EmployeeQualification>(x => x.Employee.ID).IsEqualTo(Me.ID),
  346. new Columns<EmployeeQualification>(
  347. x => x.ID, //0
  348. x => x.Expiry, //1
  349. x => x.FrontPhoto.ID //2
  350. )
  351. );
  352. if (table.Rows.Any())
  353. {
  354. List<Guid> IDs = new List<Guid>();
  355. foreach (CoreRow row in table.Rows)
  356. {
  357. List<object> list = row.Values;
  358. if (list[0] == null) { list[0] = Guid.Empty; } //0
  359. if (list[1] == null) { list[1] = DateTime.MinValue; } //1
  360. if (list[2] == null) { list[2] = Guid.Empty; } //2
  361. if (DateTime.Parse(list[1].ToString()) <= DateTime.Today.AddDays(30))
  362. {
  363. if (!Guid.Parse(list[2].ToString()).Equals(Guid.Empty))
  364. {
  365. CoreTable innerTable = new Client<Document>().Query(
  366. new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(list[2].ToString())),
  367. new Columns<Document>(x => x.Created)
  368. );
  369. CoreRow innerRow = innerTable.Rows.First();
  370. List<object> innerList = innerRow.Values;
  371. if (DateTime.Parse(innerList[0].ToString()) < DateTime.Today.AddDays(-7)) //if photo was added more than 7 days ago, added to list needing attention
  372. {
  373. if (!IDs.Contains(Guid.Parse(list[0].ToString())))
  374. IDs.Add(Guid.Parse(list[0].ToString()));
  375. }
  376. }
  377. else
  378. {
  379. if (!IDs.Contains(Guid.Parse(list[0].ToString())))
  380. IDs.Add(Guid.Parse(list[0].ToString()));
  381. }
  382. }
  383. if (Guid.Parse(list[2].ToString()).Equals(Guid.Empty))
  384. {
  385. if (!IDs.Contains(Guid.Parse(list[0].ToString())))
  386. IDs.Add(Guid.Parse(list[0].ToString()));
  387. }
  388. }
  389. QualificationsNeedingAttention = IDs.Count;
  390. }
  391. CoreTable table1 = new Client<EmployeeForm>().Query(
  392. new Filter<EmployeeForm>(x => x.Parent.ID).IsEqualTo(Me.ID).And
  393. (x => x.FormCompleted).IsEqualTo(DateTime.MinValue),
  394. new Columns<EmployeeForm>(x => x.FormCompleted)
  395. );
  396. if (table1.Rows.Any())
  397. {
  398. EmployeeFormsToDo = table1.Rows.Count;
  399. }
  400. if (QualificationsNeedingAttention > 0 || EmployeeFormsToDo > 0)
  401. {
  402. return true;
  403. }
  404. else
  405. return false;
  406. }
  407. catch
  408. {
  409. return false;
  410. }
  411. }
  412. public void CheckEmployeeFormsToDo()
  413. {
  414. try
  415. {
  416. CoreTable table1 = new Client<EmployeeForm>().Query(
  417. new Filter<EmployeeForm>(x => x.Parent.ID).IsEqualTo(Me.ID).And
  418. (x => x.FormCompleted).IsEqualTo(DateTime.MinValue),
  419. new Columns<EmployeeForm>(x => x.FormCompleted)
  420. );
  421. if (table1.Rows.Any())
  422. {
  423. EmployeeFormsToDo = table1.Rows.Count;
  424. }
  425. else
  426. {
  427. EmployeeFormsToDo = 0;
  428. }
  429. }
  430. catch
  431. {
  432. }
  433. }
  434. }
  435. }