HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/debierbaron.komma.pro/app/Komma/Shipments/ShipmentService_BASE_21009.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Shipments;

use Komma\Shipments\Models\OrderShipments;
use Picqer\Carriers\SendCloud\SendCloudApiException;

class ShipmentService
{

    private $connection;
    private $sendCloud;

    public function initSendCloud()
    {
        $this->connection = new \Picqer\Carriers\SendCloud\Connection(\Config::get('bierbaron.sendCloud.api_key'), \Config::get('bierbaron.sendCloud.api_secret'));
        $this->sendCloud = new \Picqer\Carriers\SendCloud\SendCloud($this->connection);
    }

    public function sendOrderWithSendCloud($order)
    {
        if (!$this->connection) $this->initSendCloud();

        //create a new parcel
        $parcel = $this->sendCloud->parcels();
        $parcel->name = $this->createName($order);
        $parcel->company_name = $order->shipping_company;
        $parcel->address = $this->createAddress($order);
        $parcel->email = $order->invoice_email;
        $parcel->city = $order->shipping_city;
        $parcel->postal_code = $order->shipping_postal;
        $parcel->country = strtoupper($order->shipping_country);
        $parcel->requestShipment = true;

        //If shipping country ยก= nl all is done
        switch ($order->shipping_country) {
            case 'nl':
                $parcel->shipment = ['id' => \Config::get('bierbaron.sendCloud.shipment_nl')];
                break;
            case 'nl':
                $parcel->shipment = ['id' => \Config::get('bierbaron.sendCloud.shipment_nl')];
                break;
            default:
                $parcel->shipment = ['id' => \Config::get('bierbaron.sendCloud.shipment_free')];
        }
        $parcel->order_number = $order->order_number;
        try {
            $parcel->save();
            $this->saveShipmentOrder($parcel, $order->id);

        } catch (SendCloudApiException $e) {

            \App::abort(404, $e->getMessage());
        }

    }

    private function saveShipmentOrder($parcel, $order_id)
    {
        $orderShipment = new OrderShipments();
        $orderShipment->order_id = $order_id;
        $orderShipment->parcel_id = $parcel->id;
        $orderShipment->name = $parcel->name;
        $orderShipment->company_name = $parcel->company_name;
        $orderShipment->address = $parcel->address;
        $orderShipment->city = $parcel->city;
        $orderShipment->postal_code = $parcel->postal_code;
        $orderShipment->telephone = $parcel->telephone;
        $orderShipment->email = $parcel->email;
        $orderShipment->data = json_encode($parcel->data);
        $orderShipment->country = $parcel->country['iso_2'];
        $orderShipment->shipment = $parcel->shipment['id'];
        $orderShipment->requestShipment = $parcel->requestShipment;
        $orderShipment->order_number = $parcel->order_number;
        $orderShipment->tracking_number = $parcel->tracking_number;
        $orderShipment->save();
    }

    private function createName($order)
    {
        $name = '';
        $name .= $order->shipping_first_name . ' ';
        if (!empty($order->shipping_name_insertion)) $name .= $order->shipping_name_insertion . ' ';
        $name .= $order->shipping_last_name;
        return $name;
    }

    private function createAddress($order)
    {

        $address = '';
        $address .= $order->shipping_street . ' ';
        $address .= $order->shipping_house_number . ' ';
        $address .= $order->shipping_house_number_suffix;

        return trim($address);
    }


}