using GB5Shared.DTO.Framework.Criteria;
using GB5Shared.DTO.Framework.Login;
using PayRollBLL.YearlyCalendar;
using PayRollDAL.CustomeCode.TeamCalendar;
using System;
using System.Threading.Tasks;
namespace PayRollBLL.TeamCalendar
{
public class TeamCalendarBLL : ITeamCalendarBLL
{
private readonly ITeamCalendarDAL _teamCalendarDAL;
private readonly IYearlyCalendarBLL _yearlyCalendarBLL;
public TeamCalendarBLL(
ITeamCalendarDAL teamCalendarDAL,
IYearlyCalendarBLL yearlyCalendarBLL)
{
// ── Guard: fail fast if dependencies are missing ───────────────
_teamCalendarDAL = teamCalendarDAL ?? throw new ArgumentNullException(nameof(teamCalendarDAL));
_yearlyCalendarBLL = yearlyCalendarBLL ?? throw new ArgumentNullException(nameof(yearlyCalendarBLL));
}
///
/// Returns calendar data as a serialized JSON string.
///
/// team = true → Multi-employee view
/// showAllEmployees = true → OU-based (all employees in given OUIDs filtered by status)
/// showAllEmployees = false → Hierarchy-based (junior team under employeeId filtered by level + status)
///
/// team = false → Single-employee yearly calendar (calls YearlyCalendarBLL)
///
public async Task GetFiltersTeamCalendar(
long employeeId,
int employeeLevel,
DateTime fromDate,
DateTime toDate,
bool showAllEmployees,
string ouIds,
byte employeeStatus,
bool team,
int firstNumber,
int maxNumber,
CriteriaDTO criteriaDTO,
LoginDTO loginDTO)
{
// ── Input validation ───────────────────────────────────────────
if (loginDTO == null)
throw new ArgumentNullException(nameof(loginDTO));
if (fromDate > toDate)
throw new ArgumentException(
$"fromDate ({fromDate:yyyy-MM-dd}) must be <= toDate ({toDate:yyyy-MM-dd}).");
if (firstNumber < 1)
firstNumber = 1;
if (maxNumber < firstNumber)
maxNumber = firstNumber;
// ── Route: Team vs Single employee ─────────────────────────────
if (team)
{
/*
* Multi-employee calendar
* ─────────────────────────────────────────────────────────
* showAllEmployees = true → OU based
* Fetches all employees whose WORKOUID is in ouIds,
* filtered by employeeStatus, paged by firstNumber/maxNumber.
*
* showAllEmployees = false → Hierarchy based
* Recursive CTE from employeeId downwards,
* filtered by employeeLevel + employeeStatus,
* paged by firstNumber/maxNumber.
*
* In both cases:
* - Calendar data is fetched via GET_TEAM_CALENDAR
* - Permission data via GET_TEAM_CALENDAR_PERMISSIONS
* - Both queries are scoped to the resolved employee CSV
* - Results are merged and returned as JSON
*/
return await _teamCalendarDAL.GetFiltersTeamCalendar(
employeeId,
employeeLevel,
fromDate,
toDate,
showAllEmployees,
ouIds,
employeeStatus,
team,
firstNumber,
maxNumber,
criteriaDTO,
loginDTO);
}
else
{
/*
* Single-employee yearly calendar
* ─────────────────────────────────────────────────────────
* Delegates to YearlyCalendarBLL which handles the
* SP_employeeCalendar procedure (individual pivot query).
* employeeLevel, showAllEmployees, ouIds, firstNumber,
* maxNumber are not relevant here.
*/
return await _yearlyCalendarBLL.GetYearlyCalendar(
employeeId,
fromDate,
toDate,
criteriaDTO,
loginDTO);
}
}
}
}