• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/history/invoicestatushistoryentry.php
  • Класс: Bitrix\Crm\History\InvoiceStatusHistoryEntry
  • Вызов: InvoiceStatusHistoryEntry::synchronize
static function synchronize($ownerID, array $entityFields = null)
{
	if(!is_int($ownerID))
	{
		$ownerID = (int)$ownerID;
	}

	if($ownerID <= 0)
	{
		throw new Main\ArgumentException('Owner ID must be greater than zero.', 'ownerID');
	}

	$latest = self::getLatest($ownerID);
	if(!is_array($latest))
	{
		return false;
	}

	if(!is_array($entityFields))
	{
		$dbResult = \CCrmInvoice::GetList(
			array(),
			array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'),
			false,
			false,
			array(
				'ID',
				'STATUS_ID',
				'DATE_INSERT',
				'DATE_UPDATE',
				'DATE_BILL',
				'DATE_PAY_BEFORE',
				'PAY_VOUCHER_DATE',
				'DATE_MARKED',
				'RESPONSIBLE_ID'
			)
		);
		$entityFields = is_object($dbResult) ? $dbResult->Fetch() : null;
		if(!is_array($entityFields))
		{
			return false;
		}
	}

	$responsibleID = isset($entityFields['RESPONSIBLE_ID']) ? (int)$entityFields['RESPONSIBLE_ID'] : 0;
	/** @var Date $insertDate */
	$insertDate = \CCrmDateTimeHelper::ParseDateString(
		isset($entityFields['DATE_INSERT']) ? $entityFields['DATE_INSERT'] : ''
	);
	if($insertDate === null)
	{
		$insertDate = new Date();
	}

	/** @var Date $billDate */
	$billDate = \CCrmDateTimeHelper::ParseDateString(
		isset($entityFields['DATE_BILL']) ? $entityFields['DATE_BILL'] : ''
	);
	if($billDate === null)
	{
		$billDate = $insertDate;
	}

	/** @var Date $payBeforeDate */
	$payBeforeDate = \CCrmDateTimeHelper::ParseDateString(
		isset($entityFields['DATE_PAY_BEFORE']) ? $entityFields['DATE_PAY_BEFORE'] : ''
	);
	if($payBeforeDate === null)
	{
		$payBeforeDate = new Date('9999-12-31', 'Y-m-d');
	}

	$latestResponsibleID = (int)$latest['RESPONSIBLE_ID'];
	/** @var Date $latestBillDate */
	$latestBillDate = $latest['BILL_DATE'];
	/** @var Date $latestPayBeforeDate */
	$latestPayBeforeDate = $latest['PAY_BEFORE_DATE'];

	if($responsibleID === $latestResponsibleID
		&& $billDate->getTimestamp() === $latestBillDate->getTimestamp()
		&& $payBeforeDate->getTimestamp() === $latestPayBeforeDate->getTimestamp())
	{
		return false;
	}

	InvoiceStatusHistoryTable::synchronize(
		$ownerID,
		array(
			'RESPONSIBLE_ID' => $responsibleID,
			'BILL_DATE' => $billDate,
			'PAY_BEFORE_DATE' => $payBeforeDate
		)
	);
	return true;
}