Search code examples
odooodoo-15

how to print looped data from a method


i created a method as below: but it only takes the last loop data not all the data to xml for generating pdf,so need a way to generate pdf on a loop from this method

def bsku_asin_picking(self):
    website = self.env['website'].search([('company_id', '=', self.env.company.id)], limit=1)
    # print('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%', website.label_type)
    # vals = []
    for rec in self.move_ids_without_package:
        product = self.env['product.product'].browse(rec.product_id.id)
        data = {
            'quantity': rec.product_uom_qty,
            'display_name': product.display_name,
            'product': product,
        }

        if website.label_type == 'bsku':
            data['barcode_value'] = product.bsku
        else:
            data['barcode_value'] = product.asin
        # vals.append(data)
    return self.env.ref('product_label_bsku_sin.report_product_template_label').report_action(product, data=data)

Solution

  • The variables passed through data will be available in the rendering context.

    Example:

    def bsku_asin_picking(self):
    
        website = self.env['website'].search([('company_id', '=', self.env.company.id)], limit=1)
    
        vals = []
    
        for rec in self.move_ids_without_package:
            product = rec.product_id
            product_data = {
                'quantity': rec.product_uom_qty,
                'display_name': product.display_name,
                'product': product,
            }
            if website.label_type == 'bsku':
                product_data['barcode_value'] = product.bsku
            else:
                product_data['barcode_value'] = product.asin
            vals.append(product_data)
    
        datas = {'vals': vals}
    
        return self.env.ref('product_label_bsku_sin.action_report_product_template_label').report_action(self, data=datas)
    

    Then you can loop over vals and print product data.

    Example:

    <t t-foreach="vals" t-as="product_data">
        <p t-out="product_data['quantity']"/>
        <p t-out="product_data['display_name']"/>
        <p t-out="product_data['product']"/>
        <p t-out="product_data['barcode_value']"/>
    </t>
    

    Alternative

    You can also call report_action without data and loop over the move_ids_without_package:

    Example:

    <t t-set="website_label_type" t-value="doc.env['website'].search([('company_id', '=', doc.env.company.id)], limit=1)"/>
    
    <t t-foreach="doc.move_ids_without_package" t-as="line">
        <p t-out="line.product_uom_qty"/>
        <p t-out="line.display_name"/>
        <p t-out="line.product_id.name"/>
        
        <t t-if="website_label_type == 'bsku'">
            <span t-out="line.product_id.bsku"/>
        </t>
        <t t-else="">
            <span t-out="line.product_id.asin"/>
        </t>
    </t>