Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SpeeDee Delivery Integration #97

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
'baseUrl' => $params['coldco']['baseUrl'],
'clientId' => $params['coldco']['clientId'],
'secret' => $params['coldco']['secret'],
]

],
],
];
4 changes: 4 additions & 0 deletions common/config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@
'csvBoxS3Secret' => '',
'csvBoxS3Region' => '',
'csvBoxS3Bucket' => '',

'speedeeFtpHost' => '',
'speedeeFtpUser' => 123,
'speedeeFtpPass' => '',
];
156 changes: 156 additions & 0 deletions common/models/SpeedeeManifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace common\models;

use Yii;

/**
* This is the model class for table "speedee_manifests".
*
* @property int $id
* @property int $order_id
* @property int $customer_id
* @property string $ship_from_shipper_number
* @property string $ship_from_name
* @property string $ship_from_attention
* @property string $ship_from_address_1
* @property string $ship_from_address_2
* @property string $ship_from_city
* @property int $ship_from_zip
* @property string $ship_from_country
* @property string $ship_from_email
* @property string $ship_from_phone
* @property string $ship_to_import_field
* @property string $ship_to_shipper_number
* @property string $ship_to_name
* @property string $ship_to_attention
* @property string $ship_to_address_1
* @property string $ship_to_address_2
* @property string $ship_to_city
* @property string $ship_to_country
* @property string $ship_to_email
* @property string $ship_to_phone
* @property string $reference_1
* @property string $reference_2
* @property string $reference_3
* @property string $reference_4
* @property int $weight
* @property int $length
* @property int $width
* @property int $height
* @property string $barcode
* @property int $oversized
* @property int $pickup_tag
* @property int $aod
* @property int $aod_option
* @property int $cod
* @property int $cod_value
* @property int $declared_value
* @property int $package_handling
* @property int $apply_package_handling
* @property string $ship_date
* @property string $bill_to_shipper_number
* @property int $unboxed
* @property string $manifest_filename
* @property boolean $is_manifest_sent
*
* @property Customer $customer
* @property Order $order
*/
class SpeedeeManifest extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'speedee_manifests';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['order_id', 'customer_id', 'ship_from_zip', 'weight', 'length', 'width', 'height', 'oversized', 'pickup_tag', 'aod', 'aod_option', 'cod', 'cod_value', 'declared_value', 'package_handling', 'apply_package_handling', 'unboxed'], 'integer'],
[['ship_date'], 'safe'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we ensure a data format?

[['ship_from_shipper_number', 'ship_to_shipper_number', 'bill_to_shipper_number'], 'string', 'max' => 6],
[['ship_from_name', 'ship_from_attention', 'ship_from_address_1', 'ship_from_address_2', 'ship_from_city', 'ship_from_country', 'ship_from_email', 'ship_from_phone', 'ship_to_import_field', 'ship_to_name', 'ship_to_attention', 'ship_to_address_1', 'ship_to_address_2', 'ship_to_city', 'ship_to_country', 'ship_to_email', 'ship_to_phone', 'reference_1', 'reference_2', 'reference_3', 'reference_4', 'barcode'], 'string', 'max' => 255],
[['customer_id'], 'exist', 'skipOnError' => true, 'targetClass' => Customer::class, 'targetAttribute' => ['customer_id' => 'id']],
[['order_id'], 'exist', 'skipOnError' => true, 'targetClass' => Order::class, 'targetAttribute' => ['order_id' => 'id']],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'order_id' => 'Order ID',
'customer_id' => 'Customer ID',
'ship_from_shipper_number' => 'Ship From Shipper Number',
'ship_from_name' => 'Ship From Name',
'ship_from_attention' => 'Ship From Attention',
'ship_from_address_1' => 'Ship From Address 1',
'ship_from_address_2' => 'Ship From Address 2',
'ship_from_city' => 'Ship From City',
'ship_from_zip' => 'Ship From Zip',
'ship_from_country' => 'Ship From Country',
'ship_from_email' => 'Ship From Email',
'ship_from_phone' => 'Ship From Phone',
'ship_to_import_field' => 'Ship To Import Field',
'ship_to_shipper_number' => 'Ship To Shipper Number',
'ship_to_name' => 'Ship To Name',
'ship_to_attention' => 'Ship To Attention',
'ship_to_address_1' => 'Ship To Address 1',
'ship_to_address_2' => 'Ship To Address 2',
'ship_to_city' => 'Ship To City',
'ship_to_country' => 'Ship To Country',
'ship_to_email' => 'Ship To Email',
'ship_to_phone' => 'Ship To Phone',
'reference_1' => 'Reference 1',
'reference_2' => 'Reference 2',
'reference_3' => 'Reference 3',
'reference_4' => 'Reference 4',
'weight' => 'Weight',
'length' => 'Length',
'width' => 'Width',
'height' => 'Height',
'barcode' => 'Barcode',
'oversized' => 'Oversized',
'pickup_tag' => 'Pickup Tag',
'aod' => 'Aod',
'aod_option' => 'Aod Option',
'cod' => 'Cod',
'cod_value' => 'Cod Value',
'declared_value' => 'Declared Value',
'package_handling' => 'Package Handling',
'apply_package_handling' => 'Apply Package Handling',
'ship_date' => 'Ship Date',
'bill_to_shipper_number' => 'Bill To Shipper Number',
'unboxed' => 'Unboxed',
'manifest_filename' => 'Manifest Filename',
'is_manifest_sent' => 'Manifest is Sent',
'checksum' => 'Checksum',
];
}

