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' => '',
];
11 changes: 8 additions & 3 deletions common/models/SpeedeeManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @property int $order_id
* @property int $customer_id
* @property string $ship_from_shipper_number
* @property string $ship_from_bane
* @property string $ship_from_name
* @property string $ship_from_attention
* @property string $ship_from_address_1
* @property string $ship_from_address_2
Expand Down Expand Up @@ -51,6 +51,8 @@
* @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
Expand All @@ -74,7 +76,7 @@ public function rules()
[['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_bane', '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],
[['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']],
];
Expand All @@ -90,7 +92,7 @@ public function attributeLabels()
'order_id' => 'Order ID',
'customer_id' => 'Customer ID',
'ship_from_shipper_number' => 'Ship From Shipper Number',
'ship_from_bane' => 'Ship From Bane',
'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',
Expand Down Expand Up @@ -130,6 +132,9 @@ public function attributeLabels()
'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',
];
}

Expand Down
80 changes: 68 additions & 12 deletions common/models/shipping/extension/wsdl/SpeeDeePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

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
{
Expand All @@ -13,16 +17,18 @@ class SpeeDeePlugin extends ShipmentPlugin
*/
const PLUGIN_NAME = "SpeeDee";

private string $hostProd = '66.191.64.52';
private string $hostDev = '';
private string $ftpUser = '';
private string $frpPassword = '';
private SpeedeeManifest $currentManifest;
private string $customerNumber;


public function autoload($customerId = null)
{
// TODO: Implement autoload() method.
// Shipper Id comes in from settings?
$customerMeta = CustomerMeta::find()
->where(['customer_id' => $customerId])
->andWhere(['key' => 'speedee_customer_number'])
->one();

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

public function getPluginName()
Expand All @@ -36,31 +42,81 @@ public function getPluginName()
*/
protected function ratePrepare()
{
// TODO: Implement ratePrepare() method.
return $this;
}

protected function rateExecute()
{
// TODO: Implement rateExecute() method.
return $this;
}

protected function rateProcess()
{
// TODO: Implement rateProcess() method.
return $this;
}

protected function shipmentPrepare()
{
// TODO: Implement shipmentPrepare() method.
$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()
{
// TODO: Implement shipmentExecute() method.
Yii::$app->queue->push(new \SpeeDeeShipJob([
'manifest' => $this->currentManifest,
'index' => 0 // @TODO: solve the "how to generate this" dilemma
]));
}

protected function shipmentProcess()
{
// TODO: Implement shipmentProcess() method.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function safeUp()
'ship_date' => $this->date(), // Date package was picked up by driver; default is to use processing date
'bill_to_shipper_number' => $this->string(6), // currently same as ship_to number
'unboxed' => $this->boolean()->defaultValue(false),

'manifest_filename' => $this->string()->null(),
'is_manifest_sent' => $this->boolean()->defaultValue(false),
'checksum' => $this->string()->null(),
]);

$this->addForeignKey(
Expand Down