180 lines
8.1 KiB
PHP
180 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Setting;
|
|
use App\Services\DhlProductResolver;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Request;
|
|
|
|
class SettingController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$data = [
|
|
'values' => [],
|
|
];
|
|
|
|
return view('admin.settings.index', $data);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$data = Request::all();
|
|
if (isset($data['action'])) {
|
|
if (isset($data['settings'])) {
|
|
foreach ($data['settings'] as $key => $value) {
|
|
$value['val'] = isset($value['val']) ? $value['val'] : false;
|
|
Setting::setContentBySlug($key, $value['val'], $value['type']);
|
|
}
|
|
}
|
|
|
|
// DHL-spezifische Behandlung
|
|
if ($data['action'] === 'save_dhl') {
|
|
$this->updateDhlConfigCache();
|
|
Session::flash('alert-save-dhl', 'DHL Konfiguration erfolgreich gespeichert!');
|
|
} else {
|
|
Session::flash('alert-save', '1');
|
|
}
|
|
}
|
|
|
|
return redirect(route('admin_settings'));
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function getDhlConfig()
|
|
{
|
|
// 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');
|
|
|
|
// Determine configuration priority
|
|
$useEnvPriority = config('dhl.config_source') === 'env';
|
|
|
|
return [
|
|
// 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),
|
|
'api_secret' => config('dhl.legacy.api_secret'), // Used by Tracking API
|
|
'username' => $this->getConfigValue('dhl_username', config('dhl.username'), $useEnvPriority),
|
|
'password' => $this->getConfigValue('dhl_password', config('dhl.password'), $useEnvPriority),
|
|
'billing_number' => $this->getConfigValue('dhl_billing_number', config('dhl.billing_number'), $useEnvPriority),
|
|
'sandbox' => config('dhl.legacy.sandbox', true), // Used by Tracking Service
|
|
'test_mode' => config('dhl.legacy.test_mode', true),
|
|
|
|
// Product Settings
|
|
'default_product' => $this->normalizeDhlProductCode($this->getConfigValue('dhl_product', config('dhl.default_product'), $useEnvPriority)),
|
|
'international_countries' => $this->getDhlInternationalCountries($useEnvPriority),
|
|
'label_format' => $this->getConfigValue('dhl_label_format', config('dhl.label_format'), $useEnvPriority),
|
|
'print_format' => $this->getConfigValue('dhl_print_format', config('dhl.print_format'), $useEnvPriority),
|
|
'retoure_print_format' => $this->getConfigValue('dhl_retoure_print_format', config('dhl.retoure_print_format'), $useEnvPriority),
|
|
'print_only_if_codeable' => (bool) $this->getConfigValue('dhl_print_only_if_codeable', config('dhl.print_only_if_codeable'), $useEnvPriority),
|
|
'use_queue' => $this->getConfigValue('dhl_use_queue', config('dhl.use_queue'), $useEnvPriority),
|
|
|
|
// Sender Address
|
|
'sender' => [
|
|
'company' => $this->getConfigValue('dhl_sender_company', config('dhl.sender.company'), $useEnvPriority),
|
|
'name' => $this->getConfigValue('dhl_sender_name', config('dhl.sender.name'), $useEnvPriority),
|
|
'street' => $this->getConfigValue('dhl_sender_street', config('dhl.sender.street'), $useEnvPriority),
|
|
'houseNumber' => $this->getConfigValue('dhl_sender_house_number', config('dhl.sender.houseNumber'), $useEnvPriority),
|
|
'postalCode' => $this->getConfigValue('dhl_sender_postal_code', config('dhl.sender.postalCode'), $useEnvPriority),
|
|
'city' => $this->getConfigValue('dhl_sender_city', config('dhl.sender.city'), $useEnvPriority),
|
|
'country' => $this->getConfigValue('dhl_sender_country', config('dhl.sender.country'), $useEnvPriority),
|
|
'email' => $this->getConfigValue('dhl_sender_email', config('dhl.sender.email'), $useEnvPriority),
|
|
'phone' => $this->getConfigValue('dhl_sender_phone', config('dhl.sender.phone'), $useEnvPriority),
|
|
],
|
|
|
|
// Account Numbers
|
|
'account_numbers' => [
|
|
'V01PAK' => $this->getConfigValue('dhl_account_v01pak', config('dhl.account_numbers.V01PAK'), $useEnvPriority),
|
|
'V62KP' => $this->getConfigValue('dhl_account_v62kp', $this->getConfigValue('dhl_account_v62wp', config('dhl.account_numbers.V62KP'), $useEnvPriority), $useEnvPriority),
|
|
'V53PAK' => $this->getConfigValue('dhl_account_v53pak', config('dhl.account_numbers.V53PAK'), $useEnvPriority),
|
|
'V07PAK' => $this->getConfigValue('dhl_account_v07pak', config('dhl.account_numbers.V07PAK'), $useEnvPriority),
|
|
'default' => config('dhl.account_numbers.default'),
|
|
],
|
|
|
|
// Dimensions
|
|
'dimensions' => [
|
|
'V01PAK' => config('dhl.dimensions.V01PAK'),
|
|
'V62KP' => config('dhl.dimensions.V62KP'),
|
|
'V53PAK' => config('dhl.dimensions.V53PAK'),
|
|
'V07PAK' => config('dhl.dimensions.V07PAK'),
|
|
'default' => config('dhl.dimensions.default'),
|
|
],
|
|
// Static config values (webhook, profile, legacy)
|
|
'profile' => config('dhl.profile'),
|
|
'webhook' => config('dhl.webhook'),
|
|
'legacy' => config('dhl.legacy'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get configuration value based on priority setting
|
|
*
|
|
* @param string $settingSlug The database setting slug
|
|
* @param mixed $configValue The config/env fallback value
|
|
* @param bool $useEnvPriority Whether to prioritize env over database
|
|
* @return mixed
|
|
*/
|
|
private function getConfigValue(string $settingSlug, $configValue, bool $useEnvPriority)
|
|
{
|
|
$dbValue = Setting::getContentBySlug($settingSlug);
|
|
|
|
if ($useEnvPriority) {
|
|
// ENV priority: Use config value if available, otherwise fall back to database
|
|
return $configValue ?: $dbValue;
|
|
} else {
|
|
// Database priority (default): Use database value if available, otherwise fall back to config
|
|
return $dbValue ?: $configValue;
|
|
}
|
|
}
|
|
|
|
private function normalizeDhlProductCode(?string $productCode): string
|
|
{
|
|
return $productCode === 'V62WP' ? 'V62KP' : ($productCode ?: 'V01PAK');
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
private function getDhlInternationalCountries(bool $useEnvPriority): array
|
|
{
|
|
$configCountries = config('dhl.international_countries', DhlProductResolver::DEFAULT_INTERNATIONAL_COUNTRIES);
|
|
$countries = $configCountries;
|
|
|
|
if (! $useEnvPriority) {
|
|
$countries = Setting::getContentBySlug('dhl_international_countries') ?: $configCountries;
|
|
}
|
|
|
|
return DhlProductResolver::normalizeCountryCodeList(is_array($countries) ? $countries : []);
|
|
}
|
|
|
|
/**
|
|
* Update DHL configuration cache after saving settings
|
|
*/
|
|
private function updateDhlConfigCache()
|
|
{
|
|
// Clear config cache to force reload from database
|
|
\Artisan::call('config:clear');
|
|
|
|
// Optional: Test DHL connection with new settings
|
|
try {
|
|
$dhlManager = app('Acme\Dhl\DhlManager');
|
|
// You could add a connection test here if needed
|
|
\Log::info('DHL configuration updated successfully');
|
|
} catch (\Exception $e) {
|
|
\Log::error('DHL configuration update failed: '.$e->getMessage());
|
|
}
|
|
}
|
|
}
|