{% set pricing = cart.getPricing %}
{% set billing_address = cart.invoiceAddress ?: cart.deliveryAddress %}
{% set delivery_address = cart.deliveryAddress %}
{% set use_shipping_address = cart.deliveryMethod is null or cart.deliveryMethod.category is null or not cart.deliveryMethod.category.disabledShippingAddress %}
{% set payment_method = cart.paymentMethod ?: (cart.cartCheckout ? cart.cartCheckout.paymentMethod : null) %}
{% set payment_configuration = payment_method ? payment_method.configuration : {} %}
{% set payment_text = payment_configuration['text'] ?? null %}
{% set is_proforma = cart.getDocumentLabelKey() == 'checkout_design.document_proforma' %}
{% set show_offline_payment_info = is_proforma and payment_method and payment_text and payment_method.method in ['check', 'bankTransfert', 'banktransfert'] %}
{% set invoice_number = invoice_context.invoice_number ?: cart.token %}
{% set order_reference = cart.token ?: invoice_number %}
{% set company_name = contact.editorCompany ?: contact.company %}
{% set company_address = contact.editorAddress ?: contact.address %}
{% set company_postal = contact.editorPostal ?: contact.postal %}
{% set company_city = contact.editorCity ?: contact.city %}
{% set company_vat = contact.editorTva %}
{% set company_rcs = contact.editorRcs %}
<!DOCTYPE html>
<html lang="{{ cart.locale ?: 'fr' }}">
<head>
    <meta charset="utf-8">
    <title>{{ document_label }} - {{ invoice_number }}</title>
    <style>
        @page {
            size: A4;
            margin: 12mm 12mm 14mm 12mm;
        }

        html,
        body {
            margin: 0;
            background: #ffffff;
            color: #17342f;
            font-family: DejaVu Sans, Arial, sans-serif;
            font-size: 10px;
            line-height: 1.25;
            height: auto;
        }

        .page {
            background: #ffffff;
            min-height: 271mm;
            display: flex;
            flex-direction: column;
        }

        .sheet {
            background: #ffffff;
            flex: 1 1 auto;
            display: flex;
            flex-direction: column;
        }

        .top-accent {
            display: none;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .muted {
            color: #6b7d79;
        }

        .small {
            font-size: 9px;
        }

        .tiny {
            font-size: 8px;
        }

        .right {
            text-align: right;
        }

        .uppercase {
            text-transform: uppercase;
        }

        .title {
            font-size: 26px;
            font-weight: 700;
            margin: 0;
            color: #1d2b28;
        }

        .subtitle {
            margin: 0 0 4px;
            font-size: 9px;
            font-weight: 700;
            letter-spacing: 0.12em;
            text-transform: uppercase;
            color: #7d918b;
        }

        .hero td {
            vertical-align: top;
        }

        .brand-logo {
            width: 82px;
            height: 82px;
            object-fit: contain;
            background: #f4faf8;
            border: 1px solid #dbeae4;
            padding: 6px;
        }

        .company-block {
            padding-left: 16px;
        }

        .amount-block {
            width: 170px;
            text-align: right;
        }

        .amount-label {
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            color: #7d918b;
            letter-spacing: 0.08em;
        }

        .amount-value {
            margin-top: 6px;
            font-size: 22px;
            font-weight: 700;
            color: #1d2b28;
        }

        .meta-grid {
            width: 100%;
            margin-top: 12px;
        }

        .meta-grid td {
            width: 50%;
            vertical-align: top;
            padding-right: 18px;
        }

        .address-card {
            min-height: 88px;
        }

        .message-box {
            margin-top: 8px;
            padding: 9px 11px;
            background: #ffffff;
            border: 1px solid #e2ece8;
            color: #52635f;
        }

        .facture-row {
            margin-top: 12px;
            padding-top: 10px;
            border-top: 1px solid #e2ece8;
        }

        .facture-left {
            width: 70%;
        }

        .facture-right {
            width: 30%;
            text-align: right;
            font-size: 15px;
            font-weight: 700;
        }

        .info-strip {
            width: 100%;
            margin-top: 12px;
            border-collapse: separate;
            border-spacing: 8px 0;
            margin-left: -8px;
            margin-right: -8px;
        }

        .info-strip td {
            width: 25%;
            padding: 9px 11px;
            background: #ffffff;
            border: 1px solid #e2ece8;
            vertical-align: top;
        }

        .info-label {
            font-size: 8px;
            font-weight: 700;
            letter-spacing: 0.1em;
            text-transform: uppercase;
            color: #80928d;
            margin-bottom: 8px;
        }

        .info-value {
            font-size: 12px;
            font-weight: 700;
            color: #243633;
        }

        .line-table {
            margin-top: 12px;
            border: 1px solid #e2ece8;
            overflow: hidden;
        }

        .line-table table {
            width: 100%;
            border-collapse: collapse;
        }

        .line-table thead th {
            padding: 9px 8px;
            background: #ffffff;
            color: #71837f;
            font-size: 8px;
            font-weight: 700;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            border-bottom: 1px solid #e2ece8;
        }

        .line-table tbody td {
            padding: 8px 8px;
            border-bottom: 1px solid #edf2f0;
            vertical-align: top;
        }

        .line-table tbody tr:last-child td {
            border-bottom: 0;
        }

        .article-name {
            font-size: 11px;
            font-weight: 700;
            color: #1d2b28;
            margin-bottom: 4px;
        }

        .article-desc {
            color: #7a8b87;
            font-size: 9px;
            line-height: 1.3;
        }

        .summary-wrap {
            width: 100%;
            margin-top: 12px;
            page-break-inside: avoid;
            break-inside: avoid;
        }

        .footer-stack {
            margin-top: auto;
            page-break-inside: avoid;
            break-inside: avoid;
        }

        .summary-wrap td {
            vertical-align: top;
        }

        .side-card {
            width: 42%;
            padding-right: 16px;
        }

        .side-box {
            border: 1px solid #e2ece8;
            padding: 10px 12px;
            background: #ffffff;
            page-break-inside: avoid;
            break-inside: avoid;
        }

        .side-box table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 8px;
        }

        .side-box th,
        .side-box td {
            padding: 7px 0;
            border-bottom: 1px solid #edf2f0;
        }

        .side-box tr:last-child th,
        .side-box tr:last-child td {
            border-bottom: 0;
        }

        .totals-card {
            width: 58%;
        }

        .totals-box {
            border: 1px solid #e2ece8;
            padding: 10px 12px;
            background: #ffffff;
            page-break-inside: avoid;
            break-inside: avoid;
        }

        .totals-box table {
            width: 100%;
            border-collapse: collapse;
        }

        .totals-box td {
            padding: 6px 0;
        }

        .grand-total td {
            padding-top: 14px;
            border-top: 1px solid #dbe7e2;
            font-size: 15px;
            font-weight: 700;
            color: #162623;
        }

        .terms-footer {
            margin-top: 8px;
            padding-top: 6px;
            border-top: 1px solid #e2ece8;
            font-size: 6.2px;
            line-height: 1.15;
            color: #61726d;
        }

        .terms-footer strong {
            color: #30423e;
        }
    </style>
