Created
May 27, 2022 10:21
-
-
Save megamosk/e0252dadbfee03b72e2afdd24388a0f3 to your computer and use it in GitHub Desktop.
Ставка НДС в зависимости от местоположения 1С-Битрикс
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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