Документация для разработчиков
Темная тема

Добавление сделки (лида, счета, компреда) с товарами, с применением скидок и налогов

Описание

Примеры создания различных сущностей с одновременным добавленем товаров к ним. Добавляемый товар берётся из Битрикс24 с ценой больше нуля. Все примеры добавляют товар в максимально возможном количестве вариаций, около каждой вариации есть мини комментарий с описанием какими дополнительными условиями будет отображаться товар.

Внимание! Для использования примера настройте работу класса CRest и подключите файл crest.php в файлах, где используется данный класс. Подробнее.

Прикрепление товаров к сделке

<?
$resultProduct = CRest::call(
	'crm.product.list',
	[
		'filter' => [
			'>PRICE' => 0,
		]
	]
);

if (empty($resultProduct['result']))
{
	echo 'product error, create product in b24';
	exit;
}
else
{
	$arProduct = $resultProduct['result'][0];
}

//Deal product
$resultDeal = CRest::call(
	'crm.deal.add',
	[
		'fields' => [
			'TITLE' => 'Example',
		]
	]
);
if ($ID = $resultDeal['result'])
{
	$result = CRest::call(
		'crm.deal.productrows.set',
		[
			'id' => $ID,
			'rows' => [
				[//product with auto calc tax
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
					'TAX_RATE' => 15,
					'TAX_INCLUDED' => 'N',
					'QUANTITY' => 1
				],
				[//product with tax include
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'],
					'TAX_RATE' => 15,
					'TAX_INCLUDED' => 'Y',
					'QUANTITY' => 1
				],
				[//product with discount
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'],
					'DISCOUNT_SUM' => 100,
					'DISCOUNT_TYPE_ID' => 1,//is sum discount type
					'QUANTITY' => 1
				],
				[//product with a real discount
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'] - 100,
					'DISCOUNT_SUM' => 100,
					'DISCOUNT_TYPE_ID' => 1,//is sum discount type
					'QUANTITY' => 1
				],
				[//product with discount percent
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
					'DISCOUNT_RATE' => 10,
					'DISCOUNT_TYPE_ID' => 2,//is percent discount type
					'QUANTITY' => 1
				],
				[//product with real discount percent
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'] - ($arProduct['PRICE'] * 0.1),
					'DISCOUNT_RATE' => 10,
					'DISCOUNT_TYPE_ID' => 2,//is percent discount type
					'QUANTITY' => 1
				],
			]
		]
	);
}
else
{
	echo 'error create deal';
	exit;
}

?>

Прикрепление товаров к лиду

<?
$resultProduct = CRest::call(
	'crm.product.list',
	[
		'filter' => [
			'>PRICE' => 0,
		]
	]
);

if (empty($resultProduct['result']))
{
	echo 'product error, create product in b24';
	exit;
}
else
{
	$arProduct = $resultProduct['result'][0];
}

//Lead product
$resultLead = CRest::call(
	'crm.lead.add',
	[
		'fields' => [
			'TITLE' => 'Example',
		]
	]
);
if ($ID = $resultLead['result'])
{
	$result = CRest::call(
		'crm.lead.productrows.set',
		[
			'id' => $ID,
			'rows' => [
				[//product with auto calc tax
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
					'TAX_RATE' => 15,
					'TAX_INCLUDED' => 'N',
					'QUANTITY' => 1
				],
				[//product with tax include
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'],
					'TAX_RATE' => 15,
					'TAX_INCLUDED' => 'Y',
					'QUANTITY' => 1
				],
				[//product with discount
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'],
					'DISCOUNT_SUM' => 100,
					'DISCOUNT_TYPE_ID' => 1,//is sum discount type
					'QUANTITY' => 1
				],
				[//product with a real discount
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE' => $arProduct['PRICE'] - 100,
					'DISCOUNT_SUM' => 100,
					'DISCOUNT_TYPE_ID' => 1,//is sum discount type
					'QUANTITY' => 1
				],
				[//product with discount percent
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
					'DISCOUNT_RATE' => 10,
					'DISCOUNT_TYPE_ID' => 2,//is percent discount type
					'QUANTITY' => 1
				],
				[//product with real discount percent
					'PRODUCT_ID' => $arProduct['ID'],
					'PRICE_EXCLUSIVE' => $arProduct['PRICE'] - ($arProduct['PRICE'] * 0.1),
					'DISCOUNT_RATE' => 10,
					'DISCOUNT_TYPE_ID' => 2,//is percent discount type
					'QUANTITY' => 1
				],
			]
		]
	);
}
else
{
	echo 'error create lead';
	exit;
}

?>

Создание счёта с товарами

<?
$resultProduct = CRest::call(
	'crm.product.list',
	[
		'filter' => [
			'>PRICE' => 0,
		]
	]
);

