<!DOCTYPE html>
<html lang="{{ str_replace('_','-',app()->getLocale()) }}">

    {{-- Control Variables --}}
    @php
      // Imports
        use Carbon\Carbon;
        use App\Helpers\Database\SatCatalogsHelper;

        use App\Helpers\Support\PdfResourcesHelper;
        use App\Helpers\Support\TaxCertificateHelper;

        use App\Helpers\Formats\FormatCurrencyHelper;
        use App\Helpers\Formats\QuantityConverterHelper;

      // Variables
        // Classes
        $contrast_color = PdfResourcesHelper::getTextContrastColor($tax_payment->taxFormat->base_color ?? "#0D6889");

        // Header
        $cds_issuer = TaxCertificateHelper::serialHexToSatCertificateNumber($issuer->cer_serial_number);
        $emission_date = Carbon::parse($tax_payment->certification_date)->startOfMinute()->format('d/m/Y H:i:s')." (Tiempo del Centro)";
        $certification_date = is_null($tax_payment->certification_date)
            ? "-- -- --"
            : Carbon::parse($tax_payment->certification_date)->format('d/m/Y H:i:s')." (Tiempo del Centro)";

    @endphp

<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{{ $title }}</title>

    @include('invoicing.invoices.pdf.resources.invoice-styles')
</head>

