Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/Chronorelais/Preference/Helper/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Chronorelais/Preference/Helper/Webservice.php
<?php
/**
 * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Krzysztof Majkowski <[email protected]> <[email protected]>
 */

declare(strict_types=1);

namespace Cnc\Chronorelais\Preference\Helper;

use Chronopost\Chronorelais\Helper\Data as HelperData;
use Chronopost\Chronorelais\Helper\Contract as HelperContract;
use Magento\Backend\Model\Auth\Session as AuthSession;
use Magento\Framework\Locale\Resolver;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\AddressFactory;
use Magento\Shipping\Model\CarrierFactory;

class Webservice extends \Chronopost\Chronorelais\Helper\Webservice
{
    /**
     * @var OrderRepositoryInterface
     */
    private $_orderRepository;

    /**
     * Webservice constructor.
     * @param OrderRepositoryInterface $orderRepository
     * @param \Magento\Framework\App\Helper\Context $context
     * @param CarrierFactory $carrierFactory
     * @param Resolver $resolver
     * @param AuthSession $authSession
     * @param Data $helperData
     * @param AddressFactory $addressFactory
     * @param DateTime $dateTime
     * @param HelperContract $helperContract
     */
    public function __construct(
        OrderRepositoryInterface $orderRepository,
        \Magento\Framework\App\Helper\Context $context,
        CarrierFactory $carrierFactory,
        Resolver $resolver,
        AuthSession $authSession,
        Data $helperData,
        AddressFactory $addressFactory,
        DateTime $dateTime,
        HelperContract $helperContract
    ) {
        parent::__construct($context, $carrierFactory, $resolver, $authSession, $helperData,
            $addressFactory, $dateTime, $helperContract);
        $this->_orderRepository = $orderRepository;
    }

    /**
     * @overridden
     * shipping method is array.
     * @param $orderId
     * @return string
     */
    public function getContractsHtml($orderId)
    {
        $order = $this->_orderRepository->get($orderId);

        if($contract = $this->_helperData->getContractByOrderId($orderId)) {
            $html  = '<select name="contract" style="display:none">';
            $html .= '<option value="-1" selected="selected">' . $contract["contract_name"] . '</option>';
            $html .= '</select>';
            $html .= '<span>' . $contract["contract_name"] . '</span>';
            return $html;
        } else {
            $html = '<select name="contract">';
            /**
             * KDC MODIFICATION START
             * shipping method is array
             */
            $shippingMethods = $order->getShippingMethod();
            foreach ($shippingMethods as $source => $method) {
                $chronoShippingMethod = strtolower(str_replace(' ', '', $method));
                $chronoShippingMethod = preg_replace('/.*\_/', '', $chronoShippingMethod);
                $contractShippingMethod = $this->_helperData->getCarrierContract($chronoShippingMethod);
                $contracts = $this->_helperData->getConfigContracts();
                foreach ($contracts as $id => $contract) {
                    $shippingMethodCode = explode("_", $chronoShippingMethod);
                    $shippingMethodCode = isset($shippingMethodCode[1]) ? $shippingMethodCode[1] : $shippingMethodCode[0];
                    if (!$this->shippingMethodEnabled($shippingMethodCode, $id)) {
                        continue;
                    }
                    if ($contract["number"] == $contractShippingMethod["number"]) {
                        $html .= '<option value="' . $id . '" selected="selected">' . $contract["name"] . '</option>';
                    } else {
                        $html .= '<option value="' . $id . '">' . $contract["name"] . '</option>';
                    }
                }
            }
            /** KDC MODIFICATION END */
            $html .= '</select>';

            return $html;
        }
    }

