1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class EmployeeStore : SchedulableStore<Employee>
- {
- protected override void BeforeSave(Employee entity)
- {
- //entity.HasAttachments = entity.Attachments.Count > 0;
- base.BeforeSave(entity);
- }
- private void LoadStandardLeaves(Employee entity)
- {
- var updates = new List<LeaveRequest>();
- var leaves = Provider.Load<StandardLeave>();
- foreach (var leave in leaves)
- {
- var request = new LeaveRequest().LoadFrom(leave);
- request.EmployeeLink.ID = entity.ID;
- request.Status = LeaveRequestStatus.Approved;
- updates.Add(request);
- }
- FindSubStore<LeaveRequest>().Save(updates, "Auto Generated on Employee Creation");
- }
- protected override void AfterSave(Employee entity)
- {
- base.AfterSave(entity);
- if (entity.HasOriginalValue(x => x.ID))
- {
- var origid = entity.GetOriginalValue(x => x.ID);
- if (origid == Guid.Empty)
- LoadStandardLeaves(entity);
- }
- var orig = entity.HasOriginalValue(x => x.FinishDate) ? entity.GetOriginalValue(x => x.FinishDate) : entity.FinishDate;
- if (entity.UserLink.IsValid() && orig.IsEmpty() != entity.FinishDate.IsEmpty())
- {
- var user = Provider.Load(new Filter<User>(x => x.ID).IsEqualTo(entity.UserLink.ID)).FirstOrDefault();
- if (user != null && user.Disabled == entity.FinishDate.IsEmpty())
- {
- user.Disabled = !entity.FinishDate.IsEmpty();
- Provider.Save(user);
- }
- }
- if (entity.CheckSetouts)
- {
- var others = Load(new Filter<Employee>(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.CheckSetouts).IsEqualTo(true));
- foreach (var other in others)
- {
- other.CheckSetouts = false;
- Save(other, "CheckSetouts is only allowed for one Employee");
- }
- }
- }
- }
- }
|