DataModel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. ),
  49. null,
  50. (data, error) =>
  51. {
  52. TimeSheets = data;
  53. DataChanged?.Invoke(this, typeof(TimeSheet), error);
  54. }
  55. );
  56. new Client<Employee>().Query(
  57. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),
  58. new Columns<Employee>(
  59. x => x.ID,
  60. x => x.Name
  61. ),
  62. null,
  63. (o, e) =>
  64. {
  65. Employee = o?.Rows.FirstOrDefault()?.ToObject<Employee>();
  66. DataChanged?.Invoke(this, typeof(Employee), e);
  67. }
  68. );
  69. if (canbypassgates)
  70. {
  71. new Client<Job>().Query(
  72. new Filter<Job>(X => X.Completed).IsEqualTo(DateTime.MinValue).And(x => x.JobStatus.Active).IsEqualTo(true),
  73. new Columns<Job>(
  74. X => X.ID,
  75. X => X.JobNumber,
  76. X => X.Name,
  77. X => X.SiteAddress.Location.Latitude,
  78. X => X.SiteAddress.Location.Longitude
  79. ),
  80. new SortOrder<Job>(x => x.JobNumber),
  81. (o, e) =>
  82. {
  83. Jobs = o;
  84. DataChanged?.Invoke(this, typeof(Job), e);
  85. }
  86. );
  87. }
  88. else
  89. {
  90. new Client<JobTracker>().Query(
  91. new Filter<JobTracker>(x => x.Active).IsEqualTo(true),
  92. new Columns<JobTracker>(
  93. x => x.JobLink.ID,
  94. x => x.JobLink.JobNumber,
  95. x => x.JobLink.Name,
  96. x => x.TrackerLink.DeviceID,
  97. x => x.Gate,
  98. x => x.IsJobSite
  99. ),
  100. null,
  101. (o, e) =>
  102. {
  103. Gates = o;
  104. DataChanged?.Invoke(this, typeof(JobTracker), e);
  105. }
  106. );
  107. }
  108. DataRefreshed?.Invoke();
  109. }
  110. catch { }
  111. });
  112. }
  113. }
  114. }