    /**
     * Just copied to used protected method getRetourParams();
     */
    public function createEtiquette($shipment, $mode = 'expedition', $recipient_address_type = 'returninformation', $dimensions = null, $nb_colis = 1, $contractId = null)
    {
        if ($mode == 'expedition') {
            $expeditionArray = $this->getExpeditionParams($shipment, $dimensions, $nb_colis, $contractId);
        } else {
            $expeditionArray = $this->getRetourParams($shipment, $recipient_address_type);
        }

        if ($expeditionArray) {
            $client = new \SoapClient(self::WS_SHIPPING_SERVICE, array('trace' => true));
            if($mode == 'expedition') {
                $expedition = $client->shippingMultiParcelWithReservationV3($expeditionArray);
            } else {
                $expedition = $client->shippingV7($expeditionArray);
            }

            if (!$expedition->return->errorCode) {
                return $expedition;
            } else {
                switch ($expedition->return->errorCode) {
                    case 33:
                        $message = __('An error occured during the label creation. Please check if this contract can edit labels for this carrier.');
                        break;
                    default:
                        $message = __($expedition->return->errorMessage);
                        break;
                }
                Throw new \Exception ($message);
            }
        }

        return false;
    }

    /**
     * @overridden
     * shipping method is array.
     * @param \Magento\Sales\Model\Order\Shipment $shipment
     * @param $recipient_address_type
     * @return array
     * @throws \Exception
     */
    protected function getRetourParams(\Magento\Sales\Model\Order\Shipment $shipment, $recipient_address_type)
    {
        $_order = $shipment->getOrder();
        $_shippingAddress = $shipment->getShippingAddress();
        $_billingAddress = $shipment->getBillingAddress();
        /** KDC MODIFICATION START */
        $exceptionToThrow = null;
        $methods = $_order->getShippingMethod();
        foreach ($methods as $source => $shippingMethod) {
            $shippingMethod = explode("_", $shippingMethod);
            $shippingMethod = $shippingMethod[1];
            $carrier = $this->_carrierFactory->get($shippingMethod);

            if ($_shippingAddress->getCountryId() != 'FR'
                && strpos($shippingMethod, 'chronoexpress') === false
                && strpos($shippingMethod, 'chronorelaiseur') === false
                && strpos($shippingMethod, 'chronorelaisdom') === false
                && strpos($shippingMethod, 'chronocclassic') === false
                && strpos($shippingMethod, 'chronopostsrdv') === false
            ) {
                $exceptionToThrow = new \Exception(__('Returns are only available for France'));
                continue;
            }
            $shippingMethodAllow = array(
                'chronorelaiseur',
                'chronorelais',
                'chronopost',
                'chronopostc10',
                'chronopostc18',
                'chronopostsrdv',
                'chronocclassic',
                'chronoexpress'
            );

            if (!in_array($shippingMethod, $shippingMethodAllow)) {
                $exceptionToThrow = new \Exception('Returns are not available for this delivery option ' . $shippingMethod);
                continue;
            }

            if (in_array($shippingMethod, $shippingMethodAllow)) {
                $esdParams = $header = $shipper = $customer = $recipient = $ref = $skybill = $skybillParams = $password = array();

                //header parameters
                $header = $this->getParamsHeader($_order);
                $password = $this->getPasswordContract($_order);

                $recipient = $this->getRecipientReturnAdress($recipient_address_type);

                //customer parameters
                $customer = $this->getParamsCustomer();

                //recipient parameters
                $_recipientAddress = $_shippingAddress;
                if (strpos($shippingMethod, 'chronorelais') !== false) {
                    // Nicolas, le 27/11/2014 : si Chronorelais, on doit utiliser l'adresse de facturation, non de livraison (qui est celle du relais)
                    $_recipientAddress = $_billingAddress;
                }
                $recipient_address = $_recipientAddress->getStreet();

                // Champs forcément basés sur l'adresse de livraison
                $customer_email = ($_shippingAddress->getEmail()) ? $_shippingAddress->getEmail() : ($_billingAddress->getEmail() ? $_billingAddress->getEmail() : $_order->getCustomerEmail());
                $recipientMobilePhone = $this->checkMobileNumber($_shippingAddress->getTelephone());
                $recipientName = $this->getFilledValue($_recipientAddress->getCompany()); //RelayPoint Name if chronorelais or Companyname if chronopost and
                $recipientName2 = $this->getFilledValue($_shippingAddress->getFirstname() . ' ' . $_shippingAddress->getLastname());
                //remove any alphabets in phone number

                $recipientPhone = trim(preg_replace("/[^0-9\.\-]/", " ", $_shippingAddress->getTelephone()));
                if (!isset($recipient_address[1])) {
                    $recipient_address[1] = '';
                }

                $shipper = array(
                    'shipperAdress1' => substr($this->getFilledValue($recipient_address[0]), 0, 38),
                    'shipperAdress2' => $recipient_address[1] ? substr($this->getFilledValue($recipient_address[1]), 0,
                        38) : '',
                    'shipperCity' => $this->getFilledValue($_recipientAddress->getCity()),
                    'shipperCivility' => 'M',
                    'shipperContactName' => $recipientName2,
                    'shipperCountry' => $this->getFilledValue($_recipientAddress->getCountryId()),
                    'shipperEmail' => $customer_email,
                    'shipperMobilePhone' => $recipientMobilePhone,
                    'shipperName' => $recipientName,
                    'shipperName2' => $recipientName2,
                    'shipperPhone' => $recipientPhone,
                    'shipperPreAlert' => '',
                    'shipperZipCode' => $this->getFilledValue($_recipientAddress->getPostcode()),
                );

                //ref parameters
                $recipientRef = $this->getFilledValue($_order->getRelaisId());
                if (!$recipientRef) {
                    $recipientRef = $_order->getCustomerId();
                }
                $shipperRef = $_order->getRealOrderId();

                $ref = array(
                    'recipientRef' => $recipientRef,
                    'shipperRef' => $shipperRef
                );

                //skybill parameters
                /* Livraison Samedi (Delivery Saturday) field */
                $SaturdayShipping = $this->getParamSaturdayShipping($_order, $carrier, $shippingMethod);

                $weight = $this->getParamWeight($shipment);

                // return product code
                $productCode = $this->getReturnProductCode($_shippingAddress, $shippingMethod);

                // @todo réactiver ce code quand les WS auront été mis à jour
                //$codeService = $_helper->getReturnServiceCode($productCode);
                if ($SaturdayShipping == 6) {
                    $codeService = 1;
                } else {
                    $codeService = 0;
                }

                $weight = 0; /* On met le poids à 0 car les colis sont pesé sur place */

                $skybill = array(
                    'codCurrency' => 'EUR',
                    'codValue' => '',
                    'content1' => '',
                    'content2' => '',
                    'content3' => '',
                    'content4' => '',
                    'content5' => '',
                    'customsCurrency' => 'EUR',
                    'customsValue' => '',
                    'evtCode' => 'DC',
                    'insuredCurrency' => 'EUR',
                    'insuredValue' => $this->_helperData->getOrderAdValorem($_order),
                    'objectType' => 'MAR',
                    'productCode' => $productCode,
                    'service' => $codeService,
                    'shipDate' => date('c'),
                    'shipHour' => date('H'),
                    'weight' => $weight,
                    'weightUnit' => 'KGM',
                    'height' => 1,
                    'length' => 1,
                    'width' => 1
                );

                $mode = $this->scopeConfig->getValue("chronorelais/skybillparam/mode");
                if ($shippingMethod == 'chronorelaiseur') {
                    $mode = 'PPR';
                }
                $skybillParams = array(
                    'mode' => $mode,
                    'withReservation' => 0
                );

                $expeditionArray = array(
                    'headerValue' => $header,
                    'shipperValue' => $shipper,
                    'customerValue' => $customer,
                    'recipientValue' => $recipient,
                    'refValue' => $ref,
                    'skybillValue' => $skybill,
                    'skybillParamsValue' => $skybillParams,
                    'password' => $password
                );

                return $expeditionArray;
            }
        }
        if ($exceptionToThrow) {
            throw $exceptionToThrow;
        }
        /** KDC MODIFICATION END */
    }
}

Spamworldpro Mini