2014-01-13 13:18:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-01-24 09:50:56 +00:00
|
|
|
from openerp.osv import osv
|
|
|
|
|
2014-01-13 13:18:45 +00:00
|
|
|
|
|
|
|
class account_invoice(osv.Model):
|
|
|
|
_inherit = 'account.invoice'
|
|
|
|
|
2014-01-24 10:22:01 +00:00
|
|
|
def invoice_validate_send_email(self, cr, uid, ids, context=None):
|
2014-01-24 10:54:06 +00:00
|
|
|
Composer = self.pool['mail.compose.message']
|
2014-01-23 13:42:33 +00:00
|
|
|
for invoice in self.browse(cr, uid, ids, context=context):
|
2014-01-24 06:09:45 +00:00
|
|
|
# send template only on customer invoice
|
|
|
|
if invoice.type != 'out_invoice':
|
|
|
|
continue
|
|
|
|
# subscribe the partner to the invoice
|
2014-01-23 11:25:05 +00:00
|
|
|
if invoice.partner_id.id not in invoice.message_follower_ids:
|
|
|
|
self.message_subscribe(cr, uid, [invoice.id], [invoice.partner_id.id], context=context)
|
2014-01-23 13:42:33 +00:00
|
|
|
for line in invoice.invoice_line:
|
|
|
|
if line.product_id.email_template_id:
|
2014-01-24 10:54:06 +00:00
|
|
|
# CLEANME: should define and use a clean API: message_post with a template
|
|
|
|
composer_id = Composer.create(cr, uid, {
|
2014-01-23 13:42:33 +00:00
|
|
|
'model': 'account.invoice',
|
|
|
|
'res_id': invoice.id,
|
2014-01-24 10:54:06 +00:00
|
|
|
'template_id': line.product_id.email_template_id.id,
|
|
|
|
'composition_mode': 'comment',
|
2014-01-23 13:42:33 +00:00
|
|
|
}, context=context)
|
2014-01-24 11:00:12 +00:00
|
|
|
template_values = Composer.onchange_template_id(
|
|
|
|
cr, uid, composer_id, line.product_id.email_template_id.id, 'comment', 'account.invoice', invoice.id
|
|
|
|
)['value']
|
2014-06-13 14:32:08 +00:00
|
|
|
template_values['attachment_ids'] = [(4, id) for id in template_values.get('attachment_ids', [])]
|
2014-01-24 11:00:12 +00:00
|
|
|
Composer.write(cr, uid, [composer_id], template_values, context=context)
|
2014-01-24 10:54:06 +00:00
|
|
|
Composer.send_mail(cr, uid, [composer_id], context=context)
|
2014-01-24 10:22:01 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def invoice_validate(self, cr, uid, ids, context=None):
|
|
|
|
res = super(account_invoice, self).invoice_validate(cr, uid, ids, context=context)
|
|
|
|
self.invoice_validate_send_email(cr, uid, ids, context=context)
|
2014-01-22 07:26:03 +00:00
|
|
|
return res
|