</head>
<body>
<div class="page">
    <div class="sheet">
        <div class="top-accent"></div>

        <table class="table hero">
            <tr>
                <td>
                    <table class="table">
                        <tr>
                            <td style="width: 108px;">
                                {% if contact.image and contact.image.path %}
                                    <img
                                        class="brand-logo"
                                        src="{{ absolute_url(asset(contact.image.path)|imagine_filter('logo_email_filter')) }}"
                                        alt="{{ company_name }}"
                                    >
                                {% endif %}
                            </td>
                            <td class="company-block">
                                <div style="font-size: 14px; font-weight: 700; color: #203330;">{{ company_name }}</div>
                                {% if contact.firstname or contact.lastname %}
                                    <div class="muted">{{ contact.firstname }} {{ contact.lastname }}</div>
                                {% endif %}
                                <div class="muted">{{ company_address }}</div>
                                <div class="muted">{{ company_postal }} {{ company_city }}</div>
                                {% if contact.phone1 %}<div class="muted">{{ contact.phone1 }}</div>{% endif %}
                                {% if contact.emailFrom %}<div class="muted">{{ contact.emailFrom }}</div>{% endif %}
                                {% if company_rcs %}<div class="muted">SIRET / RCS : {{ company_rcs }}</div>{% endif %}
                                {% if company_vat %}<div class="muted">TVA : {{ company_vat }}</div>{% endif %}
                            </td>
                        </tr>
                    </table>
                </td>
                <td class="amount-block">
                    <div class="amount-label">Montant total</div>
                    <div class="amount-value">{{ pricing.totalTTC|price_format }} €</div>
                </td>
            </tr>
        </table>

        <table class="table facture-row">
            <tr>
                <td class="facture-left">
                    <h1 class="title">{{ document_label }}</h1>
                </td>
                <td class="facture-right">
                    {% if cart.getDocumentLabelKey() == 'checkout_design.document_proforma' %}
                        PRO FORMA {{ invoice_number }}
                    {% else %}
                        #{{ invoice_number }}
                    {% endif %}
                </td>
            </tr>
        </table>

        <table class="meta-grid">
            <tr>
                <td>
                    <div class="subtitle">{{ use_shipping_address ? 'Adresse de livraison' : 'Méthode de livraison' }}</div>
                    <div class="address-card">
                        {% if use_shipping_address and delivery_address %}
                            <div style="font-weight: 700;">{{ delivery_address.firstname }} {{ delivery_address.lastname }}</div>
                            {% if delivery_address.company %}<div>{{ delivery_address.company }}</div>{% endif %}
                            <div>{{ delivery_address.street }}</div>
                            <div>{{ delivery_address.postal }} {{ delivery_address.city }}</div>
                            <div>{{ delivery_address.country|country_name }}</div>
                            {% if delivery_address.phone %}<div>TEL : {{ delivery_address.phone }}</div>{% endif %}
                            {% if delivery_address.email %}<div>EMAIL : {{ delivery_address.email }}</div>{% endif %}
                            <div style="margin-top: 6px;">Mode : {{ cart.deliveryMethod ? cart.deliveryMethod.name : 'Standard' }}</div>
                        {% elseif cart.deliveryMethod %}
                            <div style="font-weight: 700;">
                                {{ cart.deliveryMethod.category ? cart.deliveryMethod.category.name ~ ' : ' : '' }}{{ cart.deliveryMethod.name }}
                            </div>
                            {% if cart.deliveryMethod.description %}
                                <div style="margin-top: 6px;" class="muted">{{ cart.deliveryMethod.description|raw }}</div>
                            {% endif %}
                        {% else %}
                            <div class="muted">Aucune information de livraison renseignée.</div>
                        {% endif %}
                    </div>
                </td>
                <td>
                    <div class="subtitle">Adresse de facturation</div>
                    <div class="address-card">
                        {% if billing_address %}
                            <div style="font-weight: 700;">{{ billing_address.firstname }} {{ billing_address.lastname }}</div>
                            {% if billing_address.company %}<div>{{ billing_address.company }}</div>{% endif %}
                            <div>{{ billing_address.street }}</div>
                            <div>{{ billing_address.postal }} {{ billing_address.city }}</div>
                            <div>{{ billing_address.country|country_name }}</div>
                            {% if billing_address.phone %}<div>TEL : {{ billing_address.phone }}</div>{% endif %}
                            {% if billing_address.email %}<div>EMAIL : {{ billing_address.email }}</div>{% endif %}
                            {% if billing_address.vatId %}<div>TVA : {{ billing_address.vatId }}</div>{% endif %}
                        {% else %}
                            <div class="muted">Aucune adresse de facturation renseignée.</div>
                        {% endif %}
                    </div>
                </td>
            </tr>
        </table>

        <table class="info-strip">
            <tr>
                <td>
                    <div class="info-label">Date de facture</div>
                    <div class="info-value">{{ invoice_context.invoice_date|date('d/m/Y') }}</div>
                </td>
                <td>
                    <div class="info-label">Date de commande</div>
                    <div class="info-value">{{ invoice_context.order_date|date('d/m/Y') }}</div>
                </td>
                <td>
                    <div class="info-label">Conditions de règlement</div>
                    <div class="info-value">{{ invoice_context.payment_term_label ?: '-' }}</div>
                </td>
                <td>
                    <div class="info-label">Échéance de paiement</div>
                    <div class="info-value">
                        {% if invoice_context.payment_due_at %}
                            {{ invoice_context.payment_due_at|date('d/m/Y') }}
                        {% else %}
                            -
                        {% endif %}
                    </div>
                </td>
            </tr>
        </table>

        <div class="line-table">
            <table>
                <thead>
                <tr>
                    <th style="width: 7%;">N°</th>
                    <th style="width: 35%;">Article</th>
                    <th style="width: 11%;" class="right">Quantité</th>
                    <th style="width: 13%;" class="right">Prix unitaire HT</th>
                    <th style="width: 8%;" class="right">TVA</th>
                    <th style="width: 13%;" class="right">Montant HT</th>
                    <th style="width: 13%;" class="right">Montant TTC</th>
                </tr>
                </thead>
                <tbody>
                {% for line in pricing.lines %}
                    {% set line_tax_rate = '0%' %}
                    {% if line.unitPriceHT > 0 and line.unitPriceTTC > line.unitPriceHT %}
                        {% set line_tax_rate = (((line.unitPriceTTC - line.unitPriceHT) / line.unitPriceHT) * 100)|number_format(1, ',', ' ') ~ '%' %}
                    {% elseif line.totalHT > 0 and line.totalTTC > line.totalHT %}
                        {% set line_tax_rate = (((line.totalTTC - line.totalHT) / line.totalHT) * 100)|number_format(1, ',', ' ') ~ '%' %}
                    {% elseif line.netHT > 0 and line.tax > 0 %}
                        {% set line_tax_rate = ((line.tax / line.netHT) * 100)|number_format(1, ',', ' ') ~ '%' %}
                    {% endif %}
                    {% set article_format = '' %}
                    {% if line.typeName and line.unitLabel %}
                        {% set article_format = line.typeName ~ ' ' ~ line.unitLabel %}
                    {% elseif line.typeName %}
                        {% set article_format = line.typeName %}
                    {% elseif line.unitLabel %}
                        {% set article_format = line.unitLabel %}
                    {% elseif line.variantFullName and line.variantFullName != line.productName %}
                        {% set article_format = line.variantFullName|replace({(line.productName): ''})|trim %}
                    {% endif %}
                    {% set unit_discounted_ht = line.unitPriceDiscountHT is not null and line.unitPriceDiscountHT > 0 and line.unitPriceDiscountHT < line.unitPriceHT ? line.unitPriceDiscountHT : line.unitPriceHT %}
                    {% set unit_discounted_ttc = line.unitPriceDiscountTTC is not null and line.unitPriceDiscountTTC > 0 and line.unitPriceDiscountTTC < line.unitPriceTTC ? line.unitPriceDiscountTTC : line.unitPriceTTC %}
                    {% set unit_final_ht = line.isPromoGift ? null : unit_discounted_ht - (line.unitDegressiveHT ?: 0) %}
                    {% set unit_final_ttc = line.isPromoGift ? null : unit_discounted_ttc - (line.unitDegressiveTTC ?: 0) %}
                    {% if unit_final_ht is not null and unit_final_ht < 0 %}
                        {% set unit_final_ht = 0 %}
                    {% endif %}
                    {% if unit_final_ttc is not null and unit_final_ttc < 0 %}
                        {% set unit_final_ttc = 0 %}
                    {% endif %}
                    <tr>
                        <td>{{ loop.index }}</td>
                        <td>
                            <div class="article-name">{{ line.productName }}</div>
                            <div class="article-desc">
                                {% if article_format %}{{ article_format }}{% endif %}
                                {% if not line.isPromoGift and line.unitPriceTTC > 0 and line.unitPriceTTC > unit_discounted_ttc %}
                                    {% if article_format or line.degressiveTTC > 0 %}<br>{% endif %}
                                    Base : {{ unit_discounted_ttc|price_format }} € TTC au lieu de {{ line.unitPriceTTC|price_format }} € TTC
                                {% endif %}
                                {% if line.degressiveTTC > 0 %}
                                    {% if article_format or (not line.isPromoGift and line.unitPriceTTC > 0 and line.unitPriceTTC > unit_discounted_ttc) %}<br>{% endif %}
                                    {{ (pricing.degressiveIsBottleOffered ? 'shop.bottle_offered' : 'shop.degressive')|trans }} : -{{ line.degressiveTTC|price_format }} € TTC{% if line.unitDegressiveTTC %} (-{{ line.unitDegressiveTTC|price_format }} € TTC / unité){% endif %}
                                {% endif %}
                            </div>
                        </td>
                        <td class="right">{{ line.quantity }}</td>
                        <td class="right">
                            {% if line.isPromoGift %}
                                {{ 'shop.offered'|trans }}
                            {% else %}
                                {{ unit_final_ht|price_format }} €
                                {% if line.unitPriceHT > unit_discounted_ht %}
                                    <div class="article-desc">{{ line.unitPriceHT|price_format }} € {{ 'order_pdf.before_discount'|trans }}</div>
                                {% endif %}
                            {% endif %}
                        </td>
                        <td class="right">{{ line_tax_rate }}</td>
                        <td class="right">
                            {% if line.isPromoGift %}
                                {{ 'shop.offered'|trans }}
                            {% else %}
                                {{ line.netHT|price_format }} €
                            {% endif %}
                        </td>
                        <td class="right">
                            {% if line.isPromoGift %}
                                {{ 'shop.offered'|trans }}
                            {% else %}
                                {{ line.netTTC|price_format }} €
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="footer-stack">
            <table class="summary-wrap">
                <tr>
                    <td class="side-card">
                        <div class="side-box">
                            <div class="subtitle" style="margin-bottom: 2px;">Règlement</div>
                            <table>
                                <tbody>
                                <tr>
                                    <td>Mode de paiement</td>
                                    <td class="right">{{ invoice_context.payment_method_label ?: '-' }}</td>
                                </tr>
                                <tr>
                                    <td>Date de paiement</td>
                                    <td class="right">
                                        {% if invoice_context.payment_paid_at %}
                                            {{ invoice_context.payment_paid_at|date('d/m/Y') }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td>Référence de commande</td>
                                    <td class="right">{{ order_reference }}</td>
                                </tr>
                                {% if admin and cart.groupCode %}
                                    <tr>
                                        <td>Code partenaire</td>
                                        <td class="right">{{ cart.groupCode }}</td>
                                    </tr>
                                {% endif %}
                                </tbody>
                            </table>
                            {% if show_offline_payment_info %}
                                <div style="margin-top: 8px;" class="article-desc">{{ payment_text|raw }}</div>
                            {% endif %}
                        </div>
                        {% if admin and cart.comment %}
                            <div class="side-box" style="margin-top: 10px;">
                                <div class="subtitle" style="margin-bottom: 2px;">Commentaire client</div>
                                <div class="article-desc">{{ cart.comment|nl2br }}</div>
                            </div>
                        {% endif %}
                    </td>
                    <td class="totals-card">
                        <div class="totals-box">
                            <table>
                                <tr>
                                    <td>
                                        Total produits HT
                                        {% for promo_discount in invoice_context.promo_item_discounts %}
                                            <div class="small muted">dont remise {{ promo_discount.label }}</div>
                                        {% endfor %}
                                    </td>
                                    <td class="right">
                                        {% if invoice_context.product_total_ht_before_promos > invoice_context.product_total_ht %}
                                            <div class="small muted">{{ invoice_context.product_total_ht_before_promos|price_format }} €</div>
                                        {% endif %}
                                        {% for promo_discount in invoice_context.promo_item_discounts %}
                                            <div class="small muted">-{{ promo_discount.amount_ht|price_format }} €</div>
                                        {% endfor %}
                                        {{ invoice_context.product_total_ht|price_format }} €
                                    </td>
                                </tr>
                                <tr>
                                    <td>TVA produits{% if invoice_context.product_tax_rate_label %} ({{ invoice_context.product_tax_rate_label }}){% endif %}</td>
                                    <td class="right">{{ invoice_context.product_tax|price_format }} €</td>
                                </tr>
                                <tr>
                                    <td>Total produits TTC{% if invoice_context.product_tax_rate_label %} ({{ invoice_context.product_tax_rate_label }}){% endif %}</td>
                                    <td class="right">{{ invoice_context.product_total_ttc|price_format }} €</td>
                                </tr>
                                <tr>
                                    <td>
                                        Frais de port HT
                                        {% for promo_discount in invoice_context.promo_shipping_discounts %}
                                            <div class="small muted">dont remise {{ promo_discount.label }}</div>
                                        {% endfor %}
                                    </td>
                                    <td class="right">
                                        {% if invoice_context.shipping_ht_before_promos > invoice_context.shipping_ht %}
                                            <div class="small muted">{{ invoice_context.shipping_ht_before_promos|price_format }} €</div>
                                        {% endif %}
                                        {% for promo_discount in invoice_context.promo_shipping_discounts %}
                                            <div class="small muted">-{{ promo_discount.amount_ht|price_format }} €</div>
                                        {% endfor %}
                                        {{ invoice_context.shipping_ht|price_format }} €
                                    </td>
                                </tr>
                                <tr>
                                    <td>TVA frais de port{% if invoice_context.shipping_tax_rate_label %} ({{ invoice_context.shipping_tax_rate_label }}){% endif %}</td>
                                    <td class="right">{{ invoice_context.shipping_tax|price_format }} €</td>
                                </tr>
                                <tr>
                                    <td>Montant TTC frais de port{% if invoice_context.shipping_tax_rate_label %} ({{ invoice_context.shipping_tax_rate_label }}){% endif %}</td>
                                    <td class="right">{{ invoice_context.shipping_ttc|price_format }} €</td>
                                </tr>
                                <tr>
                                    <td>Total TVA{% if invoice_context.total_tax_rate_label %} ({{ invoice_context.total_tax_rate_label }}){% endif %}</td>
                                    <td class="right">{{ pricing.totalTax|price_format }} €</td>
                                </tr>
                                <tr class="grand-total">
                                    <td>Total TTC</td>
                                    <td class="right">{{ pricing.totalTTC|price_format }} €</td>
                                </tr>
                            </table>
                        </div>
                    </td>
                </tr>
            </table>

            <div class="terms-footer">
                <strong>{{ 'order_pdf.terms_title'|trans|upper }}</strong> -
                <strong>{{ 'order_pdf.terms_order_label'|trans }} :</strong> {{ 'order_pdf.terms_order_text'|trans }}
                |
                <strong>{{ 'order_pdf.terms_delivery_label'|trans }} :</strong> {{ 'order_pdf.terms_delivery_text'|trans }}
                |
                <strong>{{ 'order_pdf.terms_payment_label'|trans }} :</strong> {{ 'order_pdf.terms_payment_text'|trans }}
                |
                <strong>{{ 'order_pdf.terms_late_fee_label'|trans }} :</strong> {{ 'order_pdf.terms_late_fee_text'|trans }}
            </div>
        </div>
    </div>
</div>
</body>
</html>
