Odoo中从动作按钮打印报表(康虎云报表)

admin 2019-9-4 6049



在odoo中,有时候需要从Form视图界面任意位置的按钮打印报表,本教程我们通过一个小模块来演示这种方式(Odoo设置为多公司模式)。

1.) 我们首先创建一个模块“cf_invoice_reports”,然后在模块的models目录中创建文件invoice_template.py。我们在invoice_template.py中继承account.invoice模型,增加一个“sub_company_id”字段来记录公司ID。代码如下:

from odoo import fields, models, api, _
from datetime import timedelta
from odoo.exceptions import UserError, RedirectWarning, ValidationError
class ResCompany(models.Model):
    _inherit = "account.invoice"
 
    #get company id
    sub_company_id = fields.Char(string='Company',default=lambda self: self.env['res.company']._company_default_get('account.invoice').id)

2). 我们再创建一个报表模板文件 invoice_custom_template.xml,ID设置为“custom_template_1”。报表名称中的“cf_invoice_reports”是模块名称,“report_invoice_temp1”是报表模板ID。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
    <template id="report_invoice_temp1">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="web.external_layout">
                <div class="page">
<!-- You can add your report contetnt here -->
<h2>Report title</h2>
                    <p>Report 01</p>
                </div>
            </t>
        </t>
    </t>
    </template>
           <report
                id="custom_template_1"
                model="account.invoice"
                string="Tax Invoice"
                report_type="qweb-pdf"
                name="cf_invoice_reports.report_invoice_temp1"
                file="cf_invoice_reports.report_invoice_temp1"
                menu="False"
        />
    </data>
</odoo>


3). 然后再继承account.invoice_form视图,增加一个打印按钮。创建文件“custom_invoice_view.xml”并使用xpath表达式在“action_invoice_sent”按钮后面增加一个新按钮。用报表ID作为按钮的“name”属性,使用报表ID作为按钮“name”可以直接在按钮点击时调用报表。然后给按钮增加一个“invisible”属性,当公司ID不等于4时,按钮不可见。

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="account_invoice_form_inherit_view" model="ir.ui.view">
            <field name="name">account.invoice.template.inherit</field>
            <field name="model">account.invoice</field>
            <field name="inherit_id" ref="account.invoice_form"/>
            <field name="arch" type="xml">
                <xpath expr="//button[@name='action_invoice_sent']" position="after">
                    <button name="%(custom_template_1)d" string="Click Me" type="action" icon="gtk-print" attrs="{'invisible': [('sub_company_id','!=','4')]}"/>
                </xpath>
            </field>
        </record>
    </data>
</odoo>


4). 总结,使用动作按钮可以方便调用报表且可以根据domain来设置按钮可见或隐藏。


======================

另一种方法:

--------------------------------------

1、定义报表:

<report 
  id="action_products_consume_report" 
  string="Products" 
  model="mrp.production.products" 
  report_type="qweb-pdf" 
  file="mrp.products_consume_document" 
  name="mrp.products_consume_document" />



2、定义按钮:

<button name="open_report_consume" type="object" string="Report Consume"/>


3、编写按钮动作代码:

def open_report_consume(self, cr, uid, ids, context=None):
  if ids:
    if not isinstance(ids, list):
      ids = [ids]
    context = dict(context or {}, active_ids=ids, active_model=self._name)
  
  return {
    'type': 'ir.actions.report.xml',
    'report_name': 'mrp.products_consume_document',  #这个值要跟报表定义中的name相同
    'context': context,
  }


最新回复 (0)
返回