Odoo

报表运行机制

  1. OpenERP 报表的基本运行机制 OpenERP 报表的一般定义语法是:
<report id="c2c_demo_report_x" string="C2C Demo Report" model="hr.holidays" name="sandbox_c2c_reporting_tools" auto="False" header="False"/>

这个定义的含义是,在对象hr.holidays 上增加报表操作(model="hr.holidays"),该报表操作的 显示字符是 C2C Demo Report(string="C2C Demo Report"),当用户点击该操作字符(C2C Demo Report),系统调用名为 sandbox_c2c_reporting_tools(name="sandbox_c2c_reporting_tools")的 Services,该 Services 返回报表文件(PDF 或其他格式文件)。 因此,理解 OpenERP 报表机制的核心是,理解报表 Services 机制。

  1. OpenERP 的报表 Service OpenERP 的报表 Service 的基本接口定义在文件:openerp-server-6.0.3\bin\report\interface.py, 期定义如下:
report_int(netsvc.Service)
__init__(self, name, audience='*')
create(self, cr, uid, ids, datas, context=None)

init 方法中最重要的参数是 name,该参数是 Service Name,其格式是"report.xxx", xxx 必须和报 表定义时候的(name="sandbox_c2c_reporting_tools")一致,系统是通过该名字找到该 Service。

create 方法中,最重要的参数是 ids,该参数是报表操作所在的画面上,选定的对象的 id 列表。 通常,系统会为 ids 中的每一个对象出一个报表。datas 参数通常用于 Wizard 的情况,即先弹出 Wizard 画面,用户输入一些数据,点击按钮,系统再输出报表文件。在这种情况,datas 参数里保存 着用户在 Wizard 画面上输入的数据。

显然,系统的内部动作是,用户点击报表动作,系统根据 name="sandbox_c2c_reporting_tools" 找到相应 Service,调用 Service 的 Create 方法,返回报表文件。Create 方法的返回值格式是: (report_doc,mimetype)。例如,如果返回 pdf 报表,返回值是(pdf_doc,'pdf')。

  1. RML 报表 如果直接继承接口 report_int,编写 create 方法生成 pdf 文档,代码复杂,工作量大。系统提供 了 RML 格式报表,用于简化 pdf 报表开发。其基本原理是,开发 RML 格式文档,系统的 Create 方 法读取 rml 文件,渲染成 pdf 文档,输出。相关接口如下:
report_rml(report_int)

__init__(self, name, table, tmpl, xsl)

create(self, cr, uid, ids, datas, context)
report_sxw(report_rml)
__init__(self, name, table, rml=False, parser=rml_parse, header='external', store=False)

create(self, cr, uid, ids, data, context=None)

这两个派生 Class 中,create 方法的参数没有变化,init 方法增加了一些参数,说明如下:

  • table:报表关联的数据对象,渲染rml时候需要调用该对象取得数据。
  • rml:RML文件路径及名称,系统需要读取该文件渲染成PDF报表。
  • parser:渲染器,系统的实际做法是,在 create 方法中调用渲染器的有关方法,将 rml 渲染 成 pdf。 用户可以开发自己的渲染器,用于将 rml 渲染成其他格式,如 html、txt 等,实际上,系统已经 提供了 html、txt 等的渲染器。因此,开发 rml 格式的报表时候,通常只需要开发自己的渲染器 (parser),不需要开发 report_int。