From 4adb4b8d15c528f8946cc8c766fa712c3effbb85 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 9 Jul 2015 15:47:46 +0200 Subject: [PATCH] [FIX] sale, website_sale: Send email on sale order confirmation Not just when coming back from the payment provider to the payment validation route `/shop/payment/validate`. Otherwise, if you do not come back from the payment provider page, that you quit just after having paid but just before being redirected to Odoo, you do not receive the email. The change within the `sale` module, while this issue concerns `website_sale` only, has been accepted because this is a mechanism that could be used by other modules. opw-644348 --- addons/sale/sale.py | 23 +++++++++++++++++++++++ addons/website_sale/controllers/main.py | 24 +----------------------- addons/website_sale/models/payment.py | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/addons/sale/sale.py b/addons/sale/sale.py index 48ad722300b..0575c48d2bd 100644 --- a/addons/sale/sale.py +++ b/addons/sale/sale.py @@ -598,8 +598,31 @@ class sale_order(osv.osv): return True def action_button_confirm(self, cr, uid, ids, context=None): + if not context: + context = {} assert len(ids) == 1, 'This option should only be used for a single id at a time.' self.signal_workflow(cr, uid, ids, 'order_confirm') + if context.get('send_email'): + order_id = ids[0] + email_act = self.action_quotation_send(cr, uid, [order_id], context=context) + if email_act and email_act.get('context'): + composer_obj = self.pool['mail.compose.message'] + composer_values = {} + email_ctx = email_act['context'] + template_values = [ + email_ctx.get('default_template_id'), + email_ctx.get('default_composition_mode'), + email_ctx.get('default_model'), + email_ctx.get('default_res_id'), + ] + composer_values.update(composer_obj.onchange_template_id(cr, uid, None, *template_values, context=context).get('value', {})) + if not composer_values.get('email_from'): + composer_values['email_from'] = self.browse(cr, uid, order_id, context=context).company_id.email + for key in ['attachment_ids', 'partner_ids']: + if composer_values.get(key): + composer_values[key] = [(6, 0, composer_values[key])] + composer_id = composer_obj.create(cr, uid, composer_values, context=email_ctx) + composer_obj.send_mail(cr, uid, [composer_id], context=email_ctx) return True def action_wait(self, cr, uid, ids, context=None): diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 32da18da360..cee6d529e57 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -802,33 +802,11 @@ class website_sale(http.Controller): if (not order.amount_total and not tx): # Orders are confirmed by payment transactions, but there is none for free orders, # (e.g. free events), so confirm immediately - order.action_button_confirm() - # send by email - email_act = sale_order_obj.action_quotation_send(cr, SUPERUSER_ID, [order.id], context=request.context) + order.with_context(dict(context, send_email=True)).action_button_confirm() elif tx and tx.state == 'cancel': # cancel the quotation sale_order_obj.action_cancel(cr, SUPERUSER_ID, [order.id], context=request.context) - # send the email - if email_act and email_act.get('context'): - composer_obj = request.registry['mail.compose.message'] - composer_values = {} - email_ctx = email_act['context'] - template_values = [ - email_ctx.get('default_template_id'), - email_ctx.get('default_composition_mode'), - email_ctx.get('default_model'), - email_ctx.get('default_res_id'), - ] - composer_values.update(composer_obj.onchange_template_id(cr, SUPERUSER_ID, None, *template_values, context=context).get('value', {})) - if not composer_values.get('email_from') and uid == request.website.user_id.id: - composer_values['email_from'] = request.website.user_id.company_id.email - for key in ['attachment_ids', 'partner_ids']: - if composer_values.get(key): - composer_values[key] = [(6, 0, composer_values[key])] - composer_id = composer_obj.create(cr, SUPERUSER_ID, composer_values, context=email_ctx) - composer_obj.send_mail(cr, SUPERUSER_ID, [composer_id], context=email_ctx) - # clean context and session, then redirect to the confirmation page request.website.sale_reset(context=context) diff --git a/addons/website_sale/models/payment.py b/addons/website_sale/models/payment.py index 36b681a759b..32411b56c1e 100644 --- a/addons/website_sale/models/payment.py +++ b/addons/website_sale/models/payment.py @@ -23,6 +23,6 @@ class PaymentTransaction(orm.Model): if hasattr(self, tx_find_method_name): tx = getattr(self, tx_find_method_name)(cr, uid, data, context=context) if tx and tx.state == 'done' and tx.sale_order_id and tx.sale_order_id.state in ['draft', 'sent']: - self.pool['sale.order'].action_button_confirm(cr, SUPERUSER_ID, [tx.sale_order_id.id], context=context) + self.pool['sale.order'].action_button_confirm(cr, SUPERUSER_ID, [tx.sale_order_id.id], context=dict(context, send_email=True)) return res