TimeSheetStandardLeaveGrid.cs 6.2 KB

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