odoo qweb报表python后台新增属性

odoo qweb 打印pdf有三种方式:

1.常用的纯xml文件,如下(忽略menu)

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="report_sale_order_inprice_detail">
        <t t-call="dx_base_report.external_layout">
            <div t-if="docs.state=='sale' or docs.state == 'done'" class="page">
                <table class="table table-bordered">
                    <tr>
                        <td align="center" colspan="3">
                            <strong><h4>销售订单</h4></strong>
                        </td>
                    </tr>
                    <tr>
                        <td width="33%"><strong>订 单 号:</strong><span t-field="docs.name"/></td>
                        <td width="33%"><strong>下单客户:</strong><span t-field="docs.partner_id.name"/></td>
                        <td width="33%"><strong>销售订单:</strong><span t-field="docs.sale_approve_id"/></td>
                    </tr>
                </table>
            </div>
            <div t-if="docs.state != 'sale' and docs.state != 'done'" class="page">
                <h3 class="text-center">请打印销售单单据</h3>
            </div>
        </t>
    </template>
    <template id="report_sale_order_inprice">
        <t t-call="report.html_container">
            <t t-foreach="docs" t-as="o">
                 <t t-set="o" t-value="o.with_context({'lang':'zh_CN'})"/>
                 <t t-set="cn" t-value="True"/>
                 <t t-call="dx_sale_order_report.report_sale_order_inprice_detail" t-lang="'zh_CN'"/>
            </t>
        </t>
    </template>
</odoo>

2. 打印的部分内容需要自定义,但是希望使用原qweb中的docs,这时我们需要调用report_sxw的__init__方法来添加新属性,代码如下(忽略menu):

# -*- coding: utf-8 -*-
##############################################################################
#
#
##############################################################################

from odoo.report import report_sxw
from odoo import models,api


class sale_delivery_py(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(sale_delivery_py, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'get_product_name':self.get_product_name,
        })
        self.context = context

    #获取产品的属性值
    @api.multi
    def get_product_name(self, para):
        prodt=self.objects.env['product.product'].search([('id','=',para)])
        name = prodt.name+'('
        for index,tt in enumerate(prodt.attribute_value_ids):
            if index<len(prodt.attribute_value_ids)-1:
                name=name+tt.name+','
            else:
                name=name+tt.name+')'
        return name



class report_sale_delivery(models.AbstractModel):
    _name = 'report.sale_delivery_print.report_sale_delivery'
    _inherit = 'report.abstract_report'
    _template = 'sale_delivery_print.report_sale_delivery'
    _wrapped_report_class = sale_delivery_py

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

调用的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="report_sale_delivery">
        <t t-call="report.html_container">
            <div style="font-size:9px;" t-if="docs.state in ('done','assigned','partially_available')" class="page">
                <div class="row">
                    <div class="col-xs-12 text-center">
                        <strong>
                            <h4 t-esc="docs.picking_type_id.name">单</h4>
                        </strong>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-4">
                        <strong>出货单据号:</strong>
                        <p t-esc="docs.origin"/>
                    </div>
                </div>
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>行号</th>
                            <th>销售订单号</th>
                            <th>客户订单号</th>
                            <th>产品名称</th>
                            <th>客户产品编号</th>
                        </tr>
                    </thead>
                    <t t-set="i" t-value="1"/>
                    <t t-set="m" t-value="1"/>
                    <t t-set="fzz" t-value="0"/>
                    <tbody>
                        <tr style="page-break-inside : avoid" t-foreach="docs.pack_operation_product_ids"
                            t-as="product">
                            <t t-set="fzx" t-value="0"/>
                            <tr style="page-break-inside : avoid" t-foreach="product.pack_lot_ids" t-as="lot">
                                <td>
                                    <span t-esc="i"/>
                                    <t t-set="i" t-value="i+1"/>
                                </td>
                                <t t-set="m" t-value="lot_size"/>
                                <td>
                                    <span t-esc="product.linked_move_operation_ids[0].move_id.procurement_id.sale_line_id.order_id.name"/>
                                </td>
                                <td>
                                    <span t-esc="docs.client_order_ref"/>
                                </td>
                                <td>
                                    <span t-esc="get_product_name(product.product_id.id)"/><!--自定义属性的调用-->
                                </td>
                                <td>
                    </tbody>
                </table>
            </div>
            <div class="footer">
                <div class="text-right">
                    <small>
                        <span>第</span>
                        <span class="page"/>
                        <span>页 共</span>
                        <span class="topage"/>
                        <span>页</span>
                    </small>
                </div>
            </div>
            <div t-if="docs.state not in ('done','assigned','partially_available')" class="page">
                <h3 class="text-center">请打印可用、部分可用、完成单据</h3>
            </div>
        </t>
    </template>
</odoo>

xml调用:

3.有些场景需要数据重构,详情请见文章:odoo自定义报表
https://blog.csdn.net/chasenksky/article/details/79375612

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值