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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章