LMIV-SNACKAUTOMAT/app/Http/Controllers/SettingsController.php

113 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Setting;
use App\Models\Tenant;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class SettingsController extends Controller
{
public function index(): View
{
$settings = Setting::orderBy('group')->orderBy('key')->get()->groupBy('group');
return view('admin.settings.index', compact('settings'));
}
public function update(Request $request): RedirectResponse
{
$validated = $request->validate([
'settings' => 'required|array',
'settings.*' => 'nullable'
]);
foreach ($validated['settings'] as $key => $value) {
$setting = Setting::where('key', $key)->first();
if ($setting) {
// Handle boolean checkboxes (unchecked = null, checked = "1")
if ($setting->type === 'boolean') {
$value = $value ? '1' : '0';
}
$setting->update(['value' => $value]);
}
}
return redirect()->route('admin.settings.index')->with('success', 'Einstellungen erfolgreich gespeichert.');
}
/**
* Zeige Mandanten-spezifische Einstellungen
*/
public function tenantSettings(): View
{
\Log::info('TenantSettings GET Route erreicht');
// Vereinfacht: nimm einfach den ersten Mandanten
$tenant = Tenant::first();
if (!$tenant) {
\Log::error('Kein Mandant gefunden in tenantSettings!');
abort(404, 'Kein Mandant gefunden');
}
\Log::info('Tenant für Settings geladen:', [
'id' => $tenant->id,
'name' => $tenant->name,
'show_prices' => $tenant->show_prices,
'show_stock' => $tenant->show_stock
]);
return view('admin.settings.tenant', compact('tenant'));
}
/**
* Aktualisiere Mandanten-spezifische Einstellungen
*/
public function updateTenantSettings(Request $request): RedirectResponse
{
// Debug: Was kommt an?
\Log::info('Tenant Settings Update Request:', [
'all_data' => $request->all(),
'has_show_prices' => $request->has('show_prices'),
'has_show_stock' => $request->has('show_stock'),
'session_tenant_id' => session('current_tenant_id'),
'user_id' => auth()->id(),
'user_tenant_id' => auth()->user()->tenant_id ?? null
]);
// Vereinfachte Mandanten-Auswahl: nimm einfach den ersten Mandanten
$tenant = Tenant::first();
if (!$tenant) {
\Log::error('Kein Mandant gefunden!');
return redirect()->back()->with('error', 'Kein Mandant gefunden');
}
// Checkboxes senden nur Werte wenn aktiviert
$validated = [
'show_prices' => $request->has('show_prices'),
'show_stock' => $request->has('show_stock')
];
\Log::info('Updating tenant settings:', [
'tenant_id' => $tenant->id,
'data' => $validated
]);
$tenant->update($validated);
// Prüfen ob Update erfolgreich
$tenant->refresh();
\Log::info('Tenant update result:', [
'new_show_prices' => $tenant->show_prices,
'new_show_stock' => $tenant->show_stock
]);
return redirect()->route('admin.settings.tenant')
->with('success', 'Mandanten-Einstellungen erfolgreich gespeichert.');
}
}