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

110 lines
3.1 KiB
PHP

<?php
namespace App\Models;
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 Shipment extends Model
{
protected $table = 'shipment';
use HasFactory;
protected $fillable = [
'user_id',
'shipment_request_id',
'internal_shipment_id',
'shipment_id',
'tracking_number',
'tracking_url',
];
public $timestamps = true;
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected static function booted()
{
parent::boot();
static::saving(function ($shipmentDetails) {
$callbackService = App::make(CallbackService::class);
ActionLog::create([
'name' => "shipment_created",
'description' => "Shipment created, no: '{$shipmentDetails->tracking_number}'",
'user_id' => Auth::user()->id ?? 1,
'shipment_request_id' => $shipmentDetails->shipmentRequest->id
]);
Log::channel('expedice')->info('booted saving initiated in Shipment model - shipment created by ' . (Auth::user()->name ?? '///'), [
'shipment_request_id' => $shipmentDetails->shipmentRequest->id,
'user_id' => Auth::user()->id ?? 1,
]);
if ($shipmentDetails->user->details->apiCallbackURL && $shipmentDetails->user->details->callbacks_enabled) {
$callbackService->sendCallback($shipmentDetails->user->details->apiCallbackURL, [
'endpoint' => 'shipmentProcessed',
'result' => 'true',
'shipment_reference' => $shipmentDetails->shipmentRequest->shipment_reference,
'data' => [
'trackingNumber' => $shipmentDetails->tracking_number,
'trackingURL' => $shipmentDetails->tracking_url,
],
'additionalInfo' => null
]);
}
foreach ($shipmentDetails->shipmentRequest->errors as $error) {
$error->update([
'resolved' => true,
]);
}
});
}
public function shipmentRequest()
{
return $this->belongsTo(ShipmentRequest::class, 'shipment_request_id');
}
/**
* Get the user that owns the shipment.
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function label()
{
return $this->hasOne(Label::class, 'shipment_id');
}
public function processedBaseFees()
{
return $this->hasMany(ShipmentProcessedBaseFee::class, 'shipment_id');
}
public function processedExtraFees()
{
return $this->hasMany(ShipmentProcessedExtraFee::class, 'shipment_id');
}
public function trackingStatusHistory()
{
return $this->hasMany(ShipmentTrackingStatusHistory::class, 'shipment_id');
}
}