27-05-2026 DHL Modul v2.1 / Optimierung tracking
This commit is contained in:
parent
036595be94
commit
2bdc9ada3c
33 changed files with 2367 additions and 2086 deletions
|
|
@ -179,7 +179,7 @@ class DhlShipmentController extends Controller
|
|||
return '<span class="text-muted">N/A</span>';
|
||||
})
|
||||
->addColumn('customer', function ($shipment) {
|
||||
return $shipment->firstname.' '.$shipment->lastname;
|
||||
return e(trim($shipment->firstname.' '.$shipment->lastname));
|
||||
})
|
||||
->editColumn('dhl_shipment_no', function ($shipment) {
|
||||
return $shipment->dhl_shipment_no ? '<code class="text-success">'.e($shipment->dhl_shipment_no).'</code>' : '<span class="text-muted">-</span>';
|
||||
|
|
@ -573,11 +573,13 @@ class DhlShipmentController extends Controller
|
|||
private function getBillingAddressForReturn($shippingUser, array $recipient): array
|
||||
{
|
||||
if (! $shippingUser) {
|
||||
Log::warning('[DHL Controller] No shipping user found, using recipient data', [
|
||||
'recipient' => $recipient,
|
||||
Log::warning('[DHL Controller] No shipping user found, using recipient country only', [
|
||||
'order_recipient_country' => $recipient['country'] ?? null,
|
||||
]);
|
||||
|
||||
// Fallback: use recipient data but without Packstation fields
|
||||
// Fallback: use recipient data but without Packstation fields.
|
||||
// We keep ISO-2 country codes here so that ReturnsService /
|
||||
// DhlProductResolver can normalize / validate them consistently.
|
||||
return [
|
||||
'name' => trim(($recipient['firstname'] ?? '').' '.($recipient['lastname'] ?? '')),
|
||||
'name2' => $recipient['company'] ?? '',
|
||||
|
|
@ -585,7 +587,7 @@ class DhlShipmentController extends Controller
|
|||
'houseNumber' => '',
|
||||
'postalCode' => $recipient['postalCode'] ?? '',
|
||||
'city' => $recipient['city'] ?? '',
|
||||
'country' => $recipient['country'] ?? 'DEU',
|
||||
'country' => $recipient['country'] ?? DhlProductResolver::DOMESTIC_COUNTRY,
|
||||
'email' => $recipient['email'] ?? '',
|
||||
'phone' => $recipient['phone'] ?? '',
|
||||
];
|
||||
|
|
@ -609,7 +611,7 @@ class DhlShipmentController extends Controller
|
|||
'houseNumber' => $houseNumber,
|
||||
'postalCode' => $shippingUser->billing_zipcode ?? '',
|
||||
'city' => $shippingUser->billing_city ?? '',
|
||||
'country' => $shippingUser->billing_country?->code ?? 'DEU',
|
||||
'country' => $shippingUser->billing_country?->code ?? DhlProductResolver::DOMESTIC_COUNTRY,
|
||||
'email' => $shippingUser->billing_email ?? '',
|
||||
'phone' => $shippingUser->billing_phone ?? '',
|
||||
];
|
||||
|
|
@ -665,7 +667,7 @@ class DhlShipmentController extends Controller
|
|||
'houseNumber' => $recipient['houseNumber'] ?? '',
|
||||
'postalCode' => $recipient['postalCode'] ?? '',
|
||||
'city' => $recipient['city'] ?? '',
|
||||
'country' => $recipient['country'] ?? 'DEU',
|
||||
'country' => $recipient['country'] ?? DhlProductResolver::DOMESTIC_COUNTRY,
|
||||
'email' => $recipient['email'] ?? '',
|
||||
'phone' => $recipient['phone'] ?? '',
|
||||
];
|
||||
|
|
@ -688,7 +690,7 @@ class DhlShipmentController extends Controller
|
|||
'houseNumber' => $dhlConfig['sender']['house_number'] ?? '2',
|
||||
'postalCode' => $dhlConfig['sender']['postalCode'] ?? '87755',
|
||||
'city' => $dhlConfig['sender']['city'] ?? 'Kirchhaslach',
|
||||
'country' => $dhlConfig['sender']['country'] ?? 'DEU',
|
||||
'country' => $dhlConfig['sender']['country'] ?? DhlProductResolver::DOMESTIC_COUNTRY,
|
||||
'email' => $dhlConfig['sender']['email'] ?? 'versand@mivita.care',
|
||||
'phone' => $dhlConfig['sender']['phone'] ?? '+49 123 456789',
|
||||
],
|
||||
|
|
|
|||
|
|
@ -172,6 +172,7 @@ class ModalController extends Controller
|
|||
}
|
||||
|
||||
if ($data['action'] === 'create-dhl-shipment') {
|
||||
$this->authorizeDhlShipmentModal();
|
||||
$id = $data['id'] ?? null;
|
||||
$ret = $this->handleDhlShipmentModal($id, $data);
|
||||
}
|
||||
|
|
@ -202,6 +203,23 @@ class ModalController extends Controller
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the current user is allowed to use the DHL shipment modal.
|
||||
*
|
||||
* The DHL cockpit is an admin-only tool. Without this guard a logged-in
|
||||
* CRM user could call `POST /modal/load` with `action=create-dhl-shipment`
|
||||
* and an arbitrary order id and would receive that order's recipient
|
||||
* name, address, e-mail and existing shipments (IDOR).
|
||||
*/
|
||||
private function authorizeDhlShipmentModal(): void
|
||||
{
|
||||
$user = \Auth::user();
|
||||
|
||||
if (! $user || ! method_exists($user, 'isAdmin') || ! $user->isAdmin()) {
|
||||
abort(403, 'DHL shipment modal is only available for admin users.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle DHL shipment modal preparation
|
||||
*
|
||||
|
|
|
|||
|
|
@ -4,11 +4,26 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Models\Setting;
|
||||
use App\Services\DhlProductResolver;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Request;
|
||||
|
||||
class SettingController extends Controller
|
||||
{
|
||||
/**
|
||||
* In-memory cache of the resolved DHL configuration for the lifetime
|
||||
* of the current PHP process (request, queue job, CLI command).
|
||||
*
|
||||
* `getDhlConfig()` is invoked from several layers per shipment
|
||||
* (`DhlShipmentService`, `CreateShipmentJob`, `DhlShipmentController`
|
||||
* cancel/return paths, …) and each call previously triggered ~25 DB
|
||||
* queries against `settings`. Caching here turns subsequent calls
|
||||
* into pure in-memory lookups.
|
||||
*
|
||||
* @var array<string, mixed>|null
|
||||
*/
|
||||
private static ?array $cachedDhlConfig = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('admin');
|
||||
|
|
@ -31,6 +46,9 @@ class SettingController extends Controller
|
|||
if (isset($data['settings'])) {
|
||||
foreach ($data['settings'] as $key => $value) {
|
||||
$value['val'] = isset($value['val']) ? $value['val'] : false;
|
||||
if ($key === 'dhl_international_countries') {
|
||||
$value['val'] = DhlProductResolver::normalizeCountryCodeList(is_array($value['val']) ? $value['val'] : []);
|
||||
}
|
||||
Setting::setContentBySlug($key, $value['val'], $value['type']);
|
||||
}
|
||||
}
|
||||
|
|
@ -40,6 +58,9 @@ class SettingController extends Controller
|
|||
$this->updateDhlConfigCache();
|
||||
Session::flash('alert-save-dhl', 'DHL Konfiguration erfolgreich gespeichert!');
|
||||
} else {
|
||||
// Any other setting change could still affect cached config
|
||||
// values (e.g. shared sender data), so invalidate just in case.
|
||||
self::flushDhlConfigCache();
|
||||
Session::flash('alert-save', '1');
|
||||
}
|
||||
}
|
||||
|
|
@ -47,14 +68,34 @@ class SettingController extends Controller
|
|||
return redirect(route('admin_settings'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the in-process DHL configuration cache.
|
||||
*
|
||||
* Call this whenever a DHL-related setting is changed so the next
|
||||
* call to {@see getDhlConfig()} reads fresh data from the database.
|
||||
*/
|
||||
public static function flushDhlConfigCache(): void
|
||||
{
|
||||
self::$cachedDhlConfig = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get DHL configuration merged from database settings and .env values
|
||||
* Priority is controlled by DHL_CONFIG_SOURCE environment variable:
|
||||
* - 'database' (default): Database settings override .env values
|
||||
* - 'env': Environment/Config values override database settings
|
||||
*
|
||||
* The result is cached per process; use {@see flushDhlConfigCache()}
|
||||
* to invalidate the cache after changing settings.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getDhlConfig()
|
||||
{
|
||||
if (self::$cachedDhlConfig !== null) {
|
||||
return self::$cachedDhlConfig;
|
||||
}
|
||||
|
||||
// Check if we're in test/sandbox mode
|
||||
$isTestMode = config('dhl.legacy.test_mode', false) || config('dhl.legacy.sandbox', false);
|
||||
$baseUrl = $isTestMode ? config('dhl.sandbox_url') : config('dhl.base_url');
|
||||
|
|
@ -62,7 +103,7 @@ class SettingController extends Controller
|
|||
// Determine configuration priority
|
||||
$useEnvPriority = config('dhl.config_source') === 'env';
|
||||
|
||||
return [
|
||||
return self::$cachedDhlConfig = [
|
||||
// API Settings
|
||||
'base_url' => $isTestMode ? $baseUrl : $this->getConfigValue('dhl_base_url', $baseUrl, $useEnvPriority),
|
||||
'api_key' => $this->getConfigValue('dhl_api_key', config('dhl.api_key'), $useEnvPriority),
|
||||
|
|
@ -152,9 +193,14 @@ class SettingController extends Controller
|
|||
{
|
||||
$configCountries = config('dhl.international_countries', DhlProductResolver::DEFAULT_INTERNATIONAL_COUNTRIES);
|
||||
$countries = $configCountries;
|
||||
$storedCountries = Schema::hasTable('settings')
|
||||
? Setting::getContentBySlug('dhl_international_countries')
|
||||
: false;
|
||||
|
||||
if (! $useEnvPriority) {
|
||||
$countries = Setting::getContentBySlug('dhl_international_countries') ?: $configCountries;
|
||||
if (is_array($storedCountries)) {
|
||||
$countries = $storedCountries;
|
||||
} elseif (! $useEnvPriority) {
|
||||
$countries = $storedCountries ?: $configCountries;
|
||||
}
|
||||
|
||||
return DhlProductResolver::normalizeCountryCodeList(is_array($countries) ? $countries : []);
|
||||
|
|
@ -165,6 +211,10 @@ class SettingController extends Controller
|
|||
*/
|
||||
private function updateDhlConfigCache()
|
||||
{
|
||||
// Drop the in-process DHL config cache so the next call rebuilds it
|
||||
// from the freshly saved settings.
|
||||
self::flushDhlConfigCache();
|
||||
|
||||
// Clear config cache to force reload from database
|
||||
\Artisan::call('config:clear');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue