Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
persontype.php
1<?php
2
4
16
17Loc::loadMessages(__FILE__);
18
19class PersonType extends Base\Restriction
20{
27 public static function check($params, array $restrictionParams, $serviceId = 0)
28 {
29 if (is_array($restrictionParams) && isset($restrictionParams['PERSON_TYPE_ID']))
30 {
31 return in_array($params, $restrictionParams['PERSON_TYPE_ID']);
32 }
33
34 return true;
35 }
36
40 public static function validateRestriction($fields)
41 {
42 $result = new Result();
43
44 // comparing person type restriction with client type pay system
45 $serviceId = $fields['SERVICE_ID'] ?? null;
46 $needPersonTypeIds = (array) ($fields['PARAMS']['PERSON_TYPE_ID'] ?? []);
47 if ($serviceId && $needPersonTypeIds)
48 {
49 $paySystemFields = \Bitrix\Sale\PaySystem\Manager::getById($serviceId);
50 if ($paySystemFields)
51 {
52 $paySystem = new Service($paySystemFields);
53
54 $hasIndividualTypes = false;
55 $hasLegalEntityTypes = false;
56
57 $registryType = $paySystem->getField('ENTITY_REGISTRY_TYPE') ?? \Bitrix\Sale\PersonType::getRegistryType();
58 $personTypeClass = Registry::getInstance($registryType)->getPersonTypeClassName();
59
63 foreach ($needPersonTypeIds as $personTypeId)
64 {
65 if (!$hasIndividualTypes && $personTypeClass::isIndividual($personTypeId))
66 {
67 $hasIndividualTypes = true;
68 }
69
70 if (!$hasLegalEntityTypes && $personTypeClass::isEntity($personTypeId))
71 {
72 $hasLegalEntityTypes = true;
73 }
74 }
75
76 $clientType = $paySystem->getClientType();
77 if ($clientType === ClientType::B2B && $hasIndividualTypes)
78 {
79 $result->addError(
80 new Error(Loc::getMessage('SALE_PS_RESTRICTIONS_BY_PERSON_TYPE_ERROR_B2B_HAS_INDIVIDUAL'))
81 );
82 }
83 elseif ($clientType === ClientType::B2C && $hasLegalEntityTypes)
84 {
85 $result->addError(
86 new Error(Loc::getMessage('SALE_PS_RESTRICTIONS_BY_PERSON_TYPE_ERROR_B2C_HAS_ENTITY'))
87 );
88 }
89 }
90 }
91
92 return $result;
93 }
94
99 public static function extractParams(Entity $entity)
100 {
101 if ($entity instanceof CollectableEntity)
102 {
104 $collection = $entity->getCollection();
105
107 $order = $collection->getOrder();
108 }
109 elseif ($entity instanceof Order)
110 {
111 $order = $entity;
112 }
113
114 if (!$order)
115 return false;
116
117 $personTypeId = $order->getPersonTypeId();
118 return $personTypeId;
119 }
120
124 public static function getClassTitle()
125 {
126 return Loc::getMessage('SALE_PS_RESTRICTIONS_BY_PERSON_TYPE');
127 }
128
132 public static function getClassDescription()
133 {
134 return Loc::getMessage('SALE_PS_RESTRICTIONS_BY_PERSON_TYPE_DESC');
135 }
136
137 public static function getOnApplyErrorMessage(): string
138 {
139 return Loc::getMessage('SALE_PS_RESTRICTIONS_BY_PERSON_TYPE_ON_APPLY_ERROR_MSG');
140 }
141
147 public static function getParamsStructure($entityId = 0)
148 {
149 $personTypeList = array();
150
151 $dbRes = \Bitrix\Sale\PersonType::getList();
152
153 while ($personType = $dbRes->fetch())
154 $personTypeList[$personType["ID"]] = $personType["NAME"]." (".$personType["ID"].")";
155
156 return array(
157 "PERSON_TYPE_ID" => array(
158 "TYPE" => "ENUM",
159 'MULTIPLE' => 'Y',
160 "LABEL" => Loc::getMessage("SALE_SALE_PS_RESTRICTIONS_BY_PERSON_TYPE_NAME"),
161 "OPTIONS" => $personTypeList
162 )
163 );
164 }
165
170 public static function getSeverity($mode)
171 {
172 return Manager::SEVERITY_STRICT;
173 }
174}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getInstance($type)
Definition registry.php:183
static extractParams(Entity $entity)
static check($params, array $restrictionParams, $serviceId=0)