LeadSumStatisticEntry::register

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. LeadSumStatisticEntry
  4. register
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/statistics/leadsumstatisticentry.php
  • Класс: Bitrix\Crm\Statistics\LeadSumStatisticEntry
  • Вызов: LeadSumStatisticEntry::register
static function register($ownerID, array $entityFields = null, array $options = null)
{
	if(!is_int($ownerID))
	{
		$ownerID = (int)$ownerID;
	}

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

	if(!is_array($entityFields))
	{
		$dbResult = \CCrmLead::GetListEx(
			array(),
			array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'),
			false,
			false,
			array(
				'STATUS_ID', 'SOURCE_ID',
				'DATE_CREATE', 'DATE_MODIFY',
				'CURRENCY_ID', 'OPPORTUNITY',
				'ASSIGNED_BY_ID', 'UF_*'
			)
		);
		$entityFields = is_object($dbResult) ? $dbResult->Fetch() : null;
		if(!is_array($entityFields))
		{
			return;
		}
	}

	if(!is_array($options))
	{
		$options = array();
	}
	$forced = isset($options['FORCED']) ? $options['FORCED'] : false;

	$sourceID = isset($entityFields['SOURCE_ID']) ? $entityFields['SOURCE_ID'] : '';
	$statusID = isset($entityFields['STATUS_ID']) ? $entityFields['STATUS_ID'] : '';
	$semanticID = \CCrmLead::GetSemanticID($statusID);
	$isJunk = PhaseSemantics::isLost($semanticID);
	$responsibleID = isset($entityFields['ASSIGNED_BY_ID']) ? (int)$entityFields['ASSIGNED_BY_ID'] : 0;

	/** @var Date $date */
	$date = self::parseDateString(isset($entityFields['DATE_MODIFY']) ? $entityFields['DATE_MODIFY'] : '');
	if($date === null)
	{
		$date = new Date();
	}

	$day = (int)$date->format('d');
	$month = (int)$date->format('m');
	$quarter = $month <= 3 ? 1 : ($month <= 6 ? 2 : ($month <= 9 ? 3 : 4));
	$year = (int)$date->format('Y');

	$currencyID = isset($entityFields['CURRENCY_ID']) ? $entityFields['CURRENCY_ID'] : '';
	$accountingCurrencyID = \CCrmCurrency::GetAccountCurrencyID();
	$sum = isset($entityFields['OPPORTUNITY']) ? (double)$entityFields['OPPORTUNITY'] : 0.0;

	$bindingMap = self::getCurrent()->getSlotBindingMap();
	$binding = $bindingMap->get('SUM_TOTAL');
	if($binding === null)
	{
		$total = isset($entityFields['OPPORTUNITY']) ? (double)$entityFields['OPPORTUNITY'] : 0.0;
	}
	else
	{
		$bindingFieldName = $binding->getFieldName();
		if($bindingFieldName === '')
		{
			$bindingFieldName = 'OPPORTUNITY';
		}
		$total = isset($entityFields[$bindingFieldName]) ? (double)$entityFields[$bindingFieldName] : 0.0;

		if($bindingFieldName !== 'OPPORTUNITY' && $binding->getOption('ADD_PRODUCT_ROW_SUM') === 'Y')
		{
			$total += $sum;
		}
	}

	if($currencyID !== $accountingCurrencyID)
	{
		$accData = \CCrmAccountingHelper::PrepareAccountingData(
			array(
				'CURRENCY_ID' => $currencyID,
				'SUM' => $total,
				'EXCH_RATE' => isset($entityFields['EXCH_RATE']) ? $entityFields['EXCH_RATE'] : null
			)
		);
		if(is_array($accData))
		{
			$total = (double)$accData['ACCOUNT_SUM'];
		}
	}

	$sumSlots = array();
	$sumSlotFields = LeadSumStatisticsTable::getSumSlotFieldNames();
	foreach($sumSlotFields as $fieldName)
	{
		$binding = $bindingMap->get($fieldName);
		if($binding === null)
		{
			$slotSum = 0.0;
		}
		else
		{
			$bindingFieldName = $binding->getFieldName();
			$slotSum = $bindingFieldName !== '' && isset($entityFields[$bindingFieldName])
				? (double)$entityFields[$bindingFieldName] : 0.0;

			if($binding->getOption('ADD_PRODUCT_ROW_SUM') === 'Y')
			{
				$slotSum += $sum;
			}
		}

		if($currencyID !== $accountingCurrencyID)
		{
			$accData = \CCrmAccountingHelper::PrepareAccountingData(
				array(
					'CURRENCY_ID' => $currencyID,
					'SUM' => $slotSum,
					'EXCH_RATE' => isset($entityFields['EXCH_RATE']) ? $entityFields['EXCH_RATE'] : null
				)
			);
			if(is_array($accData))
			{
				$slotSum = (double)$accData['ACCOUNT_SUM'];
			}
		}

		$sumSlots[$fieldName] = $slotSum;
	}

	$latest = self::getLatest($ownerID);
	if(is_array($latest))
	{
		if($forced)
		{
			LeadSumStatisticsTable::synchronizeSumFields(
				$ownerID,
				array_merge(array('SUM_TOTAL' => $total), $sumSlots)
			);
		}

		//Update responsible user in all related records
		if($responsibleID !== (int)$latest['RESPONSIBLE_ID'])
		{
			LeadSumStatisticsTable::synchronize($ownerID, array('RESPONSIBLE_ID' => $responsibleID));
		}

		if($statusID === $latest['STATUS_ID']
			&& $currencyID === $latest['CURRENCY_ID']
			&& $total === (double)$latest['SUM_TOTAL']
			&& $sumSlots['UF_SUM_1'] === (double)$latest['UF_SUM_1']
			&& $sumSlots['UF_SUM_2'] === (double)$latest['UF_SUM_2']
			&& $sumSlots['UF_SUM_3'] === (double)$latest['UF_SUM_3']
			&& $sumSlots['UF_SUM_4'] === (double)$latest['UF_SUM_4']
			&& $sumSlots['UF_SUM_5'] === (double)$latest['UF_SUM_5'])
		{
			return;
		}
	}

	$data = array_merge(
		array(
			'OWNER_ID' => $ownerID,
			'CREATED_DATE' => $date,
			'PERIOD_YEAR' => $year,
			'PERIOD_QUARTER' => $quarter,
			'PERIOD_MONTH' => $month,
			'PERIOD_DAY' => $day,
			'RESPONSIBLE_ID' => $responsibleID,
			'STATUS_SEMANTIC_ID' => $semanticID,
			'STATUS_ID' => $statusID,
			'SOURCE_ID' => $sourceID,
			'IS_JUNK' => $isJunk ? 'Y' : 'N',
			'CURRENCY_ID' => $accountingCurrencyID,
			'SUM_TOTAL' => $total
		),
		$sumSlots
	);
	LeadSumStatisticsTable::upsert($data);
}

Добавить комментарий