Files
izyim/app/Classes/Modules/Orders/Services/ListsOrderHistory.php
Omair Saleh 43a9e0ca03 large commit
2019-05-18 12:14:31 +08:00

108 lines
6.0 KiB
PHP

<?php
namespace App\Classes\Modules\Orders\Services;
use App\Classes\Constants;
use App\Models\CustomIssues;
use App\Models\Order;
use App\Models\Warehouse;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
class ListsOrderHistory
{
public function execute(string $orderId, Carbon $createdAt, bool $complete, Carbon $lastUpdate){
/** @var Order $order */
$order = Order::findOrFail($orderId);
$orderSteps = $order->orderSteps()->where('complete', true)->orderBy('sequence')->get();
$history = new Collection();
$history->push(['date' => $createdAt->format('d-m-Y'), 'description' => 'Order number has been created']);
foreach ($orderSteps as $step){
$completeDate = Carbon::parse($step->complete_date)->format('d-m-Y');
switch ($step->reference_id){
case Constants::ORDER_PROCESSING_STEP:
if($order->warehouse_id){
$history->push(['date' => $completeDate, 'description' => Warehouse::find($order->warehouse_id)->first()->name.' confirmed as your order\'s warehouse']);
}
break;
case Constants::ORDER_NO_WAREHOUSE_PENDING_SUPPLIER:
$history->push(['date' => $completeDate, 'description' => 'Your Order has been loaded into container']);
break;
case Constants::ORDER_WAREHOUSE_RECEIVING_STEP:
$warehouse = Warehouse::find($order->warehouse_id)->first();
$history->push(['date' => $completeDate, 'description' => 'Arrived at '. $warehouse->name .' warehouse in china']);
break;
case Constants::ORDER_WAREHOUSE_PACKING_STEP:
$packages = $order->packingLists()->firstOrFail()->packages()->selectRaw(DB::raw('((width/100) * (height/100) * (length/100)) * quantity as cbm, quantity'))->get();
$history->push(['date' => $completeDate, 'description' => '['.$packages->sum('quantity').' Packages, '.number_format((float)$packages->sum('cbm'), 2, '.', '').' CBM] Your order has been loaded into container']);
$history->push(['date' => $completeDate, 'description' => 'Invoice has been issued, waiting for shipping payment']);
break;
case Constants::ORDER_SHIPPING_STEP:
$shippingArrangement = $order->shippingArrangement()->first();
if($shippingArrangement){
$shippingSchedules = $shippingArrangement->shippingSchedule()->get();
foreach ($shippingSchedules as $schedule){
if($schedule->status === 2){
$customIssue = CustomIssues::where('schedule_id', $schedule->id)->first();
if($customIssue !== null){
$history->push(['date' => $completeDate, 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Shipping Schedule failed due to: '.$customIssue->remark]);
} else {
$history->push(['date' => $completeDate, 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Shipping Schedule failed']);
}
} else {
$history->push(['date' => $completeDate, 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Your Order has been scheduled for shipping ']);
}
}
}
$history->push(['date' => $completeDate, 'description' => 'Arrived to Malaysian port']);
$history->push(['date' => $completeDate, 'description' => 'Custom clearance is successful']);
break;
case Constants::ORDER_WAREHOUSE_ARRIVAL_STEP:
$history->push(['date' => $completeDate, 'description' => 'Arrived at Malaysia\'s warehouse']);
break;
case Constants::ORDER_WAREHOUSE_UNPACKING_STEP:
$history->push(['date' => $completeDate, 'description' => 'Your Order has been unloaded and being prepared for delivery']);
break;
case Constants::ORDER_DELIVERY_STEP:
$history->push(['date' => $completeDate, 'description' => 'Out for delivery']);
break;
}
}
if($order->current_step === Constants::ORDER_SHIPPING_STEP){
$shippingArrangement = $order->shippingArrangement()->first();
if($shippingArrangement){
$shippingSchedules = $shippingArrangement->shippingSchedule()->get();
foreach ($shippingSchedules as $schedule){
if($schedule->status === 2){
$customIssue = CustomIssues::where('schedule_id', $schedule->id)->first();
if($customIssue !== null){
$history->push(['date' => $schedule->updated_at->format('d-m-Y'), 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Shipping Schedule failed due to: '.$customIssue->remark]);
} else {
$history->push(['date' => $schedule->updated_at->format('d-m-Y'), 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Shipping Schedule failed']);
}
} else {
$history->push(['date' => $schedule->updated_at->format('d-m-Y'), 'description' => '[ETD: '.$schedule->ETD.', ETA: '.$schedule->ETA.'] Your Order has been scheduled for shipping ']);
}
}
}
}
if($complete){
$history->push(['date' => $lastUpdate->format('d-m-Y'), 'description' => 'Your Order has successfully been delivered']);
}
return $history->reverse()->toArray();
}
}