From 9478019191cdc32bbde8d6557dab40964975ebd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 24 Jan 2014 11:54:06 +0100 Subject: [PATCH] [IMP] account_product_template: cleaned code of email sending. Now based on a composer, trigerring the onchange with the template, then sending the email. This replicates the email sending with the composer using a template. TODO: fix attachments not being send bzr revid: tde@openerp.com-20140124105406-09gndxy3izhdyq2n --- .../models/invoice.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/addons/account_product_template/models/invoice.py b/addons/account_product_template/models/invoice.py index 9a186820a2c..7249e4e61ec 100644 --- a/addons/account_product_template/models/invoice.py +++ b/addons/account_product_template/models/invoice.py @@ -7,8 +7,7 @@ class account_invoice(osv.Model): _inherit = 'account.invoice' def invoice_validate_send_email(self, cr, uid, ids, context=None): - mail_msg_obj = self.pool['mail.compose.message'] - template_obj = self.pool['email.template'] + Composer = self.pool['mail.compose.message'] for invoice in self.browse(cr, uid, ids, context=context): # send template only on customer invoice if invoice.type != 'out_invoice': @@ -18,19 +17,21 @@ class account_invoice(osv.Model): self.message_subscribe(cr, uid, [invoice.id], [invoice.partner_id.id], context=context) for line in invoice.invoice_line: if line.product_id.email_template_id: - template_res = template_obj.get_email_template_batch(cr, uid, template_id=line.product_id.email_template_id.id, res_ids=[line.product_id.product_tmpl_id.id], context=context) - mail = template_res[line.product_id.product_tmpl_id.id] - message_wiz_id = mail_msg_obj.create(cr, uid, { + # CLEANME: should define and use a clean API: message_post with a template + composer_id = Composer.create(cr, uid, { 'model': 'account.invoice', 'res_id': invoice.id, - 'body': mail.body_html, + 'template_id': line.product_id.email_template_id.id, + 'composition_mode': 'comment', }, context=context) - mail_msg_obj.send_mail(cr, uid, [message_wiz_id], context=context) + Composer.write( + cr, uid, [composer_id], + Composer.onchange_template_id(cr, uid, composer_id, line.product_id.email_template_id.id, 'comment', 'account.invoice', invoice.id)['value'] + ) + Composer.send_mail(cr, uid, [composer_id], context=context) return True def invoice_validate(self, cr, uid, ids, context=None): - if context is None: - context = {} res = super(account_invoice, self).invoice_validate(cr, uid, ids, context=context) self.invoice_validate_send_email(cr, uid, ids, context=context) return res