-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
7814d02
ca30024
e4cdd3e
0754635
26722cd
1daf7b6
0d6589b
586bbbd
43573e8
5f71388
fdb4eab
7265b28
cca24f5
d7463a4
948f7d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,11 @@ | |
|
||
namespace common\models\shipping\extension; | ||
|
||
use Cassandra\Date; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
{ | ||
|
@@ -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() | ||
|
@@ -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. | ||
cdwieber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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 | ||
cdwieber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
])); | ||
} | ||
|
||
protected function shipmentProcess() | ||
{ | ||
// TODO: Implement shipmentProcess() method. | ||
return $this; | ||
} | ||
} |
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()); | ||||||
cgsmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
$filename = $this->manifest->bill_to_shipper_number | ||||||
cdwieber marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
. Carbon::now()->format('Ymd') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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'], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
cdwieber marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
$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); | ||||||
} | ||||||
} | ||||||
cdwieber marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
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?