0
votes

Odoo has the Reporting part and I was wondering how (if its even possible) to send reports via mail (and create an automated action for it)?

Let's say we have to crm.phonecall.report model, how to take it's information and use it in an email template? I tried making an email template with that model and then add the same xml text as it is in the Phonecall Analysis, but that didn't work. So all the help is really appreciated.

1

1 Answers

0
votes

Following code will be enough to fulfill your mentioned requirements. It calls the email template and then attached a report attachment and finally send an email.

email = email_obj.browse(cr, uid, template_id)
attachment_obj = self.pool.get('ir.attachment')
ir_actions_report = self.pool.get('ir.actions.report.xml')

matching_reports = ir_actions_report.search(
    cr, uid, [('name', '=', 'Report_Name_here')])

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, mrp_ids, {'model': self._name, 'start_date': start, 'end_date': end}, context=context)

    if not report.attachment:
        file_name = "Production Sale Report " + datetime.strftime(datetime.now().date(), "%Y-%m-%d") + ".pdf"
        attachment_id = attachment_obj.create(cr, uid,
                                              {
                                                  'name': file_name,
                                                  'datas': result,
                                                  'datas_fname': file_name,
                                                  'type': 'binary'
                                              }, context=context)

    email_obj.write(cr, uid, template_id, {'email_from': email.email_from,
                                           'email_to': email.email_to,
                                           'subject': email.subject,
                                           'body_html': email.body_html,
                                           'email_recipients': email.email_recipients,
                                           'attachment_ids': [(6, 0, [attachment_id])],
                                           })

    email_obj.send_mail(cr, uid, template_id, False, True, context=context)

Hope this will solve your problem. Cheers