scar en Notificaciones (Solicitud de Pago) por si hay datos ahí try { $notificacion = \Illuminate\Support\Facades\DB::table('Comunicaciones_Notificaciones') ->where('sucursal_id', $ordenBase->sucursal_id) ->where(function ($q) use ($ordenId, $ordenBase) { $q->where('data', 'LIKE', "%$ordenId%") ->orWhere('data', 'LIKE', "%{$ordenBase->recurso_id}%"); }) ->whereIn('tipo', ['solicitud_pago_pos', 'solicitud_pago_efectivo', 'pago_tarjeta_pos', 'pago_efectivo']) ->orderBy('fecha', 'desc') ->first(); if ($notificacion) { $notifData = json_decode($notificacion->data, true); $billingData = $notifData['comprobante_datos'] ?? $notifData['payload']['comprobante_datos'] ?? null; if ($billingData) { $mappedCliente = [ 'tipo_documento' => (string) ($notifData['comprobante_tipo'] == 'FACTURA' ? '6' : '1'), 'numero_documento' => $billingData['ruc'] ?? $billingData['dni'] ?? $billingData['documento_numero'] ?? '00000000', 'razon_social' => $billingData['razon_social'] ?? $billingData['nombre_completo'] ?? 'CLIENTE VARIOS', 'direccion' => $billingData['direccion'] ?? null ]; } } } catch (\Exception $e) { \Illuminate\Support\Facades\Log::warning("⚠️ [Ticket/Generar] Error en fallback de cliente: " . $e->getMessage()); } } // 2. Obtener Items/Detalles de la orden (o de la mesa solo si es pre-cuenta explícita) $esPreCuenta = ($request->query('tipo') === 'pre-cuenta'); if ($esPreCuenta && $ordenBase->recurso_id) { $items = \Illuminate\Support\Facades\DB::table('Ventas_Detalle_Orden as d') ->join('Ventas_Ordenes as o', 'd.orden_id', '=', 'o.id_ordenes') ->leftJoin('Ventas_Productos as p', 'd.producto_id', '=', 'p.id_producto') ->where('o.recurso_id', $ordenBase->recurso_id) ->whereIn('o.estado', ['PENDIENTE', 'POR_CONFIRMAR', 'POR_REVISAR', 'EN_PREPARACION', 'LISTO', 'ENTREGADO']) ->select('d.*', 'p.nombre as producto_nombre') ->get(); } else { $items = \Illuminate\Support\Facades\DB::table('Ventas_Detalle_Orden as d') ->leftJoin('Ventas_Productos as p', 'd.producto_id', '=', 'p.id_producto') ->where('d.orden_id', $ordenId) ->select('d.*', 'p.nombre as producto_nombre') ->g