TimeSheetStandardLeaveGrid.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Media.Imaging;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.WPF;
  10. namespace PRSDesktop
  11. {
  12. public class TimeSheetStandardLeaveGrid : DynamicDataGrid<StandardLeave>
  13. {
  14. public DateTime From { get; set; }
  15. public DateTime To { get; set; }
  16. public TimeSheetStandardLeaveGrid() : base()
  17. {
  18. ColumnsTag = "TimeSheetStandardLeave";
  19. Options
  20. .BeginUpdate()
  21. .Clear()
  22. .Add(DynamicGridOption.SelectColumns)
  23. .EndUpdate();
  24. ActionColumns.Add(new DynamicImageColumn(SelectedImage, SelectedAction));
  25. HiddenColumns.Add(x => x.LeaveType.ID);
  26. HiddenColumns.Add(x => x.From);
  27. HiddenColumns.Add(x => x.FromTime);
  28. HiddenColumns.Add(x => x.To);
  29. HiddenColumns.Add(x => x.ToTime);
  30. HiddenColumns.Add(x => x.Name);
  31. }
  32. protected override void GenerateColumns(DynamicGridColumns columns)
  33. {
  34. columns.Add<StandardLeave, String>(x => x.Name, 0, "Name", "", Alignment.MiddleLeft);
  35. columns.Add<StandardLeave, String>(x => x.LeaveType.Description, 200, "Type", "", Alignment.MiddleCenter);
  36. columns.Add<StandardLeave, DateTime>(x => x.From, 80, "", "dd MMM yy", Alignment.MiddleCenter);
  37. columns.Add<StandardLeave, DateTime>(x => x.To, 80, "", "dd MMM yy", Alignment.MiddleCenter);
  38. }
  39. private BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  40. private List<Guid> selectedrows = new List<Guid>();
  41. private bool SelectedAction(CoreRow? arg)
  42. {
  43. if (arg == null)
  44. {
  45. foreach (var row in Data.Rows)
  46. ToggleRow(row);
  47. }
  48. else
  49. ToggleRow(arg);
  50. return false;
  51. }
  52. private void ToggleRow(CoreRow row)
  53. {
  54. Guid id = row.Get<StandardLeave, Guid>(x => x.ID);
  55. if (selectedrows.Contains(id))
  56. selectedrows.Remove(id);
  57. else
  58. selectedrows.Add(id);
  59. InvalidateRow(row);
  60. }
  61. private BitmapImage? SelectedImage(CoreRow? arg)
  62. {
  63. return arg == null
  64. ? tick
  65. : selectedrows.Contains(arg.Get<StandardLeave,Guid>(x=>x.ID))
  66. ? tick
  67. : null;
  68. }
  69. protected override void Reload(Filters<StandardLeave> criteria, Columns<StandardLeave> columns, ref SortOrder<StandardLeave>? sort, Action<CoreTable?, Exception?> action)
  70. {
  71. criteria.Add(new InABox.Core.Filter<StandardLeave>(x => x.From).IsLessThanOrEqualTo(To)
  72. .And(x => x.To).IsGreaterThanOrEqualTo(From)
  73. .And(x =>x.Processed).IsEqualTo(DateTime.MinValue));
  74. base.Reload(criteria, columns, ref sort, action);
  75. }
  76. public void Process(IProgress<String> progress)
  77. {
  78. if (!selectedrows.Any())
  79. return;
  80. progress.Report("Loading Standard Leaves");
  81. var empdates = new Filter<Employee>().All();
  82. empdates.Ands.Add(new Filter<Employee>(x => x.StartDate).IsEqualTo(DateTime.MinValue)
  83. .Or(x => x.StartDate).IsLessThanOrEqualTo(To));
  84. empdates.Ands.Add(new Filter<Employee>(x => x.FinishDate).IsEqualTo(DateTime.MinValue)
  85. .Or(x => x.FinishDate).IsGreaterThanOrEqualTo(From));
  86. var employees = new Client<Employee>().Query(
  87. empdates,
  88. new Columns<Employee>(x => x.ID)
  89. .Add(x=>x.Name)
  90. .Add(x => x.Roster)
  91. .Add(x => x.RosterStart)
  92. .Add(x => x.StartDate)
  93. .Add(x => x.FinishDate)
  94. ).Rows.Select(x=>x.ToObject<Employee>()).ToArray();
  95. //var employeeids = employees.Select(x=>x.ID).ToArray();
  96. var rosters = employees.ToDictionary<Employee, Guid, String>(x => x.ID, x => x.Roster);
  97. int i = 0;
  98. List<StandardLeave> leaves = new List<StandardLeave>();
  99. foreach (var row in Data.Rows)
  100. {
  101. if (selectedrows.Contains(row.Get<StandardLeave,Guid>(x=>x.ID)))
  102. {
  103. var leave = row.ToObject<StandardLeave>();
  104. foreach (var employee in employees)
  105. {
  106. var roster = !String.IsNullOrWhiteSpace(rosters[employee.ID])
  107. ? Serialization.Deserialize<List<EmployeeRosterItem>>(rosters[employee.ID])?.OrderBy(x => x.Day).ToArray()
  108. : null;
  109. progress.Report($"Processing Standard Leave {((double)i * 100.0F)/((double)selectedrows.Count * employees.Length):F2}%");
  110. var timesheets = RosterUtils.CreateLeaveTimesheets(
  111. employee,
  112. roster,
  113. leave.From,
  114. leave.FromTime,
  115. leave.To,
  116. leave.ToTime,
  117. leave.LeaveType.ID,
  118. (timesheet) =>
  119. {
  120. timesheet.Notes = leave.Name;
  121. timesheet.StandardLeaveLink.ID = leave.ID;
  122. }
  123. );
  124. new Client<TimeSheet>().Save(timesheets, "Updated from TimeSheet leave Processor");
  125. i++;
  126. }
  127. leave.Processed = DateTime.Now;
  128. leaves.Add(leave);
  129. }
  130. }
  131. if (leaves.Any())
  132. {
  133. progress.Report("Saving Standard Leaves");
  134. new Client<StandardLeave>().Save(leaves, "Updated from TimeSheet leave Processor");
  135. }
  136. }
  137. }
  138. }