EmployeeRoster.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. [Caption("Roster")]
  8. public class EmployeeRoster : Entity, IRemotable, IPersistent, INumericAutoIncrement<EmployeeRoster>, IOneToMany<Employee>,
  9. ILicense<TimeManagementLicense>
  10. {
  11. private static readonly Dictionary<string, Func<EmployeeRoster, TimeSpan>> times = new Dictionary<string, Func<EmployeeRoster, TimeSpan>>()
  12. {
  13. { "Start", x => x.Start },
  14. { "Finish", x => x.Finish },
  15. { "Break", x => x.Break },
  16. { "Start2", x => x.Start2 },
  17. { "Finish2", x => x.Finish2 },
  18. { "Break2", x => x.Break2 }
  19. };
  20. [NullEditor]
  21. public EmployeeLink Employee { get; set; }
  22. [IntegerEditor(Alignment = Alignment.MiddleCenter, Editable = Editable.Disabled)]
  23. [EditorSequence(1)]
  24. [Caption("Day")]
  25. public int Day { get; set; }
  26. [TextBoxEditor(Editable = Editable.Disabled, Width = 150)]
  27. [EditorSequence(2)]
  28. public string Description { get; set; }
  29. [CheckBoxEditor]
  30. [EditorSequence(3)]
  31. [Caption("Rostered On?")]
  32. public bool Enabled { get; set; }
  33. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  34. [EditorSequence(4)]
  35. public TimeSpan Start { get; set; }
  36. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  37. [EditorSequence(5)]
  38. public TimeSpan Finish { get; set; }
  39. [DurationEditor(Format = "hh:mm", Visible = Visible.Default, Editable = Editable.Enabled, Width = 70)]
  40. [EditorSequence(6)]
  41. public TimeSpan Break { get; set; }
  42. [CheckBoxEditor]
  43. [EditorSequence(7)]
  44. [Caption("Split")]
  45. public bool SplitShift { get; set; }
  46. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  47. [EditorSequence(8)]
  48. [Caption("Start 2")]
  49. public TimeSpan Start2 { get; set; }
  50. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  51. [EditorSequence(9)]
  52. [Caption("Finish 2")]
  53. public TimeSpan Finish2 { get; set; }
  54. [DurationEditor(Format = "hh:mm", Visible = Visible.Default, Editable = Editable.Enabled, Width = 70)]
  55. [EditorSequence(10)]
  56. [Caption("Break 2")]
  57. public TimeSpan Break2 { get; set; }
  58. [EditorSequence(11)]
  59. public EmployeeActivityLink UsualActivity { get; set; }
  60. [DoubleEditor(Format = "F2", Visible = Visible.Default, Editable = Editable.Disabled, Summary = Summary.Sum, Width = 70,
  61. Alignment = Alignment.MiddleCenter)]
  62. [EditorSequence(12)]
  63. [Caption("Total")]
  64. public double Duration { get; set; }
  65. public Expression<Func<EmployeeRoster, int>> AutoIncrementField()
  66. {
  67. return x => x.Day;
  68. }
  69. public Filter<EmployeeRoster> AutoIncrementFilter()
  70. {
  71. return new Filter<EmployeeRoster>(x => x.Employee.ID).IsEqualTo(Employee.ID);
  72. }
  73. protected override void Init()
  74. {
  75. base.Init();
  76. Employee = new EmployeeLink();
  77. UsualActivity = new EmployeeActivityLink();
  78. }
  79. protected override void DoPropertyChanged(string name, object before, object after)
  80. {
  81. base.DoPropertyChanged(name, before, after);
  82. if (times.ContainsKey(name))
  83. Duration = RecalculateTimes(name, (TimeSpan)after);
  84. }
  85. private double RecalculateTimes(string name, TimeSpan after)
  86. {
  87. double result = 0.0F;
  88. result = GetTime("Finish", name, after) - (GetTime("Start", name, after) + GetTime("Break", name, after));
  89. result = result + GetTime("Finish2", name, after) - (GetTime("Start2", name, after) + GetTime("Break2", name, after));
  90. return result;
  91. }
  92. private double GetTime(string column, string name, TimeSpan after)
  93. {
  94. if (string.Equals(column, name))
  95. return after.TotalHours;
  96. return times[column].Invoke(this).TotalHours;
  97. }
  98. }
  99. }