294 lines
11 KiB
PHP
294 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\VendingMachine;
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class VendingDisplayController extends Controller
|
|
{
|
|
/**
|
|
* Display the main vending machine interface
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
// Zeige allgemeine Info-Seite mit Verweis auf LMIV-Automat.de
|
|
return view('vending.welcome');
|
|
}
|
|
|
|
/**
|
|
* Display a specific vending machine by machine number (cross-tenant)
|
|
*/
|
|
public function showMachineByNumber($machineNumber): View
|
|
{
|
|
// Finde erste aktive Maschine mit dieser machine_number
|
|
$machine = VendingMachine::where('machine_number', $machineNumber)
|
|
->where('is_active', true)
|
|
->with(['tenant', 'slots.products'])
|
|
->first();
|
|
|
|
if (!$machine) {
|
|
abort(404, 'Automat mit dieser Nummer nicht gefunden');
|
|
}
|
|
|
|
// Lade alle Automaten des gleichen Mandanten
|
|
$vendingMachines = VendingMachine::where('tenant_id', $machine->tenant_id)
|
|
->where('is_active', true)
|
|
->get();
|
|
|
|
$selectedMachine = $machine;
|
|
$tenant = $machine->tenant;
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Display a specific machine using legacy route
|
|
*/
|
|
public function showMachine(VendingMachine $machine): View
|
|
{
|
|
if (!$machine->is_active) {
|
|
abort(404, 'Automat nicht verfügbar');
|
|
}
|
|
|
|
// Optional: Überprüfe Domain-basierte Mandanten-Zugriffsberechtigung
|
|
$currentDomain = request()->getHost();
|
|
if ($currentDomain !== 'localhost' && $currentDomain !== '127.0.0.1' && $currentDomain !== '0.0.0.0') {
|
|
$tenant = \App\Models\Tenant::where('domain', $currentDomain)->first();
|
|
if ($tenant && $machine->tenant_id !== $tenant->id) {
|
|
abort(404, 'Automat nicht für diese Domain verfügbar');
|
|
}
|
|
}
|
|
|
|
$machine->load(['slots.products']);
|
|
|
|
// Filtere verfügbare Automaten basierend auf Mandant
|
|
$query = VendingMachine::where('is_active', true);
|
|
$tenant = null;
|
|
if ($currentDomain !== 'localhost' && $currentDomain !== '127.0.0.1' && $currentDomain !== '0.0.0.0') {
|
|
$tenant = \App\Models\Tenant::where('domain', $currentDomain)->first();
|
|
if ($tenant) {
|
|
$query->where('tenant_id', $tenant->id);
|
|
}
|
|
}
|
|
|
|
$vendingMachines = $query->get();
|
|
$selectedMachine = $machine;
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Show product details with LMIV information
|
|
*/
|
|
public function showProduct(Product $product): View
|
|
{
|
|
return view('vending.product-details', compact('product'));
|
|
}
|
|
|
|
/**
|
|
* Show products in a specific slot
|
|
*/
|
|
public function showSlot($machine, $slot): View
|
|
{
|
|
$vendingMachine = VendingMachine::findOrFail($machine);
|
|
$slotRecord = $vendingMachine->slots()->where('slot_number', $slot)->firstOrFail();
|
|
$slotRecord->load(['products']);
|
|
|
|
return view('vending.slot-details', [
|
|
'machine' => $vendingMachine,
|
|
'slot' => $slotRecord
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display vending machines for a specific tenant
|
|
*/
|
|
public function indexByTenant(\App\Models\Tenant $tenant): View
|
|
{
|
|
// Für nicht-eingeloggte Benutzer: Weiterleitung zur ersten verfügbaren Maschine
|
|
if (!auth()->check()) {
|
|
$firstMachine = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if ($firstMachine) {
|
|
return redirect()->route('vending.tenant.machine', [
|
|
'tenant' => $tenant->slug,
|
|
'machine' => $firstMachine->machine_number
|
|
]);
|
|
}
|
|
}
|
|
|
|
$vendingMachines = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('is_active', true)
|
|
->get();
|
|
$selectedMachine = null;
|
|
|
|
if ($vendingMachines->isNotEmpty()) {
|
|
$selectedMachine = $vendingMachines->first();
|
|
$selectedMachine->load(['slots.products']);
|
|
}
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Display a specific vending machine for a tenant
|
|
*/
|
|
public function showMachineByTenant(\App\Models\Tenant $tenant, $machineNumber): View
|
|
{
|
|
// Finde Automat basierend auf machine_number und tenant
|
|
$machine = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('machine_number', $machineNumber)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$machine) {
|
|
abort(404, 'Automat nicht für diesen Mandanten verfügbar');
|
|
}
|
|
|
|
$machine->load(['slots.products']);
|
|
|
|
// Für nicht-eingeloggte Benutzer: nur die eine Maschine anzeigen
|
|
if (auth()->check()) {
|
|
// Eingeloggte Benutzer sehen alle Automaten des Mandanten
|
|
$vendingMachines = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('is_active', true)
|
|
->get();
|
|
} else {
|
|
// Öffentliche Benutzer sehen nur diese eine Maschine
|
|
$vendingMachines = collect([$machine]);
|
|
}
|
|
$selectedMachine = $machine;
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Show product details for a tenant
|
|
*/
|
|
public function showProductByTenant($publicSlug, $productId): View
|
|
{
|
|
// Finde Tenant basierend auf public_slug
|
|
$tenant = \App\Models\Tenant::where('public_slug', $publicSlug)
|
|
->where('is_active', true)
|
|
->firstOrFail();
|
|
|
|
// Finde Produkt basierend auf ID und Tenant
|
|
$product = \App\Models\Product::where('id', $productId)
|
|
->where('tenant_id', $tenant->id)
|
|
->firstOrFail();
|
|
|
|
return view('vending.product-details', compact('product', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Show slot details for a tenant
|
|
*/
|
|
public function showSlotByTenant(\App\Models\Tenant $tenant, $machineNumber, $slotNumber): View
|
|
{
|
|
// Finde Automat basierend auf machine_number und tenant
|
|
$machine = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('machine_number', $machineNumber)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$machine) {
|
|
abort(404, 'Automat nicht für diesen Mandanten verfügbar');
|
|
}
|
|
|
|
$slot = $machine->slots()->where('slot_number', $slotNumber)->firstOrFail();
|
|
$slot->load(['products']);
|
|
|
|
return view('vending.slot-details', compact('machine', 'slot', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Display overview of all tenants
|
|
*/
|
|
public function tenantsOverview(): View
|
|
{
|
|
$tenants = \App\Models\Tenant::where('is_active', true)
|
|
->withCount(['vendingMachines' => function($query) {
|
|
$query->where('is_active', true);
|
|
}])
|
|
->get();
|
|
|
|
return view('vending.tenants', compact('tenants'));
|
|
}
|
|
|
|
/**
|
|
* Display a machine using public slug (for QR codes)
|
|
*/
|
|
public function showMachineByPublicSlug($publicSlug, $machineNumber): View
|
|
{
|
|
// Finde Mandant basierend auf public_slug oder slug
|
|
$tenant = \App\Models\Tenant::where('public_slug', $publicSlug)
|
|
->orWhere('slug', $publicSlug)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$tenant) {
|
|
abort(404, 'Mandant mit diesem Slug nicht gefunden');
|
|
}
|
|
|
|
// Finde Automat basierend auf machine_number und tenant
|
|
$machine = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('machine_number', $machineNumber)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$machine) {
|
|
abort(404, 'Automat nicht für diesen Mandanten verfügbar');
|
|
}
|
|
|
|
$machine->load(['slots.products']);
|
|
|
|
// Für öffentliche URLs: nur die eine Maschine anzeigen, Admins sehen alle
|
|
if (auth()->check()) {
|
|
// Eingeloggte Benutzer sehen alle Automaten des Mandanten
|
|
$vendingMachines = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('is_active', true)
|
|
->get();
|
|
} else {
|
|
// Öffentliche Benutzer sehen nur diese eine Maschine
|
|
$vendingMachines = collect([$machine]);
|
|
}
|
|
|
|
$selectedMachine = $machine;
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
|
|
/**
|
|
* Display vending machines for a tenant using public slug
|
|
*/
|
|
public function indexByPublicSlug($publicSlug): View
|
|
{
|
|
// Finde Mandant basierend auf public_slug oder slug
|
|
$tenant = \App\Models\Tenant::where('public_slug', $publicSlug)
|
|
->orWhere('slug', $publicSlug)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$tenant) {
|
|
abort(404, 'Mandant mit diesem Slug nicht gefunden');
|
|
}
|
|
|
|
$vendingMachines = VendingMachine::where('tenant_id', $tenant->id)
|
|
->where('is_active', true)
|
|
->get();
|
|
$selectedMachine = null;
|
|
|
|
if ($vendingMachines->isNotEmpty()) {
|
|
$selectedMachine = $vendingMachines->first();
|
|
$selectedMachine->load(['slots.products']);
|
|
}
|
|
|
|
return view('vending.index', compact('vendingMachines', 'selectedMachine', 'tenant'));
|
|
}
|
|
}
|