if (empty($resultProduct['result']))
{
	echo 'product error, create product in b24';
	exit;
}
else
{
	$arProduct = $resultProduct['result'][0];
}
$resultCompany = CRest::call(
	'crm.company.add',
	[
		'fields' => [
			'TITLE' => 'Example',
		]
	]
);

if ($iCompanyID = $resultCompany['result'])
{
	$resultInvoice = CRest::call(
		'crm.invoice.add',
		[
			'fields' => [
				'ORDER_TOPIC' => 'Invoice by company with product',
				'UF_COMPANY_ID' => $iCompanyID,
				'PERSON_TYPE_ID' => 1,//1 is company in CRest::call('crm.persontype.list')
				'PAY_SYSTEM_ID' => 20,//some in CRest::call('sale.paysystem.list')
				'STATUS_ID' => 'N',
				'DATE_INSERT' => date(DATE_ATOM),
				'DATE_BILL' => date(DATE_ATOM),
				'DATE_PAY_BEFORE' => date(DATE_ATOM, time() + 3600 * 24 * 20),//20 day pay

				'PRODUCT_ROWS' => [
					[//product with tax
						'PRODUCT_ID' => $arProduct['ID'],
						'PRODUCT_NAME' => $arProduct['NAME'],
						'PRICE' => $arProduct['PRICE'] + ($arProduct['PRICE'] * 0.15),
						'VAT_RATE' => 0.15,
						'QUANTITY' => 1
					],
					[//product with discount sum, percent not supported
						'PRODUCT_ID' => $arProduct['ID'],
						'PRODUCT_NAME' => $arProduct['NAME'],
						'PRICE' => $arProduct['PRICE'],
						'DISCOUNT_PRICE' => 100,
						'QUANTITY' => 1
					],
					[//product with real discount sum, percent not supported
						'PRODUCT_ID' => $arProduct['ID'],
						'PRODUCT_NAME' => $arProduct['NAME'],
						'PRICE' => $arProduct['PRICE'] - 100,
						'DISCOUNT_PRICE' => 100,
						'QUANTITY' => 1
					],
				],
			]
		]
	);
}
?>

Прикрепление товаров к компред

<?
$resultProduct = CRest::call(
	'crm.product.list',
	[
		'filter' => [
			'>PRICE' => 0,
		]
	]
);

if (empty($resultProduct['result']))
{
	echo 'product error, create product in b24';
	exit;
}
else
{
	$arProduct = $resultProduct['result'][0];
}
$resultCompany = CRest::call(
	'crm.company.add',
	[
		'fields' => [
			'TITLE' => 'Example',
		]
	]
);

if ($iCompanyID = $resultCompany['result'])
{
	$resultQuote = CRest::call(
		'crm.quote.add',
		[
			'fields' => [
				"TITLE" => "Quote by company with product",
				"OPENED" => "Y",
				"ASSIGNED_BY_ID" => 1,
				"CURRENCY_ID" => "USD",
				"BEGINDATE" => date(DATE_ATOM),
				"CLOSEDATE" => date(DATE_ATOM, time() + 3600 * 24 * 20),//20 day
				'COMPANY_ID' => $iCompanyID,
				'STATUS_ID' => 'N',
			]
		]
	);
	if ($ID = $resultQuote['result'])
	{
		$result = CRest::call(
			'crm.quote.productrows.set',
			[
				'id' => $ID,
				'rows' => [
					[//product with auto calc tax
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
						'TAX_RATE' => 15,
						'TAX_INCLUDED' => 'N',
						'QUANTITY' => 1
					],
					[//product with tax include
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE' => $arProduct['PRICE'],
						'TAX_RATE' => 15,
						'TAX_INCLUDED' => 'Y',
						'QUANTITY' => 1
					],
					[//product with discount
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE' => $arProduct['PRICE'],
						'DISCOUNT_SUM' => 100,
						'DISCOUNT_TYPE_ID' => 1,//is sum discount type
						'QUANTITY' => 1
					],
					[//product with a real discount
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE' => $arProduct['PRICE'] - 100,
						'DISCOUNT_SUM' => 100,
						'DISCOUNT_TYPE_ID' => 1,//is sum discount type
						'QUANTITY' => 1
					],
					[//product with discount percent
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE_EXCLUSIVE' => $arProduct['PRICE'],
						'DISCOUNT_RATE' => 10,
						'DISCOUNT_TYPE_ID' => 2,//is percent discount type
						'QUANTITY' => 1
					],
					[//product with real discount percent
						'PRODUCT_ID' => $arProduct['ID'],
						'PRICE_EXCLUSIVE' => $arProduct['PRICE'] - ($arProduct['PRICE'] * 0.1),
						'DISCOUNT_RATE' => 10,
						'DISCOUNT_TYPE_ID' => 2,//is percent discount type
						'QUANTITY' => 1
					],
				]
			]
		);
	}
	else
	{
		echo 'Error create quote';
	}
}
else
{
	echo 'Error create company';
}
?>


© «Битрикс», 2001-2024, «1С-Битрикс», 2024