403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/lpost.delivery/lib/Order/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/lpost.delivery/lib/Order/OrderManager.php
<?php

namespace Lpost\Delivery\Order;

use Bitrix\Main\Web\Json;
use Bitrix\Sale\Order;
use Bitrix\Sale\Shipment;
use Lpost\Delivery\Handlers\Pvz;
use Lpost\Delivery\Handlers\Zone;
use Lpost\Delivery\Order\FieldIntegration\FieldIntegrationAddress;
use Lpost\Delivery\Order\FieldIntegration\FieldIntegrationEmail;
use Lpost\Delivery\Order\FieldIntegration\FieldIntegrationFIO;
use Lpost\Delivery\Order\FieldIntegration\FieldIntegrationPhone;
use Lpost\Delivery\Repository\RepositoryPvz;
use Lpost\Delivery\Repository\RepositoryZone;
use Lpost\Delivery\Settings;


\Bitrix\Main\Loader::IncludeModule('sale');

/**
 * ����� ����� ��� ������� ������� ������� \Bitrix\Sale\Order
 */
class OrderManager
{
    /**
     * @var Order
     */
    protected $order;

    /**
     * @param Order $order
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * @param Order $order
     * @return $this
     */
    public function setOrder(Order $order): OrderManager
    {
        $this->order = $order;
        return $this;
    }

    /**
     * @return Order
     */
    public function getOrder(): Order
    {
        return $this->order;
    }

    /**
     * ����� ������ � ����� ����������� �������������� ���
     *
     * @param string $address
     * @return false|void
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\NotImplementedException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     */
    public function setOrderAddress(string $address)
    {
        if(!$addressCode = (new FieldIntegrationAddress($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }

        $propertyCollection = $this->order->getPropertyCollection();
        if($propertyCollection->getAddress()) {
            $propertyCollection->getAddress()->setValue($address);
        }
        else {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $addressCode) {
                    $property->setValue($address);
                }
            }
        }
    }


    /**
     * ������ �� �������������
     *
     * @return array|string|null
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\NotImplementedException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     */
    public function getLocationId()
    {
        $propertyCollection = $this->order->getPropertyCollection();
        return $propertyCollection->getDeliveryLocation()->getValue();
    }

    /**
     * ������ ����� ������ ����������� �������������� ���
     *
     * @return array|false|string|null
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\NotImplementedException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     */
    public function getOrderAddress()
    {
        if(!$addressCode = (new FieldIntegrationAddress($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }
        
        $propertyCollection = $this->order->getPropertyCollection();

        if($address = $propertyCollection->getAddress()) {

            return str_replace(["\r\n", "\r", "\n"], ' ',  strip_tags($address->getValue()));
        }
        else {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $addressCode) {
                    return str_replace(["\r\n", "\r", "\n"], ' ',  strip_tags($property->getValue()));
                }
            }
        }

        return false;
    }

    /**
     * ������ ��� �������� "�����" ����������� �������������� ���
     *
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function getPropCodeAddress()
    {
        if(!$addressCode = (new FieldIntegrationAddress($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }

        $propertyCollection = $this->order->getPropertyCollection();

        if($address = $propertyCollection->getAddress()) {
            return $address->getField("CODE");
        }
        else {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $addressCode) {
                    return $property->getField('CODE');
                }
            }
        }

        return false;
    }

    /**
     * ������ ������� �������� �������������� ��� "����� ����������"
     * @return int|string|null
     */
    public function getDepartureNumber()
    {
        $field = Settings::getInstance()->getOption("field_departure_number");

        if ($field == "ID") {
            return $this->getOrder()->getId();
        } else {
            return $this->getOrder()->getField("ACCOUNT_NUMBER");
        }
    }

