static function setStatusIdle($userId, $result = true, $idleStart = null)
{
$userId = intval($userId);
if ($userId <= 0 || !self::isActive() || !self::isRegisterIdle())
return false;
$addIdleStatus = (bool)$result;
$dateStart = (new BitrixMainTypeDateTime())->format('Y-m-d').' 00:00:00';
$orm = BitrixTimemanModelEntriesTable::getList(Array(
'select' => Array(
'ID',
'USER_ID',
'ABSENCE_ID' => 'ABSENCE.ID',
'ABSENCE_TIME_START' => 'ABSENCE.TIME_START',
'ABSENCE_DATE_START' => 'ABSENCE.DATE_START',
'ABSENCE_TYPE' => 'ABSENCE.TYPE',
),
'filter' => Array(
'=USER_ID' => $userId,
'>=DATE_START' => new BitrixMainDBSqlExpression('?', $dateStart),
'=DATE_FINISH' => null,
),
'runtime' => Array(
new BitrixMainEntityReferenceField(
'ABSENCE',
'BitrixTimemanModelAbsenceTable',
array(
"=ref.USER_ID" => "this.USER_ID",
"=ref.ACTIVE" => new BitrixMainDBSqlExpression('?', 'Y'),
">=ref.DATE_START" => new BitrixMainDBSqlExpression('?', $dateStart),
),
array("join_type"=>"left")
)
)
));
$todayStart = new BitrixMainTypeDateTime((new BitrixMainTypeDateTime())->format('Y-m-d').' 00:00:00', 'Y-m-d H:i:s');
$dayIsOpen = false;
$entryId = 0;
while ($entry = $orm->fetch())
{
$dayIsOpen = true;
$entryId = $entry['ID'];
if ($entry['ABSENCE_ID'])
{
if ($entry['ABSENCE_TYPE'] == self::TYPE_IDLE && $addIdleStatus)
{
$addIdleStatus = false;
continue;
}
$intersect = self::getIntersectWithCalendar($userId);
$autoAgree = false;
if (
(bool)$intersect
&& $entry['ABSENCE_DATE_START']->getTimestamp() < $intersect['DATE_FROM']->getTimestamp()
&& $intersect['DATE_FROM']->getTimestamp() > $todayStart->getTimestamp()
)
{
$dateFinish = $intersect['DATE_FROM'];
}
else
{
$dateFinish = new BitrixMainTypeDateTime();
$autoAgree = (bool)$intersect;
}
$timeFinish = $dateFinish->getTimestamp() - $todayStart->getTimestamp();
$duration = $timeFinish - $entry['ABSENCE_TIME_START'];
$fields = Array(
'ACTIVE' => 'N',
'DATE_FINISH' => $dateFinish,
'TIME_FINISH' => $timeFinish,
'DURATION' => $duration,
'SOURCE_FINISH' => self::SOURCE_IDLE_EVENT,
'IP_FINISH' => $_SERVER['REMOTE_ADDR'],
);
if ($autoAgree)
{
$fields['REPORT_TYPE'] = self::REPORT_TYPE_WORK;
$fields['REPORT_TEXT'] = Loc::getMessage('TIMEMAN_ABSENCE_REPORT_FROM_CALENDAR', ['#TITLE#' => $intersect['TITLE']]);
$fields['REPORT_CALENDAR_ID'] = $intersect['ID'];
}
BitrixTimemanModelAbsenceTable::update($entry['ABSENCE_ID'], $fields);
$martaSend = false;
if (!$autoAgree && self::isReportEnableForUser($entry['USER_ID'], self::convertSecondsToMinutes($duration)))
{
$martaSend = true;
BitrixPullEvent::add($entry['USER_ID'], Array(
'module_id' => 'timeman',
'command' => 'timeControlCommitAbsence',
'params' => Array(
'absenceId' => $entry['ABSENCE_ID'],
'dateStart' => date('c', $entry['ABSENCE_DATE_START']->getTimestamp()),
'dateFinish' => date('c', $dateFinish->getTimestamp()),
'duration' => $duration
)
));
}
if (
false // TODO remove debug case
&& self::isReportEnableForUser($entry['USER_ID'])
)
{
BitrixMainIOFile::putFileContents(
$_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/timecontrol-debug.log",
print_r([
'ACTION' => 'FINISH',
'USER_ID' => $entry['USER_ID'],
'MARTA_SEND' => $martaSend? 'Y':'N',
'AUTO_AGREE' => $autoAgree? 'Y':'N',
Array(
'absenceId' => $entry['ABSENCE_ID'],
'dateStart' => date('c', $entry['ABSENCE_DATE_START']->getTimestamp()),
'dateFinish' => date('c', $dateFinish->getTimestamp()),
'duration' => $duration
)
], 1),
BitrixMainIOFile::APPEND
);
}
}
}
if ($dayIsOpen && $addIdleStatus)
{
if (!$idleStart)
{
$idleStart = new BitrixMainTypeDateTime();
}
$timeStart = $idleStart->getTimestamp() - $todayStart->getTimestamp();
$result = BitrixTimemanModelAbsenceTable::add(Array(
'ENTRY_ID' => $entryId,
'USER_ID' => $userId,
'TYPE' => self::TYPE_IDLE,
'DATE_START' => $idleStart,
'TIME_START' => $timeStart,
'SOURCE_START' => self::SOURCE_IDLE_EVENT,
'IP_START' => $_SERVER['REMOTE_ADDR'],
));
if (
false // TODO remove debug case
&& $result && self::isReportEnableForUser($userId)
)
{
BitrixMainIOFile::putFileContents(
$_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/timecontrol-debug.log",
print_r([
'ACTION' => 'START',
'USER_ID' => $userId,
Array(
'absenceId' => $result->getId(),
'dateStart' => date('c', $idleStart->getTimestamp()),
)
], 1),
BitrixMainIOFile::APPEND
);
}
}
return true;
}