• Модуль: sender
  • Путь к файлу: ~/bitrix/modules/sender/lib/trigger/manager.php
  • Класс: BitrixSenderTriggerManager
  • Вызов: Manager::stop
static function stop($chain, $data, $setGoal)
{
	if(!$data || empty($data['EMAIL']))
	{
		return;
	}

	$code = $data['EMAIL'];
	$typeId = RecipientType::detect($data['EMAIL']);
	$code = RecipientNormalizer::normalize($code, $typeId);

	// if mailing continue, then stop it
	$recipientDb = PostingRecipientTable::getList(array(
		'select' => array('ID', 'ROOT_ID', 'POSTING_ID', 'STATUS', 'POSTING_STATUS' => 'POSTING.STATUS'),
		'filter' => array(
			'=CONTACT.CODE' => $code,
			'=CONTACT.TYPE_ID' => $typeId,
			'=POSTING.MAILING_ID' => $chain['MAILING_ID'],
			'=STATUS' => array(
				PostingRecipientTable::SEND_RESULT_NONE,
				PostingRecipientTable::SEND_RESULT_WAIT,
			)
		),
		'limit' => 1
	));
	if($recipient = $recipientDb->fetch())
	{
		// if mailing continue, then stop it and the next was riched
		$updateFields = array('STATUS' => PostingRecipientTable::SEND_RESULT_DENY);
		ModelPostingRecipientTable::update($recipient['ID'], $updateFields);

		// change status of posting if all emails sent
		if(!in_array($recipient['POSTING_STATUS'], array(PostingTable::STATUS_NEW, PostingTable::STATUS_PART)))
		{
			$recipientCountDb = PostingRecipientTable::getList(array(
				'select' => array('POSTING_ID'),
				'filter' => array(
					'=POSTING_ID' => $recipient['POSTING_ID'],
					'=STATUS' => array(
						PostingRecipientTable::SEND_RESULT_NONE,
						PostingRecipientTable::SEND_RESULT_WAIT,
					)
				),
				'limit' => 1
			));
			if(!$recipientCountDb->fetch())
			{
				ModelPostingTable::update($recipient['POSTING_ID'], ['STATUS' => PostingTable::STATUS_SENT]);
			}
		}
	}

	if(!$setGoal)
	{
		return;
	}

	// set flag of taking the goal to last success sending
	$recipientDb = PostingRecipientTable::getList(array(
		'select' => array('ID', 'DATE_DENY'),
		'filter' => array(
			'=CONTACT.CODE' => $code,
			'=CONTACT.TYPE_ID' => $typeId,
			'=POSTING.MAILING_ID' => $chain['MAILING_ID'],
			'=STATUS' => array(
				PostingRecipientTable::SEND_RESULT_SUCCESS
			)
		),
		'order' => array('DATE_SENT' => 'DESC', 'ID' => 'DESC'),
		'limit' => 1
	));
	if($recipient = $recipientDb->fetch())
	{
		if(empty($recipient['DATE_DENY']))
		{
			ModelPostingRecipientTable::update($recipient['ID'], ['DATE_DENY' => new DateTime]);
		}
	}
}