@php $errors = $errors ?? new \Illuminate\Support\ViewErrorBag(); // Resolución de Usuario y Tema Dinámico $user = $user ?? Auth::guard('admin')->user() ?? Auth::guard('web')->user() ?? Auth::user(); $convertColor = function($color) { if (empty($color)) return null; if (str_starts_with($color, '#')) return $color; if (str_starts_with($color, '0x')) return '#' . substr($color, -6); return $color; }; $getContrastColor = function($hex) { if(!$hex) return '#ffffff'; $hex = str_replace('#', '', $hex); if(strlen($hex) == 3) $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]; $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); $yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000; return ($yiq >= 128) ? '#0f172a' : '#ffffff'; }; // --- CONSULTA DINÁMICA DE COLORES DEL DUEÑO --- $ownerColorPreferido = null; $ownerColorFondoPreferido = null; if ($user && !empty($user->empresa_id)) { try { $ownerProfileIds = \Illuminate\Support\Facades\DB::table('dbo.Seguridad_Perfiles') ->where('empresa_id', $user->empresa_id) ->where('nombre', 'LIKE', '%DUEÑO%') ->pluck('id_perfil'); if ($ownerProfileIds->isNotEmpty()) { $ownerUser = \Illuminate\Support\Facades\DB::table('dbo.Seguridad_Usuarios') ->where('empresa_id', $user->empresa_id) ->whereIn('perfil_id', $ownerProfileIds) ->first(); if ($ownerUser) { $ownerColorPreferido = $ownerUser->color_preferido; $ownerColorFondoPreferido = $ownerUser->color_fondo_preferido; } } } catch (\Exception $e) {} } $panelColor = $convertColor($ownerColorPreferido ?? $user->color_preferido ?? null) ?? '#ea580c'; $bgColor = $convertColor($ownerColorFondoPreferido ?? $user->color_fondo_preferido ?? null) ?? '#f8fafc'; $primaryColor = $panelColor; $contrastColor = $getContrastColor($primaryColor); $isLight = ($contrastColor === '#0f172a'); $activeTab = 'terminal'; // --- RESOLUCIÓN DINÁMICA DE SUCURSAL Y CAJA ACTIVA --- $sucursalId = $user->sucursal_asignada_id ?? $user->sucursal_id ?? session('sucursal_id') ?? null; $cajaActivaId = null; $cajaActiva = null; $activeShiftHistorial = null; try { $cajaQ = \Illuminate\Support\Facades\DB::table('Pagos_Cajas_Historial as ch') ->join('Pagos_Cajas_Diarias as cd', 'ch.caja_id', '=', 'cd.id_cajaDi') ->where('cd.estado', 'activo') ->whereNull('ch.fecha_cierre'); $userId = $user->id_usuario ?? $user->id ?? null; if ($userId) { $cajaActiva = (clone $cajaQ)->where('cd.usuario_id', $userId)->orderBy('ch.fecha_apertura', 'desc')->first(); } if (empty($cajaActiva) && $sucursalId) { $cajaActiva = (clone $cajaQ)->where('cd.sucursal_id', $sucursalId)->orderBy('ch.fecha_apertura', 'desc')->first(); } if (empty($cajaActiva)) { $cajaActiva = (clone $cajaQ)->orderBy('ch.fecha_apertura', 'desc')->first(); } if ($cajaActiva) { $cajaActivaId = $cajaActiva->caja_id ?? $cajaActiva->id_cajaDi; $activeShiftHistorial = $cajaActiva; if (!$sucursalId && !empty($cajaActiva->sucursal_id)) { $sucursalId = $cajaActiva->sucursal_id; } } } catch (\Exception $e) {} $sucursalNombre = 'PITER POS'; $sucursalImagen = null; $sucursalDireccion = ''; $sucursalTelefono = ''; if ($sucursalId) { try { $suc = \Illuminate\Support\Facades\DB::table('Nucleo_Sucursales') ->where('id_sucursal', $sucursalId) ->first(); if ($suc) { if (!empty($suc->nombre)) { $sucursalNombre = trim($suc->nombre); } if (!empty($suc->imagen_url)) { $sucursalImagen = trim($suc->imagen_url); } if (!empty($suc->direccion)) { $sucursalDireccion = trim($suc->direccion); } if (!empty($suc->telefono)) { $sucursalTelefono = trim($suc->telefono); } } } catch (\Exception $e) {} } elseif ($user && !empty($user->empresa_id)) { try { $suc = \Illuminate\Support\Facades\DB::table('Nucleo_Sucursales') ->where('empresa_id', $user->empresa_id) ->first(); if ($suc) { if (!empty($suc->nombre)) { $sucursalNombre = trim($suc->nombre); } if (!empty($suc->imagen_url)) { $sucursalImagen = trim($suc->imagen_url); } if (!empty($suc->direccion)) { $sucursalDireccion = trim($suc->direccion); } if (!empty($suc->telefono)) { $sucursalTelefono = trim($suc->telefono); } } } catch (\Exception $e) {} } // Fallback: Si no tiene imagen en sucursal, buscar logo_url en Nucleo_Empresas if (!$sucursalImagen && $user && !empty($user->empresa_id)) { try { $emp = \Illuminate\Support\Facades\DB::table('Nucleo_Empresas') ->where('id_empresa', $user->empresa_id) ->first(); if ($emp && !empty($emp->logo_url)) { $sucursalImagen = trim($emp->logo_url); } } catch (\Exception $e) {} } // Construir URL accesible para la imagen del logo $sucursalLogoUrl = null; if (!empty($sucursalImagen)) { $cleanImg = ltrim(str_replace(['\\'], '/', $sucursalImagen), '/'); if (str_starts_with($cleanImg, 'http')) { $sucursalLogoUrl = $cleanImg; } elseif (str_starts_with($cleanImg, 'uploads/')) { $sucursalLogoUrl = '/' . $cleanImg; } else { $sucursalLogoUrl = '/uploads/' . $cleanImg; } } @endphp