(06)odoo报表

----------
更新时间:
18:06 2016-09-18 星期日
18:13 2016-04-05 星期二
10:31 2016-03-01 星期二
----------
* odoo8 采用是Qweb报表,废弃了7.0中的RML报表
       
* 创建业务报表
    报表是通过Qweb的技术来建立的
    #一般报表结构如下:
     purchase
       ├ views
       │  ├ report_purchaseorder.xml (报表模板)
       │  ├ report_purchasequotaion.xml
       ├ purchase_report.xml (模块中报表定义 模块名_report.xml)   
      
     当要建立报表时,就按这样的结构来创建,   
    
    #注意,报表,用到了wkhtmltopdf依赖
      http://wkhtmltopdf.org/downloads.html   wkhtmltopdf-0.12.1 版本
      $ sudo apt-get remove --purge wkhtmltopdf
      $ cd /tmp
      $ wget http://downloads.sourceforge.net/project/wkhtmltopdf/archive/0.12.1/
              wkhtmltox-0.12.1_linux-trusty-adm64.deb
             
      $ sudo dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb
 
    #报表定义purchase_report.xml:(注册报表)
      <?xml version="1.0" encoding="utf-8"?>
        <openerp>
            <data>
                <report
                    string="Purchase Order"
                    id="action_report_purchase_order"
                    model="purchase.order"
                    report_type="qweb-pdf"
                    name="purchase.report_purchaseorder"
                    file="purchase.report_purchaseorder"
                />

<report
                    string="Request for Quotation"
                    id="report_purchase_quotation"
                    model="purchase.order"
                    report_type="qweb-pdf"
                    name="purchase.report_purchasequotation"
                    file="purchase.report_purchasequotation"
                />
            </data>
        </openerp>
        @string: 在打印按钮那里的显示名称
        @id:外id标识
        @name: 完整主模板名称 模块名.主板模名称  用于管理和调用
        @file: 完整模板文件名 模块名.模板文件名  用于更好地关联模板
        @model:相关的模型显示在那个模块进行打印报表 通常在各视图中的打印那里会出现
         上面表示在采购订单相关视图时,会出现“打印-询价单/采购订单”
        @report_type: 报表类型 是 qweb-pdf 或 qweb-html
        @report_name: 打印出来的文件名
        @groups_id:指定权限
        --------
        attachment_use="True"
        attachment="(object.state in ('open','paid')) and
        ('INV'+(object.number or '').replace('/','')+'.pdf')"
        @attachment_use:使用附件,这样不会一直查数据库,只能当数据变化时才会查数据库
        @attachment:附件的名称,当我们下载下来的文件名
        ------
             
        <report> 是 ir.actions.report.xml 模型的快捷方式
       
   
        <record id="action_report_purchase_order" model="ir.actions.report.xml">
                <field name="name">Purchase Order</field>
                <field name="model">purchase.order</field>
                <field name="report_type">qweb-pdf</field>
                <field name="template_name">
                    purchase.report_purchaseorder
                </field>
                <field name="paper_format_id" ref="report.paperformat_euro"/>
                #指定的纸张 可以到 addons/report/data/report_paperformat.xml 查看可用纸张规格
        </record>
        报表可以在 设置->技术->报表 可以查到
         t-call 调用的标准报告架构,这个很重要
         report.external_layout 包含头部和尾部 ,也可以只用 report.internal_layout 只用
         基础架构的头部,其它部分自己写
        
    # 报表模板 report_purchaseorder.xml
        # /addons/purchase/views/report_purchase.xml:
        <?xml version="1.0" encoding="utf-8"?>
            <openerp>
            <data>
            <template id="report_purchaseorder_document">
                <t t-call="report.external_layout"> <!--套上头尾的模板布局-->
                    <div class="page">
                        <div class="oe_structure"/>   
                         <div class="row">
                            <div class="col-xs-6"> <!--bootstrap布局方式-->
                                <strong>Shipping address:</strong>
                                <div t-if="o.dest_address_id">
                                    <div t-field="o.dest_address_id"
                                        t-field-options='{"widget": "contact", "fields": ["address", "name", "phone", "fax"], "no_marker": true}'/>
                                </div>

<div t-if="not o.dest_address_id and o.picking_type_id and o.picking_type_id.warehouse_id">
                                    <span t-field="o.picking_type_id.warehouse_id.name"/>
                                    <div t-field="o.picking_type_id.warehouse_id.partner_id"
                                        t-field-options='{"widget": "contact", "fields": ["address", "phone", "fax"], "no_marker": true}'/>
                                </div>
                            </div>
                            <div class="col-xs-5 col-xs-offset-1">
                                <div t-field="o.partner_id"
                                    t-field-options='{"widget": "contact", "fields": ["address", "name", "phone", "fax"], "no_marker": true}'/>
                                    <p t-if="o.partner_id.vat">VAT: <span t-field="o.partner_id.vat"/></p>
                            </div>
                        </div>                       
                         ...
                        <div class="oe_structure"/>
                    </div>
                </t>
            </template>

