• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/lead.php
  • Класс: Bitrix\Crm\Volume\Lead
  • Вызов: Lead::clearEntity
public function clearEntity()
{
	if (!$this->canClearEntity())
	{
		return -1;
	}

	$connection = \Bitrix\Main\Application::getConnection();

	$query = $this->prepareQuery();

	$dropped = -1;
	if ($this->prepareFilter($query))
	{
		$query
			->addSelect('ID', 'LEAD_ID')
			->setLimit(self::MAX_ENTITY_PER_INTERACTION)
			->setOrder(array('ID' => 'ASC'))
		;

		if ($this->getProcessOffset() > 0)
		{
			$query->where('ID', '>', $this->getProcessOffset());
		}

		$res = $query->exec();

		$dropped = 0;
		$entity = new \CCrmLead(false);
		while ($lead = $res->fetch())
		{
			$this->setProcessOffset($lead['LEAD_ID']);

			$connection->startTransaction();

			if ($entity->Delete($lead['LEAD_ID'], array('CURRENT_USER' => $this->getOwner())) !== false)
			{
				$connection->commitTransaction();
				$this->incrementDroppedEntityCount();
				$dropped ++;
			}
			else
			{
				$connection->rollbackTransaction();

				$err = '';
				global $APPLICATION;
				if ($APPLICATION instanceof \CMain)
				{
					$err = $APPLICATION->GetException();
				}
				if ($err == '')
				{
					$err = 'Deletion failed with lead #'.$lead['LEAD_ID'];
				}
				$this->collectError(new Main\Error($err, self::ERROR_DELETION_FAILED));

				$this->incrementFailCount();
			}

			if ($this->hasTimeLimitReached())
			{
				break;
			}
		}
	}
	else
	{
		$this->collectError(new Main\Error('Filter error', self::ERROR_DELETION_FAILED));
	}

	return $dropped;
}