    /**
     * ������ ������� �������� ��� ����������� �������������� ���
     *
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function getOrderName()
    {
        if(!$nameCode = (new FieldIntegrationFIO($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }

        $propertyCollection = $this->order->getPropertyCollection();

        if($name = $propertyCollection->getPayerName()) {
            return $name->getValue();
        } else {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $nameCode)
                {
                    return $property->getValue();
                }
            }
        }

        return false;
    }

    /**
     * ������ ������� �������� ������� ����������� �������������� ���
     *
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function getOrderPhone()
    {
        if(!$phoneCode = (new FieldIntegrationPhone($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }

        $propertyCollection = $this->order->getPropertyCollection();

        if ($phone = $propertyCollection->getPhone()) {
            return $phone->getValue();
        } else {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $phoneCode) {
                    return $property->getValue();
                }
            }
        }

        return false;
    }

    /**
     * ������ ������� �������� Email ����������� �������������� ���
     *
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function getOrderEmail()
    {
        if(!$emailCode = (new FieldIntegrationEmail($this->order->getPersonTypeId()))->getOptionCode()) {
            return false;
        }

        $propertyCollection = $this->order->getPropertyCollection();
        if($email = $propertyCollection->getUserEmail())
        {
            return $email->getValue();
        }
        else
        {
            foreach ($propertyCollection as $property) {
                if ($property->getField('CODE') == $emailCode)
                {
                    return $property->getValue();
                }
            }
        }
        return false;
    }

    /**
     * ������ �������� ������ �� ���� �������������� ���
     *
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function getOrderProp($integrationName)
    {
        $code = Settings::getInstance()->getOption($integrationName);
        $propertyCollection = $this->order->getPropertyCollection();

        foreach ($propertyCollection as $property) {
            if ($property->getField('CODE') == $code) {
                return $property->getValue();
            }
        }

        return false;
    }

    /**
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ObjectPropertyException
     * @throws \Bitrix\Main\SystemException
     * @throws \Bitrix\Main\NotImplementedException
     */
    public function writeAdditionalDataToField($fieldCode, $data, $hash = false)
    {
        $propertyCollection = $this->order->getPropertyCollection();

        foreach ($propertyCollection as $property) {
            if ($property->getField('CODE') == $fieldCode) {
                if($hash)
                    $property->setField('VALUE', "\"#$data\"");
                else
                    $property->setField('VALUE', $data);
            }
        }
    }

    /**
     * ��������� ������
     *
     * @throws \Bitrix\Main\ArgumentNullException
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\ArgumentException
     */
    public function saveOrder()
    {
        $this->order->save();
    }

    /**
     * ��������� ��������
     *
     * @throws \Bitrix\Main\ArgumentNullException
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\ObjectNotFoundException
     * @throws \Bitrix\Main\ArgumentException
     */
    public function saveShipment()
    {
        $this->getShipment()->save();
    }

    /**
     * ������ �� �������� � �������� ��������� �����
     *
     * @throws \Bitrix\Main\ArgumentNullException
     * @throws \Bitrix\Main\ArgumentException
     */
    public function isLpost()
    {
        /**
         * @var Shipment $shipment
         */
        foreach ($this->order->getShipmentCollection() as $shipment) {
            if (!$shipment->isSystem()) {
                return $this->isLpostDeliveryId($shipment->getDeliveryId());
            }
        }
    }

    /**
     * ������ �� �������� �� ��������� �����
     *
     * @param int $deliveryId
     * @return bool
     * @throws \Bitrix\Main\ArgumentException
     */
    public static function isLpostDeliveryId(int $deliveryId): bool
    {
        $deliveryList = DeliveryManager::getLpostDeliveryList();

        foreach ($deliveryList as $arDelivery) {
            if($arDelivery['ID'] == $deliveryId) {
                return true;
            }
        }

        return false;
    }

    /**
     * ������ �������� ������
     *
     * @return \Bitrix\Sale\Delivery\Services\Base|null
     * @throws \Bitrix\Main\ArgumentNullException
     * @throws \Bitrix\Main\SystemException
     */
    public function getDelivery()
    {
        return $this->getShipment()->getDelivery();
    }

    /**
     * ������ �� �������� ������
     *
     * @return int
     */
    public function getDeliveryId()
    {
        return $this->getShipment()->getDeliveryId();
    }

    /**
     * ��������� ������ ������ � ������������ �������� � ��������
     *
     * @param $code
     * @param $value
     * @return void
     * @throws \Bitrix\Main\ArgumentException
     */
    public function setParamField($code, $value)
    {
        $params = $this->getShipment()->getField("PARAMS");
        if(!empty($params)){
            $params = Json::decode($params);
            $params[$code] = $value;
        }
        else{
            $params = [
                $code => $value
            ];
        }
        $this->setShipmentField("PARAMS", Json::encode($params));
    }

