Help

How to programatically run a report and add it as an attachment?

Closed
Avatar
Julian Solorzano

The question has been closed

by
Serpent Consulting Services Pvt. Ltd.
on 11/14/2013 02:12:00

Hello, I know I can run a report by returning an ir.actions.report.xml action from a method. Like this:

            datas = {
                'ids': context.get('active_ids',[]),
                'model': some model
                'form': data
            }
            return {
                'type': 'ir.actions.report.xml',
                'report_name': some report
                'datas': datas,
            }

But how do I specify that I want an attachment to be created, as well as the attachment name?

I knowI shouldn't have the need to do this in code. The report should run and attach by itself, but for some reason the "attachment" field and the "use attachment" chekbox are not working for custom reports. So, is there some way to do the attachment thing manually (for example, getting the generated binary file using the API)?

Thanks.

Avatar
Discard
2 Answers
1
Best Answer

Hello Julian Solorzano,

You may make take reference of following code.

The following code generates the report and creates the attachment.

def cover_print(self, cr, uid, ids, context=None):
    attachment_obj = self.pool.get('ir.attachment')
    for record in self.browse(cr, uid, ids, context=context):
        ir_actions_report = self.pool.get('ir.actions.report.xml')
        matching_reports = ir_actions_report.search(cr, uid, [('name','=','report_service_name_given_in_xml')])
        if matching_reports:
            report = ir_actions_report.browse(cr, uid, matching_reports[0])
            report_service = 'report.' + report.report_name
            service = netsvc.LocalService(report_service)
            (result, format) = service.create(cr, uid, [record.id], {'model': self._name}, context=context)
            eval_context = {'time': time, 'object': record}
            if not report.attachment or not eval(report.attachment, eval_context):
                # no auto-saving of report as attachment, need to do it manually
                result = base64.b64encode(result)
                file_name = re.sub(r'[^a-zA-Z0-9_-]', '_', 'Your Report Name')
                file_name += ".pdf"
                attachment_id = attachment_obj.create(cr, uid,
                    {
                        'name': file_name,
                        'datas': result,
                        'datas_fname': file_name,
                        'res_model': self._name,
                        'res_id': record.id,
                        'type': 'binary'
                    }, context=context)
    return True

Hope this will help you.

Thanks,

Serpent Consulting Services.

4 Comments
Avatar
Discard
Avatar
Julian Solorzano
-

Perfect, thanks

Avatar
Sayed Anisul Hoque
-

matching_reports = ir_actions_report.search(cr, uid, [('name','=','report_service_name_given_in_xml')]) --> this returning as "matching_reports::: []" can you please tell why empty list is created ?? moreover can you please tell what is report_service_name_given_in_xml ?

Avatar
Sayed Anisul Hoque
-

report_service = 'report.' + report.report_name service = netsvc.LocalService(report_service) (result, format) = service.create(cr, uid, [record.id], {'model': self._name}, context=context) Above line was giving me error since the openerp.netsvc.LocalService() function is deprecated. This could be solved as per below editing result, format = openerp.report.render_report(cr, uid, [record.id], report.report_name, {'model': self._name}, context=context)

Avatar
Sebin Siby
-

From where I have to call this function?

I have a wizard for the report to be generated.

1
Avatar
Navrang Oza
Best Answer

Hi,

I think Invoices report is the best example of attaching a generated report in OpenERP interface.
You can get it from here.

<report
    auto="False"
    id="account_invoices"
    model="account.invoice"
    name="account.invoice"
    rml="account/report/account_print_invoice.rml"
    string="Invoices"
    attachment="(object.state in ('open','paid')) and ('INV'+(object.number or '').replace('/','')+'.pdf')"
    attachment_use="True"
    usage="default"
    />

To save report as attachment attachment and attachment_use are compulsory or you can manually add your generated report to attachment by following steps :

  1. Generate report and save in your file system.
  2. Then go to the form view of the record and click on Attachment(s) on top and add there. Below is the screenshot.
  3. You can see all your attachments which are saved in OpenERP in Knowledge -> Documents -> Documents.

image description

Avatar
Discard