shipping/src/app/Models/ShipmentStatusHistory.php
2025-01-14 22:09:35 +01:00

118 lines
4.4 KiB
PHP

<?php
namespace App\Models;
use App\Factories\CarrierServiceFactory;
use App\Services\CallbackService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ShipmentStatusHistory extends Model
{
protected $table = 'shipment_status_history';
use HasFactory;
protected $fillable = [
'shipment_request_id',
'shipment_status_id',
'status_note',
];
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
/**
* Get the shipment that owns the status history.
*/
public function shipmentRequest()
{
return $this->belongsTo(ShipmentRequest::class, 'shipment_request_id');
}
/**
* Get the shipment status that owns the status history.
*/
public function shipmentStatus()
{
return $this->belongsTo(ShipmentStatus::class, 'shipment_status_id');
}
protected static function booted()
{
parent::boot();
static::creating(function ($shipmentStatusHistory) {
$callbackService = App::make(CallbackService::class);
ActionLog::create([
'name' => "status_changed",
'description' => "Status changed to '{$shipmentStatusHistory->shipmentStatus->name}'",
'user_id' => Auth::user()->id ?? 1,
'shipment_request_id' => $shipmentStatusHistory->shipmentRequest->id
]);
Log::channel('expedice')->info("Status changed to {$shipmentStatusHistory->shipmentStatus->name} by " . (Auth::user()->name ?? '///'), [
'shipment_request_id' => $shipmentStatusHistory->shipmentRequest->id
]);
if(in_array($shipmentStatusHistory->shipment_status_id, [
ShipmentStatus::STATUS_ADDRESS_INSUFFICIENT,
ShipmentStatus::STATUS_OUT_OF_STOCK,
ShipmentStatus::STATUS_IS_REPAIR,
ShipmentStatus::STATUS_PROGRAMMING_MODEL_REQUIRED,
ShipmentStatus::STATUS_PROGRAMMING_PROCESS,
ShipmentStatus::STATUS_CANCELED,
])) {
// auto batch remove statuses
if (!is_null($shipmentStatusHistory->shipmentRequest->batch)) {
ShipmentRequestBatchItem::where('shipment_request_id', $shipmentStatusHistory->shipmentRequest->id)->delete();
}
}
if($shipmentStatusHistory->shipment_status_id === ShipmentStatus::STATUS_CANCELED) {
try {
if(!empty($shipmentStatusHistory->shipmentRequest->shipment)) {
$carrierService = CarrierServiceFactory::create($shipmentStatusHistory->shipmentRequest->carrier);
$res = $carrierService->cancelShipment($shipmentStatusHistory->shipmentRequest->shipment);
if ($res) {
$shipmentStatusHistory->shipmentRequest?->shipment?->label?->delete();
$shipmentStatusHistory->shipmentRequest?->shipment?->delete();
}
}
}
catch(\Exception $e) {
}
}
// Example of a callback or logging action when a new status history is created
if ($shipmentStatusHistory->shipmentRequest->user->details->apiCallbackURL && $shipmentStatusHistory->shipmentRequest->user->details->callbacks_enabled) {
$callbackService->sendCallback($shipmentStatusHistory->shipmentRequest->user->details->apiCallbackURL, [
'endpoint' => 'shipmentStatusHistoryCreated',
'result' => 'true',
'data' => [
'trackingNumber' => $shipmentStatusHistory->shipmentRequest->shipment?->tracking_number ?? "",
'trackingURL' => $shipmentStatusHistory->shipmentRequest->shipment?->tracking_url ?? "",
],
'shipment_reference' => $shipmentStatusHistory->shipmentRequest->shipment_reference,
'status_id' => $shipmentStatusHistory->shipment_status_id,
'status_name' => $shipmentStatusHistory->shipmentStatus->name,
'status_desc' => $shipmentStatusHistory->shipmentStatus->description,
]);
}
});
}
}