LeaveRequest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. public enum LeaveRequestStatus
  8. {
  9. NotSubmitted,
  10. InProgress,
  11. Approved,
  12. Rejected
  13. }
  14. public class LeaveRequestOpenFormCount : CoreAggregate<LeaveRequest, LeaveRequestForm, Guid>
  15. {
  16. public override Expression<Func<LeaveRequestForm, Guid>> Aggregate => x => x.ID;
  17. public override AggregateCalculation Calculation => AggregateCalculation.Count;
  18. public override Filter<LeaveRequestForm>? Filter =>
  19. new Filter<LeaveRequestForm>(x => x.FormCompleted).IsEqualTo(DateTime.MinValue);
  20. public override Dictionary<Expression<Func<LeaveRequestForm, object>>, Expression<Func<LeaveRequest, object>>> Links =>
  21. new Dictionary<Expression<Func<LeaveRequestForm, object>>, Expression<Func<LeaveRequest, object>>>()
  22. {
  23. { LeaveRequestForm => LeaveRequestForm.Parent.ID, LeaveRequest => LeaveRequest.ID }
  24. };
  25. }
  26. public class LeaveRequestTotalFormCount : CoreAggregate<LeaveRequest, LeaveRequestForm, Guid>
  27. {
  28. public override Expression<Func<LeaveRequestForm, Guid>> Aggregate => x => x.ID;
  29. public override AggregateCalculation Calculation => AggregateCalculation.Count;
  30. public override Dictionary<Expression<Func<LeaveRequestForm, object>>, Expression<Func<LeaveRequest, object>>> Links =>
  31. new Dictionary<Expression<Func<LeaveRequestForm, object>>, Expression<Func<LeaveRequest, object>>>()
  32. {
  33. { LeaveRequestForm => LeaveRequestForm.Parent.ID, LeaveRequest => LeaveRequest.ID }
  34. };
  35. }
  36. [UserTracking("Holiday Calendar")]
  37. [Caption("Leave Requests")]
  38. public class LeaveRequest : Entity, IRemotable, IPersistent, ILicense<LeaveManagementLicense>, IDataEntryInstance
  39. {
  40. [EditorSequence(1)]
  41. [RequiredColumn]
  42. public EmployeeLink EmployeeLink { get; set; }
  43. [EditorSequence(2)]
  44. public LeaveRequestActivityLink LeaveType { get; set; }
  45. [DateEditor]
  46. [RequiredColumn]
  47. [EditorSequence(3)]
  48. public DateTime From { get; set; }
  49. [TimeOfDayEditor]
  50. [EditorSequence(4)]
  51. public TimeSpan FromTime { get; set; }
  52. [DateEditor]
  53. [RequiredColumn]
  54. [EditorSequence(5)]
  55. public DateTime To { get; set; }
  56. [TimeOfDayEditor]
  57. [EditorSequence(6)]
  58. public TimeSpan ToTime { get; set; } = new TimeSpan(23, 59, 59);
  59. [EditorSequence(7)]
  60. public DocumentLink Request { get; set; }
  61. [MemoEditor]
  62. [EditorSequence(8)]
  63. public string Notes { get; set; }
  64. [EditorSequence(9)]
  65. [Caption("Approval Set")]
  66. public LeaveRequestApprovalSetLink ApprovalSet { get; set; }
  67. private class OutstandingApprovalsFormula : ComplexFormulaGenerator<LeaveRequest, string>
  68. {
  69. public override IComplexFormulaNode<LeaveRequest, string> GetFormula() =>
  70. Aggregate(AggregateCalculation.Concat,
  71. x => x.Property(x => x.Employee.Code),
  72. filter: new Filter<LeaveRequestApproval>(x => x.Status).IsEqualTo(LeaveRequestApprovalStatus.NotYetApproved))
  73. .WithLink(x => x.LeaveRequest.ID, x => x.ID);
  74. }
  75. [ComplexFormula(typeof(OutstandingApprovalsFormula))]
  76. [Editable(Editable.Hidden)]
  77. public string OutstandingApprovals { get; set; }
  78. private class ApprovalStatusFormula : ComplexFormulaGenerator<LeaveRequest, LeaveRequestApprovalStatus>
  79. {
  80. public override IComplexFormulaNode<LeaveRequest, LeaveRequestApprovalStatus> GetFormula() =>
  81. Aggregate<LeaveRequestApproval>(
  82. AggregateCalculation.Maximum,
  83. x=>x.Property(p=>p.Status)
  84. ).WithLink(x=>x.LeaveRequest.ID,x=>x.ID);
  85. }
  86. [Editable(Editable.Hidden)]
  87. [ComplexFormula(typeof(ApprovalStatusFormula))]
  88. public LeaveRequestApprovalStatus ApprovalStatus { get; set; }
  89. [LoggableProperty]
  90. [EditorSequence(10)]
  91. [RequiredColumn]
  92. [EnumLookupEditor(typeof(LeaveRequestStatus))]
  93. public LeaveRequestStatus Status { get; set; } = LeaveRequestStatus.InProgress;
  94. [Caption("Response")]
  95. [MemoEditor]
  96. [EditorSequence(11)]
  97. public string StatusNotes { get; set; }
  98. [EditorSequence("Internal Notes", 1)]
  99. [TimestampEditor]
  100. public DateTime DataEntered { get; set; }
  101. [MemoEditor]
  102. [EditorSequence("Internal Notes", 2)]
  103. public string InternalNotes { get; set; }
  104. [TimestampEditor(Editable = Editable.Disabled)]
  105. [EditorSequence("Internal Notes", 3)]
  106. [RequiredColumn]
  107. public DateTime Processed { get; set; }
  108. [Obsolete("Replaced By Status", true)]
  109. [NullEditor]
  110. public DateTime Approved { get; set; }
  111. [Obsolete("Replaced By StatusNotes", true)]
  112. [NullEditor]
  113. public string ApprovalNotes { get; set; }
  114. [NullEditor]
  115. [EntityRelationship(DeleteAction.Cascade)]
  116. public StandardLeaveLink PublicHoliday { get; set; }
  117. [IntegerEditor(Visible = Visible.Optional,Editable = Editable.Hidden)]
  118. [Aggregate(typeof(LeaveRequestOpenFormCount))]
  119. public int OpenForms { get; set; }
  120. [IntegerEditor(Visible = Visible.Optional,Editable = Editable.Hidden)]
  121. [Aggregate(typeof(LeaveRequestTotalFormCount))]
  122. public int FormCount { get; set; }
  123. //protected override void DoPropertyChanged(string name, object? before, object? after)
  124. //{
  125. // base.DoPropertyChanged(name, before, after);
  126. // if (String.Equals(name,"Status"))
  127. // {
  128. // var status = (LeaveRequestStatus)after;
  129. // if (status == LeaveRequestStatus.Approved)
  130. // Approved = Approved.IsEmpty() ? DateTime.Now : Approved;
  131. // else
  132. // Approved = DateTime.MinValue;
  133. // }
  134. //}
  135. public LeaveRequest LoadFrom(StandardLeave standardleave)
  136. {
  137. PublicHoliday.ID = standardleave.ID;
  138. From = standardleave.From;
  139. FromTime = standardleave.FromTime;
  140. To = standardleave.To;
  141. ToTime = standardleave.ToTime;
  142. LeaveType.ID = standardleave.LeaveType.ID;
  143. Notes = standardleave.Name;
  144. return this;
  145. }
  146. //protected override string ToSort()
  147. //{
  148. // if (Approved == DateTime.MinValue)
  149. // return String.Format("0{0:yyyyMMdd}{1}",From,EmployeeLink.Name);
  150. // else
  151. // return String.Format("1{0:yyyyMMdd}{1}", DateTime.MinValue.Add(DateTime.MaxValue.Subtract(From)), EmployeeLink.Name);
  152. //}
  153. public static SortOrder<LeaveRequest> SortOrder()
  154. {
  155. return new SortOrder<LeaveRequest>(x => x.From, SortDirection.Descending).ThenBy(x => x.EmployeeLink.Name);
  156. }
  157. public override string ToString()
  158. {
  159. return string.Format("{0}: {1} {2:dd MMM yy} - {3:dd MMM yy} {4}", EmployeeLink.Name, LeaveType.Description, From, To, Status);
  160. }
  161. static LeaveRequest()
  162. {
  163. LinkedProperties.Register<LeaveRequest,LeaveRequestApprovalSetLink,Guid>(x=>x.EmployeeLink.LeaveRequestApprovalSet, x=>x.ID, x=>x.ApprovalSet.ID);
  164. LinkedProperties.Register<LeaveRequest,LeaveRequestApprovalSetLink,string>(x=>x.EmployeeLink.LeaveRequestApprovalSet, x=>x.Code, x=>x.ApprovalSet.Code);
  165. LinkedProperties.Register<LeaveRequest,LeaveRequestApprovalSetLink,string>(x=>x.EmployeeLink.LeaveRequestApprovalSet, x=>x.Description, x=>x.ApprovalSet.Description);
  166. }
  167. }
  168. }