- Модуль: timeman
- Путь к файлу: ~/bitrix/modules/timeman/lib/service/agent/violationnotifieragent.php
- Класс: BitrixTimemanServiceAgentViolationNotifierAgent
- Вызов: ViolationNotifierAgent::notifyIfShiftMissed
static function notifyIfShiftMissed($shiftPlanId)
{
$shiftPlan = DependencyManager::getInstance()->getShiftPlanRepository()->findActiveById(
$shiftPlanId,
['*', 'SHIFT'],
Query::filter()->where('SHIFT.DELETED', ShiftTable::DELETED_NO)
);
if (!$shiftPlan)
{
return '';
}
$shiftPlan->setMissedShiftAgentId(0);
DependencyManager::getInstance()->getShiftPlanRepository()->save($shiftPlan);
$schedule = DependencyManager::getInstance()->getScheduleRepository()
->findActiveByShiftId($shiftPlan->getShiftId(), ['*']);
if (!$schedule || !$schedule->isShifted())
{
return '';
}
$utcStart = $shiftPlan->obtainShift()->buildUtcStartByShiftplan($shiftPlan);
$minStart = clone $utcStart;
if ($schedule->getAllowedMaxShiftStartOffset() > 0)
{
$minStart->sub(new DateInterval('PT' . $schedule->getAllowedMaxShiftStartOffset() . 'S'));
}
$maxStart = clone $utcStart;
$maxStart->add(new DateInterval('PT' . $shiftPlan->obtainShift()->getDuration() . 'S'));
$record = DependencyManager::getInstance()->getWorktimeRepository()->findByUserShiftSchedule(
$shiftPlan->getUserId(),
$shiftPlan->obtainShift()->getId(),
$schedule->getId(),
['ID', 'RECORDED_START_TIMESTAMP'],
Query::filter()
->where('RECORDED_START_TIMESTAMP', '>=', $minStart->getTimestamp())
->where('RECORDED_START_TIMESTAMP', '<=', $maxStart->getTimestamp())
);
if ($record)
{
return '';
}
$userCodes = EntityCodesHelper::buildDepartmentCodes(
DependencyManager::getInstance()->getDepartmentRepository()->getAllUserDepartmentIds($shiftPlan->getUserId())
);
$userCodes[] = EntityCodesHelper::getAllUsersCode();
$userCodes[] = EntityCodesHelper::buildUserCode($shiftPlan->getUserId());
$violationRulesList = DependencyManager::getInstance()->getViolationRulesRepository()
->findAllByScheduleId(
$schedule->getId(),
[
'ID',
'ENTITY_CODE',
'MISSED_SHIFT_START',
'USERS_TO_NOTIFY',
],
Query::filter()
->where('MISSED_SHIFT_START', ViolationRulesTable::MISSED_SHIFT_IS_TRACKED)
->whereIn('ENTITY_CODE', $userCodes)
);
if ($violationRulesList->count() === 0)
{
return '';
}
$params = (new WorktimeViolationParams())
->setShift($shiftPlan->obtainShift())
->setSchedule($schedule)
->setRecord($record)
->setShiftPlan($shiftPlan);
$toUserIds = [];
$manager = DependencyManager::getInstance()
->getViolationManager();
foreach ($violationRulesList as $violationRules)
{
$params->setViolationRules($violationRules);
$violationResult = $manager->buildMissedShiftViolation($params);
if (empty($violationResult->getViolations()))
{
continue;
}
$toUserIds = array_merge(
$toUserIds,
$violationRules->getNotifyUserIds(ViolationRulesTable::USERS_TO_NOTIFY_SHIFT_MISSED_START, $shiftPlan->getUserId())
);
}
$toUserIds = array_unique($toUserIds);
if (empty($toUserIds))
{
return '';
}
$fromUser = DependencyManager::getInstance()
->getScheduleRepository()
->getUsersBaseQuery()
->addSelect('PERSONAL_GENDER')
->where('ID', $shiftPlan->getUserId())
->exec()
->fetch();
if (!$fromUser)
{
return '';
}
$name = UserHelper::getInstance()->getFormattedName($fromUser);
$gender = $fromUser['PERSONAL_GENDER'] === 'F' ? '_FEMALE' : '_MALE';
$notifyText = Loc::getMessage(
'TM_VIOLATION_NOTIFIER_AGENT_MISSED_SHIFT_NOTIFICATION' . $gender,
['#USER_NAME#' => $name]
);
foreach ($toUserIds as $toUserId)
{
$notificationParams = new NotificationParameters();
$notificationParams->messageType = 'S';
$notificationParams->fromUserId = $fromUser['ID'];
$notificationParams->toUserId = $toUserId;
$notificationParams->notifyType = 2;
$notificationParams->notifyModule = 'timeman';
$notificationParams->notifyEvent = 'shift_missed';
$notificationParams->notifyMessage = $notifyText;
DependencyManager::getInstance()
->getNotifier($schedule)
->sendMessage($notificationParams);
}
return '';
}