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

274 lines
9.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\VendingMachine;
use App\Http\Controllers\Concerns\HasTenantSecurity;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rule;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Illuminate\Http\Response;
class VendingMachineController extends Controller
{
use HasTenantSecurity;
/**
* Display a listing of vending machines.
*/
public function index(): View
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$vendingMachines = VendingMachine::with(['tenant', 'slots'])
->when($tenantId, function($query, $tenantId) {
return $query->where('tenant_id', $tenantId);
})
->paginate(10);
return view('admin.vending-machines.index', compact('vendingMachines'));
}
/**
* Show the form for creating a new vending machine.
*/
public function create(): View
{
$this->ensureCorrectTenantSession();
return view('admin.vending-machines.create');
}
/**
* Store a newly created vending machine.
*/
public function store(Request $request): RedirectResponse
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$request->validate([
'name' => 'required|string|max:255',
'machine_number' => [
'required',
'string',
'max:50',
Rule::unique('vending_machines')->where(function ($query) use ($tenantId) {
return $query->where('tenant_id', $tenantId);
})
],
'location' => 'required|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
$vendingMachine = VendingMachine::create([
'name' => $request->name,
'machine_number' => $request->machine_number,
'location' => $request->location,
'description' => $request->description,
'is_active' => $request->boolean('is_active', true),
'tenant_id' => $tenantId,
]);
return redirect()->route('vending-machines.show', $vendingMachine)
->with('success', 'Automat erfolgreich erstellt.');
}
/**
* Display the specified vending machine.
*/
public function show($id): View
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$vendingMachine = VendingMachine::with(['tenant', 'slots.products'])
->where('id', $id)
->when($tenantId, function($query, $tenantId) {
return $query->where('tenant_id', $tenantId);
})
->first();
if (!$vendingMachine) {
return redirect()->route('vending-machines.index')
->with('error', 'Automat nicht gefunden oder Sie haben keine Berechtigung.');
}
return view('admin.vending-machines.show', compact('vendingMachine'));
}
/**
* Show the form for editing the specified vending machine.
*/
public function edit($id): View
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$vendingMachine = VendingMachine::where('id', $id);
if ($tenantId) {
$vendingMachine->where('tenant_id', $tenantId);
}
$vendingMachine = $vendingMachine->first();
if (!$vendingMachine) {
return redirect()->route('vending-machines.index')
->with('error', 'Automat nicht gefunden oder Sie haben keine Berechtigung.');
}
return view('admin.vending-machines.edit', compact('vendingMachine'));
}
/**
* Update the specified vending machine.
*/
public function update(Request $request, $id): RedirectResponse
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$vendingMachine = VendingMachine::where('id', $id);
if ($tenantId) {
$vendingMachine->where('tenant_id', $tenantId);
}
$vendingMachine = $vendingMachine->first();
if (!$vendingMachine) {
return redirect()->route('vending-machines.index')
->with('error', 'Automat nicht gefunden oder Sie haben keine Berechtigung.');
}
$request->validate([
'name' => 'required|string|max:255',
'machine_number' => [
'required',
'string',
'max:50',
Rule::unique('vending_machines')->where(function ($query) use ($tenantId) {
return $query->where('tenant_id', $tenantId);
})->ignore($vendingMachine->id)
],
'location' => 'required|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
$vendingMachine->update([
'name' => $request->name,
'machine_number' => $request->machine_number,
'location' => $request->location,
'description' => $request->description,
'is_active' => $request->boolean('is_active', true),
]);
return redirect()->route('vending-machines.show', $vendingMachine)
->with('success', 'Automat erfolgreich aktualisiert.');
}
/**
* Remove the specified vending machine.
*/
public function destroy($id): RedirectResponse
{
$this->ensureCorrectTenantSession();
$tenantId = $this->getCurrentTenantId();
$vendingMachine = VendingMachine::where('id', $id);
if ($tenantId) {
$vendingMachine->where('tenant_id', $tenantId);
}
$vendingMachine = $vendingMachine->first();
if (!$vendingMachine) {
return redirect()->route('vending-machines.index')
->with('error', 'Automat nicht gefunden oder Sie haben keine Berechtigung.');
}
$vendingMachine->delete();
return redirect()->route('vending-machines.index')->with('success', 'Automat erfolgreich gelöscht.');
}
/**
* Generate QR code for vending machine
*/
public function generateQrCode($id)
{
// Automat mit Tenant-Beziehung suchen
$vendingMachine = VendingMachine::with('tenant')->find($id);
if (!$vendingMachine) {
return response('Fehler: Automat mit ID ' . $id . ' nicht gefunden.', 404)
->header('Content-Type', 'text/plain');
}
// Prüfe Tenant-Berechtigung für Tenant-Admins
if (\Auth::user()->isTenantAdmin()) {
if (!$vendingMachine->tenant_id || $vendingMachine->tenant_id !== \Auth::user()->tenant_id) {
return response('Fehler: Keine Berechtigung für Automat ' . $id . '. VM Tenant: ' . $vendingMachine->tenant_id . ', User Tenant: ' . \Auth::user()->tenant_id, 403)
->header('Content-Type', 'text/plain');
}
}
// Prüfe ob Mandant und public_slug vorhanden
if (!$vendingMachine->tenant || !$vendingMachine->tenant->public_slug) {
return response('Fehler: Kein öffentlicher Slug konfiguriert für Tenant: ' . ($vendingMachine->tenant ? $vendingMachine->tenant->name : 'null'), 400)
->header('Content-Type', 'text/plain');
}
// Prüfe ob machine_number vorhanden
if (!$vendingMachine->machine_number) {
return response('Fehler: Keine Maschinennummer konfiguriert für Automat: ' . $vendingMachine->name, 400)
->header('Content-Type', 'text/plain');
}
// Erstelle URL für den QR-Code (mandantenspezifisch)
$url = route('vending.public.machine', [
'publicSlug' => $vendingMachine->tenant->public_slug,
'machineNumber' => $vendingMachine->machine_number
]);
try {
// Verwende SVG für bessere Kompatibilität
$writer = new SvgWriter();
$filename = sprintf(
'qr-code-%s-automat-%s.svg',
$vendingMachine->tenant->public_slug,
$vendingMachine->machine_number
);
// Erstelle QR-Code mit einfacher Konfiguration
$qrCode = new QrCode($url);
$result = $writer->write($qrCode);
return response($result->getString())
->header('Content-Type', $result->getMimeType())
->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
} catch (\Exception $e) {
return response('QR-Code Fehler: ' . $e->getMessage(), 500)
->header('Content-Type', 'text/plain');
}
}
private function ensureCorrectTenantSession()
{
// Implementation details...
}
private function getCurrentTenantId()
{
return session('current_tenant') ? session('current_tenant')->id : \Auth::user()->tenant_id;
}
}