DataModel.cs 20 KB

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