EmployeeStore.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace Comal.Stores
  7. {
  8. public class EmployeeStore : SchedulableStore<Employee>
  9. {
  10. protected override void BeforeSave(Employee entity)
  11. {
  12. //entity.HasAttachments = entity.Attachments.Count > 0;
  13. base.BeforeSave(entity);
  14. }
  15. private void LoadStandardLeaves(Employee entity)
  16. {
  17. var updates = new List<LeaveRequest>();
  18. var leaves = Provider.Load<StandardLeave>();
  19. foreach (var leave in leaves)
  20. {
  21. var request = new LeaveRequest().LoadFrom(leave);
  22. request.EmployeeLink.ID = entity.ID;
  23. request.Status = LeaveRequestStatus.Approved;
  24. updates.Add(request);
  25. }
  26. FindSubStore<LeaveRequest>().Save(updates, "Auto Generated on Employee Creation");
  27. }
  28. protected override void AfterSave(Employee entity)
  29. {
  30. base.AfterSave(entity);
  31. if (entity.HasOriginalValue(x => x.ID))
  32. {
  33. var origid = entity.GetOriginalValue(x => x.ID);
  34. if (origid == Guid.Empty)
  35. LoadStandardLeaves(entity);
  36. }
  37. var orig = entity.HasOriginalValue(x => x.FinishDate) ? entity.GetOriginalValue(x => x.FinishDate) : entity.FinishDate;
  38. if (entity.UserLink.IsValid() && orig.IsEmpty() != entity.FinishDate.IsEmpty())
  39. {
  40. var user = Provider.Load(new Filter<User>(x => x.ID).IsEqualTo(entity.UserLink.ID)).FirstOrDefault();
  41. if (user != null && user.Disabled == entity.FinishDate.IsEmpty())
  42. {
  43. user.Disabled = !entity.FinishDate.IsEmpty();
  44. Provider.Save(user);
  45. }
  46. }
  47. if (entity.CheckSetouts)
  48. {
  49. var others = Load(new Filter<Employee>(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.CheckSetouts).IsEqualTo(true));
  50. foreach (var other in others)
  51. {
  52. other.CheckSetouts = false;
  53. Save(other, "CheckSetouts is only allowed for one Employee");
  54. }
  55. }
  56. }
  57. }
  58. }