odoo 10自定义报告..sum行数..by product_id

时间:2018-11-15 08:27:38

标签: python odoo odoo-10

odoo 10报告...问题... 为什么get_qty无法将数据发送到docargs? 我想要行数据...数量总计...由product_id相同... 为什么xml t-foreach =“ data”可以为我获取任何数据

class ReportStockInventorySummary(models.AbstractModel):
            _name = 'report.stock.inventory.summary'

            def get_qty(self, docids):
                docs = self.env["stock.inventory"].browse(docids)
                lines = self.env["stock.inventory.line"].search([('inventory_id', '=', docs.id)])
                data = list()
                show_data = list()
                for x in lines:
                    data.append({
                        "line_location_id": x.location_id,
                        "line_product_id": x.product_id,
                        "line_product_qty": x.product_qty,
                    })

                for i, g in groupby(sorted(data), key=lambda x: x['line_product_id']):
                    show_data.append([i, sum(v['line_product_qty'] for v in g)])

            @api.multi
            def render_html(self, docids, data):
                report = self.env['report']
                self.model = self.env.context.get('active_model')
                docs = self.env["stock.inventory"].browse(docids)


                docargs = {
                    'doc_ids': docids,
                    'doc_model': self.model,
                    'docs': docs,
                    'data': self.get_qty //* i want sum product_qty by product_id *//
                }
                return report.render("stock_inventory_report.report_stock_inventory_template", docargs)

            <?xml version="1.0"?>
        <odoo>
            <data>
                <template id="report_stock_inventory_template">
                    <t t-call="report.html_container">
                        <t t-foreach="docs" t-as="o">
                            <div class="page">
                                <h2>Report title</h2>
                                <p>This object's name is
                                    <span t-field="o.name"/>
                                </p>
                                <table class="table table-condensed">
                                        <thead>
                                            <tr>
                                                <th><strong>Location</strong></th>
                                                <th><strong>Product</strong></th>
                                                <th class="text-right"><strong>Quantity</strong></th>
                                            </tr>
                                        </thead>
    <!---- why t-foreach="data" cant show any data for me -->
                                        <tr t-foreach="data" t-as="line">
                                            <td><span t-esc="line['line_location_id']" /></td>
                                            <td><span t-esc="line['line_product_id']" /></td>
                                            <td><span t-esc="line['line_product_qty']" /></td>
                                        </tr>


                                        <tr>
                                            <td></td>
                                        </tr>
                                    </table>
                            </div>
                        </t>
                    </t>
                </template>
            </data>
        </odoo>

我想从qweb中的stock.inventory.lines显示数据和字段(location_id,product_id,product_qty),在上述py中需要更改什么,以及如何创建视图? 初学者有类似的例子吗?

1 个答案:

答案 0 :(得分:0)

您正在将方法get_qty绑定到data,但是应该改为调用它。

'data': self.get_qty(doc_ids)

更好的是不要使用data键,因为它已经被另一个进程使用。只需创建您自己的密钥,然后在报告模板中使用它即可,而不是数据:

'get_qty': self.get_qty(doc_ids)

并且您的方法未返回任何响应。 None。我认为show_data是您的返回结果,因此只需返回即可。

def get_qty(self, docids):
    show_data = []
    # ...
    return show_data