Assignment.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Job { get; set; }
  59. [NullEditor]
  60. [Obsolete("Replaced with Job")]
  61. public JobLink JobLink { get; set; }
  62. private class JobITPLookup : LookupDefinitionGenerator<JobITP, Assignment>
  63. {
  64. public override Filter<JobITP> DefineFilter(Assignment[] items)
  65. {
  66. if (items.Length == 1)
  67. return Filter<JobITP>.Where(x => x.Job.ID).IsEqualTo(items.First().Job.ID);
  68. return LookupFactory.DefineFilter<JobITP>();
  69. }
  70. public override Columns<Assignment> DefineFilterColumns()
  71. => Columns.None<Assignment>().Add(x => x.Job.ID);
  72. }
  73. [LookupDefinition(typeof(JobITPLookup))]
  74. [EditorSequence(10)]
  75. public JobITPLink ITP { get; set; }
  76. [NullEditor]
  77. [Obsolete("Replaced with Actual.Start", true)]
  78. public TimeSpan Start { get; set; }
  79. [NullEditor]
  80. [Obsolete("Replaced with Actual.Duration", true)]
  81. public TimeSpan Duration { get; set; }
  82. [NullEditor]
  83. [Obsolete("Replaced with Actual.Finish", true)]
  84. public TimeSpan Finish { get; set; }
  85. [EditorSequence(11)]
  86. [CoreTimeEditor]
  87. public TimeBlock Booked { get; set; }
  88. [EditorSequence(12)]
  89. [CoreTimeEditor]
  90. public TimeBlock Actual { get; set; }
  91. [TimestampEditor]
  92. [RequiredColumn]
  93. [EditorSequence(13)]
  94. public DateTime Completed { get; set; }
  95. [Editable(Editable.Disabled)]
  96. [Comment("The duration this Assignment was treated as having for costing purposes")]
  97. [EditorSequence("Processing", 1)]
  98. public double CostedHours { get; set; }
  99. [CurrencyEditor]
  100. [Editable(Editable.Disabled)]
  101. [Comment("Cost of the Assignment; calculated from duration, overtime rules and the employee's hourly rate.")]
  102. [EditorSequence("Processing", 2)]
  103. public double Cost { get; set; }
  104. [EditorSequence("Processing", 3)]
  105. public ActualCharge Charge { get; set; }
  106. [TimestampEditor]
  107. [EditorSequence("Processing", 4)]
  108. public DateTime Processed { get; set; }
  109. [NullEditor]
  110. [EntityRelationship(DeleteAction.Cascade)]
  111. public LeaveRequestLink LeaveRequestLink { get; set; }
  112. [NullEditor]
  113. [EntityRelationship(DeleteAction.Cascade)]
  114. public DeliveryLink Delivery { get; set; }
  115. [NullEditor]
  116. [EntityRelationship(DeleteAction.SetNull)]
  117. public InvoiceLink Invoice { get; set; }
  118. [NullEditor]
  119. public MeetingDetails Meeting { get; set; }
  120. private class JobScopeLookup : LookupDefinitionGenerator<JobScope, Assignment>
  121. {
  122. public override Filter<JobScope> DefineFilter(Assignment[] items)
  123. {
  124. var item = items?.Length == 1 ? items[0] : null;
  125. if (item != null)
  126. return Filter<JobScope>.Where(x => x.Job.ID).IsEqualTo(item.Job.ID).And(x => x.Status.Approved).IsEqualTo(true);
  127. return Filter.None<JobScope>();
  128. }
  129. public override Columns<Assignment> DefineFilterColumns()
  130. => Columns.None<Assignment>().Add(x => x.Job.ID);
  131. }
  132. [LookupDefinition(typeof(JobScopeLookup))]
  133. public JobScopeLink JobScope { get; set; }
  134. public Expression<Func<Assignment, int>> AutoIncrementField()
  135. {
  136. return x => x.Number;
  137. }
  138. public Filter<Assignment> AutoIncrementFilter()
  139. {
  140. return null;
  141. }
  142. public int AutoIncrementDefault() => 1;
  143. public static TimeSpan EffectiveTime(TimeSpan actual, TimeSpan booked) => actual.Ticks != 0L ? actual : booked;
  144. public TimeSpan EffectiveStartTime()
  145. {
  146. return EffectiveTime(Actual.Start, Booked.Start);
  147. }
  148. public DateTime EffectiveStart()
  149. {
  150. return Date.Add(EffectiveStartTime());
  151. }
  152. public TimeSpan EffectiveFinishTime()
  153. {
  154. // If we have an actual finish, always use that
  155. // otherwise use EffectiveStart() + booked.duration
  156. return EffectiveTime(
  157. Actual.Finish,
  158. EffectiveTime(Actual.Start, Booked.Start)
  159. .Add(Booked.Duration)
  160. );
  161. }
  162. public DateTime EffectiveFinish()
  163. {
  164. return Date.Add(
  165. EffectiveFinishTime()
  166. );
  167. }
  168. static Assignment()
  169. {
  170. LinkedProperties.Register<Assignment, ActivityCharge, bool>(ass => ass.ActivityLink.Charge, chg => chg.Chargeable, ass => ass.Charge.Chargeable);
  171. Classes.JobScope.LinkScopeProperties<Assignment>();
  172. }
  173. }
  174. }