Assignment.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. /// <summary>
  8. /// An assignment represents an anticipated booking for an employee, much like a diary entry
  9. /// <exclude />
  10. /// </summary>
  11. [UserTracking("Assignments")]
  12. [Caption("Assignments")]
  13. public class Assignment : Entity, IPersistent, IRemotable, INumericAutoIncrement<Assignment>, IOneToMany<Employee>, IOneToMany<Job>,
  14. IOneToMany<Kanban>, ILicense<SchedulingControlLicense>, IJobActivity, IOneToMany<Invoice>, IJobScopedItem,
  15. IInvoiceable
  16. {
  17. [IntegerEditor(Editable = Editable.Hidden)]
  18. [EditorSequence(1)]
  19. public int Number { get; set; }
  20. [DateEditor]
  21. [EditorSequence(2)]
  22. [RequiredColumn]
  23. public DateTime Date { get; set; }
  24. [EntityRelationship(DeleteAction.Cascade)]
  25. [EditorSequence(3)]
  26. [RequiredColumn]
  27. public EmployeeLink EmployeeLink { get; set; }
  28. // [EntityRelationship(DeleteAction.Cascade)]
  29. // [EditorSequence(4)]
  30. // public EquipmentLink Equipment { get; set; }
  31. [TextBoxEditor]
  32. [EditorSequence(5)]
  33. public string Title { get; set; }
  34. [MemoEditor]
  35. [EditorSequence(6)]
  36. public string Description { get; set; }
  37. [EditorSequence(7)]
  38. public AssignmentActivityLink ActivityLink { get; set; }
  39. private class KanbanLookup : LookupDefinitionGenerator<Kanban, Assignment>
  40. {
  41. public override Filter<Kanban> DefineFilter(Assignment[] items)
  42. {
  43. if (items == null || !items.Any())
  44. return LookupFactory.DefineFilter<Kanban>();
  45. var employeeID = items.First().EmployeeLink.ID;
  46. // Open tasks which the employee of this assignment is an observer of.
  47. return Filter<Kanban>.Where(x => x.Closed).IsEqualTo(DateTime.MinValue)
  48. .And(x => x.ID).InQuery(Filter<KanbanSubscriber>.Where(x => x.Employee.ID).IsEqualTo(employeeID), x => x.Kanban.ID);
  49. }
  50. public override Columns<Assignment> DefineFilterColumns()
  51. => Columns.None<Assignment>().Add(x => x.EmployeeLink.ID);
  52. }
  53. [LookupDefinition(typeof(KanbanLookup))]
  54. [EditorSequence(8)]
  55. [EntityRelationship(DeleteAction.SetNull)]
  56. public KanbanLink Task { get; set; }
  57. [EditorSequence(9)]
  58. public JobLink JobLink { get; set; }
  59. private class JobITPLookup : LookupDefinitionGenerator<JobITP, Assignment>
  60. {
  61. public override Filter<JobITP> DefineFilter(Assignment[] items)
  62. {
  63. if (items.Length == 1)
  64. return Filter<JobITP>.Where(x => x.Job.ID).IsEqualTo(items.First().JobLink.ID);
  65. return LookupFactory.DefineFilter<JobITP>();
  66. }
  67. public override Columns<Assignment> DefineFilterColumns()
  68. => Columns.None<Assignment>().Add(x => x.JobLink.ID);
  69. }
  70. [LookupDefinition(typeof(JobITPLookup))]
  71. [EditorSequence(10)]
  72. public JobITPLink ITP { get; set; }
  73. [NullEditor]
  74. [Obsolete("Replaced with Actual.Start", true)]
  75. public TimeSpan Start { get; set; }
  76. [NullEditor]
  77. [Obsolete("Replaced with Actual.Duration", true)]
  78. public TimeSpan Duration { get; set; }
  79. [NullEditor]
  80. [Obsolete("Replaced with Actual.Finish", true)]
  81. public TimeSpan Finish { get; set; }
  82. [EditorSequence(11)]
  83. [CoreTimeEditor]
  84. public TimeBlock Booked { get; set; }
  85. [EditorSequence(12)]
  86. [CoreTimeEditor]
  87. public TimeBlock Actual { get; set; }
  88. [TimestampEditor]
  89. [RequiredColumn]
  90. [EditorSequence(13)]
  91. public DateTime Completed { get; set; }
  92. [Editable(Editable.Disabled)]
  93. [Comment("The duration this Assignment was treated as having for costing purposes")]
  94. [EditorSequence("Processing", 1)]
  95. public double CostedHours { get; set; }
  96. [CurrencyEditor]
  97. [Editable(Editable.Disabled)]
  98. [Comment("Cost of the Assignment; calculated from duration, overtime rules and the employee's hourly rate.")]
  99. [EditorSequence("Processing", 2)]
  100. public double Cost { get; set; }
  101. [EditorSequence("Processing", 3)]
  102. public ActualCharge Charge { get; set; }
  103. [TimestampEditor]
  104. [EditorSequence("Processing", 4)]
  105. public DateTime Processed { get; set; }
  106. [NullEditor]
  107. [EntityRelationship(DeleteAction.Cascade)]
  108. public LeaveRequestLink LeaveRequestLink { get; set; }
  109. [NullEditor]
  110. [EntityRelationship(DeleteAction.Cascade)]
  111. public DeliveryLink Delivery { get; set; }
  112. [NullEditor]
  113. [EntityRelationship(DeleteAction.SetNull)]
  114. public InvoiceLink Invoice { get; set; }
  115. [NullEditor]
  116. public MeetingDetails Meeting { get; set; }
  117. private class JobScopeLookup : LookupDefinitionGenerator<JobScope, Assignment>
  118. {
  119. public override Filter<JobScope> DefineFilter(Assignment[] items)
  120. {
  121. var item = items?.Length == 1 ? items[0] : null;
  122. if (item != null)
  123. return Filter<JobScope>.Where(x => x.Job.ID).IsEqualTo(item.JobLink.ID).And(x => x.Status.Approved).IsEqualTo(true);
  124. return Filter.None<JobScope>();
  125. }
  126. public override Columns<Assignment> DefineFilterColumns()
  127. => Columns.None<Assignment>().Add(x => x.JobLink.ID);
  128. }
  129. [LookupDefinition(typeof(JobScopeLookup))]
  130. public JobScopeLink JobScope { get; set; }
  131. public Expression<Func<Assignment, int>> AutoIncrementField()
  132. {
  133. return x => x.Number;
  134. }
  135. public Filter<Assignment> AutoIncrementFilter()
  136. {
  137. return null;
  138. }
  139. public int AutoIncrementDefault() => 1;
  140. public static TimeSpan EffectiveTime(TimeSpan actual, TimeSpan booked) => actual.Ticks != 0L ? actual : booked;
  141. public TimeSpan EffectiveStartTime()
  142. {
  143. return EffectiveTime(Actual.Start, Booked.Start);
  144. }
  145. public DateTime EffectiveStart()
  146. {
  147. return Date.Add(EffectiveStartTime());
  148. }
  149. public TimeSpan EffectiveFinishTime()
  150. {
  151. // If we have an actual finish, always use that
  152. // otherwise use EffectiveStart() + booked.duration
  153. return EffectiveTime(
  154. Actual.Finish,
  155. EffectiveTime(Actual.Start, Booked.Start)
  156. .Add(Booked.Duration)
  157. );
  158. }
  159. public DateTime EffectiveFinish()
  160. {
  161. return Date.Add(
  162. EffectiveFinishTime()
  163. );
  164. }
  165. static Assignment()
  166. {
  167. LinkedProperties.Register<Assignment, ActivityCharge, bool>(ass => ass.ActivityLink.Charge, chg => chg.Chargeable, ass => ass.Charge.Chargeable);
  168. Classes.JobScope.LinkScopeProperties<Assignment>();
  169. }
  170. }
  171. }