• Модуль: timeman
  • Путь к файлу: ~/bitrix/modules/timeman/lib/absence.php
  • Класс: BitrixTimemanAbsence
  • Вызов: Absence::searchOfflineDesktopUsersWithActiveDayAgent
static function searchOfflineDesktopUsersWithActiveDayAgent()
{
	if (
		!self::isActive()
		|| !self::isRegisterDesktop()
		|| !BitrixMainLoader::includeModule('im')
	)
	{
		return "BitrixTimemanAbsence::searchOfflineDesktopUsersWithActiveDayAgent();";
	}

	$desktopLastDate = (new BitrixMainTypeDateTime())->format('Y-m-d').' 00:00:00';

	$filter = Array(
		'>=DATE_START' => new BitrixMainDBSqlExpression('?', $desktopLastDate),
		'=DATE_FINISH' => null,
		'=USER.IS_ONLINE' => 'Y',
	);

	$requestReport = BitrixMainConfigOption::get('timeman', 'request_report', "0");
	if ($requestReport == "1")
	{
	}
	else if ($requestReport == "0")
	{
		return "BitrixTimemanAbsence::searchOfflineDesktopUsersWithActiveDayAgent();";
	}
	else
	{
		$filter['=USER_ID'] = Json::decode($requestReport);
	}

	$orm = BitrixTimemanModelEntriesTable::getList(Array(
		'select' => Array(
			'ID',
			'USER_ID',
			'DATE_START',
			'DESKTOP_LAST_DATE' => 'STATUS.DESKTOP_LAST_DATE',
			'ABSENCE_ID' => 'ABSENCE.ID',
			'ABSENCE_ACTIVE' => 'ABSENCE.ACTIVE',
			'ABSENCE_TYPE' => 'ABSENCE.TYPE',
			'ABSENCE_TIME_START' => 'ABSENCE.TIME_START',
			'ABSENCE_DATE_START' => 'ABSENCE.DATE_START',
		),
		'filter' => $filter,
		'runtime' => Array(
			new BitrixMainEntityReferenceField(
				'ABSENCE',
				'BitrixTimemanModelAbsenceTable',
				array(
					"=ref.USER_ID" => "this.USER_ID",
					">=ref.DATE_START" => new BitrixMainDBSqlExpression('?', $desktopLastDate),
				),
				array("join_type"=>"left")
			),
			new BitrixMainEntityReferenceField(
				'STATUS',
				'BitrixImModelStatusTable',
				array(
					"=ref.USER_ID" => "this.USER_ID",
				),
				array("join_type"=>"left")
			)
		),
	));

	$todayStart = new BitrixMainTypeDateTime((new BitrixMainTypeDateTime())->format('Y-m-d').' 00:00:00', 'Y-m-d H:i:s');

	$users = [];
	while($entry = $orm->fetch())
	{
		if (
			isset($users[$entry['USER_ID']])
			&& (int)$users[$entry['USER_ID']]['ABSENCE_ID'] >= (int)$entry['ABSENCE_ID']
		)
		{
			continue;
		}

		$users[$entry['USER_ID']] = $entry;
	}

	foreach ($users as $userId => $entry)
	{
		if (in_array($entry['ABSENCE_TYPE'], [self::TYPE_DESKTOP_OFFLINE, self::TYPE_OFFLINE]))
		{
			continue;
		}

		$dateNow = new BitrixMainTypeDateTime();

		$dateCheck = new BitrixMainTypeDateTime();
		$dateCheck->add('-'.BitrixMainUserTable::getSecondsForLimitOnline().' SECONDS');

		$desktopLastDateServer = $entry['DESKTOP_LAST_DATE'] instanceof BitrixMainTypeDateTime? $entry['DESKTOP_LAST_DATE']: false;
		$desktopLastDate = $desktopLastDateServer? $entry['DESKTOP_LAST_DATE']: $dateCheck;

		if ($desktopLastDate->getTimestamp() > $dateCheck->getTimestamp())
		{
			continue;
		}

		// give 10 minutes to get from the authorization terminal to the workplace
		if (
			$desktopLastDate->getTimestamp() < $entry['DATE_START']->getTimestamp()
			&& $dateNow->getTimestamp() < $entry['DATE_START']->getTimestamp()+600
		)
		{
			continue;
		}

		// if the time of the last activity of the desktop is less than the beginning of the working day
		if ($desktopLastDate->getTimestamp() < $entry['DATE_START']->getTimestamp())
		{
			$desktopLastDate = $dateNow;
		}

		$desktopLastTime = $desktopLastDate->getTimestamp() - $todayStart->getTimestamp();
		$desktopLastTimeText = ($desktopLastDateServer? $desktopLastDateServer->format(DateTime::getFormat()): Loc::getMessage('TIMEMAN_ABSENCE_EMPTY_INFO'));

		BitrixTimemanModelAbsenceTable::add(Array(
			'ENTRY_ID' => $entry['ID'],
			'USER_ID' => $entry['USER_ID'],
			'DATE_START' => $desktopLastDate,
			'TIME_START' => $desktopLastTime,
			'DATE_FINISH' => $desktopLastDate,
			'TIME_FINISH' => $desktopLastTime,
			'DURATION' => 0,
			'ACTIVE' => 'N',
			'SYSTEM_TEXT' => Loc::getMessage('TIMEMAN_ABSENCE_TEXT_DESKTOP_LAST_DATE', ['#TIME#' => $desktopLastTimeText]),
			'TYPE' => self::TYPE_DESKTOP_OFFLINE,
			'SOURCE_START' => self::SOURCE_DESKTOP_OFFLINE_AGENT,
		));
	}

	return "BitrixTimemanAbsence::searchOfflineDesktopUsersWithActiveDayAgent();";
}