/**
* @return \yii\db\ActiveQuery
*/
public function getCustomer()
{
return $this->hasOne('common\models\Customer', ['id' => 'customer_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getOrder()
{
return $this->hasOne('common\models\Order', ['id' => 'order_id']);
}
}
122 changes: 122 additions & 0 deletions common/models/shipping/extension/wsdl/SpeeDeePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace common\models\shipping\extension;

use Cassandra\Date;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used?

use common\models\CustomerMeta;
use common\models\shipping\ShipmentPlugin;
use common\models\SpeedeeManifest;
use Yii;

class SpeeDeePlugin extends ShipmentPlugin
{
/**
* Plugin Name
*
* @var string Constant
*/
const PLUGIN_NAME = "SpeeDee";

private SpeedeeManifest $currentManifest;
private string $customerNumber;


public function autoload($customerId = null)
{
$customerMeta = CustomerMeta::find()
->where(['customer_id' => $customerId])
->andWhere(['key' => 'speedee_customer_number'])
->one();

$this->customerNumber = $customerMeta->value;
}

public function getPluginName()
{
return self::PLUGIN_NAME;
}

/**
*
* @return mixed|void
*/
protected function ratePrepare()
{
return $this;
}

protected function rateExecute()
{
return $this;
}

protected function rateProcess()
{
return $this;
}

protected function shipmentPrepare()
{
$manifest = new SpeedeeManifest();
// Shipper Information
$manifest->ship_from_shipper_number = $this->customerNumber;
$manifest->ship_from_name = $this->shipment->sender_company;
$manifest->ship_from_address_1 = $this->shipment->sender_address1;
$manifest->ship_from_address_2 = $this->shipment->sender_address2;
$manifest->ship_from_city = $this->shipment->sender_city;
$manifest->ship_from_zip = $this->shipment->sender_postal_code;
$manifest->ship_from_country = $this->shipment->sender_country;
$manifest->ship_from_email = $this->shipment->sender_email;
$manifest->ship_from_phone = $this->shipment->sender_phone;

// Recipient Information
$manifest->ship_to_import_field = ''; // Would be the speedee internal recipient ID if we had it.
$manifest->ship_to_shipper_number = ''; // Probably wouldn't have this.
$manifest->ship_to_name = $this->shipment->recipient_company ?? $this->shipment->recipient_contact;
$manifest->ship_to_attention = ! $this->shipment->recipient_is_residential ? substr($this->shipment->recipient_contact, 0, 35) : '';
$manifest->ship_to_address_1 = $this->shipment->recipient_address1;
$manifest->ship_to_address_2 = $this->shipment->recipient_address2;
$manifest->ship_to_city = $this->shipment->recipient_city;
$manifest->ship_to_country = $this->shipment->recipient_country;
$manifest->ship_to_email = $this->shipment->recipient_email;
$manifest->ship_to_phone = $this->shipment->recipient_phone;
$manifest->reference_1 = $this->shipment->order_id; // Additional Reference Field (Usually Invoice Number). 2, 3, 4 are also available for use.
$manifest->weight = $this->shipment->getTotalWeight();

$package = $this->shipment->getPackages()[0];
$manifest->height = $package->height;
$manifest->length = $package->length;
$manifest->width = $package->width;

$manifest->oversized = false; // All package sizes are valid
$manifest->pickup_tag = ''; // TODO: find this?
$manifest->aod = false; // TODO: Find signature required
$manifest->aod_option = 0; // TODO: Map int values to option
$manifest->cod = false; // TODO: Find COD options
$manifest->cod_value = 0;
$manifest->package_handling = 0; // TODO: Package handling rate calc?
$manifest->apply_package_handling = false;
$manifest->ship_date = date("Y-m-d H:i:s");
$manifest->bill_to_shipper_number = $this->customerNumber;
$manifest->unboxed = false; // If our cartonization forgets to send a box, re-evaluate our lives

$manifest->save();

$this->currentManifest = $manifest;

return $this;
}

protected function shipmentExecute()
{
Yii::$app->queue->push(new \SpeeDeeShipJob([
'manifest' => $this->currentManifest,
'index' => 0 // @TODO: solve the "how to generate this" dilemma
]));
}

protected function shipmentProcess()
{
return $this;
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"2amigos/2fa-library": "^2.0",
"2amigos/qrcode-library": "^2.0",
"frostealth/yii2-aws-s3": "~2.0",
"league/csv": "^9.8"
"league/csv": "^9.8",
"league/flysystem-ftp": "^3.0",
"nesbot/carbon": "^2.64"
},
"require-dev": {
"yiisoft/yii2-debug": "~2.1.0",
Expand Down
83 changes: 83 additions & 0 deletions console/jobs/speedee/SpeeDeeShipJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use yii\base\BaseObject;
use yii\queue\RetryableJobInterface;
use \common\models\SpeedeeManifest;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use Carbon\Carbon;

class SpeeDeeShipJob extends BaseObject implements RetryableJobInterface
{
public SpeedeeManifest $manifest;
public int $index;
public function execute($queue)
{
$temp = fopen('php://temp/maxmemory:1048576', 'w');
fputcsv($temp, $this->manifest->toArray());

$filename = $this->manifest->bill_to_shipper_number
. Carbon::now()->format('Ymd')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
. Carbon::now()->format('Ymd')
. (new \DateTime())->format('Ymd')

Requiring whole carbon package just for that is an overkill.

. $this->index;

$checksum = sha1($temp);

$adapter = new FtpAdapter(
FtpConnectionOptions::fromArray([
'host' => Yii::$app->params['speedeeFtpHost'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. But we may need to store customers user and pass in meta data and pull from there.

'root' => '/',
'username' => Yii::$app->params['speedeeFtpUser'],
'password' => Yii::$app->params['speedeeFtpPass'],
'port' => 21,
'ssl' => false,
'timeout' => 90,
'utf8' => false,
'passive' => true,
'transferMode' => FTP_BINARY,
'systemType' => null, // 'windows' or 'unix'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check these params

'ignorePassiveAddress' => null, // true or false
'timestampsOnUnixListingsEnabled' => false, // true or false
'recurseManually' => true // true
])
);

$filesystem = new League\Flysystem\Filesystem($adapter);

try {
$filesystem->write('/' . $filename, $temp);
} catch (\League\Flysystem\FilesystemException $e) {
// @TODO: handle exception
}

fclose($temp);

// Validate remote file
try {
$validate = sha1($filesystem->read('/' . $filename));
if ($validate !== $checksum) {
throw new Exception('Manifest ' . $filename . ' did not pass checksum.');
}
} catch (\League\Flysystem\FilesystemException $e) {
// @TODO: handle filesystem exception
} catch (Exception $e) {
// @TODO: handle general exception
}

$this->manifest->manifest_filename = $filename;
$this->manifest->is_manifest_sent = true;
$this->manifest->checksum = $checksum;
$this->manifest->save();


}

public function getTtr()
{
return 120;
}

public function canRetry($attempt, $error)
{
return ($attempt < 3) && ($error instanceof \League\Flysystem\FilesystemException) || ($error instanceof Exception);
}
}
Loading