- Модуль: timeman
- Путь к файлу: ~/bitrix/modules/timeman/lib/service/schedule/shiftservice.php
- Класс: BitrixTimemanServiceScheduleShiftService
- Вызов: ShiftService::deleteFutureShiftPlans
public function deleteFutureShiftPlans($schedule, $shiftParam = null, $activeUserIds = [])
{
$result = (new ScheduleServiceResult())->setSchedule($schedule);
$shifts = ShiftCollection::buildShiftCollection($shiftParam);
if ($shiftParam === null)
{
$shifts = $this->shiftRepository->findShiftsBySchedule($schedule->getId());
}
if (empty($activeUserIds))
{
$activeUserIds = $this->shiftPlanRepository->findUserIdsByShiftIds($shifts->getIdList());
}
if ($shifts->count() === 0 || empty($activeUserIds))
{
return $result;
}
$deleteShifts = [];
$utcNow = TimeHelper::getInstance()->getUtcNowTimestamp();
foreach ($activeUserIds as $activeUserId)
{
$userToday = TimeHelper::getInstance()->getUserDateTimeNow($activeUserId);
$userToday->setTimezone(new DateTimeZone('UTC'));
$shiftPlan = (new ShiftPlan($default = false))
->setDateAssigned(new BitrixMainTypeDate($userToday->format(ShiftPlanTable::DATE_FORMAT), ShiftPlanTable::DATE_FORMAT))
->setUserId($activeUserId);
foreach ($shifts->getAll() as $shift)
{
$utcUserStart = $shift->buildUtcStartByShiftplan($shiftPlan);
if (!$utcUserStart)
{
continue;
}
if ($utcUserStart->getTimestamp() + $shift->getDuration() < $utcNow)
{
$utcUserStart->add(new DateInterval('P1D'));
}
$deleteShifts[$utcUserStart->format(ShiftPlanTable::DATE_FORMAT)][$shift->getId()][] = $activeUserId;
}
}
if (empty($deleteShifts))
{
return $result;
}
foreach ($deleteShifts as $dateFormatted => $items)
{
$userIds = [];
foreach ($items as $shiftId => $userIdsForShift)
{
$userIds = array_merge($userIds, $userIdsForShift);
}
$userIds = array_unique($userIds);
$filter = Query::filter()
->whereIn('USER_ID', $userIds)
->whereIn('SHIFT_ID', array_keys($items))
->where('DATE_ASSIGNED', '>=', new BitrixMainTypeDate($dateFormatted, ShiftPlanTable::DATE_FORMAT));
$shiftPlans = $this->shiftPlanRepository->findAllActive(['ID', 'MISSED_SHIFT_AGENT_ID'], $filter);
if (!empty($shiftPlans->getMissedShiftAgentIdList()))
{
$agentIds = $shiftPlans->getMissedShiftAgentIdList();
foreach ($agentIds as $index => $agentId)
{
if ($agentId <= 0)
{
unset($agentIds[$index]);
}
}
$this->worktimeAgentManager->deleteAgentsByIds(array_filter($agentIds));
}
if (!empty($shiftPlans->getIdList()))
{
$this->shiftPlanRepository->updateAll(
$shiftPlans->getIdList(),
[
'DELETED' => ShiftPlanTable::DELETED_YES,
'DELETED_AT' => TimeHelper::getInstance()->getUtcNowTimestamp(),
'MISSED_SHIFT_AGENT_ID' => 0,
]
);
}
}
return (new ScheduleServiceResult())->setSchedule($schedule);
}