@php $document = is_array($document ?? null) ? $document : []; $details = is_array($details ?? null) ? $details : []; $fiscalSettings = is_array($fiscalSettings ?? null) ? $fiscalSettings : []; $dianResolution = is_array($dianResolution ?? null) ? $dianResolution : []; $factusConfigured = (bool) ($factusConfigured ?? false); $ticketSize = in_array(($ticketSize ?? '80mm'), ['58mm', '80mm'], true) ? $ticketSize : '80mm'; $docType = (string) ($document['tipo_documento'] ?? 'factura'); $docLabel = $docType === 'nota_credito' ? 'NOTA CREDITO' : ($docType === 'nota_debito' ? 'NOTA DEBITO' : 'FACTURA'); $subtotal = (float) ($document['subtotal'] ?? 0); $discount = (float) ($document['venta_descuento'] ?? 0); $taxes = (float) ($document['impuestos'] ?? 0); $total = (float) ($document['total'] ?? 0); $paid = (float) ($document['total_pagado'] ?? 0); $paymentMethods = trim((string) ($document['metodos_pago'] ?? '')); $cufe = trim((string) ($document['cufe'] ?? '')); $qrPath = trim((string) ($document['qr_path'] ?? '')); $resolutionNumber = trim((string) ($dianResolution['numero_resolucion'] ?? ($fiscalSettings['resolucion_dian'] ?? ''))); $resolutionPrefix = trim((string) ($dianResolution['prefijo'] ?? '')); $resolutionRangeFrom = trim((string) ($dianResolution['rango_desde'] ?? '')); $resolutionRangeTo = trim((string) ($dianResolution['rango_hasta'] ?? '')); $resolutionDateFromRaw = trim((string) ($dianResolution['fecha_desde'] ?? '')); $resolutionDateToRaw = trim((string) ($dianResolution['fecha_hasta'] ?? '')); $resolutionDateFrom = $resolutionDateFromRaw; $resolutionDateTo = $resolutionDateToRaw; if ($resolutionDateFromRaw !== '' && ($ts = strtotime($resolutionDateFromRaw))) { $resolutionDateFrom = date('Y/m/d', $ts); } if ($resolutionDateToRaw !== '' && ($ts = strtotime($resolutionDateToRaw))) { $resolutionDateTo = date('Y/m/d', $ts); } $fiscalRegime = trim((string) ($fiscalSettings['regimen_fiscal'] ?? '')); $fiscalActivity = trim((string) ($fiscalSettings['actividad_economica'] ?? '')); $isVatResponsible = trim((string) ($fiscalSettings['responsable_iva'] ?? '0')) === '1'; $saleDateRaw = trim((string) ($document['fecha_emision'] ?? '')); $saleDate = $saleDateRaw; $saleTime = ''; if ($saleDateRaw !== '' && ($timestamp = strtotime($saleDateRaw))) { $saleDate = date('d/m/Y', $timestamp); $saleTime = date('H:i', $timestamp); } $lineItems = []; $taxSummary = []; $totalItems = 0.0; $lineTaxTotal = 0.0; $lineNetBaseTotal = 0.0; $lineGrossTotal = 0.0; $lineDiscountTotal = 0.0; foreach ($details as $line) { $lineQty = (float) ($line['cantidad'] ?? 0); $lineUnitPrice = (float) ($line['precio_unitario'] ?? 0); $lineGross = round($lineQty * $lineUnitPrice, 2); $lineTax = (float) ($line['impuesto'] ?? 0); $lineTotal = (float) ($line['total_linea'] ?? 0); $lineBase = max(0.0, $lineTotal - $lineTax); $lineDiscount = max(0.0, round($lineGross - $lineBase, 2)); $lineGrossTotal += $lineGross; $lineNetBaseTotal += $lineBase; $lineTaxTotal += $lineTax; $lineDiscountTotal += $lineDiscount; $totalItems += $lineQty; $lineItems[] = [ 'descripcion' => (string) ($line['descripcion'] ?? ''), 'cantidad' => $lineQty, 'gross' => $lineGross, 'base' => $lineBase, 'impuesto' => $lineTax, 'descuento' => $lineDiscount, 'total' => $lineTotal, ]; if ($lineTax <= 0.0001) { continue; } $rate = $lineBase > 0.0001 ? round(($lineTax / $lineBase) * 100, 2) : 0.0; $rateKey = number_format($rate, 2, '.', ''); if (! isset($taxSummary[$rateKey])) { $taxSummary[$rateKey] = ['base' => 0.0, 'impuesto' => 0.0, 'tasa' => $rate]; } $taxSummary[$rateKey]['base'] += $lineBase; $taxSummary[$rateKey]['impuesto'] += $lineTax; } ksort($taxSummary, SORT_NATURAL); $discountLabel = $discount > 0.0001 ? $discount : $lineDiscountTotal; $taxableBase = max(0.0, round($subtotal - $discountLabel, 2)); $computedTotal = round($taxableBase + $taxes, 2); $finalTotal = abs($computedTotal - $total) <= 0.02 ? $total : $computedTotal; $itemsLabel = (int) round($totalItems) > 0 ? (int) round($totalItems) : count($details); $hasFeEvidence = $cufe !== '' || $qrPath !== ''; $showResolutionBlock = ($resolutionNumber !== '' || $resolutionPrefix !== '' || $resolutionRangeFrom !== ''); $widthMm = $ticketSize === '58mm' ? '58mm' : '80mm'; $bodyFont = $ticketSize === '58mm' ? '10px' : '11px'; $compactFont = $ticketSize === '58mm' ? '9px' : '10px'; $totalFont = $ticketSize === '58mm' ? '15px' : '17px'; $taxGrid = $ticketSize === '58mm' ? '34px 48px 36px 1fr' : '44px 56px 44px 1fr'; @endphp