如何在视图Qweb中对字段求和?

时间:2016-03-30 20:39:59

标签: python openerp odoo-9

我正在account.invoice模型中创建一个报告(qweb视图),并希望为每个发票行汇总一些字段,如下图所示:

enter image description here

问题是不按我的要求对字段求和。我该怎么办?

cantidad_producto = fields.Integer(string='Cantidad Producto', store=True, readonly=True, compute='cantidad_consolidado')
total_precio_unitario = fields.Monetary(string='Total precio unitario', store=True, readonly=True, compute='cantidad_consolidado')
total_precio_neto = fields.Monetary(string='Total Cantidad Producto', store=True, readonly=True, compute='cantidad_consolidado')

这是我在文件account.invoice.py中的函数compute:

@api.one
@api.depends('invoice_line_ids.quantity', 'invoice_line_ids.price_unit', 'invoice_line_ids.price_subtotal')
def cantidad_consolidado(self):
    self.cantidad_producto = sum(line.quantity for line in self.invoice_line_ids)
    self.total_precio_unitario = sum(line.price_unit for line in self.invoice_line_ids)
    self.total_precio_neto = sum(line.price_subtotal for line in self.invoice_line_ids

最后,这是我的观看代码:

<p>Resumen</p>
<table class="table table-bordered">
  <thead>
    <tr>
      <th class="text-center">Total Cantidad</th>
      <th class="text-center">Total Precio Unitario</th>
      <th class="text-center">Total Precio(Neto)</th>
    </tr>
  </thead>
  <tbody class="invoice_tbody">
    <td class="text-center">
      <span t-esc="cantidad_producto" />
    </td>
    <td class="text-center">
      <span t-esc="total_precio_unitario" />
    </td>
    <td class="text-center">
      <span t-esc="total_precio_neto" />
    </td>
  </tbody>
</table>

为什么不汇总报告中的字段?有人可以告诉我,请帮助我。

1 个答案:

答案 0 :(得分:1)

您需要定义一个继承自 report_sxw.rml_parse 类的类,在该类中,您需要定义 init 方法,您需要在其中添加方法名称 localcontext 字典的 KEY

class sale_quotation_report(report_sxw.rml_parse):

    def __init__(self, cr, uid, name, context): 
        super(sale_quotation_report, self).__init__(cr, uid, name, context=context)
            self.localcontext.update({
                    ‘get_total’: self._get_total,
            })

    def _get_total(self, lines, field):
        total = 0.0
        for line in lines :
            total += line.product_uom_qty or 0.0
        return total

还需要定义一个继承自 osv.AbstractModel

的类
class report_saleorderqweb(osv.AbstractModel):
    _name = ‘module_name.report_sale_order_qweb’
    _inherit = ‘report.abstract_report’
    _template = ‘module_name.report_sale_order_qweb’
    _wrapped_report_class = sale_quotation_report

Here,

_name = ‘report.<module_name>.<report_name>’
_inherit = ‘report.abstract_report’
_template = ‘<module_name>.<report_name>’
_wrapped_report_class = <parser_class_name>

最后从模板中调用该方法

<t t-foreach=”docs” t-as=”o”>
    <span t-esc=”get_total(o.order_line)”/>
</t>

您可以参考Steps to create Qweb report