CAllCrmQuote::savePdf

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmQuote
  4. savePdf
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_quote.php
  • Класс: \CAllCrmQuote
  • Вызов: CAllCrmQuote::savePdf
static function savePdf($quote_id, $pay_system_id, &$error = null)
{
	$error = false;

	if (!\CModule::includeModule('sale'))
	{
		$error = 'MODULE SALE NOT INCLUDED!';
		return false;
	}

	$quote_id = (int) $quote_id;
	if ($quote_id <= 0)
	{
		$error = 'QUOTE_ID IS NOT FOUND!';
		return false;
	}

	if (!\CCrmQuote::checkReadPermission($quote_id))
	{
		$error = 'PERMISSION DENIED!';
		return false;
	}

	$pay_system_id = (int) $pay_system_id;
	if ($pay_system_id <= 0)
	{
		$error = 'PAY_SYSTEM_ID ID NOT FOUND!';
		return false;
	}

	$dbResult = \CCrmQuote::getList(
		array(),
		array(
			'ID' => $quote_id,
			'CHECK_PERMISSIONS' => 'N',
		),
		false,
		false,
		array('*', 'UF_*')
	);
	$quoteFields = is_object($dbResult) ? $dbResult->fetch() : null;
	if (!is_array($quoteFields))
	{
		$error = 'QUOTE IS NOT FOUND!';
		return false;
	}

	$paymentData = \CCrmQuote::prepareSalePaymentData($quoteFields);
	if (!is_array($paymentData))
	{
		$error = 'COULD NOT PREPARE PAYMENT DATA!';
		return false;
	}

	$dbPaySysAction = \CSalePaySystemAction::getList(
		array(),
		array(
			'PAY_SYSTEM_ID'  => $pay_system_id,
			'PERSON_TYPE_ID' => $quoteFields['PERSON_TYPE_ID'],
		),
		false,
		false,
		array('ACTION_FILE', 'PARAMS', 'ENCODING')
	);

	$paySysActionFields = $dbPaySysAction->fetch();
	if (!is_array($paySysActionFields))
	{
		$error = 'COULD NOT FIND PAYMENT SYSTEM ACTION!';
		return false;
	}

	$actionFilePath = isset($paySysActionFields['ACTION_FILE']) ? $paySysActionFields['ACTION_FILE'] : '';
	if (!is_string($actionFilePath) || $actionFilePath === '')
	{
		$error = 'COULD NOT FIND PAYMENT SYSTEM ACTION FILE!';
		return false;
	}

	$actionFilePath = $_SERVER['DOCUMENT_ROOT'].$actionFilePath;
	$actionFilePath = str_replace('\\', '/', $actionFilePath);
	while (mb_substr($actionFilePath, mb_strlen($actionFilePath) - 1, 1) == '/')
		$actionFilePath = mb_substr($actionFilePath, 0, mb_strlen($actionFilePath) - 1);

	if (!file_exists($actionFilePath))
	{
		$error = 'COULD NOT FIND PAYMENT SYSTEM ACTION FILE!';
		return false;
	}
	else if (is_dir($actionFilePath))
	{
		$actionFilePath = $actionFilePath.'/payment.php';
		if (!file_exists($actionFilePath))
		{
			$error = 'COULD NOT FIND PAYMENT SYSTEM ACTION FILE!';
			return false;
		}
	}

	\CSalePaySystemAction::initParamArrays(
		$paymentData['ORDER'],
		0,
		$paySysActionFields['PARAMS'],
		array(
			"PROPERTIES"     => $paymentData['PROPERTIES'],
			"BASKET_ITEMS"   => $paymentData['CART_ITEMS'],
			'TAX_LIST'       => $paymentData['TAX_LIST'],
			'REQUISITE'      => $paymentData['REQUISITE'],
			'BANK_DETAIL'    => $paymentData['BANK_DETAIL'],
			'CRM_COMPANY'    => $paymentData['CRM_COMPANY'],
			'CRM_CONTACT'    => $paymentData['CRM_CONTACT'],
			'MC_REQUISITE'   => $paymentData['MC_REQUISITE'],
			'MC_BANK_DETAIL' => $paymentData['MC_BANK_DETAIL'],
			'CRM_MYCOMPANY'  => $paymentData['CRM_MYCOMPANY']
		),
		$paymentData['PAYMENT'],
		$paymentData['SHIPMENT']
	);

	$origRequest = $_REQUEST;
	$_REQUEST['pdf'] = true;
	$_REQUEST['GET_CONTENT'] = 'Y';

	$pdfContent = include($actionFilePath);

	$_REQUEST['pdf'] = $origRequest['pdf'] ?? false;
	$_REQUEST['GET_CONTENT'] = $origRequest['GET_CONTENT'] ?? 'N';

	$fileName = "quote_{$quote_id}.pdf";
	$fileData = array(
		'name' => $fileName,
		'type' => 'application/pdf',
		'content' => $pdfContent,
		'MODULE_ID' => 'crm'
	);

	$fileID = \CFile::saveFile($fileData, 'crm');
	if ($fileID <= 0)
	{
		$error = 'COULD NOT SAVE FILE!';
		return false;
	}

	return $fileID;
}

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