<template id="report_purchaseorder">
                <t t-call="report.html_container">
                    <t t-foreach="doc_ids" t-as="doc_id">
                        <!--采用合作伙伴设定的语言来翻译报表-->
                        <t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', 'purchase.report_purchaseorder_document')"/>
                    </t>
                </t>
            </template>
            </data>
            </openerp>
           
            来一段没有翻译的模板
            <template id="report_invoice">
                <t t-call="report.html_container">
                    <t t-foreach="docs" t-as="o">
                        <t t-call="report.external_layout">
                            <div class="page">
                                <h2>Report title</h2>
                                <p>This object's name is <span t-field="o.name"/></p>
                            </div>
                        </t>
                    </t>
                </t>
            </template>
           
        @docs 当前报表记录集
        @doc_ids 报表记录集的ids列表
        @doc_model 报表录集对应的模型
        @translate_doc 翻译报表
        <t t-foreach="doc_ids" t-as="doc_id">
          <t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', account.report_invoice_document')"/>
        </t>
        @user 打印报表的人
        @res_company 当前用户的公司
           
         @调用了report.external_layout 模板 所在的布局文件在 /addons/report/views/layouts.xml
         @注意translate_doc 中的 purchase.report_purchaseorder_document ,这个要报表视图id前加模块名
          这个名称和模板的id名称要一致
         @模板名称 report_purchaseorder_documen  和模板名称 report_purchaseorder
          这样在报表定义时 purchase.report_purchaseorder 就可以关联到定义的模板
         @ 大家可能注意到了o 对于o是代表当前报表模型数据 在/addons/report/models/report.xml
           translate_doc 这个方法
           def translate_doc(self, cr, uid, doc_id, model, lang_field, template, values, context=None):
                ctx = context.copy()
                doc = self.pool[model].browse(cr, uid, doc_id, context=ctx)
                qcontext = values.copy()
                # Do not force-translate if we chose to display the report in a specific lang
                if ctx.get('translatable') is True:
                    qcontext['o'] = doc
                else:
                    # Reach the lang we want to translate the doc into
                    ctx['lang'] = eval('doc.%s' % lang_field, {'doc': doc})
                    qcontext['o'] = self.pool[model].browse(cr, uid, doc_id, context=ctx)
                return self.pool['ir.ui.view'].render(cr, uid, template, qcontext, context=ctx)
            看到o就是报表中的的对象,若选择多个就是objects ,单个用o
            报表中除了html的布局,剩下的就是用qweb语言来写的,和python类似。

#在报表中呈现数据   
    可用的变量:
      docs 循环打印出一个记录集
      doc_ids 一个记录集的ids列表打印出来
      doc_model 调用哪个模型来处理
      time 时间
      user 本次执行报告的用户
      res_company  当前用户所在的公司
    
     
      字段值用 t-field 属性 还可以用其补充属性 t-field-options
      上一道菜:
       <h2 t-field="o.name"/>
            <p t-field="o.user_id.name"/>
            <ul>
            <t t-foreach="o.message_follower_ids" t-as="f">
                <li>
                    <img t-if="f.image_small"
                         t-att-src="'data:image/png;base64,%s' % f.image_small"
                         style="max-height: 45px;"/>
                    <span t-field="f.name"/>
                </li>
            </t>
        </ul>
        有货币字段:
         <span t-field="o.amount"
            t-field-options='{
            "widget": "monetary",
            "display_currency": "o.pricelist_id.currency_id"}'/>

<div t-field="res_company.partner_id"
            t-field-options='{
            "widget": "contact",
            "fields": ["address", "name", "phone", "fax"],
            "no_marker": true}' />
           
         支持语言翻译
         <template id="report_todo_task_template">
            <t t-call="report.html_container">
                <t t-foreach="doc_ids" t-as="doc_id">
                <t t-raw="translate_doc(doc_id, doc_model,
                    'user_id.partner_id.lang',
                    'todo_kanban.report_todo_task_doc')"/>
                </t>
            </t>
         </template>
         translate_doc
        
         
            注:col-xs-N  N代表多少列 像bootstrap写法,一行划成12列
                <t t-if="f_last"> 循环到了最后
               
               
         #纸张格式

<record id="report_todo_task_action" model="ir.actions.report.xml">
                <field name="name">To-do Tasks</field>
                <field name="model">todo.task</field>
                <field name="report_type">qweb-html</field>
                <field name="template_name">
                    todo_kanban.report_todo_task_template
                </field>
                <field name="paper_format_id" ref="report.paperformat_euro"/>
            </record>        
            paper_format_id 引用了 report.paperformat_euro 这个格式,这个值,可以在
            设置->技术->报告->纸张格式
           
            定义纸张格式
            <record id="paperformat_frenchcheck" model="report.paperformat">
                <field name="name">French Bank Check</field>
                <field name="default" eval="True"/>
                <field name="format">custom</field>
                <field name="page_height">80</field>
                <field name="page_width">175</field>
                <field name="orientation">Portrait</field>
                <field name="margin_top">3</field>
                <field name="margin_bottom">3</field>
                <field name="margin_left">3</field>
                <field name="margin_right">3</field>
                <field name="header_line" eval="False"/>
                <field name="header_spacing">3</field>
                <field name="dpi">80</field>
            </record>
           
           
           
        #模板加入模块中(__openerp__.py)
       'data': [
       
        'views/report_purchaseorder.xml',
        'views/report_purchasequotation.xml',
        ],

#加入条件码:
        <img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>
        <img t-att-src="'/report/barcode/?
            type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>
           
        # 报表加入css
        <template id="report_saleorder_style" inherit_id="report.layout">
          <xpath expr="//style" position="after">
            <style type="text/css">
              .example-css-class {
                background-color: red;
              }
            </style>
          </xpath>
        </template>
       
    #自定义报表
      默认有 get_html方法来调用报表传递数据,这时,我们只要定义模型时,重写这个方法就
      可以完成自定义报表输出
        from openerp import api, models

class ParticularReport(models.AbstractModel):
        _name = 'report.module.report_name'
        @api.multi
        def render_html(self, data=None):
            report_obj = self.env['report']
            report = report_obj._get_report_from_name('module.report_name')
            docargs = {
                'doc_ids': self._ids,
                'doc_model': report.model,
                'docs': self,
            }
            return report_obj.render('module.report_name', docargs)

上一篇:springMVC中利用model在JSTL进行回填值


下一篇:C#秘密武器之扩展方法