<data priority="50">

    <xpath expr="//table[@name='invoice_line_table']//th[1]" position="before">
        <t t-if="o.invoice_line_ids.sale_line_ids or o.invoice_line_ids.purchase_line_id">
            <th id="position" class="text-start">
                <span>Pos</span>
            </th>
        </t>
    </xpath>

    <xpath expr="//h2" position="before">
        <!-- Get all sale orders without duplicates -->
        <t t-set="sale_orders" t-value="list(set([line.order_id for line in o.invoice_line_ids.sale_line_ids]))"/>
        <!-- Store if has sale orders -->
        <t t-set="has_sale_orders" t-value="len(sale_orders) &gt; 0"/>
        <!-- Store if has multiple sale orders -->
        <t t-set="has_multiple_sale_orders" t-value="len(sale_orders) &gt; 1"/>
        <!-- Store if has lines without sale orders -->
        <t t-set="has_lines_without_sale_orders" t-value="len(o.invoice_line_ids.filtered(lambda l: not l.sale_line_ids)) &gt; 0"/>
    </xpath>

    <xpath expr="//h2/span[3]" position="replace">
        <span t-if="o.move_type == 'out_invoice' and o.state == 'posted' and not has_multiple_sale_orders">Rechnung</span>
        <span t-if="o.move_type == 'out_invoice' and o.state == 'posted' and has_multiple_sale_orders">Sammelrechnung</span>
    </xpath>

    <xpath expr="//t[@t-foreach='lines']" position="replace">
        <!-- Add empty sale order if there are lines without sale orders -->
        <t t-if="not has_sale_orders or has_lines_without_sale_orders">
            <t t-set="null" t-value="sale_orders.append(False)"/>
        </t>
        <!-- <p t-esc="sale_orders"/> -->
        <!-- <p t-esc="has_sale_orders"/> -->
        <!-- <p t-esc="has_multiple_sale_orders"/> -->
        <!-- <p t-esc="has_lines_without_sale_orders"/> -->
        <t t-foreach="sale_orders" t-as="sale_order">
            <tr t-if="has_sale_orders and sale_order" class="o_line_note">
                <td colspan="99" style="padding-top: 1rem;">
                    <strong>
                        <span>Verkaufsauftrag </span>
                        <span t-esc="sale_order.name"/>
                        <span t-if="sale_order.client_order_ref">/</span>
                        <span t-if="sale_order.client_order_ref" t-esc="sale_order.client_order_ref"/>
                        <span> vom </span>
                        <span t-esc="sale_order.date_order" t-options="{'widget': 'date'}"/>
                        <span>:</span>
                    </strong>
                </td>
            </tr>
            <tr t-if="not sale_order" class="o_line_note">
                <td colspan="99" style="padding-top: 1rem;">
                    <strong>
                        <span>Weitere Positionen: </span>
                    </strong>
                </td>
            </tr>

            <t t-foreach="lines" t-as="line">
                <!-- Show line if sale order is set and linked or not sale order is set and line is unlinked  -->

                <t t-if="(sale_order and sale_order in line.sale_line_ids.order_id) or (not line.sale_line_ids and not sale_order)">
                    <t t-set="current_subtotal" t-value="current_subtotal + line.price_subtotal" groups="account.group_show_line_subtotals_tax_excluded"/>
                    <t t-set="current_subtotal" t-value="current_subtotal + line.price_total" groups="account.group_show_line_subtotals_tax_included"/>
                    <tr t-att-class="'bg-200 font-weight-bold o_line_section' if line.display_type == 'line_section' else 'font-italic o_line_note' if line.display_type == 'line_note' else ''">

                        <t t-if="line.display_type == 'product'" name="account_invoice_line_accountable">
                            <td id="position">
                                <span t-field="line.position"/>
                            </td>
                            <td name="account_invoice_line_name">
                                <span t-field="line.name" t-options="{'widget': 'text'}"/>
                                <t t-if="line.product_id.country_of_origin.code">
                                    <br/>
                                    <span>Country of Origin: </span>
                                    <span t-field="line.product_id.country_of_origin.code"/>
                                </t>
                                <t t-if="line.product_id.hs_code">
                                    <br/>
                                    <span>HS Code: </span>
                                    <span t-field="line.product_id.hs_code"/>
                                </t>
                                <t t-if="line.product_id.weight">
                                    <br/>
                                    <span>Weight: <span t-field="line.product_id.weight"/>
                                    </span>
                                    <span t-field="line.product_id.weight_uom_id.display_name"/>
                                </t>
                            </td>
                            <td class="text-end">
                                <t t-if="line.product_uom_id.id == 1">
                                    <span id="product_qty" t-field="line.quantity" t-options="{'widget': 'integer'}"/>
                                </t>
                                <t t-else="">
                                    <span id="product_qty" t-field="line.quantity"/>
                                </t>
                                <span t-field="line.product_uom_id" groups="uom.group_uom"/>
                            </td>
                            <td t-attf-class="text-end {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                <span class="text-nowrap" t-field="line.price_unit"/>
                            </td>
                            <td t-if="display_discount" t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                <span class="text-nowrap" t-field="line.discount"/>
                            </td>
                            <td t-attf-class="text-left {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                <span t-esc="', '.join(map(lambda x: (x.description or x.name), line.tax_ids))" id="line_tax_ids"/>
                            </td>
                            <td class="text-end o_price_total">
                                <span class="text-nowrap" t-field="line.price_subtotal"/>
                            </td>
                        </t>
                        <t t-if="line.display_type == 'line_section'">
                            <td colspan="99">
                                <span t-field="line.name" t-options="{'widget': 'text'}"/>
                            </td>
                            <t t-set="current_section" t-value="line"/>
                            <t t-set="current_subtotal" t-value="0"/>
                        </t>
                        <t t-if="line.display_type == 'line_note'">
                            <td colspan="99">
                                <span t-field="line.name" t-options="{'widget': 'text'}"/>
                            </td>
                        </t>
                    </tr>
                    <t t-if="current_section and (line_last or lines[line_index+1].display_type == 'line_section')">
                        <tr class="is-subtotal text-end">
                            <td colspan="99">
                                <strong class="mr16">Subtotal</strong>
                                <span t-esc="current_subtotal" t-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: o.currency_id}"/>
                            </td>
                        </tr>
                    </t>
                </t>
            </t>
        </t>
    </xpath>
</data>