• Модуль: timeman
  • Путь к файлу: ~/bitrix/modules/timeman/lib/service/worktime/action/shiftsmanager.php
  • Класс: BitrixTimemanServiceWorktimeActionShiftsManager
  • Вызов: ShiftsManager::buildShiftWithDate
private function buildShiftWithDate(DateTime $userDateTime, $checkRelevant, ?ShiftWithDate $previousShitWithDate = null): ?ShiftWithDate
{
	if (!$this->activeSchedules->count() === 0 || $this->activeSchedules->hasFlextime() ||
		$this->activeSchedules->obtainActiveShifts()->count() === 0)
	{
		return null;
	}
	/** @var ShiftWithDate[] $nextShiftsWithDates */
	$nextShiftsWithDates = [];
	foreach ($this->activeSchedules->getAll() as $schedule)
	{
		$daysToCheck = $checkRelevant ? $this->getSearchDaysForRelevantShift($schedule) : $this->getSearchDaysForNextShift($schedule);
		$nextShiftsWithDates = array_merge($nextShiftsWithDates, $this->calculateShiftsWithDate($schedule, $userDateTime, $checkRelevant, $daysToCheck, $previousShitWithDate));
	}
	$nextShiftsWithDates = array_filter($nextShiftsWithDates);

	if (count($nextShiftsWithDates) <= 1)
	{
		return empty($nextShiftsWithDates) ? null : reset($nextShiftsWithDates);
	}
	else
	{
		$shiftedShifts = [];
		$fixedShifts = [];
		foreach ($nextShiftsWithDates as $shiftWithDate)
		{
			if ($shiftWithDate->getSchedule()->isShifted())
			{
				$shiftedShifts[$shiftWithDate->getDateTimeStart()->getTimestamp()] = $shiftWithDate;
			}
			else
			{
				$fixedShifts[$shiftWithDate->getDateTimeStart()->getTimestamp()] = $shiftWithDate;
			}
		}
		if (count($shiftedShifts) > 1)
		{
			ksort($shiftedShifts);
			if ($checkRelevant)
			{
				foreach ($shiftedShifts as $shiftedShift)
				{
					/** @var ShiftWithDate $shiftedShift */
					if ($this->hasShiftPlan($shiftedShift))
					{
						return $shiftedShift;
					}
				}
			}
			return reset($shiftedShifts);
		}
		if (count($fixedShifts) > 0)
		{
			if (count($fixedShifts) > 1)
			{
				ksort($fixedShifts);
			}
			return reset($fixedShifts);
		}
		return empty($shiftedShifts) ? null : reset($shiftedShifts);
	}
}