Odoo Help


How to auto attach PDF to e-mail when inheriting function?

on 7/14/15, 8:10 AM 1,769 views

Hi guys,

I want to automatically attach my custom report when the user clicks on 'Send by Email' (function action_quotation_send). The first thing that I did is create a custom report. Here is the important code from it:

 <?xml version="1.0" encoding="utf-8"?>
<template id="report_saleorder_document_expand">
<!--My report code -->

<template id="report_saleorder_expand">
    <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', 'aa_maatwerk_u_sentric.report_saleorder_document_expand')"/>

I then created a new report link in XML:

 <?xml version="1.0" encoding="utf-8"?>
            string="Offerte / Order gedetailleerd"

I then created an e-mail template, which is almost identical to the default e-mail that is sent. I've only changed the ref and the name here:

 <!--Email template -->
        <record id="email_template_edi_sale_expand" model="email.template">
            <field name="name">Advanced Sales Order - Send by Email</field>
            <field name="email_from">${(object.user_id.email or '')|safe}</field>
            <field name="subject">${object.company_id.name|safe} ${object.state in ('draft', 'sent') and 'Quotation' or 'Order'} (Ref ${object.name or 'n/a' })</field>
            <field name="partner_to">${object.partner_invoice_id.id}</field>
            <field name="model_id" ref="sale.model_sale_order"/>
            <field name="auto_delete" eval="True"/>
            <field name="report_template" ref="report_sale_order_expand"/>
            <field name="report_name">${(object.name or '').replace('/','_')}_${object.state == 'draft' and 'draft' or ''}</field>
            <field name="lang">${object.partner_id.lang}</field>
            <field name="body_html"><![CDATA[
<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
    <p>Hello ${object.partner_id.name},</p>
    <p>Here is your ${object.state in ('draft', 'sent') and 'quotation' or 'order confirmation'} from ${object.company_id.name}: </p>
    <p style="border-left: 1px solid #8e0000; margin-left: 30px;">
       &nbsp;&nbsp;<strong>REFERENCES</strong><br />
       &nbsp;&nbsp;Order number: <strong>${object.name}</strong><br />
       &nbsp;&nbsp;Order total: <strong>${object.amount_total} ${object.pricelist_id.currency_id.name}</strong><br />
       &nbsp;&nbsp;Order date:  ${format_tz(object.date_order, tz=user.tz, context={'lang':object.partner_id.lang})} <br />
       % if object.origin:
       &nbsp;&nbsp;Order reference: ${object.origin}<br />
       % endif
       % if object.client_order_ref:
       &nbsp;&nbsp;Your reference: ${object.client_order_ref}<br />
       % endif
       % if object.user_id:
       &nbsp;&nbsp;Your contact: <a href="mailto:${object.user_id.email or ''}?subject=Order ${object.name}">${object.user_id.name}</a>
       % endif
    % if object.paypal_url:
    <p>It is also possible to directly pay with Paypal:</p>
        <a style="margin-left: 120px;" href="${object.paypal_url}">
            <img class="oe_edi_paypal_button" src="/sale/static/img/btn_paynowcc_lg.gif"/>
    % endif
    <p>If you have any question, do not hesitate to contact us.</p>
    <p>Thank you for choosing ${object.company_id.name or 'us'}!</p>
    <div style="width: 375px; margin: 0px; padding: 0px; background-color: #8E0000; border-top-left-radius: 5px 5px; border-top-right-radius: 5px 5px; background-repeat: repeat no-repeat;">
        <h3 style="margin: 0px; padding: 2px 14px; font-size: 12px; color: #DDD;">
            <strong style="text-transform:uppercase;">${object.company_id.name}</strong></h3>
    <div style="width: 347px; margin: 0px; padding: 5px 14px; line-height: 16px; background-color: #F2F2F2;">
        <span style="color: #222; margin-bottom: 5px; display: block; ">
        % if object.company_id.street:
        % endif
        % if object.company_id.street2:
        % endif
        % if object.company_id.city or object.company_id.zip:
            ${object.company_id.zip} ${object.company_id.city}<br/>
        % endif
        % if object.company_id.country_id:
            ${object.company_id.state_id and ('%s, ' % object.company_id.state_id.name) or ''} ${object.company_id.country_id.name or ''}<br/>
        % endif
        % if object.company_id.phone:
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
                Phone:&nbsp; ${object.company_id.phone}
        % endif
        % if object.company_id.website:
                Web :&nbsp;<a href="${object.company_id.website}">${object.company_id.website}</a>

And eventually I inherited the function action_quotation_send to modify it to my needs:

 class sale_order(osv.osv):
    _inherit = 'sale.order'
    def action_quotation_send(self, cr, uid, ids, context=None):
        This function opens a window to compose an email, with the edi sale template message loaded by default
        assert len(ids) == 1, 'This option should only be used for a single id at a time.'
        ir_model_data = self.pool.get('ir.model.data')
            template_id = ir_model_data.get_object_reference(cr, uid, 'aa_maatwerk_u_sentric', 'email_template_edi_sale_expand')[1]
        except ValueError:
            template_id = False
            compose_form_id = ir_model_data.get_object_reference(cr, uid, 'mail', 'email_compose_message_wizard_form')[1]
        except ValueError:
            compose_form_id = False 
        ctx = dict()
            'default_model': 'sale.order',
            'default_res_id': ids[0],
            'default_use_template': bool(template_id),
            'default_template_id': template_id,
            'default_composition_mode': 'comment',
            'mark_so_as_sent': True
        return {
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form_id, 'form')],
            'view_id': compose_form_id,
            'target': 'new',
            'context': ctx,

But when I want to install the module I will get the following error:

 File "/odoo/odoo-server/openerp/addons/base/ir/ir_model.py", line 908, in xmlid_lookup
    raise ValueError('External ID not found in the system: %s' % (xmlid))
ParseError: "External ID not found in the system: aa_maatwerk_u_sentric.report_sale_order_expand" while parsing /odoo/odoo-server/addons/aa_maatwerk_u_sentric/templates.xml:432

So what exactly am I missing or referencing incorrect? I don't see what is wrong..



Might be there is an issue inside your __openerp__.py / 'data' : ['email_template.xml' , 'report.xml']. If email_template is before report file in the given list then system raise this error.

I've converted your comment to an answer since this was exactly the problem! This really makes sense since the module doesn't know about it yet at that point. Thanks a lot Emipro! Accepted and upvoted, enjoy that karma. :)

on 7/15/15, 2:05 AM

At the end, your problem is solved, that matters !

Emipro Technologies Pvt. Ltd.
on 7/15/15, 2:06 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/14/15, 8:10 AM
Seen: 1769 times
Last updated: 9/17/15, 2:59 PM