EmployeeRosterItem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. public class EmployeeRosterItem : BaseObject, IImportable, IExportable
  8. {
  9. private static readonly Dictionary<string, Func<EmployeeRosterItem, TimeSpan>> times = new Dictionary<string, Func<EmployeeRosterItem, TimeSpan>>()
  10. {
  11. { "Start", x => x.Start },
  12. { "Finish", x => x.Finish },
  13. { "Break", x => x.Break },
  14. { "Start2", x => x.Start2 },
  15. { "Finish2", x => x.Finish2 },
  16. { "Break2", x => x.Break2 }
  17. };
  18. [IntegerEditor(Alignment = Alignment.MiddleCenter, Editable = Editable.Disabled)]
  19. [EditorSequence(1)]
  20. [Caption("Day")]
  21. public int Day { get; set; }
  22. [TextBoxEditor(Editable = Editable.Disabled)]
  23. [EditorSequence(2)]
  24. public string Description { get; set; }
  25. [CheckBoxEditor]
  26. [EditorSequence(3)]
  27. [Caption("Rostered On?")]
  28. public bool Enabled { get; set; }
  29. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  30. [EditorSequence(4)]
  31. public TimeSpan Start { get; set; }
  32. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  33. [EditorSequence(5)]
  34. public TimeSpan Finish { get; set; }
  35. [DurationEditor(Format = "hh:mm", Visible = Visible.Default, Editable = Editable.Enabled, Width = 70)]
  36. [EditorSequence(6)]
  37. public TimeSpan Break { get; set; }
  38. [CheckBoxEditor]
  39. [EditorSequence(7)]
  40. [Caption("Split")]
  41. public bool SplitShift { get; set; }
  42. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  43. [EditorSequence(8)]
  44. [Caption("Start 2")]
  45. public TimeSpan Start2 { get; set; }
  46. [TimeOfDayEditor(Format = "hh:mm", Width = 70)]
  47. [EditorSequence(9)]
  48. [Caption("Finish 2")]
  49. public TimeSpan Finish2 { get; set; }
  50. [DurationEditor(Format = "hh:mm", Visible = Visible.Default, Editable = Editable.Enabled, Width = 70)]
  51. [EditorSequence(10)]
  52. [Caption("Break 2")]
  53. public TimeSpan Break2 { get; set; }
  54. [DoubleEditor(Format = "F2", Visible = Visible.Default, Editable = Editable.Disabled, Summary = Summary.Sum, Width = 70,
  55. Alignment = Alignment.MiddleCenter)]
  56. [EditorSequence(12)]
  57. [Caption("Total")]
  58. public double Duration { get; set; }
  59. protected override void DoPropertyChanged(string name, object before, object after)
  60. {
  61. base.DoPropertyChanged(name, before, after);
  62. if (times.ContainsKey(name))
  63. Duration = RecalculateTimes(name, (TimeSpan)after);
  64. }
  65. private double RecalculateTimes(string name, TimeSpan after)
  66. {
  67. double result = 0.0F;
  68. result = GetTime("Finish", name, after) - (GetTime("Start", name, after) + GetTime("Break", name, after));
  69. result = result + GetTime("Finish2", name, after) - (GetTime("Start2", name, after) + GetTime("Break2", name, after));
  70. return result;
  71. }
  72. private double GetTime(string column, string name, TimeSpan after)
  73. {
  74. if (string.Equals(column, name))
  75. return after.TotalHours;
  76. return times[column].Invoke(this).TotalHours;
  77. }
  78. public static EmployeeRosterItem[] FromJSON(String? json)
  79. {
  80. return !String.IsNullOrWhiteSpace(json)
  81. ? Serialization.Deserialize<List<EmployeeRosterItem>>(json)?.OrderBy(x => x.Day).ToArray()
  82. ?? new EmployeeRosterItem[] { }
  83. : new EmployeeRosterItem[] { };
  84. }
  85. public static String ToJSON(EmployeeRosterItem[] items)
  86. {
  87. return Serialization.Serialize(items);
  88. }
  89. }
  90. }