23-01-2026

This commit is contained in:
Kevin Adametz 2026-01-23 17:35:23 +01:00
parent a939cd51ef
commit a8b395e20d
248 changed files with 29342 additions and 4805 deletions

View file

@ -2,8 +2,9 @@
namespace App\Jobs;
use App\Models\DhlShipment;
use App\Services\DhlApiService;
use Acme\Dhl\Models\DhlShipment;
use Acme\Dhl\Services\ShippingService;
use Acme\Dhl\Support\DhlClient;
use Exception;
use Illuminate\Bus\Queueable as BusQueueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -56,7 +57,7 @@ class CancelShipmentJob implements ShouldQueue
{
$this->dhlShipment = $dhlShipment;
$this->options = $options;
// Set queue name based on priority
if (isset($options['priority']) && $options['priority'] === 'high') {
$this->onQueue('high-priority');
@ -73,28 +74,40 @@ class CancelShipmentJob implements ShouldQueue
try {
Log::info('[DHL Queue] Starting shipment cancellation job', [
'shipment_id' => $this->dhlShipment->id,
'shipment_number' => $this->dhlShipment->shipment_number,
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
'attempt' => $this->attempts(),
]);
$dhlService = new DhlApiService();
// Get DHL configuration
$settingController = new \App\Http\Controllers\SettingController;
$dhlConfig = $settingController->getDhlConfig();
// Create DHL client
$dhlClient = new DhlClient(
$dhlConfig['base_url'],
$dhlConfig['api_key'],
$dhlConfig['username'],
$dhlConfig['password']
);
// Create shipping service
$shippingService = new ShippingService($dhlClient);
// Cancel the shipment
$success = $dhlService->cancelShipment($this->dhlShipment);
$success = $shippingService->cancelLabel($this->dhlShipment->dhl_shipment_no);
if ($success) {
Log::info('[DHL Queue] Shipment cancelled successfully', [
'shipment_id' => $this->dhlShipment->id,
'shipment_number' => $this->dhlShipment->shipment_number,
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
]);
} else {
throw new Exception('Cancellation returned false');
}
} catch (Exception $e) {
Log::error('[DHL Queue] Shipment cancellation failed', [
'shipment_id' => $this->dhlShipment->id,
'shipment_number' => $this->dhlShipment->shipment_number,
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
'error' => $e->getMessage(),
'attempt' => $this->attempts(),
'max_tries' => $this->tries,
@ -106,6 +119,11 @@ class CancelShipmentJob implements ShouldQueue
'shipment_id' => $this->dhlShipment->id,
'error' => $e->getMessage(),
]);
// Update shipment status to indicate cancellation failed
$this->dhlShipment->update([
'status' => 'failed',
]);
}
throw $e; // Re-throw to trigger retry mechanism
@ -121,15 +139,22 @@ class CancelShipmentJob implements ShouldQueue
{
Log::error('[DHL Queue] CancelShipmentJob permanently failed', [
'shipment_id' => $this->dhlShipment->id,
'shipment_number' => $this->dhlShipment->shipment_number,
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);
// You could implement additional failure handling here:
// - Send notification to admin
// - Create manual task for staff to handle cancellation
// - Update shipment status to indicate cancellation failed
// Update shipment status to indicate cancellation failed
try {
$this->dhlShipment->update([
'status' => 'failed',
]);
} catch (Exception $e) {
Log::error('[DHL Queue] Could not update shipment status after failure', [
'shipment_id' => $this->dhlShipment->id,
'error' => $e->getMessage(),
]);
}
}
/**

View file

@ -2,8 +2,10 @@
namespace App\Jobs;
use App\Models\DhlShipment;
use App\Services\DhlApiService;
use Acme\Dhl\Models\DhlShipment;
use Acme\Dhl\Services\ReturnsService;
use Acme\Dhl\Support\DhlClient;
use App\Http\Controllers\SettingController;
use Exception;
use Illuminate\Bus\Queueable as BusQueueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -56,7 +58,7 @@ class CreateReturnLabelJob implements ShouldQueue
{
$this->originalShipment = $originalShipment;
$this->options = $options;
// Set queue name based on priority
if (isset($options['priority']) && $options['priority'] === 'high') {
$this->onQueue('high-priority');
@ -73,29 +75,39 @@ class CreateReturnLabelJob implements ShouldQueue
try {
Log::info('[DHL Queue] Starting return label creation job', [
'original_shipment_id' => $this->originalShipment->id,
'original_shipment_number' => $this->originalShipment->shipment_number,
'original_shipment_number' => $this->originalShipment->dhl_shipment_no,
'attempt' => $this->attempts(),
]);
$dhlService = new DhlApiService();
// Get DHL configuration
$settingController = new SettingController();
$dhlConfig = $settingController->getDhlConfig();
// Create the return label
$returnShipment = $dhlService->createReturnLabel(
$this->originalShipment,
$this->options
// Initialize DHL client
$dhlClient = new DhlClient(
$dhlConfig['base_url'],
$dhlConfig['api_key'],
$dhlConfig['username'],
$dhlConfig['password']
);
// Use ReturnsService instead of ShippingService
$returnsService = new ReturnsService($dhlClient);
// Prepare return label data from original shipment
$returnData = $this->prepareReturnLabelData($dhlConfig);
Log::info('[DHL Queue] Prepared return data', [
'returnData' => $returnData,
]);
// Create the return label using ReturnsService (with automatic fallback)
$result = $returnsService->createReturn($returnData);
Log::info('[DHL Queue] Return label created successfully', [
'original_shipment_id' => $this->originalShipment->id,
'return_shipment_id' => $returnShipment->id,
'return_shipment_number' => $returnShipment->shipment_number,
'return_shipment_number' => $result['returnNumber'] ?? 'N/A',
]);
// Trigger follow-up actions if specified
if (isset($this->options['auto_track']) && $this->options['auto_track']) {
\App\Jobs\TrackShipmentJob::dispatch($returnShipment)->delay(now()->addMinutes(5));
}
} catch (Exception $e) {
Log::error('[DHL Queue] Return label creation failed', [
'original_shipment_id' => $this->originalShipment->id,
@ -116,6 +128,48 @@ class CreateReturnLabelJob implements ShouldQueue
}
}
/**
* Prepare return label data based on original shipment
*/
private function prepareReturnLabelData(array $dhlConfig): array
{
$order = $this->originalShipment->shoppingOrder;
$recipient = $this->originalShipment->recipient ?? [];
return [
'order_id' => $order->id,
'original_shipment_id' => $this->originalShipment->id,
'weight_kg' => $this->originalShipment->weight_kg,
'label_format' => $this->originalShipment->label_format ?? 'PDF',
// Shipper: Customer sends back to us (swap addresses)
'shipper' => [
'name' => trim(($recipient['firstname'] ?? '') . ' ' . ($recipient['lastname'] ?? '')),
'name2' => $recipient['company'] ?? '',
'street' => $recipient['street'] ?? '',
'houseNumber' => $recipient['houseNumber'] ?? '',
'postalCode' => $recipient['postalCode'] ?? '',
'city' => $recipient['city'] ?? '',
'country' => $recipient['country'] ?? 'DEU',
'email' => $recipient['email'] ?? '',
'phone' => $recipient['phone'] ?? '',
],
// Consignee: Our warehouse (from settings)
'consignee' => [
'name' => $dhlConfig['sender']['company'] ?? 'mivita care gmbh',
'name2' => $dhlConfig['sender']['name'] ?? '',
'street' => $dhlConfig['sender']['street'] ?? 'Leinfeld',
'houseNumber' => $dhlConfig['sender']['house_number'] ?? '2',
'postalCode' => $dhlConfig['sender']['postalCode'] ?? '87755',
'city' => $dhlConfig['sender']['city'] ?? 'Kirchhaslach',
'country' => $dhlConfig['sender']['country'] ?? 'DEU',
'email' => $dhlConfig['sender']['email'] ?? 'versand@mivita.care',
'phone' => $dhlConfig['sender']['phone'] ?? '+49 123 456789',
],
];
}
/**
* Handle a job failure.
*
@ -125,7 +179,7 @@ class CreateReturnLabelJob implements ShouldQueue
{
Log::error('[DHL Queue] CreateReturnLabelJob permanently failed', [
'original_shipment_id' => $this->originalShipment->id,
'original_shipment_number' => $this->originalShipment->shipment_number,
'original_shipment_number' => $this->originalShipment->dhl_shipment_no,
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);