<body>
    {{-- Cancelled (Watermark) --}}
    @include('invoicing.invoices.pdf.resources.cancel-watermark', ['tax_document' => $tax_payment])

    {{-- Header --}}
    <header>
        {{-- Logo --}}
        <div class="row">
            <img class="secondary-logo" src="{{ $logo_src }}" alt="Hotel Logo">
        </div>
    </header>

    {{-- Footer --}}
    <footer class="text-center">
        <a href="https://harsi.mx/" class="no-spacing-text text-muted">
            HARSI HAZLO SIMPLE, HAZLO ÚTIL ©®
        </a>
        <p class="no-spacing-text">
            No Certificado SAT {{ $tax_payment->sat_cert_number }} /
            Fecha y hora de Certificación PAC {{ $certification_date }}
        </p>
        <p class="no-spacing-text">
            ESTE DOCUMENTO ES UNA REPRESENTACIÓN IMPRESA DE UN CFDI
        </p>
        <a href="https://harsi.mx/" class="no-spacing-text text-muted">
            https://harsi.mx/
        </a>
    </footer>

    {{-- Content --}}
    <main>

        {{-- Receiver - Issuer - CFDI Data --}}
        <section class="mt-2 px-0_5 row">

            {{-- Issuer & Receiver Data --}}
            <div class="col-6">
                <table class="table-borderless mixed-data"><tbody>

                    {{-- Issuer (RFC) --}}
                    <tr>
                        <td class="fw-semi-bold">RFC emisor:</td>
                        <td class="ps-1">{{ $issuer->tax_id }}</td>
                    </tr>

                    {{-- Issuer (Name) --}}
                    <tr>
                        <td class="fw-semi-bold">Nombre emisor:</td>
                        <td class="ps-1">{{ $issuer->legal_name }}</td>
                    </tr>

                    {{-- Receiver (RFC) --}}
                    <tr>
                        <td class="fw-semi-bold">RFC receptor:</td>
                        <td class="ps-1">{{ $tax_payment->receptor->tax_id }}</td>
                    </tr>

                    {{-- Receiver (Name) --}}
                    <tr>
                        <td class="fw-semi-bold">Nombre receptor:</td>
                        <td class="ps-1">{{ $tax_payment->receptor->legal_name }}</td>
                    </tr>

                    {{-- Receiver (CP) --}}
                    <tr>
                        <td class="fw-semi-bold">Código postal del receptor:</td>
                        <td class="ps-1">{{ $tax_payment->receptor->zip }}</td>
                    </tr>

                    {{-- Receiver (Tax System) --}}
                    <tr>
                        <td class="fw-semi-bold">Régimen fiscal del receptor:</td>
                        <td class="ps-1">{{ SatCatalogsHelper::getItem('c_RegimenFiscal', $tax_payment->receptor->tax_system, false) }}</td>
                    </tr>

                    {{-- Receiver (CFDI Use) --}}
                    <tr>
                        <td class="fw-semi-bold">Uso de CFDI:</td>
                        <td class="ps-1">{{ SatCatalogsHelper::getItem('c_UsoCFDI', $tax_payment->cfdi_use, false) }}</td>
                    </tr>

                </tbody></table>
            </div>

            {{-- CFDI Data --}}
            <div class="col-6">
                <table class="table-borderless mixed-data"><tbody>

                    {{-- Invoice (SAT UUID) --}}
                    <tr>
                        <td class="fw-semi-bold">Folio fiscal:</td>
                        <td class="ps-1">{{ $tax_payment->sat_uuid }}</td>
                    </tr>

                    {{-- Invoice (CSD Number) --}}
                    <tr>
                        <td class="fw-semi-bold">No. de serie del CSD:</td>
                        <td class="ps-1">{{ $cds_issuer }}</td>
                    </tr>

                    {{-- Invoice (Series & Folio) --}}
                    <tr>
                        <td class="fw-semi-bold">Serie y folio:</td>
                        <td class="ps-1">{{ $payment_format_serie }}</td>
                    </tr>

                    {{-- Issuer (CP) --}}
                    <tr>
                        <td class="fw-semi-bold">Código postal del emisor:</td>
                        <td class="ps-1">{{ $issuer->zip }}</td>
                    </tr>

                    {{-- Invoice (Emission Date) --}}
                    <tr>
                        <td class="fw-semi-bold">Fecha y hora de operación:</td>
                        <td class="ps-1">{{ $emission_date }}</td>
                    </tr>

                    {{-- Invoice (Type) --}}
                    <tr>
                        <td class="fw-semi-bold">Efecto de Comprobante:</td>
                        <td class="ps-1">{{ SatCatalogsHelper::getItem('c_TipoDeComprobante', "P", false) }}</td>
                    </tr>

                    {{-- Issuer (Tax System) --}}
                    <tr>
                        <td class="fw-semi-bold">Régimen Fiscal:</td>
                        <td class="ps-1">{{ SatCatalogsHelper::getItem('c_RegimenFiscal', $issuer->tax_system, false) }}</td>
                    </tr>

                </tbody></table>
            </div>

        </section>

        {{-- Concepts --}}
        <section class="mt-1 row">

            {{-- Title --}}
            <p class="mb-1 small fw-semi-bold">Conceptos:</p>

            {{-- Concepts Summary --}}
            <table class="text-center row certify-table table-strong-bordered">
                <thead class="semi-pale-background">
                    <tr>
                        <th width="12.5%" class="py-0_5">Clave del Producto y/o Servicio</th>
                        <th width="auto" class="py-0_5">Cantidad</th>
                        <th width="12.5%" class="py-0_5">Clave de Unidad</th>
                        <th width="12.5%" class="py-0_5">Unidad</th>
                        <th width="12.5%" class="py-0_5">Valor Unitario</th>
                        <th width="12.5%" class="py-0_5">Importe</th>
                        <th width="12.5%" class="py-0_5">Descuento</th>
                        <th width="14%" class="py-0_5">Objeto Impuesto</th>
                    </tr>
                </thead>
                <tbody class="table-bordered">
                        {{-- Fiscal Data --}}
                        <tr>
                            <td class="px-0_5">{{ SatCatalogsHelper::getProdServ('84111506', false) }}</td>
                            <td class="px-0_5">1</td>
                            <td class="px-0_5">ACT</td>
                            <td class="px-0_5">{{ SatCatalogsHelper::getUnit("ACT", false) }}</td>
                            <td class="px-0_5">
                                {{ FormatCurrencyHelper::formatCurrency("0.000000", true, 6) }}
                            </td>
                            <td class="px-0_5">
                                {{ FormatCurrencyHelper::formatCurrency("0.00", true, 6) }}
                            </td>
                            <td class="px-0_5">
                                {{ FormatCurrencyHelper::formatCurrency("0.000000", true, 6) }}
                            </td>
                            <td class="px-0_5">
                                {{ SatCatalogsHelper::getItem('c_ObjetoImp', "01", false) }}
                            </td>
                        </tr>

                        {{-- Taxes & Description --}}
                        <tr>
                            {{-- Description --}}
                            <td class="px-0_5 fw-bold semi-pale-background">
                                Descripción
                            </td>
                            <td colspan="3" class="px-0_5 py-0_5 text-start">
                                <span class="fw-semi-bold">Pago</span>
                            </td>

                            {{-- Taxes --}}
                            <td colspan="4" class="py-0 cell-without-borders">
                                <table class="text-center row certify-table table-borderless">
                                    <thead><tr>
                                        <th class="py-0_5">Impuestos</th>
                                        <th class="py-0_5">Tipo</th>
                                        <th class="py-0_5">Base</th>
                                        <th class="py-0_5">Tipo Factor</th>
                                        <th class="py-0_5">Tasa o Cuota</th>
                                        <th class="py-0_5">Importe</th>
                                    </tr></thead>

                                    <tbody>
                                            <tr>
                                                <td>IVA</td>
                                                <td>Trasladado</td>
                                                <td>Exento</td>
                                                <td>{{ FormatCurrencyHelper::formatCurrency("0.000000", true, 6) }}</td>
                                                <td>{{ QuantityConverterHelper::str_toStrPercentage(0.0) }}</td>
                                                <td>{{ FormatCurrencyHelper::formatCurrency("0.000000", true, 6) }}</td>
                                            </tr>
                                    </tbody>
                                </table>
                            </td>

                        </tr>
                </tbody>
            </table>

        </section>

        {{-- Totals & Null Payment Data --}}
        <section class="px-3 mt-0 row">

            {{-- Payment Data --}}
            <div class="col-6">
                <table class="table-borderless mixed-data">
                    {{-- Head Spacemen --}}
                    <thead>
                        <tr>
                            <th class="py-0">&nbsp;</th>
                            <th class="py-0"></th>
                        </tr>
                    </thead>

                    {{-- Body --}}
                    <tbody>

                        {{-- Currency --}}
                        <tr>
                            <td class="fw-semi-bold">Moneda:</td>
                            <td class="ps-6">XXX</td>
                        </tr>

                        {{-- Amount in Letters --}}
                        <tr>
                            <td class="fw-semi-bold">Importe en Letras:</td>
                            <td class="ps-6">{{ QuantityConverterHelper::convertToPesosText(0.00, false) }}</td>
                        </tr>

                    </tbody>
                </table>
            </div>

            {{-- Totals & Federal Taxes Breakdown --}}
            <div class="col-6">
                <table class="container table-borderless mixed-data">
                    {{-- Head Spacemen --}}
                    <thead>
                        <tr>
                            <th width="25%" class="py-0">&nbsp;</th>
                            <th class="py-0"></th>
                        </tr>
                    </thead>

                    {{-- Body --}}
                    <tbody>

                        {{-- Subtotal --}}
                        <tr>
                            <td class="fw-semi-bold">Subtotal</td>
                            <td class="text-end">{{ FormatCurrencyHelper::formatCurrency("0.00") }}</td>
                        </tr>

                        {{-- Total --}}
                        <tr>
                            <td class="pt-0_5 fw-semi-bold">Total</td>
                            <td class="pt-0_5 text-end fw-semi-bold">{{ FormatCurrencyHelper::formatCurrency("0.00") }}</td>
                        </tr>

                    </tbody>
                </table>
            </div>

        </section>

        {{-- Payment Data --}}
        <section class="mt-0 row">

            {{-- Title --}}
            <p class="mb-1 small fw-semi-bold">Recepción de Pagos:</p>

            {{-- General Data --}}
            <table class="text-center row certify-table table-strong-bordered">
                <thead class="semi-pale-background">
                    <tr>
                        <th class="py-0_5">Fecha de Pago</th>
                        <th class="py-0_5">Forma de Pago</th>
                        <th class="py-0_5">Moneda</th>
                        <th class="py-0_5">Tipo de Cambio</th>
                        <th class="py-0_5">Monto</th>
                    </tr>
                </thead>
                <tbody class="table-bordered">
                    <tr>
                        <td class="px-0_5">
                            {{ Carbon::parse($tax_payment->payment_date)->format('d/m/Y H:i:s') }}
                        </td>
                        <td class="px-0_5">
                            {{ SatCatalogsHelper::getItem('c_FormaPago', $tax_payment->payment_form, false) }}
                        </td>
                        <td class="px-0_5">
                            {{ SatCatalogsHelper::getItem('c_Moneda', $tax_payment->currency, false) }}
                        </td>
                        <td class="px-0_5">
                            {{ QuantityConverterHelper::removeUnnecessaryDecimal($tax_payment->exchange_rate ?? "1.00") }}
                        </td>
                        <td class="px-0_5">
                            {{ FormatCurrencyHelper::formatCurrency($tax_payment->amount_total, $tax_payment->currency) }}
                        </td>
                    </tr>
                </tbody>
            </table>

            {{-- General Data --}}
            <table class="text-center row certify-table table-strong-bordered">
                <thead>
                    <tr class="semi-pale-background">
                        <th class="py-0_5" colspan="7">Documentos Relacionados</th>
                    </tr>
                    <tr class="pale-background">
                        <th class="py-0_5">Folio Fiscal</th>
                        <th width="5%" class="py-0_5">Folio</th>
                        <th width="20%" class="py-0_5">Obj. Impuesto</th>
                        <th width="10%" class="py-0_5">Parcialidad</th>
                        <th width="13%" class="py-0_5">Saldo Anterior</th>
                        <th width="13%" class="py-0_5">Importe</th>
                        <th width="13%" class="py-0_5">Insoluto</th>
                    </tr>
                </thead>
                <tbody class="table-bordered">
                    @foreach ($tax_payment->taxPaymentDocs as $payment_doc)
                        @php
                          // Invoices data
                            $invoice = $payment_doc->taxInvoice;
                            $currency = $invoice->currency;

                            $invoice_format_serie = ($invoice->taxFormat?->serie ?? $invoice->invoice_type)
                                ."-{$invoice->serial_consecutive}";

                          // Get concepts
                            $invoice_first_item = $invoice->taxItems->first();

                          // Calculate IVA applied
                            $applied_rate = 0.0;
                            $is_withholding = false;

                            if ($invoice_first_item && is_array($invoice_first_item->tax_profile)){
                              // IVA Traslado
                                $applied_rate = (float) data_get($invoice_first_item->tax_profile, 'taxes.iva.rate', 0.0);

                              // IVA Retención
                                if ($applied_rate == 0.0) {
                                    $applied_rate = (float) data_get($invoice_first_item->tax_profile, 'withholding_taxes.iva.rate', 0.0);
                                    $is_withholding = $applied_rate > 0;
                                }
                            }

                            $format_amount_paid = $payment_doc->amount_paid;

                                if ($is_withholding) { // Retención
                                    $applied_tax_import = round($format_amount_paid * $applied_rate, 2);
                                } else { // Traslado
                                    $applied_tax_import = ($applied_rate > 0)
                                        ? round(($format_amount_paid * $applied_rate)/(1 + $applied_rate), 2)
                                        : 0.00;
                                }
                        @endphp

                        {{-- Invoice Document Data --}}
                        <tr>
                            <td>{{ $invoice->sat_uuid }}</td>
                            <td>{{ $invoice_format_serie }}</td>
                            <td>{{ SatCatalogsHelper::getItem('c_ObjetoImp', "02", false) }}</td>
                            <td>{{ $payment_doc->installment_number }}</td>

                            <td>{{ FormatCurrencyHelper::formatCurrency($payment_doc->previous_balance, $currency) }}</td>
                            <td>{{ FormatCurrencyHelper::formatCurrency($payment_doc->amount_paid, $currency) }}</td>
                            <td>{{ FormatCurrencyHelper::formatCurrency($payment_doc->remaining_balance, $currency) }}</td>
                        </tr>

                        {{-- IVA Breakdown --}}
                        <tr>
                            <td class="px-3 text-start cell-with-borders" colspan="7">
                                {{ $is_withholding ? 'IVA Retenido' : 'IVA Traslado' }}

                                {{ QuantityConverterHelper::str_toStrPercentage($applied_rate) }}
                                = {{ FormatCurrencyHelper::formatCurrency($applied_tax_import, $currency) }}
                            </td>
                        </tr>
                    @endforeach

                </tbody>
            </table>
        </section>

        {{-- SAT Certificates --}}
        <section class="mt-1">

            {{-- CFDI Seal & SAT Seal --}}
            <div class="row">
                {{-- CFDI Seal --}}
                <p class="px-1">
                    <span class="fw-bold">Sello digital del CFDI:</span><br>
                    <span class="wrapped-text">{{ $tax_payment->cfdi_seal }}</span>
                </p>

                {{-- SAT Seal --}}
                <p class="px-1 mb-0">
                    <span class="fw-bold">Sello digital del SAT:</span><br>
                    <span class="wrapped-text">{{ $tax_payment->sat_seal }}</span>
                </p>
            </div>

            {{-- QR - Original String --}}
            <div class="row">

                {{-- QR Code --}}
                <div class="col-3">
                    <img class="left-qr-code" src="{{ $qr_stamp }}" alt="Invoice QR">
                    <p class="text-center pe-4">
                        <span class="fw-semi-bold">Prov. Cert.</span>
                        {{ data_get($tax_payment->response_payload, 'stamp.rfc_provider_cert', 'PPD101129EA3') }}
                    </p>
                </div>

                {{-- Original String --}}
                <div class="col-8">
                    <p class="pe-1">
                        <span class="fw-bold">Cadena Original del Complemento de certificación digital del SAT:</span><br>
                        <span class="wrapped-text">{{ $tax_payment->tfd_original_string }}</span>
                    </p>
                </div>

            </div>
        </section>

    </main>

</body>

</html>
