Skip to content

Instantly share code, notes, and snippets.

@megamosk
Created May 27, 2022 10:21
Show Gist options
  • Save megamosk/e0252dadbfee03b72e2afdd24388a0f3 to your computer and use it in GitHub Desktop.
Save megamosk/e0252dadbfee03b72e2afdd24388a0f3 to your computer and use it in GitHub Desktop.
Ставка НДС в зависимости от местоположения 1С-Битрикс
<?php
use Bitrix\Main;
use Bitrix\Main\Loader;
Loader::includeModule("catalog");
\Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'OnSaleBasketBeforeSaved',
'OnSaleBasketBeforeSavedHandler'
);
function OnSaleBasketBeforeSavedHandler(Main\Event $event)
{
$basket = $event->getParameter("ENTITY");
foreach ($basket as &$basketItem) {
$basketItem->setFields(array("PRODUCT_PROVIDER_CLASS" => "CustomCatalogProvider"));
}
}
class CustomCatalogProvider extends \CCatalogProductProvider
{
/**
* GetProductData
*
* @param mixed $params
* @return void
*/
public static function GetProductData($params)
{
$result = parent::GetProductData($params);
$session = \Bitrix\Main\Application::getInstance()->getSession();
$vatRate = !empty($session->has('CUSTOM_VAT_RATE')) ? $session->has('CUSTOM_VAT_RATE') : 0.21; //set IVA 21% by default
$result['VAT_RATE'] = $vatRate;
$result['VAT_INCLUDED'] = 'N';
return $result;
}
}
\Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'OnSalePropertyValueSetField',
'OnSalePropertyValueSetFieldHandler'
);
/**
* OnSalePropertyValueSetFieldHandler
*
* @param mixed $event
* @return void
*/
function OnSalePropertyValueSetFieldHandler(\Bitrix\Main\Event $event)
{
$entity = $event->getParameter("ENTITY");
$arOrderVals = $entity->getFields()->getValues();
$codeField = $arOrderVals['CODE'];
if ($codeField == 'LOCATION') {
$cityCode = $event->getParameter('VALUE');
if (!empty($cityCode)) {
$arCity = \Bitrix\Sale\Location\LocationTable::getList(array(
'filter' => [
'CODE' => $cityCode
],
'select' => ['*'],
))->fetch();
if ($arCity['PARENT_ID'] > 0) {
$queryId = $arCity['PARENT_ID'];
} else {
$queryId = $arCity['ID'];
}
$locationGroup = \Bitrix\Sale\Location\GroupLocationTable::getList(array(
'filter' => [
'LOCATION_ID' => $queryId
],
'select' => ['*'],
))->fetch();
$groupId = $locationGroup['LOCATION_GROUP_ID'];
if ($groupId == 1) { //sin IVA
$vatRate = 0;
} elseif ($groupId == 2) { //IVA 21%
$vatRate = 0.21;
} else {
$vatRate = 0.07; //IVA 7%
}
$session->set('CUSTOM_VAT_RATE', $vatRate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment