• Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/mailingmanager.php
  • Класс: BitrixSenderMailingManager
  • Вызов: MailingManager::checkPeriod
static function checkPeriod($isAgentExec = true)
{
	$isAgentExecInSetting = !RuntimeEnv::isReiteratedJobCron();
	if(($isAgentExec && !$isAgentExecInSetting) || (!$isAgentExec && $isAgentExecInSetting))
	{
			return "";
	}

	$dateTodayPhp = new DateTime();
	$datetimeToday = TypeDateTime::createFromPhp(clone $dateTodayPhp);
	$dateToday = clone $dateTodayPhp;
	$dateToday = TypeDate::createFromPhp($dateToday->setTime(0,0,0));
	$dateTomorrow = clone $dateTodayPhp;
	$dateTomorrow = TypeDate::createFromPhp($dateTomorrow->setTime(0,0,0))->add('1 DAY');
	$arDateFilter = array($dateToday, $dateTomorrow);

	$chainDb = MailingChainTable::getList(array(
		'select' => array(
			'ID', 'LAST_EXECUTED', 'POSTING_ID',
			'MONTHS_OF_YEAR', 'DAYS_OF_MONTH', 'DAYS_OF_WEEK', 'TIMES_OF_DAY'
		),
		'filter' => array(
			'=REITERATE' => 'Y',
			'=MAILING.ACTIVE' => 'Y',
			'=IS_TRIGGER' => 'N',
			'=STATUS' => MailingChainTable::STATUS_WAIT,
			//'!> $arDateFilter,
		)
	));
	while($arMailingChain = $chainDb->fetch())
	{
		$lastExecuted = $arMailingChain['LAST_EXECUTED'];
		/* @var BitrixMainTypeDateTime $lastExecuted*/
		if($lastExecuted && $lastExecuted->getTimestamp() >= $dateToday->getTimestamp())
		{
			continue;
		}


		$timeOfExecute = static::getDateExecute(
			$dateTodayPhp,
			$arMailingChain["DAYS_OF_MONTH"],
			$arMailingChain["DAYS_OF_WEEK"],
			$arMailingChain["TIMES_OF_DAY"],
			$arMailingChain["MONTHS_OF_YEAR"]
		);

		if($timeOfExecute)
		{
			$arUpdateMailChain = array('LAST_EXECUTED' => $datetimeToday);

			$postingDb = PostingTable::getList(array(
				'select' => array('ID'),
				'filter' => array(
					'=MAILING_CHAIN_ID' => $arMailingChain['ID'],
					'> $arDateFilter
				)
			));
			$arPosting = $postingDb->fetch();
			if(!$arPosting)
			{
				$postingId = MailingChainTable::initPosting($arMailingChain['ID']);
			}
			else
			{
				$postingId = $arPosting['ID'];
				$arUpdateMailChain['POSTING_ID'] = $postingId;
				PostingTable::initGroupRecipients($postingId);
			}

			if ($postingId)
			{
				$arUpdateMailChain['STATUS'] = MailingChainTable::STATUS_SEND;
				$arUpdateMailChain['AUTO_SEND_TIME'] = TypeDateTime::createFromPhp($timeOfExecute);
			}

			$result = ModelLetterTable::update($arMailingChain['ID'], $arUpdateMailChain);
			if (!$result->isSuccess())
			{
				return "";
			}
		}
	}

	(new RuntimeReiteratedJob())->actualize();
	return '';
}