File: D:/HostingSpaces/SBogers10/debierbaron.komma.pro/data/app/Komma/Shipments/ShipmentService.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\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;
try {
$parcel->save();
} catch (SendCloudApiException $e) {
\Log::error('Something wrong with ' . $parcel->order_number . ': ' . $e->getMessage());
return false;
}
$this->saveShipment($parcel, $model);
return true;
}
/**
* Save the shipment whith data from SendCloud,
*
* @param $parcel
* @param $model , Order or Subscription
* @param $model , Order or Subscription
*/
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);
}
}