• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/report/handler/customers/regularcustomers.php
  • Класс: Bitrix\Crm\Integration\Report\Handler\Customers\RegularCustomers
  • Вызов: RegularCustomers::getMultipleGroupedData
public function getMultipleGroupedData()
{
	$calculatedData = $this->getCalculatedData();
	$baseCurrency = \CCrmCurrency::GetAccountCurrencyID();
	$totalEarnings = 0;

	$normalizedData = [];
	foreach ($calculatedData as $row)
	{
		[$type, $currency, $dealCount, $clientCount, $amount] = array_values($row);

		if($currency != $baseCurrency)
		{
			$amount = \CCrmCurrency::ConvertMoney($amount, $currency, $baseCurrency);
		}

		[$min, $max] = static::findGroup($dealCount);
		$group = $min . "_" . $max;

		if(!$normalizedData[$group])
		{
			$normalizedData[$group] = [
				'countTotal' => 0,
				'countCompany' => 0,
				'countContact' => 0,
				'amountCompany' => 0,
				'amountContact' => 0
			];
		}

		$normalizedData[$group]['countTotal'] += $clientCount;
		$totalEarnings += $amount;
		if($type == \CCrmOwnerType::ContactName)
		{
			$normalizedData[$group]['countContact'] += $clientCount;
			$normalizedData[$group]['amountContact'] += $amount;
		}
		else // if ($type == \CCrmOwnerType::CompanyName)
		{
			$normalizedData[$group]['countCompany'] += $clientCount;
			$normalizedData[$group]['amountCompany'] += $amount;
		}
	}

	$items = [];
	$labels = [];
	$i = 0;
	foreach ($normalizedData as $group => $fields)
	{
		[$min, $max] = explode("_", $group);
		$labels[$group] = static::getGroupLabel($min, $max);

		$totalAmount = $fields['amountCompany'] + $fields['amountContact'];
		$totalAmountFormatted = \CCrmCurrency::MoneyToString($totalAmount, $baseCurrency);

		$items[] = [
			'groupBy' => $group,
			'value' => $fields['countTotal'],
			'balloon' => [
				'color' => static::COLORS[$i++ % 14],
				'countContact' => $fields['countContact'],
				'countCompany' => $fields['countCompany'],
				'contactsUrl' => $this->getTargetUrl('/crm/contact/analytics/list/', ['OWNER_TYPE' => \CCrmOwnerType::ContactName, 'DEALS_from' => $min, 'DEALS_to' => $max]),
				'companiesUrl' => $this->getTargetUrl('/crm/company/analytics/list/', ['OWNER_TYPE' => \CCrmOwnerType::CompanyName, 'DEALS_from' => $min, 'DEALS_to' => $max]),
				'countClients' => $fields['countContact'] + $fields['countCompany'],
				'totalAmountFormatted' => $totalAmountFormatted,
				'earningsPercent' => $totalEarnings > 0 ? round(($totalAmount / $totalEarnings) * 100, 2) : 0
			]
			//"label" => $fields['countTotal']
		];
	}

	$result = [
		"items" => $items,
		"config" => [
			"groupsLabelMap" => $labels,
			"reportTitle" => $this->getFormElementValue("label"),
			"reportColor" => $this->getFormElementValue("color"),
			"reportTitleShort" => $this->getFormElementValue("label"),
			"reportTitleMedium" => $this->getFormElementValue("label"),
		]
	];
	return $result;
}