Вот пример, писал давно уже, с одного из проектов. Суть сделать ограничение вывода оплаты, в случае, если сумма заказа больше суммы на внутреннем счете пользователя (в рамках этого проекта - просто польз. свойство, приходит из 1С). По аналогии можете доработать:
Код |
---|
<?php
use Bitrix\Sale\Services\Base;
use Bitrix\Sale\Internals\Entity;
use Bitrix\Sale\Payment;
class CWbrPersonScorePayRestriction extends Base\Restriction
{
public static function getClassTitle()
{
return 'По счёту юр. лица';
}
public static function getClassDescription()
{
return 'Для юр. лица будет выводиться только если у него достаточно средств';
}
/**
* @param $params
* @param array $restrictionParams
* @param int $serviceId
*
* @return bool
*/
public static function check($params, array $restrictionParams, $serviceId = 0)
{
if ($params['PRICE_PAYMENT'] === null) {
return true;
}
global $USER;
$arUser = $USER->GetByID($USER->GetID())->Fetch();
/*
* Если не юрик - то показываем всегда
* */
if ($arUser["UF_PERSON_TYPE"] != 4) {
return true;
}
$need = ($restrictionParams["AVAIL_SCORE"] == "Y") ? true : false;
$price = (float)$params['PRICE_PAYMENT'];
$score = (float)$arUser["UF_CONTRACT_BALANCE"];
if ($need === true) {
return (bool)($price <= $score);
}
return (bool)($price > $score);
}
/**
* @param Entity $entity
*
* @return array
*/
protected static function extractParams(Entity $entity)
{
$orderPrice = null;
$paymentPrice = null;
if ($entity instanceof Payment) {
/** @var PaymentCollection $collection */
$collection = $entity->getCollection();
/** @var Order $order */
$order = $collection->getOrder();
$orderPrice = $order->getPrice();
$paymentPrice = $entity->getField('SUM');
}
return array(
'PRICE_PAYMENT' => $paymentPrice,
'PRICE_ORDER' => $orderPrice,
);
}
/**
* @param $entityId
*
* @return array
* @throws \Bitrix\Main\ArgumentException
*/
public static function getParamsStructure($entityId = 0)
{
$arCase = array(
"Y" => "Доступно средств",
"N" => "Недоступно средств",
);
return array(
"AVAIL_SCORE" => array(
"TYPE" => "ENUM",
'MULTIPLE' => 'N',
"LABEL" => "По счёту юрика",
"OPTIONS" => $arCase,
),
);
}
} |