3namespace Sale\Handlers\Delivery\Additional\RusPost\Reliability;
5use Bitrix\Main\ArgumentNullException;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Main\Web\HttpClient;
10use Bitrix\Sale\Delivery;
11use Bitrix\Sale\Internals\ReliabilityTable;
13use Bitrix\Sale\PropertyValueCollectionBase;
14use Bitrix\Sale\Shipment;
16Loader::registerAutoLoadClasses(
19 __NAMESPACE__.
'\Reliability' =>
'handlers/delivery/additional/ruspost/reliability/reliability.php',
20 __NAMESPACE__.
'\ReliabilityCollection' =>
'handlers/delivery/additional/ruspost/reliability/reliabilitycollection.php',
21 __NAMESPACE__.
'\Requester' =>
'handlers/delivery/additional/ruspost/reliability/requester.php'
43 public static function getReliabilityCollection(
int $deliveryId, ReliabilityCollection $collection)
47 throw new ArgumentNullException(
'deliveryId');
50 if($collection->count() <= 0)
55 $notFoundInDb = $collection->getHashList();
58 $stored = self::getTableClass()::query()
59 ->addFilter(
'HASH', $collection->getHashList())
60 ->whereNotNull(
"RELIABILITY")
66 $notFoundInDb = array_diff($collection->getHashList(), $stored->getHashList());
68 if (empty($notFoundInDb))
74 $requested = self::requestReliability(
76 $collection->filterByHashes($notFoundInDb)
79 $result =
new ReliabilityCollection();
80 $result->setItems($collection->getAll());
84 $result->setItems($stored->getAll());
89 $result->setItems($requested->getAll());
106 public static function getReliabilityValue(
int $deliveryId,
string $fullName,
string $address,
string $phone)
108 $collection =
new ReliabilityCollection();
109 $collection->add(Reliability::create($fullName, $address, $phone));
110 $hash = self::createHash($fullName, $address, $phone);
112 $resultCollection = self::getReliabilityCollection(
117 if($reliability = $resultCollection->getByPrimary(
$hash))
119 return $reliability->getReliability();
122 return self::UNKNOWN;
131 public static function prepareData(
int $deliveryId,
int $attempt = 0)
139 $collection = self::getTableClass()::query()
140 ->whereNull(
'RELIABILITY')
144 if($collection->count() <= 0)
150 if($requestedCollection = self::requestReliability($deliveryId, $collection))
152 $requestedCollection->saveItems();
158 return self::createAgentName($deliveryId, --$attempt);
170 private static function extractProperties(Shipment $shipment)
172 if (!(
$order = $shipment->getOrder()))
195 private static function extractDataFromProps(PropertyValueCollectionBase
$props)
201 if($address =
$props->getAddress())
203 if($address->getValue() <>
'')
205 $addressValue = (string)$address->getValue();
209 if($payerName =
$props->getPayerName())
211 if($payerName->getValue() <>
'' )
213 $fullNameValue = (string)$payerName->getValue();
217 if($phone =
$props->getPhone())
219 if($phone->getValue() <>
'' )
221 $phoneValue = (string)$phone->getValue();
225 if($addressValue ==
'' && $fullNameValue ==
'' && $phoneValue ==
'')
230 return[$fullNameValue, $addressValue, $phoneValue];
243 public static function onShipmentSave($expectedDeliveryId, Event
$event)
246 if (!($shipment =
$event->getParameter(
'ENTITY')))
252 if (!($delivery = $shipment->getDelivery()))
257 if(!self::isDeliverySuitable($delivery, $expectedDeliveryId))
262 if(!
$props = self::extractProperties($shipment))
272 list($fullName, $address, $phone) =
$data;
273 $hash = self::createHash($fullName, $address, $phone);
275 if(!($reliability = self::getTableClass()::getByPrimary(
$hash)->fetchObject()))
277 $reliability = Reliability::create($fullName, $address, $phone);
278 $reliability->save();
281 self::addAgent($delivery->getId(), 3);
294 public static function onSaleAdminOrderInfoBlockShow($expectedDeliveryId, Event
$event)
299 $findSuitable =
false;
302 foreach (
$order->getShipmentCollection() as $shipment)
304 if($shipment->isSystem())
309 if(!($delivery = $shipment->getDelivery()))
314 if (self::isDeliverySuitable($delivery, $expectedDeliveryId))
316 $findSuitable =
true;
321 $result = new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS);
338 list($fullName, $address, $phone) =
$data;
339 $reliability = self::getReliabilityValue($delivery->getId(), $fullName, $address, $phone);
346 'bitrix:sale.delivery.ruspost.reliability',
348 [
'RELIABILITY' => $reliability]
351 $value = ob_get_contents();
354 return new \Bitrix\Main\EventResult(
355 \Bitrix\Main\EventResult::SUCCESS,
358 'TITLE' => Loc::getMessage(
'SALE_DLVRS_RELIABILITY_TITLE'),
374 private static function requestReliability(
int $deliveryId, ReliabilityCollection $collection)
381 if($collection->count() <= 0)
386 $delivery = Delivery\Services\Manager::getObjectById($deliveryId);
388 if(!$delivery || !self::isDeliverySuitable($delivery))
394 if(!($deliveryRequest = $delivery->getDeliveryRequestHandler()))
399 $deliveryRequest->setHttpClient(self::createHttpClient());
401 return (
new Requester($deliveryRequest))
402 ->request($collection);
409 private static function isDeliverySuitable(Delivery\Services\Base $delivery, $expectedDeliveryId = 0)
411 if (get_class($delivery) !==
'Sale\Handlers\Delivery\AdditionalProfile' || $delivery->getId() <= 0)
418 if ($delivery->getParentService()->getServiceType() !==
'RUSPOST')
423 if($expectedDeliveryId > 0)
425 if(!($deliveryRequest = $delivery->getDeliveryRequestHandler()))
430 if($deliveryRequest->getHandlingDeliveryServiceId() != $expectedDeliveryId)
445 public static function createHash(
string $fullName,
string $address,
string $phone)
447 return md5(trim($fullName).trim($address).trim($phone));
453 public static function install(
int $deliveryId)
456 $eventManager->registerEventHandler(
'sale',
'OnSaleShipmentEntitySaved' ,
'sale',
'\Sale\Handlers\Delivery\Additional\RusPost\Reliability\Service',
'onShipmentSave', 100,
"", [$deliveryId]);
457 $eventManager->registerEventHandler(
'sale',
'onSaleAdminOrderInfoBlockShow' ,
'sale',
'\Sale\Handlers\Delivery\Additional\RusPost\Reliability\Service',
'onSaleAdminOrderInfoBlockShow', 100,
"", [$deliveryId]);
463 public static function unInstall(
int $deliveryId)
466 $eventManager->unRegisterEventHandler(
'sale',
'OnSaleShipmentEntitySaved' ,
'sale',
'\Sale\Handlers\Delivery\Additional\RusPost\Reliability\Service',
'onShipmentSave',
"", [$deliveryId]);
467 $eventManager->unRegisterEventHandler(
'sale',
'onSaleAdminOrderInfoBlockShow' ,
'sale',
'\Sale\Handlers\Delivery\Additional\RusPost\Reliability\Service',
'onSaleAdminOrderInfoBlockShow',
"", [$deliveryId]);
473 private static function createHttpClient()
475 return new HttpClient(
array(
477 "socketTimeout" => 5,
478 "streamTimeout" => 5,
487 private static function addAgent(
int $deliveryId,
int $attempts)
494 \CAgent::AddAgent(self::createAgentName($deliveryId, $attempts),
"sale",
"N", 60,
"",
"Y");
502 private static function createAgentName(
int $deliveryId,
int $attempts)
504 return '\Sale\Handlers\Delivery\Additional\RusPost\Reliability\Service::prepareData('.$deliveryId.
', '.$attempts.
');';
510 private static function getTableClass()
512 return ReliabilityTable::class;
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static GetAnonymousUserID()
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)