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, ]); } }); } }