Nella v8,
al fine di inviare, dalla scheda del contratto, al cliente una email con il contratto stesso,
ho preparato un modello di email, un button ed un metodo.
Il modello di email
<record id="contract_email_template" model="email.template">
<field name="name">Contract sent by e-mail template</field>
<field name="email_from">${object.company_id and object.company_id.email or ''}</field>
<field name="subject">Your contract ${object.name}</field>
<field name="email_to">${object.email|safe}</field>
<field name="lang">${object.lang}</field>
<field name="model_id" ref="analytic.model_account_analytic_account"/>
<field name="auto_delete" eval="True"/>
<field name="body_html">
<![CDATA[
Hi ${(object.partner_id.name)}, <br/><br/>
here's your contract.
Enjoy.
<br/>
${(object.company_id.name)}
]]>
</field>
</record>
un button
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_account_analytic_send_email_with_contract" model="ir.ui.view">
<field name="name">analytic.analytic.account.form.send_contract</field>
<field name="model">account.analytic.account</field>
<field name="inherit_id" ref="analytic.view_account_analytic_account_form"/>
<field name="arch" type="xml">
<xpath expr="//form/sheet/div" position="inside">
<button name="send_email_with_contract" type="object" string="Sent the contract" class="oe_highlight"/>
</xpath>
</field>
</record>
</data>
</openerp>
un metodo
# -*- coding: utf-8 -*-
from openerp import models, fields, api
class res_partner(models.Model):
_inherit = 'account.analytic.account'
@api.multi
def send_email_with_contract(self):
'''
This function opens a window to compose an email, with the template message loaded by default
'''
self.ensure_one()
ir_model_data = self.env['ir.model.data']
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
# In Odoo 9 'mail.template'
template_id = self.env['email.template'].search([('name', '=', 'Contract send by e-mail template')], limit=1)
# template_id = self.env['email.template'].search([('name', '=', 'Invoice - Send by Email')], limit=1)
ctx = dict()
ctx.update({
'default_model': 'res.partner',
'default_res_id': False,
'default_use_template': True,
'default_template_id': template_id.id or False,
'default_composition_mode': 'comment',
'skip_notification': 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,
}
Ora, di tutto questo codice
il problema pare stare in
template_id = self.env.
search([('name', '=', 'Contract send by e-mail template')], limit=1)
perché
non trova il template dell'email, ed apre una schermata per l'invio, ma vuota.
Il mio template Contract send by e-mail template lo trovo fra i modelli possibili, in odoo, quindi, non capisco perché non lo trovi...
Andrea