<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <template id="report_ekg_packing_document">
        <t t-set="partners" t-value="docs.mapped('partner_id')"/>
        <t
            t-set="lines"
            t-value="docs.filtered(lambda p:p.partner_id).mapped('move_ids').filtered(lambda x: x.product_uom_qty)"
        />
        <t t-set="products" t-value="lines.mapped('product_id')"/>
        <t t-set="pagination" t-value="9"/>
        <t t-set="partner_chunks" t-value="[partners[i:i+pagination] for i in range(0, len(partners), pagination)]"/>
        <t t-foreach="partner_chunks" t-as="partner_chunk">
            <div style="page-break-inside: avoid;">
                <table class="table table-bordered table-sm  w-auto border-dark" name="nfu_ekg_packing_list">
                    <thead>
                        <tr>
                            <th class="product align-top text-start">Product</th>
                            <th class="code align-top text-truncate">Internal<br/>Reference</th>
                            <th class="packaging align-top text-truncate">Batch<br/>Size</th>
                            <t t-foreach="partner_chunk" t-as="partner">
                                <th class="partner align-top">
                                        <div class="text-truncate">
                                            <t t-if="last_first">
                                                <t t-esc="partner.lastname"/>,<br/>
                                                <t t-esc="partner.firstname"/>
                                            </t>
                                            <t t-else="">
                                                <t t-esc="partner.firstname"/><br/>
                                                <t t-esc="partner.lastname"/>
                                            </t>
                                    </div>
                                </th>
                            </t>
                        </tr>
                    </thead>
                    <t t-foreach="products" t-as="product">
                        <tr>
                            <td class="product text-truncate"><span t-esc="product.name"/></td>
                            <td class="code text-truncate"><span t-esc="product.default_code"/></td>
                            <td class="packaging text-truncate">
                                <t t-foreach="product.packaging_ids" t-as="packaging">
                                    <span t-esc="packaging.name"/>
                                </t>
                            </td>
                            <t t-foreach="partner_chunk" t-as="partner">
                                <td>
                                    <t
                                        t-set="qty"
                                        t-value="sum(lines.filtered(lambda p:p.picking_id.partner_id.id == partner.id).filtered(lambda p:p.product_id.id == product.id).mapped('product_uom_qty'))"
                                    />
                                    <t t-if="qty" t-esc="qty"/>
                                </td>
                            </t>
                        </tr>
                    </t>
                </table>
            </div>
        </t>
    </template>


    <template id="report_ekg_packing_list">
        <t t-call="web.html_container">
            <t t-call="web.basic_layout">
                <t t-call="nfu_ekg_stock.report_ekg_packing_document"/>
            </t>
        </t>
    </template>

    <template id="report_ekg_packing_list_lf">
        <t t-call="web.html_container">
            <t t-call="web.basic_layout">
                <t t-set="last_first" t-value="True"/>
                <t t-call="nfu_ekg_stock.report_ekg_packing_document"/>
            </t>
        </t>
    </template>
</odoo>
