<?xml version="1.0"?>
<t name="Sales Order Portal Content" t-name="sale.sale_order_portal_content">
        <!-- Intro -->
        <div id="introduction" t-attf-class="#{'border-bottom-0 pt-0 pb-3' if report_type == 'html' else ''}">
            <div class="d-flex gap-2" id="intro_row">
                <h2>
                    <t t-out="sale_order.type_name"/> -
                    <em t-out="sale_order.name"/>
                </h2>
            </div>
        </div>
        <div id="content">
            <div id="informations" class="row">
                <span id="transaction_info">
                    <div t-if="sale_order.get_portal_last_transaction() and not invoices and sale_order.state in ('sent', 'sale') and portal_confirmation == 'pay' and not success and not error" t-att-data-order-id="sale_order.id">
                        <t t-if="sale_order.transaction_ids">
                            <t t-call="payment.state_header">
                            <t t-set="tx" t-value="sale_order.get_portal_last_transaction()"/>
                        </t>
                    </t>
                    </div>
                </span>
                <!-- Information -->
                <div id="sale_info" class="col-12 col-lg-6 mb-4">
                    <span id="sale_info_title">
                        <h5 class="mb-1">Sale Information</h5>
                        <hr class="mt-1 mb-2"/>
                    </span>
                    <table class="table table-borderless table-sm">
                        <tbody style="white-space:nowrap" id="sale_info_table">
                            <tr>
                                <th t-if="sale_order.state in ['sale', 'cancel']" class="ps-0 pb-0">Order Date:</th>
                                <th t-else="" class="ps-0 pb-0">Date:</th>
                                <td class="w-100 pb-0 text-wrap"><span t-field="sale_order.date_order" t-options="{&quot;widget&quot;: &quot;date&quot;}"/></td>
                            </tr>
                            <tr t-if="sale_order.validity_date and sale_order.state in ['draft', 'sent']">
                                <th class="ps-0 pb-0">Expiration Date:</th>
                                <td class="w-100 pb-0 text-wrap"><span t-field="sale_order.validity_date" t-options="{&quot;widget&quot;: &quot;date&quot;}"/></td>
                            </tr>
                            <tr t-if="sale_order.client_order_ref">
                                <th class="ps-0 pb-0">Your Reference:</th>
                                <td class="w-100 pb-0 text-wrap"><span t-field="sale_order.client_order_ref"/></td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <!-- ======  Customer Information  ====== -->
                <div id="customer_info" class="col-12 col-lg-6 mb-4">
                    <h5 class="mb-1">
                        <t t-if="sale_order.partner_shipping_id == sale_order.partner_invoice_id">
                            Invoicing and Shipping Address
                        </t>
                        <t t-else="">
                            Invoicing Address
                        </t>
                        <small t-if="sale_order.partner_id == sale_order.partner_invoice_id == sale_order.env.user.partner_id">
                            <a class="small" t-attf-href="/my/account?redirect={{sale_order.get_portal_url()}}">
                                <i class="fa fa-fw fa-pencil"/>
                            </a>
                        </small>
                    </h5>
                    <hr class="mt-1 mb-2"/>
                    <div t-field="sale_order.partner_invoice_id" t-options="{'widget': 'contact', 'fields': ['name', 'address', 'phone', 'email']}"/>
                    <span t-if="sale_order.partner_shipping_id != sale_order.partner_invoice_id" id="shipping_address" class="col-lg-6">
                        <br/>
                        <h5 class="mb-1">
                            Shipping Address
                        </h5>
                        <hr class="mt-1 mb-2"/>
                        <div t-field="sale_order.partner_shipping_id" t-options="{ &quot;widget&quot;: &quot;contact&quot;, &quot;fields&quot;: [ &quot;name&quot;, &quot;address&quot;]}"/>
                    </span>
                </div>
                <t t-set="invoices" t-value="sale_order.invoice_ids.filtered(lambda i: i.state not in ['draft', 'cancel']).sorted('date', reverse=True)[:3]"/>
                <div id="sale_invoices" t-if="invoices and sale_order.state in ['sale', 'cancel']" class="col-12 col-lg-6 mb-4">
                    <h5 class="mb-1">Last Invoices</h5>
                    <hr class="mt-1 mb-2"/>
                    <t t-foreach="invoices" t-as="i">
                        <t t-set="report_url" t-value="i.get_portal_url()"/>
                        <t t-set="authorized_tx_ids" t-value="i.authorized_transaction_ids"/>
                        <div class="d-flex flex-column gap-2">
                            <div class="d-flex flex-column">
                                <div class="d-flex align-items-center justify-content-between">
                                    <a t-att-href="report_url">
                                        <span t-out="i.name"/>
                                    </a>
                                    <div t-if="i.payment_state in ('paid', 'in_payment')" class="small badge rounded-pill text-bg-success orders_label_text_align">
                                        <i class="fa fa-fw fa-check"/> Paid
                                    </div>
                                    <div t-elif="i.payment_state == 'reversed'" class="small badge text-bg-success orders_label_text_align">
                                        <i class="fa fa-fw fa-check"/> Reversed
                                    </div>
                                    <div t-elif="authorized_tx_ids" class="small badge rounded-pill text-bg-success orders_label_text_align">
                                        <i class="fa fa-fw fa-check"/> Authorized
                                    </div>
                                    <div t-else="" class="small badge rounded-pill text-bg-info orders_label_text_align">
                                        <i class="fa fa-fw fa-clock-o"/> Waiting Payment
                                    </div>
                                </div>
                                <div class="small d-lg-inline-block">Date: <span class="text-muted" t-field="i.invoice_date"/></div>
                            </div>
                        </div>
                    </t>
                </div>
            </div>

            <section id="details" style="page-break-inside: auto;">
                <t t-if="product_documents">
                    <h4 id="details">Documents</h4>
                    <div class="d-flex flex-grow-1 flex-wrap gap-1 mb32">
                        <t t-foreach="product_documents" t-as="document_sudo">
                            <div class="bg-light p-2 rounded">
                                <div class="position-relative text-center">
                                    <t t-set="attachment_sudo" t-value="document_sudo.ir_attachment_id"/>
                                    <t t-set="target" t-value="attachment_sudo.type == 'url' and '_blank' or '_self'"/>
                                    <a t-att-href="sale_order.get_portal_url('/document/' + str(document_sudo.id))" t-att-target="target" class="d-flex flex-row">
                                        <div class="o_image" t-att-title="attachment_sudo.name" t-att-data-mimetype="attachment_sudo.mimetype" t-attf-data-src="/web/image/#{attachment_sudo.id}/100x80?access_token=#{attachment_sudo.access_token}"/>
                                        <div class="o_portal_product_document align-self-center" t-out="attachment_sudo.name"/>
                                    </a>
                                </div>
                            </div>
                        </t>
                    </div>
                </t>

                <t t-set="display_discount" t-value="True in [line.discount &gt; 0 for line in sale_order.order_line]"/>
                <t t-set="display_taxes" t-value="any(line.tax_ids for line in sale_order.order_line)"/>

                <div class="table-responsive">
                    <table t-att-data-order-id="sale_order.id" t-att-data-token="sale_order.access_token" class="table table-sm" id="sales_order_table">
                        <thead>
                            <tr>
                                <th class="text-start" id="product_name_header">Products</th>
                                <th class="text-end" id="product_qty_header">Quantity</th>
                                <th t-attf-class="text-end {{ 'd-none d-sm-table-cell' if report_type == 'html' else '' }}" id="product_unit_price_header">
                                    Unit Price
                                </th>
                                <th t-if="display_discount" t-attf-class="text-end {{ 'd-none d-sm-table-cell' if report_type == 'html' else '' }}" id="product_discount_header">
                                    <span>Disc.%</span>
                                </th>
                                <th t-if="display_taxes" t-attf-class="text-end {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}" id="taxes_header">
                                    <span>Taxes</span>
                                </th>
                                <th class="text-end" id="subtotal_header">
                                    <span>Amount</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody class="sale_tbody">

                            <t t-set="current_subtotal" t-value="0"/>
                            <t t-set="lines_to_report" t-value="sale_order._get_order_lines_to_report()"/>

                            <t t-foreach="lines_to_report" t-as="line">

                                <t t-set="current_subtotal" t-value="current_subtotal + line.price_subtotal"/>
                                <t t-set="is_note" t-value="line.display_type == 'line_note'"/>
                                <tr t-att-class="'fw-bold o_line_section' if (                                         line.display_type == 'line_section'                                         or line.product_type == 'combo'                                     )                                     else 'fst-italic o_line_note' if is_note                                     else ''">
                                    <t name="product_line_row" t-if="not line.display_type and line.product_type != 'combo'">
                                        <td id="product_name">
                                            <span t-field="line.name"/>
                                        </td>
                                        <td class="text-end" id="quote_qty_td">
                                            <div id="quote_qty">
                                                <span t-field="line.product_uom_qty"/>
                                                <span t-field="line.product_uom_id"/>
                                            </div>
                                        </td>
                                        <td t-attf-class="text-end {{ 'd-none d-sm-table-cell' if report_type == 'html' else '' }}">
                                            <div t-if="line.discount &gt;= 0" t-field="line.price_unit" t-att-style="line.discount and 'text-decoration: line-through' or None" t-att-class="(line.discount and 'text-danger' or '') + ' text-end'"/>
                                            <div t-if="line.discount">
                                                <t t-out="(1-line.discount / 100.0) * line.price_unit" t-options="{&quot;widget&quot;: &quot;float&quot;, &quot;decimal_precision&quot;: &quot;Product Price&quot;}"/>
                                            </div>
                                        </td>
                                        <td t-if="display_discount" t-attf-class="text-end {{ 'd-none d-sm-table-cell' if report_type == 'html' else '' }}">
                                            <strong t-if="line.discount &gt; 0" class="text-info">
                                                <t t-out="((line.discount % 1) and '%s' or '%d') % line.discount"/>%
                                            </strong>
                                        </td>
                                        <td t-if="display_taxes" t-attf-class="text-end {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}" id="taxes">
                                            <span t-out="', '.join(map(lambda x: (x.name), line.tax_ids))"/>
                                        </td>
                                        <td t-if="not line.is_downpayment" class="text-end" id="subtotal">
                                        <span class="oe_order_line_price_subtotal" t-field="line.price_subtotal"/>
                                        </td>
                                    </t>
                                    <t t-if="line.display_type == 'line_section' or line.product_type == 'combo'">
                                        <td colspan="99">
                                            <span t-field="line.name"/>
                                        </td>
                                        <t t-set="current_section" t-value="line"/>
                                        <t t-set="current_subtotal" t-value="0"/>
                                    </t>
                                    <t t-if="is_note">
                                        <td colspan="99">
                                            <span t-field="line.name"/>
                                        </td>
                                    </t>
                                </tr>
                                <tr t-if="current_section and (                                         line_last                                         or lines_to_report[line_index+1].display_type == 'line_section'                                         or lines_to_report[line_index+1].product_type == 'combo'                                         or (                                             line.combo_item_id                                             and not lines_to_report[line_index+1].combo_item_id                                         )                                     ) and not line.is_downpayment" class="is-subtotal text-end">
                                    <t t-set="current_section" t-value="None"/>
                                    <td colspan="99">
                                        <strong class="mr16">Subtotal</strong>
                                        <span t-out="current_subtotal" t-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: sale_order.currency_id}"/>
                                    </td>
                                </tr>
                            </t>
                        </tbody>
                    </table>
                </div>

                <div id="total" name="total" style="page-break-inside: avoid;">
                    <div class="col-xs-7 col-md-5 ms-auto">
                        <t t-call="sale.sale_order_portal_content_totals_table"/>
                    </div>
                </div>
            </section>

            <section t-if="sale_order.signature" id="signature" name="Signature">
                <div class="row mt-4" name="signature">
                    <div t-attf-class="#{'col-3' if report_type != 'html' else 'col-sm-7 col-md-4'} ms-auto text-center">
                        <h5>Signature</h5>
                        <img t-att-src="image_data_uri(sale_order.signature)" style="max-height: 6rem; max-width: 100%;"/>
                        <p t-field="sale_order.signed_by"/>
                    </div>
                </div>
            </section>

            <section t-if="not is_html_empty(sale_order.note)" id="terms" class="mt-4">
                <h4 class="">Terms &amp; Conditions</h4>
                <hr class="mt-0 mb-1"/>
                <em t-field="sale_order.note"/>
            </section>

            <section t-if="sale_order.payment_term_id" class="mt-4">
                <h4 class="">Payment terms</h4>
                <hr class="mt-0 mb-1"/>
                <span t-field="sale_order.payment_term_id.note"/>
            </section>
        </div>
    </t>

    