DataModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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);
  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. RefreshTimeSheet();
  27. RefreshEmployee();
  28. RefreshJobs(canbypassgates);
  29. }
  30. private void RefreshTimeSheet()
  31. {
  32. Task.Run(() =>
  33. {
  34. try
  35. {
  36. CoreTable table = new Client<TimeSheet>().Query(
  37. new Filter<TimeSheet>(x => x.EmployeeLink.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)
  38. .And(x => x.Date).IsEqualTo(DateTime.Today)
  39. .And(x => x.Finish).IsEqualTo(new TimeSpan()),
  40. new Columns<TimeSheet>(
  41. x => x.ID,
  42. x => x.Date,
  43. x => x.Start,
  44. x => x.Notes,
  45. x => x.JobLink.ID,
  46. x => x.JobLink.JobNumber,
  47. x => x.JobLink.Name,
  48. x => x.ActivityLink.ID,
  49. x => x.ActivityLink.Code,
  50. x => x.ActivityLink.Description,
  51. x => x.StartLocation.Latitude,
  52. x => x.StartLocation.Longitude,
  53. x => x.StartLocation.Timestamp,
  54. x => x.EmployeeLink.ID)
  55. );
  56. TimeSheets = table;
  57. DataChanged?.Invoke(this, typeof(TimeSheet));
  58. DataRefreshed?.Invoke();
  59. }
  60. catch { }
  61. });
  62. }
  63. private void RefreshEmployee()
  64. {
  65. Task.Run(() =>
  66. {
  67. try
  68. {
  69. CoreTable table = new Client<Employee>().Query(
  70. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),
  71. new Columns<Employee>(
  72. x => x.ID,
  73. x => x.Name
  74. ));
  75. if (table.Rows.Any())
  76. {
  77. Employee = table?.Rows.FirstOrDefault()?.ToObject<Employee>();
  78. DataChanged?.Invoke(this, typeof(Employee));
  79. DataRefreshed?.Invoke();
  80. }
  81. }
  82. catch { }
  83. });
  84. }
  85. private void RefreshJobs(bool canbypassgates)
  86. {
  87. Task.Run(() =>
  88. {
  89. try
  90. {
  91. if (canbypassgates)
  92. {
  93. CoreTable table = new Client<Job>().Query(
  94. new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue).And(x => x.JobStatus.Active).IsEqualTo(true),
  95. new Columns<Job>(
  96. X => X.ID,
  97. X => X.JobNumber,
  98. X => X.Name,
  99. X => X.SiteAddress.Location.Latitude,
  100. X => X.SiteAddress.Location.Longitude),
  101. new SortOrder<Job>(x => x.JobNumber));
  102. if (table.Rows.Any())
  103. {
  104. Jobs = table;
  105. DataChanged?.Invoke(this, typeof(Job));
  106. DataRefreshed?.Invoke();
  107. }
  108. }
  109. else
  110. {
  111. CoreTable table = new Client<JobTracker>().Query(
  112. new Filter<JobTracker>(x => x.Active).IsEqualTo(true),
  113. new Columns<JobTracker>(
  114. x => x.JobLink.ID,
  115. x => x.JobLink.JobNumber,
  116. x => x.JobLink.Name,
  117. x => x.TrackerLink.DeviceID,
  118. x => x.Gate,
  119. x => x.IsJobSite
  120. ));
  121. if (table.Rows.Any())
  122. {
  123. Gates = table;
  124. //DataChanged?.Invoke(this, typeof(JobTracker), e);
  125. DataRefreshed?.Invoke();
  126. }
  127. }
  128. }
  129. catch { }
  130. });
  131. }
  132. }
  133. }