    /**
     * �������� ������� ��� � ����������� ��������� ��������
     *
     * @throws \Bitrix\Main\ArgumentException
     */
    public function getParamField($code)
    {
        $params = $this->getShipment()->getField("PARAMS");
        if(!empty($params)){
            $params = Json::decode($params);
            return $params[$code];
        }
        return null;
    }

    /**
     * �������� ������ ������� ����������� ����� � ��������
     *
     * @return mixed
     * @throws \Bitrix\Main\ArgumentException
     */
    public function getParamFields()
    {
        $params = $this->getShipment()->getField("PARAMS");
        if(!empty($params)){
            $params = Json::decode($params);
            return $params;
        }

        return [];
    }

    /**
     * ��������� ������ ������� � ������������ ��� � ��������
     *
     * @param $data
     * @return void
     * @throws \Bitrix\Main\ArgumentException
     */
    public function setParamFields($data = [])
    {
        $this->setShipmentField("PARAMS", Json::encode($data));
    }

    /**
     * ������ ���� ��������
     *
     * @param $code
     * @return string|null
     */
    public function getShipmentField($code)
    {
        return $this->getShipment()->getField($code);
    }

    /**
     * ������� ������� � ���� ��������
     *
     * @param $code
     * @param $value
     * @return void
     * @throws \Bitrix\Main\ArgumentOutOfRangeException
     * @throws \Bitrix\Main\NotSupportedException
     */
    public function setShipmentField($code, $value)
    {
        $this->getShipment()->setField($code, $value);
    }

    /**
     * ������ �������� � ������
     *
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ArgumentNullException
     */
    public function getShipment()
    {
        foreach ($this->order->getShipmentCollection() as $shipment) {
            if (!$shipment->isSystem()) {
                return $shipment;
            }
        }
    }

    /**
     * ������ ������ �������� (���, ���)
     *
     * @return false|string
     */
    public function getTypeDelivery()
    {
        return $this->getTypeDeliveryById($this->getDeliveryId());
    }

    /**
     * ������ ������ �������� (pvz, zone) � ����� �� ��������
     *
     * @param $deliveryId
     * @return false|string
     * @throws \Bitrix\Main\ArgumentException
     */
    public function getTypeDeliveryById($deliveryId)
    {
        $delivery = DeliveryManager::getLpostDeliveryById($deliveryId);

        if(strpos($delivery['CLASS_NAME'], \Lpost\Delivery\Handlers\Pvz::class)) {
            return Pvz::TYPE;
        }

        if(strpos($delivery['CLASS_NAME'], \Lpost\Delivery\Handlers\Zone::class)) {
            return Zone::TYPE;
        }

        return false;

    }

    /**
     * ������ ���� ����� �����
     *
     * @return void
     * @throws \Bitrix\Main\ArgumentException
     * @throws \Bitrix\Main\ArgumentNullException
     */
    public function getTrackerNumber()
    {
        
        $isNotResend = Settings::getInstance()->getOption('is_not_resend_order_for_del_sipment');
        if($isNotResend){
            $resOrder = \Lpost\Delivery\Entity\TrackingTable::getList([
                'filter' => [
                    'id_order' => $this->order->getId()
                ],
                'select' => ['id_order_lpost'],
                'order'  => ['id' => 'desc']
            ])->fetch();
            if(!empty($resOrder['id_order_lpost']))
                return $resOrder['id_order_lpost'];
        }
        
        foreach ($this->order->getShipmentCollection() as $shipment) {
            if (!$shipment->isSystem()) {
                return $shipment->getField('TRACKING_NUMBER');
            }
        }
    }

    /**
     * �������� �� ���������� � ������, ���� ��� �������� �� ����������� pickupId
     *
     * @param $pickupId
     * @return bool
     */
    public function isPrepayment($pickupId): bool
    {
        $isCash = false;

        if($this->getTypeDelivery() == Zone::TYPE) {
            $zone = (new RepositoryZone())->getByPickupId($pickupId);
            $isCash = $zone['is_cash'];
        }

        if($this->getTypeDelivery() == Pvz::TYPE) {
            $pvz = (new RepositoryPvz())->getByPickupId($pickupId);
            $isCash = $pvz['is_cash'];
        }

        if($isCash !== false AND (int)$isCash === 0 AND !$this->order->isPaid()) {
            return true;
        }

        return false;
    }

    /**
     * ������ ������� ������
     *
     * @return \Bitrix\Sale\BasketBase|null
     */
    public function getBasket()
    {
        return $this->order->getBasket();
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit