• Модуль: timeman
  • Путь к файлу: ~/bitrix/modules/timeman/lib/service/schedule/shiftplanservice.php
  • Класс: BitrixTimemanServiceScheduleShiftPlanService
  • Вызов: ShiftPlanService::checkOverlappingPlans
private function checkOverlappingPlans(ShiftPlanForm $shiftPlanForm, Shift $shift)
{
	$shiftStart = $shift->buildUtcStartByUserId($shiftPlanForm->userId, $shiftPlanForm->getDateAssignedUtc());
	$shiftStart->setTimezone(TimeHelper::getInstance()->getUserTimezone($shiftPlanForm->userId));

	$shiftWithDate = new ShiftWithDate($shift, $shift->obtainSchedule(), $shiftStart);

	$dateFrom = clone $shiftWithDate->getDateTimeStart();
	$dateFrom->sub(new DateInterval('P1D'));

	$dateTo = clone $shiftWithDate->getDateTimeStart();
	$dateTo->add(new DateInterval('P1D'));
	$collection = $this->shiftPlanRepository->findAllByUserDates(
		$shiftPlanForm->userId,
		new Date($dateFrom->format('Y-m-d'), 'Y-m-d'),
		new Date($dateTo->format('Y-m-d'), 'Y-m-d')
	);

	$overlappingShifts = [];
	foreach ($collection->getAll() as $shiftPlan)
	{
		$start = $shiftPlan->buildShiftStartDateTimeUtc($shiftPlan->obtainShift());
		if (!$start)
		{
			continue;
		}
		$start->setTimezone(TimeHelper::getInstance()->getUserTimezone($shiftPlanForm->userId));
		$comparing = new ShiftWithDate($shiftPlan->obtainShift(), $shiftPlan->obtainSchedule(), $start);
		if ($comparing->isEqualsTo($shiftWithDate)
			||
			$comparing->getDateTimeStart()->getTimestamp() >= $shiftWithDate->getDateTimeEnd()->getTimestamp()
			||
			$comparing->getDateTimeEnd()->getTimestamp() <= $shiftWithDate->getDateTimeStart()->getTimestamp()
		)
		{
			continue;
		}
		$overlappingShifts[] = $comparing;
	}

	if (count($overlappingShifts) > 0)
	{
		foreach ($overlappingShifts as $otherShiftWithDate)
		{
			return (new ShiftPlanServiceResult())
				->setShiftWithDate($otherShiftWithDate)
				->addError(new Error(
					'Creating shift plan overlaps with another shift plan',
					ShiftPlanServiceResult::ERROR_CODE_OVERLAPPING_SHIFT_PLAN
				));
		}
	}
	return new ShiftPlanServiceResult();
}