DataModel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Comal.Classes;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. namespace comal.timesheets
  8. {
  9. public delegate void DataChangedEvent(object sender, Type t, Exception e);
  10. public delegate void DataRefreshedEvent();
  11. public class DataModel
  12. {
  13. public event DataRefreshedEvent DataRefreshed;
  14. public event DataChangedEvent DataChanged;
  15. public Employee Employee { get; private set; }
  16. public CoreTable TimeSheets { get; set; }
  17. public CoreTable Gates { get; private set; }
  18. public CoreTable Jobs { get; private set; }
  19. public bool CanBypassGates { get; private set; }
  20. public DataModel()
  21. {
  22. }
  23. public void Refresh(bool canbypassgates)
  24. {
  25. CanBypassGates = canbypassgates;
  26. Task.Run(() =>
  27. {
  28. try
  29. {
  30. new Client<TimeSheet>().Query(
  31. new Filter<TimeSheet>(x => x.EmployeeLink.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)
  32. .And(x => x.Date).IsEqualTo(DateTime.Today)
  33. .And(x => x.Finish).IsEqualTo(new TimeSpan()),
  34. new Columns<TimeSheet>(
  35. x => x.ID,
  36. x => x.Date,
  37. x => x.Start,
  38. x => x.Notes,
  39. x => x.JobLink.ID,
  40. x => x.JobLink.JobNumber,
  41. x => x.JobLink.Name,
  42. x => x.ActivityLink.ID,
  43. x => x.ActivityLink.Code,
  44. x => x.ActivityLink.Description,
  45. x => x.StartLocation.Latitude,
  46. x => x.StartLocation.Longitude,
  47. x => x.StartLocation.Timestamp,
  48. x => x.EmployeeLink.ID
  49. ),
  50. null,
  51. (data, error) =>
  52. {
  53. TimeSheets = data;
  54. DataChanged?.Invoke(this, typeof(TimeSheet), error);
  55. }
  56. );
  57. new Client<Employee>().Query(
  58. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),
  59. new Columns<Employee>(
  60. x => x.ID,
  61. x => x.Name
  62. ),
  63. null,
  64. (o, e) =>
  65. {
  66. Employee = o?.Rows.FirstOrDefault()?.ToObject<Employee>();
  67. DataChanged?.Invoke(this, typeof(Employee), e);
  68. }
  69. );
  70. if (canbypassgates)
  71. {
  72. new Client<Job>().Query(
  73. new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue).And(x => x.JobStatus.Active).IsEqualTo(true),
  74. new Columns<Job>(
  75. X => X.ID,
  76. X => X.JobNumber,
  77. X => X.Name,
  78. X => X.SiteAddress.Location.Latitude,
  79. X => X.SiteAddress.Location.Longitude
  80. ),
  81. new SortOrder<Job>(x => x.JobNumber),
  82. (o, e) =>
  83. {
  84. Jobs = o;
  85. DataChanged?.Invoke(this, typeof(Job), e);
  86. }
  87. );
  88. }
  89. else
  90. {
  91. new Client<JobTracker>().Query(
  92. new Filter<JobTracker>(x => x.Active).IsEqualTo(true),
  93. new Columns<JobTracker>(
  94. x => x.JobLink.ID,
  95. x => x.JobLink.JobNumber,
  96. x => x.JobLink.Name,
  97. x => x.TrackerLink.DeviceID,
  98. x => x.Gate,
  99. x => x.IsJobSite
  100. ),
  101. null,
  102. (o, e) =>
  103. {
  104. Gates = o;
  105. DataChanged?.Invoke(this, typeof(JobTracker), e);
  106. }
  107. );
  108. }
  109. DataRefreshed?.Invoke();
  110. }
  111. catch { }
  112. });
  113. }
  114. }
  115. }