1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
installer.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\Delivery\YandexTaxi\Installer;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\Error;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Result;
9use Bitrix\Sale\Delivery\ExtraServices\Checkbox;
10use Bitrix\Sale\Delivery\ExtraServices\Enum;
11use Bitrix\Sale\Delivery\ExtraServices\Table;
12use Bitrix\Sale\Delivery\Restrictions\ByPublicMode;
13use Bitrix\Sale\Delivery\Services\Manager;
14use Bitrix\Sale\Internals\OrderPropsGroupTable;
15use Bitrix\Sale\Internals\OrderPropsRelationTable;
16use Bitrix\Sale\Internals\OrderPropsTable;
17use Bitrix\Sale\Internals\ServiceRestrictionTable;
18use Bitrix\Sale\PersonTypeTable;
19use Bitrix\Sale\Registry;
20use Sale\Handlers\Delivery\YandexTaxi\Api\Tariffs\Repository;
21use Sale\Handlers\Delivery\YandexTaxi\Common\OrderEntitiesCodeDictionary;
22use Sale\Handlers\Delivery\YandexTaxi\Common\TariffNameBuilder;
23
29final class Installer
30{
32 private $tariffsRepository;
33
35 private $tariffNameBuilder;
36
42 public function __construct(Repository $tariffsRepository, TariffNameBuilder $tariffNameBuilder)
43 {
44 $this->tariffsRepository = $tariffsRepository;
45 $this->tariffNameBuilder = $tariffNameBuilder;
46 }
47
52 public function install(int $serviceId): Result
53 {
54 $result = new Result();
55
56 $orderPropsResult = $this->installOrderProperties();
57 if (!$orderPropsResult->isSuccess())
58 {
59 return $result->addErrors($orderPropsResult->getErrors());
60 }
61 $propertyIds = $orderPropsResult->getData()['PROPERTY_IDS'];
62
63 $tariffs = $this->tariffsRepository->getTariffs();
64 $profileSort = 100;
65 foreach ($tariffs as $tariff)
66 {
67 $installProfileResult = $this->installProfile($serviceId, $tariff, $profileSort);
68 if (!$installProfileResult->isSuccess())
69 {
70 return $result->addErrors($installProfileResult->getErrors());
71 }
72 $profileSort += 100;
73
74 $profileId = $installProfileResult->getData()['ID'];
75
76 $attachPropertiesResult = $this->attachOrderProperties($profileId, $propertyIds);
77 if (!$attachPropertiesResult->isSuccess())
78 {
79 return $result->addErrors($attachPropertiesResult->getErrors());
80 }
81
82 $extraServicesResult = $this->installExtraServices($profileId, $tariff);
83 if (!$extraServicesResult->isSuccess())
84 {
85 return $result->addErrors($extraServicesResult->getErrors());
86 }
87 }
88
89 $this->installRestriction($serviceId);
90
91 return $result;
92 }
93
100 protected function installProfile(int $serviceId, array $tariff, int $sort = 100): Result
101 {
102 $result = new Result();
103
104 $parentServiceFields = Manager::getById($serviceId);
105 if (!$parentServiceFields)
106 {
107 return $result->addError(new Error('Parent service not found'));
108 }
109
110 $addResult = Manager::add(
111 [
112 'CODE' => sprintf(
113 'YANDEX_TAXI_%s',
114 mb_strtoupper($tariff['name'])
115 ),
116 'NAME' => $this->tariffNameBuilder->getTariffName($tariff),
117 'DESCRIPTION' => Loc::getMessage(
118 sprintf(
119 'SALE_YANDEX_TAXI_TARIFF_%s_DESCRIPTION',
120 mb_strtoupper($tariff['name'])
121 )
122 ),
123 'LOGOTIP' => \CFile::SaveFile(
124 \CFile::MakeFileArray(
125 sprintf(
126 '%s/bitrix/modules/sale/handlers/delivery/yandextaxi/logos/%s.png',
127 Application::getDocumentRoot(),
128 mb_strtolower($tariff['name'])
129 )
130 ),
131 'sale/delivery/logotip'
132 ),
133 'PARENT_ID' => $serviceId,
134 'CLASS_NAME' => '\Sale\Handlers\Delivery\YandextaxiProfile',
135 'SORT' => $sort,
136 'ACTIVE' => 'Y',
137 'CONFIG' => [
138 'MAIN' => [
139 'PROFILE_TYPE' => $tariff['name'],
140 ]
141 ],
142 'XML_ID' => Manager::generateXmlId(),
143 'CURRENCY' => $parentServiceFields['CURRENCY'],
144 'ALLOW_EDIT_SHIPMENT' => $parentServiceFields['ALLOW_EDIT_SHIPMENT'],
145 'VAT_ID' => $parentServiceFields['VAT_ID'],
146 ]
147 );
148
149 if (!$addResult->isSuccess())
150 {
151 return $result->addErrors($addResult->getErrors());
152 }
153
154 return $result->setData(['ID' => $addResult->getId()]);
155 }
156
160 protected function installOrderProperties(): Result
161 {
162 $result = new Result();
163
167 $payerTypeGroupsMapping = [];
168 $payerTypes = PersonTypeTable::getList(
169 ['filter' => ['ENTITY_REGISTRY_TYPE' => \Bitrix\Sale\Registry::REGISTRY_TYPE_ORDER]]
170 )->fetchAll();
171
172 foreach ($payerTypes as $payerType)
173 {
174 $existingGroup = OrderPropsGroupTable::getList(
175 [
176 'filter' => [
177 'PERSON_TYPE_ID' => $payerType['ID'],
178 'CODE' => 'DELIVERY_SERVICE',
179 ]
180 ]
181 )->fetch();
182
183 if ($existingGroup)
184 {
185 $payerTypeGroupsMapping[$payerType['ID']] = $existingGroup['ID'];
186 continue;
187 }
188
189 $persistResult = OrderPropsGroupTable::add(
190 [
191 'PERSON_TYPE_ID' => $payerType['ID'],
192 'NAME' => Loc::getMessage('SALE_YANDEX_TAXI_DELIVERY_PROP_GROUP_NAME'),
193 'CODE' => 'DELIVERY_SERVICE',
194 ]
195 );
196 if (!$persistResult->isSuccess())
197 {
198 return $result->addError(new Error('Can not add property group'));
199 }
200
201 $payerTypeGroupsMapping[$payerType['ID']] = $persistResult->getId();
202 }
203
207 $propertyIds = [];
208 foreach ($payerTypes as $payerType)
209 {
210 $properties = [
211 [
212 'ATTRIBUTE' => 'IS_ADDRESS_FROM',
213 'CODE' => 'DELIVERY_SERVICE_ADDRESS_FROM',
214 'NAME' => Loc::getMessage('SALE_YANDEX_TAXI_PROP_FROM_NAME'),
215 'TYPE' => 'ADDRESS',
216 'REQUIRED' => 'Y',
217 'MULTIPLE' => 'N',
218 'SETTINGS' => [],
219 ],
220 [
221 'ATTRIBUTE' => 'IS_ADDRESS_TO',
222 'CODE' => 'DELIVERY_SERVICE_ADDRESS_TO',
223 'NAME' => Loc::getMessage('SALE_YANDEX_TAXI_PROP_TO_NAME'),
224 'TYPE' => 'ADDRESS',
225 'REQUIRED' => 'Y',
226 'MULTIPLE' => 'N',
227 'SETTINGS' => [],
228 ],
229 [
230 'CODE' => OrderEntitiesCodeDictionary::COMMENT_FOR_DRIVER_PROPERTY_CODE,
231 'NAME' => Loc::getMessage('SALE_YANDEX_TAXI_COMMENT_FOR_DRIVER'),
232 'TYPE' => 'STRING',
233 'REQUIRED' => 'N',
234 'MULTIPLE' => 'N',
235 'SETTINGS' => ['MULTILINE' => 'Y'],
236 ],
237 ];
238
239 foreach ($properties as $property)
240 {
241 $payerTypeId = $payerType['ID'];
242
243 if (!isset($payerTypeGroupsMapping[$payerTypeId]))
244 {
245 return $result->addError(new Error('Property group not found'));
246 }
247
248 $propertyGroupId = $payerTypeGroupsMapping[$payerTypeId];
249
250 $filter = [
251 'PERSON_TYPE_ID' => $payerTypeId,
252 'PROPS_GROUP_ID' => $propertyGroupId,
253 'TYPE' => $property['TYPE'],
254 'ENTITY_TYPE' => Registry::ENTITY_SHIPMENT,
255 ];
256 if (isset($property['ATTRIBUTE']))
257 {
258 $filter[$property['ATTRIBUTE']] = 'Y';
259 }
260 else
261 {
262 $filter['CODE'] = $property['CODE'];
263 }
264
265 $existingProperty = OrderPropsTable::getList(['filter' => $filter])->fetch();
266
268 'NAME' => $property['NAME'],
269 'ACTIVE' => 'Y',
270 'USER_PROPS' => 'N',
271 'IS_FILTERED' => 'N',
272 'REQUIRED' => $property['REQUIRED'],
273 'MULTIPLE' => $property['MULTIPLE'],
274 'SETTINGS' => $property['SETTINGS'],
275 'ENTITY_REGISTRY_TYPE' => Registry::REGISTRY_TYPE_ORDER,
276 'DEFAULT_VALUE' => '',
277 'DESCRIPTION' => '',
278 ];
279
280 if (isset($property['ATTRIBUTE']))
281 {
282 $propertyFields[$property['ATTRIBUTE']] = 'Y';
283 }
284
285 if ($existingProperty)
286 {
287 $persistResult = OrderPropsTable::update(
288 $existingProperty['ID'],
290 );
291 if (!$persistResult->isSuccess())
292 {
293 return $result->addError(new Error('Property can not be installed'));
294 }
295
296 $propertyIds[] = $existingProperty['ID'];
297 }
298 else
299 {
300 $persistResult = OrderPropsTable::add(
301 array_merge(
303 [
304 'PERSON_TYPE_ID' => $payerTypeId,
305 'PROPS_GROUP_ID' => $propertyGroupId,
306 'CODE' => $property['CODE'],
307 'TYPE' => $property['TYPE'],
308 'ENTITY_TYPE' => Registry::ENTITY_SHIPMENT,
309 ]
310 )
311 );
312 if (!$persistResult->isSuccess())
313 {
314 return $result->addError(new Error('Property can not be installed'));
315 }
316
317 $propertyIds[] = $persistResult->getId();
318 }
319 }
320 }
321
322 return $result->setData(
323 [
324 'PROPERTY_IDS' => $propertyIds
325 ]
326 );
327 }
328
334 protected function attachOrderProperties(int $serviceId, array $propertyIds): Result
335 {
336 $result = new Result();
337
338 foreach ($propertyIds as $propertyId)
339 {
340 $fields = [
341 'PROPERTY_ID' => $propertyId,
342 'ENTITY_TYPE' => 'D',
343 'ENTITY_ID' => $serviceId,
344 ];
345
346 $existingRecord = OrderPropsRelationTable::getList(
347 [
348 'filter' => [
349 'PROPERTY_ID' => $propertyId,
350 'ENTITY_TYPE' => 'D',
351 'ENTITY_ID' => $serviceId,
352 ]
353 ]
354 )->fetch();
355
356 if (!$existingRecord)
357 {
358 $relationAddResult = OrderPropsRelationTable::add($fields);
359 if (!$relationAddResult->isSuccess())
360 {
361 $result->addErrors($relationAddResult->getErrors());
362 }
363 }
364 }
365
366 return $result;
367 }
368
374 protected function installExtraServices(int $serviceId, array $tariff): Result
375 {
376 $result = new Result();
377
381 $addResult = Table::add(
382 [
383 'ACTIVE' => 'Y',
384 'RIGHTS' => 'YYY',
385 'DELIVERY_ID' => $serviceId,
386 'CODE' => OrderEntitiesCodeDictionary::DOOR_DELIVERY_EXTRA_SERVICE_CODE,
387 'NAME' => Loc::getMessage('SALE_YANDEX_TAXI_TO_DOOR_DELIVERY'),
388 'CLASS_NAME' => '\\' . Checkbox::class,
389 'INIT_VALUE' => 'Y',
390 'PARAMS' => ['PRICE' => 0.00],
391 ]
392 );
393 if (!$addResult->isSuccess())
394 {
395 return $result->addErrors($addResult->getErrors());
396 }
397
398 $listNullValue = 'null';
399 foreach ($tariff['supported_requirements'] as $supportedRequirement)
400 {
401 if ($supportedRequirement['type'] === 'multi_select')
402 {
403 foreach ($supportedRequirement['options'] as $option)
404 {
405 $addResult = Table::add(
406 [
407 'ACTIVE' => 'Y',
408 'RIGHTS' => 'YYY',
409 'DELIVERY_ID' => $serviceId,
410 'CODE' => $option['value'],
411 'NAME' => Loc::getMessage(
412 sprintf(
413 'SALE_YANDEX_TAXI_EXTRA_SERVICE_%s',
414 mb_strtoupper($option['value'])
415 )
416 ),
417 'CLASS_NAME' => '\\' . Checkbox::class,
418 'INIT_VALUE' => 'N',
419 'PARAMS' => ['PRICE' => 0.00],
420 ]
421 );
422 if (!$addResult->isSuccess())
423 {
424 return $result->addErrors($addResult->getErrors());
425 }
426 }
427 }
428 elseif ($supportedRequirement['type'] === 'select')
429 {
430 $params = [
431 $listNullValue => [
432 'TITLE' => Loc::getMessage('SALE_YANDEX_TAXI_EXTRA_SERVICE_LIST_NOT_SELECTED'),
433 'PRICE' => 0.00,
434 'CODE' => $listNullValue,
435 ]
436 ];
437
438 foreach ($supportedRequirement['options'] as $option)
439 {
440 $value = (string)$option['value'];
441
442 $titleLang = Loc::getMessage(
443 sprintf(
444 'SALE_YANDEX_TAXI_EXTRA_SERVICE_%s_OPTION_%s',
445 mb_strtoupper($supportedRequirement['name']),
446 mb_strtoupper($value)
447 )
448 );
449
450 $params[$value] = [
451 'TITLE' => $titleLang ?: $value,
452 'PRICE' => 0.00,
453 'CODE' => $value,
454 ];
455 }
456
457 $addResult = Table::add(
458 [
459 'ACTIVE' => 'Y',
460 'RIGHTS' => 'YYY',
461 'DELIVERY_ID' => $serviceId,
462 'CODE' => $supportedRequirement['name'],
463 'NAME' => Loc::getMessage(
464 sprintf(
465 'SALE_YANDEX_TAXI_EXTRA_SERVICE_%s',
466 mb_strtoupper($supportedRequirement['name'])
467 )
468 ),
469 'CLASS_NAME' => '\\' . Enum::class,
470 'INIT_VALUE' => $listNullValue,
471 'PARAMS' => [
472 'PRICES' => $params,
473 ],
474 ]
475 );
476 if (!$addResult->isSuccess())
477 {
478 return $result->addErrors($addResult->getErrors());
479 }
480 }
481 }
482
483 return $result;
484 }
485
489 protected function installRestriction(int $serviceId): void
490 {
491 ServiceRestrictionTable::add(
492 [
493 'SORT' => 100,
494 'SERVICE_ID' => $serviceId,
495 'PARAMS' => ['PUBLIC_SHOW' => 'N'],
496 'SERVICE_TYPE' => '0',
497 'CLASS_NAME' => '\\' . ByPublicMode::class
498 ]
499 );
500 }
501}
static getList(array $parameters=array())
Определения datamanager.php:431
static add(array $data)
Определения datamanager.php:877
static add(array $data)
Определения manager.php:333
const ENTITY_SHIPMENT
Определения registry.php:19
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
$value
Определения Param.php:39
trait Error
Определения error.php:11
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$option
Определения options.php:1711
$propertyFields
Определения yandex_run.php:558
$fields
Определения yandex_run.php:501