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/data/app/Komma/Shipments/ShipmentService.php.orig
<?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\Shipment;
use Picqer\Carriers\SendCloud\SendCloudApiException;

class ShipmentService
{

    private $connection;
    private $sendCloud;

    /**
     * Initialize the SendCloud class
     */
    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);
    }

    /**
     * Send an order/subscription with SendCloud
     *
     * @param $model
     */
    public function sendOrderWithSendCloud($model)
    {
        //Initialize SendCloud if necessary
        if (!$this->connection) $this->initSendCloud();

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

        //Get different shipping methods per country
        switch ($model->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')];
        }
        //Set order number when set
        if (!empty($model->order_number)) $parcel->order_number = $model->order_number;
        //Set subscription bumber wheb set
        if (!empty($model->subscription_number)) $parcel->order_number = 'sub_' . $model->subscription_number;git


        try {
            $parcel->save();
        } catch (SendCloudApiException $e) {
<<<<<<< HEAD
            \App::abort(404, $e->getMessage());
=======
            \Log::error('Something wrong with ' . $parcel->order_number . ': ' . $e->getMessage());
            return false;
>>>>>>> 315c7bfc63ac11f2004e183faf27c9fa54d1c085
        }

        $this->saveShipment($parcel, $model);

<<<<<<< HEAD
=======
        return true;
>>>>>>> 315c7bfc63ac11f2004e183faf27c9fa54d1c085

    }

    /**
     * Save the shipment whith data from SendCloud,
     *
     * @param $parcel
<<<<<<< HEAD
     * @param $model, Order or Subscription
=======
     * @param $model , Order or Subscription
>>>>>>> 315c7bfc63ac11f2004e183faf27c9fa54d1c085
     */
    private function saveShipment($parcel, $model)
    {
        $shipments = new Shipment();
        //Shippable
        $shipments->shippable_id = $model->id;
        $shipments->shippable_type = get_class($model);

        //Parcel data
        $shipments->parcel_id = $parcel->id;
        $shipments->name = $parcel->name;
        $shipments->company_name = $parcel->company_name;
        $shipments->address = $parcel->address;
        $shipments->city = $parcel->city;
        $shipments->postal_code = $parcel->postal_code;
        $shipments->telephone = $parcel->telephone;
        $shipments->email = $parcel->email;
        $shipments->data = json_encode($parcel->data);
        $shipments->country = $parcel->country['iso_2'];
        $shipments->shipment = $parcel->shipment['id'];
        $shipments->requestShipment = $parcel->requestShipment;
        $shipments->order_number = $parcel->order_number;
        $shipments->tracking_number = $parcel->tracking_number;
        $shipments->save();
    }

    /**
     * Glue the name strings together
     *
     * @param $model
     * @return string
     */
    private function createName($model)
    {
        $name = '';
        $name .= $model->shipping_first_name . ' ';
        if (!empty($model->shipping_name_insertion)) $name .= $model->shipping_name_insertion . ' ';
        $name .= $model->shipping_last_name;

        return $name;
    }

    /**
     * Glue the address strings together
     * @param $model
     * @return string
     */
    private function createAddress($model)
    {
        $address = '';
        $address .= $model->shipping_street . ' ';
        $address .= $model->shipping_house_number . ' ';
        $address .= $model->shipping_house_number_suffix;

        return trim($address);
    }


}