@php $productsPerPage = $productsPerPage ?? 16; $totalProducts = $products->count(); $totalPages = ceil($totalProducts / $productsPerPage); $gridClass = 'grid-' . ($productsPerPage == 8 ? '2x4' : ($productsPerPage == 12 ? '3x4' : ($productsPerPage == 16 ? '4x4' : ($productsPerPage == 20 ? '4x5' : '4x6')))); $rateType = $rateType ?? 'real'; // Función para obtener precio en moneda específica $getPriceInCurrency = function($product, $targetCurrency, $useLegalRate = false) { // Intentar obtener precio de las unidades primero (unit_1_pvp_1, unit_2_pvp_1, etc.) $price = 0; $baseCurrency = $product->currency ?? 'USD'; // Buscar precio en unidades (prioridad: unidad 1, nivel 1) for ($unit = 1; $unit <= 3; $unit++) { if ($product->{"tab_enabled_{$unit}"} ?? false) { for ($level = 1; $level <= 3; $level++) { $pvpField = "unit_{$unit}_pvp_{$level}"; if (isset($product->$pvpField) && $product->$pvpField > 0) { $price = (float) $product->$pvpField; $unitCurrency = $product->{"unit_{$unit}_currency"} ?? $baseCurrency; if ($unitCurrency) { $baseCurrency = $unitCurrency; } break 2; // Salir de ambos loops } } } } // Si no hay precio en unidades, usar selling_price if ($price == 0) { $price = (float) ($product->selling_price ?? 0); } if ($price == 0) { return 0; } // Si el producto ya está en la moneda objetivo y no se requiere conversión especial if ($baseCurrency == $targetCurrency && !($targetCurrency == 'USD' && $useLegalRate)) { return $price; } // Obtener precio en USD primero $priceUSD = 0; if ($baseCurrency == 'USD') { $priceUSD = $price; } else { // Si tiene precio USD guardado, usarlo if ($product->selling_price_usd && $product->selling_price_usd > 0) { $priceUSD = (float) $product->selling_price_usd; } else { // Convertir desde moneda local a USD try { $rateFrom = \App\Models\ExchangeRate::getRateRecord($baseCurrency, 'USD', now()->toDateString()); if ($rateFrom) { if ($rateFrom->calculation_type == 'dual') { // Para dual, usar rate_real para convertir a USD $rateValue = (float) $rateFrom->rate_real; } else { $rateValue = (float) $rateFrom->rate; } // Convertir de moneda local a USD: dividir $priceUSD = $price / ($rateValue ?: 1); } else { // Fallback: asumir que el precio ya está en USD $priceUSD = $price; } } catch (\Exception $e) { $priceUSD = $price; } } } // Si la moneda objetivo es USD if ($targetCurrency == 'USD') { return $priceUSD; } // Si la moneda objetivo es REF (Precio Real en: USD * TASA REAL / TASA LEGAL) if ($targetCurrency == 'REF') { try { // Buscar tasa dual para calcular REF // REF = (USD * TASA REAL) / TASA LEGAL // Primero necesitamos encontrar una tasa dual (generalmente USD a VEF/BS) $dualRate = \App\Models\ExchangeRate::where('from_currency', 'USD') ->where('calculation_type', 'dual') ->where('is_active', true) ->where('effective_date', '<=', now()->toDateString()) ->orderBy('effective_date', 'desc') ->first(); if ($dualRate && $dualRate->rate_real && $dualRate->rate_legal) { $tasaReal = (float) $dualRate->rate_real; $tasaLegal = (float) $dualRate->rate_legal; if ($tasaLegal > 0) { return ($priceUSD * $tasaReal) / $tasaLegal; } } // Fallback: retornar precio USD si no hay tasa dual return $priceUSD; } catch (\Exception $e) { return $priceUSD; } } // Convertir de USD a la moneda objetivo try { $rateTo = \App\Models\ExchangeRate::getRateRecord('USD', $targetCurrency, now()->toDateString()); if ($rateTo) { if ($rateTo->calculation_type == 'dual') { // Para dual, usar rate_real o rate_legal según corresponda if ($useLegalRate && $rateTo->rate_legal) { $rateValue = (float) $rateTo->rate_legal; } else { $rateValue = (float) $rateTo->rate_real; } } else { $rateValue = (float) $rateTo->rate; } return $priceUSD * $rateValue; } // Fallback: si no hay tasa, intentar usar precio original return $price; } catch (\Exception $e) { return $price; } }; // Función para formatear precio $formatPrice = function($price, $currency) { if ($currency == 'USD') { return '$' . number_format($price, 2, '.', ','); } elseif ($currency == 'VEF' || $currency == 'BS') { return 'Bs. ' . number_format($price, 2, ',', '.'); } elseif ($currency == 'EUR') { return '€' . number_format($price, 2, ',', '.'); } elseif ($currency == 'REF') { return 'REF ' . number_format($price, 2, '.', ','); } else { return number_format($price, 2, '.', ',') . ' ' . $currency; } }; @endphp @php $catalogType = $catalogType ?? 'name'; $currentGroup = null; @endphp @for($page = 0; $page < $totalPages; $page++) @endfor