AssignmentEditDataModel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using Comal.Classes;
  8. using comal.timesheets;
  9. using InABox.Core;
  10. using Syncfusion.Drawing;
  11. using Syncfusion.Pdf;
  12. using Xamarin.Forms.Internals;
  13. namespace comal.timesheets
  14. {
  15. public class AssignmentFormInstance : INotifyPropertyChanged
  16. {
  17. private Guid _id = Guid.Empty;
  18. public Guid ID
  19. {
  20. get => _id;
  21. set => SetField(ref _id, value);
  22. }
  23. private String _description = "";
  24. public String Description
  25. {
  26. get => _description;
  27. set => SetField(ref _description, value);
  28. }
  29. private bool _completed = false;
  30. public bool Completed
  31. {
  32. get => _completed;
  33. set => SetField(ref _completed, value);
  34. }
  35. public event PropertyChangedEventHandler PropertyChanged;
  36. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  37. {
  38. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  39. }
  40. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  41. {
  42. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  43. field = value;
  44. OnPropertyChanged(propertyName);
  45. return true;
  46. }
  47. }
  48. public class AssignmentEditDataModel : SingleDataModel<Assignment, AssignmentEditDataModelItem>
  49. {
  50. public Guid JobID { get; set; }
  51. public Guid EmployeeID { get; set; }
  52. public List<KeyValuePair<Guid, String>> Activities { get; private set; }
  53. public List<AssignmentFormInstance> Forms { get; private set; }
  54. public PointF Coordinates { get; set; }
  55. public override Columns<Assignment> Columns => new Columns<Assignment>(x => x.ID)
  56. .Add(x=>x.Number)
  57. .Add(x => x.Title)
  58. .Add(x => x.Description)
  59. .Add(x => x.Date)
  60. .Add(x => x.Start)
  61. .Add(x => x.Duration)
  62. .Add(x => x.Finish)
  63. .Add(c => c.JobLink.ID)
  64. .Add(c => c.JobLink.JobNumber)
  65. .Add(c => c.JobLink.Name)
  66. .Add(c => c.ActivityLink.ID)
  67. .Add(c => c.ActivityLink.Code)
  68. .Add(c => c.ActivityLink.Description)
  69. .Add(c => c.EmployeeLink.ID)
  70. .Add(c => c.ActivityLink.Color)
  71. .Add(c => c.Completed);
  72. public override void BeforeLoad(MultiQuery query, Guid id)
  73. {
  74. base.BeforeLoad(query, id);
  75. query.Add<EmployeeActivity>(
  76. new Filter<EmployeeActivity>(x => x.ID).IsEqualTo(EmployeeID),
  77. new Columns<EmployeeActivity>(x=>x.Activity.ID).Add(x=>x.Activity.Code).Add(x=>x.Activity.Description)
  78. );
  79. if (JobID != Guid.Empty)
  80. {
  81. query.Add<Job>(
  82. new Filter<Job>(x => x.ID).IsEqualTo(JobID),
  83. new Columns<Job>(x => x.SiteAddress.Location.Latitude)
  84. .Add(x => x.SiteAddress.Location.Longitude)
  85. );
  86. }
  87. query.Add<AssignmentForm>(
  88. new Filter<AssignmentForm>(x => x.Parent.ID).IsEqualTo(id),
  89. new Columns<AssignmentForm>(x => x.ID)
  90. .Add(x => x.Form.Code)
  91. .Add(x => x.Form.Description)
  92. .Add(x => x.FormCompleted)
  93. );
  94. }
  95. public override void AfterLoad(MultiQuery query, Guid id)
  96. {
  97. base.AfterLoad(query, id);
  98. Activities = query.Get<EmployeeActivity>().ToDictionary<EmployeeActivity, Guid, String>(
  99. x => x.Activity.ID,
  100. (row) => String.Format("{0}: {1}", row.Get<EmployeeActivity, String>(c => c.Activity.Code),
  101. row.Get<EmployeeActivity, String>(c => c.Activity.Description)),
  102. x => x.Activity.Code
  103. ).ToList();
  104. if (JobID != Guid.Empty)
  105. {
  106. Coordinates = query.Get<Job>().Rows.Select(r => new PointF(
  107. (float)r.Get<Job, double>(c => c.SiteAddress.Location.Longitude),
  108. (float)r.Get<Job, double>(c => c.SiteAddress.Location.Latitude)
  109. )).FirstOrDefault();
  110. }
  111. else
  112. Coordinates = PointF.Empty;
  113. Forms = query.Get<AssignmentForm>()
  114. .Rows
  115. .Select(row => new AssignmentFormInstance()
  116. {
  117. ID = row.Get<AssignmentForm,Guid>(c=>c.ID),
  118. Description = row.Get<AssignmentForm, String>(c => c.Form.Description),
  119. Completed = !row.Get<AssignmentForm,DateTime>(c=>c.FormCompleted).IsEmpty()
  120. }).ToList();
  121. //.ToDictionary<AssignmentForm, Guid, String>(
  122. //x => x.ID,
  123. //(row) => String.Format("{0}: {1}", row.Get<AssignmentForm, String>(c => c.Form.Code),
  124. //
  125. //x => x.Form.Code
  126. //).ToList();
  127. }
  128. }
  129. }