<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <template id="report_delivery_document" inherit_id="stock.report_delivery_document">
        <div class="page" position="before">
            <t t-set="report_lines" t-value="o.get_delivery_report_lines()" />
        </div>
        <!-- overrides when the picking is not done -->
        <xpath
            expr='//table[@name="stock_move_table"]/tbody/t[@t-set="lines"]'
            position="attributes"
        >
            <attribute name="t-value">report_lines</attribute>
        </xpath>

        <xpath
            expr='//table[@name="stock_move_table"]/tbody/tr//span[@t-field="move.product_id"]/..'
            position="attributes"
        >
            <attribute name="t-if">False</attribute>
        </xpath>

        <xpath
            expr='//table[@name="stock_move_table"]/tbody/tr//span[@t-field="move.product_id"]/..'
            position="after"
        >
            <t t-if="move.id">
                <td>
                    <span t-field="move.product_id" />
                    <p t-if="move.description_picking != move.product_id.name">
                        <span t-field="move.description_picking" />
                    </p>
                </td>
            </t>
            <t t-else="">
                <th>
                    <span t-esc="move.description_picking" />
                </th>
            </t>
        </xpath>

        <xpath
            expr='//table[@name="stock_move_table"]/tbody/tr/td/span[@t-field="move.product_uom_qty"]'
            position="attributes"
        >
            <attribute name="t-if">move.id</attribute>
        </xpath>

        <xpath
            expr='//table[@name="stock_move_table"]/tbody/tr/td/span[@t-field="move.product_uom"]'
            position="attributes"
        >
            <attribute name="t-if">move.id</attribute>
        </xpath>

        <!-- overrides when the picking is done -->
        <xpath
            expr='//table[@name="stock_move_line_table"]/tbody//tr[@t-as="move_line"]'
            position="attributes"
        >
            <attribute name="t-foreach">o.get_delivery_report_lines()</attribute>
        </xpath>
        <xpath
            expr="//tbody/t[@t-else='']//tr[@t-as='move_line']"
            position="attributes"
        >
            <attribute name="t-foreach">o.get_delivery_report_lines()</attribute>
        </xpath>

        <xpath
            expr="//table[@name='stock_backorder_table']/../p[last()]"
            position="attributes"
        >
            <attribute name="t-if">False</attribute>
        </xpath>

        <!-- Remove "All items could not be shipped..." sentence
            Substitute it by a message indicating if all goods have been delivered. -->
        <xpath expr='//t[@t-if="o.backorder_ids and backorders"]' position="attributes">
            <attribute name="t-if">False</attribute>
            <attribute name="name">stock_backorder_block</attribute>
        </xpath>
        <xpath expr='//t[@name="stock_backorder_block"]' position="after">
            <t t-if="o.picking_type_id.code == 'outgoing' and get_remaining_to_deliver">
                <t t-set="remainings" t-value="get_remaining_to_deliver(o)" />
                <t t-if="not remainings">
                    <p
                        class="remaining-to-deliver-separator"
                    >All ordered items have been delivered.</p>
                </t>
                <t t-else="">
                    <p class="remaining-to-deliver-separator">Remaining to deliver:</p>
                    <table class="table table-sm" name="remaining_to_deliver_table">
                        <tbody>
                            <tr t-foreach="remainings" t-as="remaining">
                                <td>
                                    <span
                                        t-att-class="'font-weight-bold' if remaining['is_header'] else None"
                                        t-esc="remaining['concept']"
                                    />
                                </td>
                                <td style="text-align: right">
                                    <t t-if="not remaining['is_header']">
                                        <span
                                            t-esc="remaining['qty']"
                                            t-options="{'widget': 'float',
                                                          'precision': rounding_to_precision(remaining['uom'].rounding)}"
                                        />
                                        <span
                                            class="remaining_uom_name"
                                            t-field="remaining['uom'].name"
                                        />
                                    </t>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </t>
            </t>
        </xpath>

    </template>

    <template
        id="stock_report_delivery_has_serial_move_line_inherit"
        inherit_id="stock.stock_report_delivery_has_serial_move_line"
    >
        <!-- Odoo tries to display both description and product name.
        When the line is a SO section, we only want to display a description.
        Enable default behavior when line has an id, only display the description otherwise. -->
        <xpath expr="//span[@t-field='move_line.product_id']/.." position="attributes">
            <attribute name="t-if">move_line.id</attribute>
        </xpath>
        <xpath expr="//span[@t-field='move_line.product_id']/.." position="after">
            <td t-else="">
                <span t-field="move_line.description_picking" />
            </td>
        </xpath>
        <!-- Disable the rest, when line doesn't have an id -->
        <xpath expr='//span[@t-field="move_line.lot_id.name"]' position="attributes">
            <attribute name="t-if">move_line.id</attribute>
        </xpath>

        <xpath expr='//span[@t-field="move_line.lot_id.name"]' position="attributes">
            <attribute name="t-if">move_line.id</attribute>
        </xpath>

        <xpath expr='//span[@t-field="move_line.qty_done"]' position="attributes">
            <attribute name="t-if">move_line.id</attribute>
        </xpath>

        <xpath expr='//span[@t-field="move_line.product_uom_id"]' position="attributes">
            <attribute name="t-if">move_line.id</attribute>
        </xpath>
    </template>

    <template
        id="report_delivery_document_inherit_sale_stock"
        inherit_id="sale_stock.report_delivery_document_inherit_sale_stock"
        priority="120"
    >
        <!-- Remove the customer reference in the header if there is more than one -->
        <xpath
            expr='//div[@t-if="o.sudo().sale_id.client_order_ref"]'
            position="attributes"
        >
            <attribute name="t-if">len(o.get_customer_refs())==1</attribute>
        </xpath>

        <!-- Replace the customer refs with the one based on the move line displayed -->
        <xpath
            expr='//p[@t-field="o.sudo().sale_id.client_order_ref"]'
            position="attributes"
        >
            <attribute name="t-if">False</attribute>
        </xpath>
        <xpath
            expr='//p[@t-field="o.sudo().sale_id.client_order_ref"]'
            position="after"
        >
            <p class="m-0">
                <t t-esc="o.get_customer_refs().pop()" />
            </p>
        </xpath>
    </template>
</odoo>
