• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/tracking/internals/expensespack.php
  • Класс: Bitrix\Crm\Tracking\Internals\ExpensesPackTable
  • Вызов: ExpensesPackTable::addExpenses
static function addExpenses($sourceId, Main\Type\Date $from, Main\Type\Date $to, $sum, $currencyId, $actions, $comment = null)
{
	if (!$sourceId || !$sum || !$currencyId)
	{
		return;
	}

	if ($from->getTimestamp() > $to->getTimestamp())
	{
		return;
	}

	$date = clone $from;
	$days = (int) (($to->getTimestamp() - $from->getTimestamp()) / 86400) + 1;
	if ($days > 365)
	{
		return;
	}

	$stepSum = round($sum / $days, 2);
	$stepSumModulo = round($sum - $stepSum * $days, 2);

	$stepActions = (int) ($actions / $days);
	$stepActionModulo = $actions - $stepActions * $days;

	for ($i = 1; $i <= $days; $i++)
	{
		$currentSum = $stepSum;
		if ($stepSumModulo && $i === 1)
		{
			$currentSum = $stepSum + $stepSumModulo;
		}

		$currentActions = $stepActions;
		if ($stepActionModulo && $i === 1)
		{
			$currentActions = $stepActions + $stepActionModulo;
		}

		static::add([
			'SOURCE_ID' => $sourceId,
			'DATE_STAT' => $date,
			'ACTIONS' => $currentActions,
			'EXPENSES' => $currentSum,
			'CURRENCY_ID' => $currencyId,
			'COMMENT' => $comment
		]);

		$date->add('+1 day');
	}
}