Skip to content

Instantly share code, notes, and snippets.

@cichondev

cichondev/a.php

Created Jul 2, 2020
Embed
What would you like to do?
#!/usr/bin/env php
<?php
use App\Domains\Shipment\Shipment;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Application;
use App\Domains\Connector\Connector;
use App\Domains\Connector\Integrators\CloudHQIntegrator;
use App\Domains\ShipmentItem\ShipmentItem;
use App\Domains\Store\Store;
use App\Services\CloudHQ\ApiRequest;
use App\Services\CloudHQ\SalesOrder\SalesOrderApiFactory;
use App\Services\CloudHQ\ShipSalesOrder\ShipSalesOrderApiFactory;
use Illuminate\Support\Facades\DB;
define('LARAVEL_START', microtime(true));
require __DIR__.'/vendor/autoload.php';
/** @var Application $app */
$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$kernel->handle(
$request = Illuminate\Http\Request::capture()
);
if (! isset($argv[1]) ||
! isset($argv[2]) ||
! isset($argv[3])
) {
exit("\n\nERROR: Invalid arguments!\n\n");
}
$beginShipmentDate = $argv[1];
$endShipmentDate = $argv[2];
$storeId = $argv[3];
$chunkLength = $argv[4] ?? 100;
/** @var Store $store */
$store = Store::query()->find($storeId);
print "\nQuerying Shipments...\n";
$query = Shipment::query();
$query->whereRaw('(select count(shipment_item.id) from shipment_item where shipment.id = shipment_item.shipment_id and shipment_item.shipment_return_id IS NOT NULL) IS FALSE')
->whereDate('shipment_date', '>=', $beginShipmentDate)
->whereDate('shipment_date', '<=', $endShipmentDate)
->where('invalid_track_number', 'is', FALSE)
->where('store_id', $storeId)
->select(['id', 'shipment_reference']);
print "Reimporting...\n";
$works = $query->chunk($chunkLength, function (Collection $shipments) use ($store) {
reimport($shipments, $store);
print "{$shipments->count()} items processed...\n";
});
if ($works) {
print "\nProcess completed successfully!\n";
} else {
print "\nError!\n";
}
function reimport(Collection $shipments, Store $store)
{
$connector = $store->getShipmentConnector();
$reimportedShipments = pullShipments($connector, $shipments->pluck('shipment_reference')->toArray());
/** @var Shipment $reimportedShipment */
/** @var Shipment $shipment */
foreach ($reimportedShipments['items'] as $reimportedShipment) {
foreach ($shipments as $shipment) {
if ($reimportedShipment->shipment_reference == $shipment->shipment_reference) {
updateShipment($reimportedShipment, $shipment);
break;
}
}
}
}
function pullShipments(Connector $connector, array $shipmentReferences)
{
global $app;
$integrator = $app->make(CloudHQIntegrator::class);
$shipmentRequest = new ApiRequest();
$shipmentRequest->addFilter('ShipSalesOrderNo', 'Contains', implode(',', $shipmentReferences));
$shipmentsApi = ShipSalesOrderApiFactory::build($connector->configuration['host']);
$ordersApi = SalesOrderApiFactory::build($connector->configuration['host']);
$integrator->setup(
$connector->configuration['api_key'],
$ordersApi,
$shipmentsApi,
$connector->store->getCarriersIdentifier(),
$connector->store->in_store_pickup_code
);
return $integrator->pullShipments($shipmentRequest);
}
function updateShipment(Shipment $reimportedShipment, Shipment $shipment)
{
try {
DB::beginTransaction();
$shipment->external_identifier = $reimportedShipment->external_identifier;
$shipment->shipment_reference = $reimportedShipment->shipment_reference;
$shipment->order_reference = $reimportedShipment->order_reference;
$shipment->street_address = $reimportedShipment->street_address;
$shipment->city = $reimportedShipment->city;
$shipment->state = $reimportedShipment->state;
$shipment->postal_code = $reimportedShipment->postal_code;
$shipment->country = $reimportedShipment->country;
$shipment->customer_name = $reimportedShipment->customer_name;
$shipment->customer_reference = $reimportedShipment->customer_reference;
$shipment->customer_email = $reimportedShipment->customer_email;
$shipment->customer_sms_number = $reimportedShipment->customer_sms_number;
$shipment->is_in_store_pickup = $reimportedShipment->is_in_store_pickup;
$shipment->shipment_date = $reimportedShipment->shipment_date;
$shipment->order_date = $reimportedShipment->order_date;
$shipment->payment_methods = $reimportedShipment->payment_methods;
$shipment->external_updated_at = $reimportedShipment->external_updated_at;
$shipment->update();
/** @var ShipmentItem $item */
foreach ($shipment->items as $item) {
$item->delete();
}
/** @var ShipmentItem $item */
foreach ($reimportedShipment->items as $item) {
$item->shipment_id = $shipment->id;
$item->save();
}
$shipment->items = $reimportedShipment->items;
DB::commit();
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
return $shipment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.