From c7d6097a8092654c10841e3dad4c815f179f8e66 Mon Sep 17 00:00:00 2001
From: Haresh Shyara <hsh@odoo.com>
Date: Mon, 17 Apr 2017 16:24:55 +0530
Subject: [PATCH] [IMP] account, sale, l10n_ch, purchase: replace mail composer
 inheritance by message_post override

Currently composer is inherited in those four modules to add a specific
behavior when sending the invoice / quotation by email. Purpose is to
mark it as sent, using a magic context key.

This commit keeps the feature but changes the implementation. It is now
done by overriding message_post which should be the entry point for all
message-related features.
---
 addons/account/models/account_invoice.py      | 22 ++++++----------
 addons/l10n_ch/models/__init__.py             |  1 -
 addons/l10n_ch/models/account_invoice.py      |  7 ++++++
 addons/l10n_ch/models/mail_compose_message.py | 25 -------------------
 addons/purchase/models/purchase.py            | 23 +++++++----------
 addons/sale/models/sale.py                    |  7 ++++++
 addons/sale/wizard/__init__.py                |  1 -
 addons/sale/wizard/mail_compose_message.py    | 17 -------------
 8 files changed, 30 insertions(+), 73 deletions(-)
 delete mode 100644 addons/l10n_ch/models/mail_compose_message.py
 delete mode 100644 addons/sale/wizard/mail_compose_message.py

diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py
index ce099bd0ea48..846701b2b161 100644
--- a/addons/account/models/account_invoice.py
+++ b/addons/account/models/account_invoice.py
@@ -579,6 +579,13 @@ class AccountInvoice(models.Model):
             'context': ctx,
         }
 
+    @api.multi
+    @api.returns('self', lambda value: value.id)
+    def message_post(self, **kwargs):
+        if self.env.context.get('mark_invoice_as_sent'):
+            self.filtered(lambda inv: not inv.sent).write({'sent': True})
+        return super(AccountInvoice, self.with_context(mail_post_autofollow=True)).message_post(**kwargs)
+
     @api.multi
     def compute_taxes(self):
         """Function used in other module to compute the taxes on a fresh invoice created (onchanges did not applied)"""
@@ -1797,18 +1804,3 @@ class AccountPaymentTermLine(models.Model):
     def _onchange_option(self):
         if self.option in ('last_day_current_month', 'last_day_following_month'):
             self.days = 0
-
-
-class MailComposeMessage(models.TransientModel):
-    _inherit = 'mail.compose.message'
-
-    @api.multi
-    def send_mail(self, auto_commit=False):
-        context = self._context
-        if context.get('default_model') == 'account.invoice' and \
-                context.get('default_res_id') and context.get('mark_invoice_as_sent'):
-            invoice = self.env['account.invoice'].browse(context['default_res_id'])
-            if not invoice.sent:
-                invoice.sent = True
-            self = self.with_context(mail_post_autofollow=True)
-        return super(MailComposeMessage, self).send_mail(auto_commit=auto_commit)
diff --git a/addons/l10n_ch/models/__init__.py b/addons/l10n_ch/models/__init__.py
index acb15c35730c..cf7550fd3684 100644
--- a/addons/l10n_ch/models/__init__.py
+++ b/addons/l10n_ch/models/__init__.py
@@ -3,7 +3,6 @@
 
 from . import res_config_settings
 from . import account_invoice
-from . import mail_compose_message
 from . import mail_template
 from . import res_bank
 from . import res_company
diff --git a/addons/l10n_ch/models/account_invoice.py b/addons/l10n_ch/models/account_invoice.py
index bcb9935e0a9e..8912b763db7a 100644
--- a/addons/l10n_ch/models/account_invoice.py
+++ b/addons/l10n_ch/models/account_invoice.py
@@ -166,3 +166,10 @@ class AccountInvoice(models.Model):
             rslt['context']['l10n_ch_mark_isr_as_sent'] = True
 
         return rslt
+
+    @api.multi
+    @api.returns('self', lambda value: value.id)
+    def message_post(self, **kwargs):
+        if self.env.context.get('l10n_ch_mark_isr_as_sent'):
+            self.filtered(lambda inv: not inv.l10n_ch_isr_sent).write({'l10n_ch_isr_sent': True})
+        return super(AccountInvoice, self.with_context(mail_post_autofollow=True)).message_post(**kwargs)
diff --git a/addons/l10n_ch/models/mail_compose_message.py b/addons/l10n_ch/models/mail_compose_message.py
deleted file mode 100644
index 652571492a78..000000000000
--- a/addons/l10n_ch/models/mail_compose_message.py
+++ /dev/null
@@ -1,25 +0,0 @@
-# -*- coding: utf-8 -*-
-# Part of Odoo. See LICENSE file for full copyright and licensing details.
-
-from odoo import api, fields, models, _
-
-
-class MailComposer(models.TransientModel):
-    _inherit = 'mail.compose.message'
-
-    @api.multi
-    def send_mail(self, auto_commit=False):
-        """ Method overriden to mark ISR as sent once a mail containing
-        it in attachment has been sent.
-        """
-        context = self._context
-        if context.get('default_model') == 'account.invoice' and \
-                context.get('default_res_id') and \
-                context.get('l10n_ch_mark_isr_as_sent', False):
-
-            invoice = self.env['account.invoice'].browse(context['default_res_id'])
-            invoice = invoice.with_context(mail_post_autofollow=True)
-            invoice.l10n_ch_isr_sent = True
-            invoice.message_post(body=_("ISR sent"))
-
-        return super(MailComposer, self).send_mail(auto_commit=auto_commit)
\ No newline at end of file
diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py
index bb9f8d2346a1..10c3f0f77820 100644
--- a/addons/purchase/models/purchase.py
+++ b/addons/purchase/models/purchase.py
@@ -307,7 +307,8 @@ class PurchaseOrder(models.Model):
             'default_template_id': template_id,
             'default_composition_mode': 'comment',
             'custom_layout': "purchase.mail_template_data_notification_email_purchase_order",
-            'force_email': True
+            'force_email': True,
+            'mark_rfq_as_sent': True,
         })
         return {
             'name': _('Compose Email'),
@@ -321,6 +322,13 @@ class PurchaseOrder(models.Model):
             'context': ctx,
         }
 
+    @api.multi
+    @api.returns('self', lambda value: value.id)
+    def message_post(self, **kwargs):
+        if self.env.context.get('mark_rfq_as_sent'):
+            self.filtered(lambda o: o.state == 'draft').write({'state': 'sent'})
+        return super(PurchaseOrder, self.with_context(mail_post_autofollow=True)).message_post(**kwargs)
+
     @api.multi
     def print_quotation(self):
         return self.env.ref('purchase.report_purchase_quotation').report_action(self)
@@ -1083,16 +1091,3 @@ class ProductCategory(models.Model):
         'account.account', string="Price Difference Account",
         company_dependent=True,
         help="This account will be used to value price difference between purchase price and accounting cost.")
-
-
-class MailComposeMessage(models.TransientModel):
-    _inherit = 'mail.compose.message'
-
-    @api.multi
-    def send_mail(self, auto_commit=False):
-        if self._context.get('default_model') == 'purchase.order' and self._context.get('default_res_id'):
-            if not self.filtered('subtype_id.internal'):
-                order = self.env['purchase.order'].browse([self._context['default_res_id']])
-                if order.state == 'draft':
-                    order.state = 'sent'
-        return super(MailComposeMessage, self.with_context(mail_post_autofollow=True)).send_mail(auto_commit=auto_commit)
diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py
index b98d3ee31a70..071c62375a8f 100644
--- a/addons/sale/models/sale.py
+++ b/addons/sale/models/sale.py
@@ -496,6 +496,13 @@ class SaleOrder(models.Model):
             'context': ctx,
         }
 
+    @api.multi
+    @api.returns('self', lambda value: value.id)
+    def message_post(self, **kwargs):
+        if self.env.context.get('mark_so_as_sent'):
+            self.filtered(lambda o: o.state == 'draft').write({'state': 'sent'})
+        return super(SaleOrder, self.with_context(mail_post_autofollow=True)).message_post(**kwargs)
+
     @api.multi
     def force_quotation_send(self):
         for order in self:
diff --git a/addons/sale/wizard/__init__.py b/addons/sale/wizard/__init__.py
index 5d8b76139cb3..46b1c608a3d6 100644
--- a/addons/sale/wizard/__init__.py
+++ b/addons/sale/wizard/__init__.py
@@ -2,4 +2,3 @@
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
 from . import sale_make_invoice_advance
-from . import mail_compose_message
\ No newline at end of file
diff --git a/addons/sale/wizard/mail_compose_message.py b/addons/sale/wizard/mail_compose_message.py
deleted file mode 100644
index 73d81116d50b..000000000000
--- a/addons/sale/wizard/mail_compose_message.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# -*- coding: utf-8 -*-
-# Part of Odoo. See LICENSE file for full copyright and licensing details.
-
-from odoo import api, models
-
-
-class MailComposeMessage(models.TransientModel):
-    _inherit = 'mail.compose.message'
-
-    @api.multi
-    def send_mail(self, auto_commit=False):
-        if self._context.get('default_model') == 'sale.order' and self._context.get('default_res_id') and self._context.get('mark_so_as_sent'):
-            order = self.env['sale.order'].browse([self._context['default_res_id']])
-            if order.state == 'draft':
-                order.state = 'sent'
-            self = self.with_context(mail_post_autofollow=True)
-        return super(MailComposeMessage, self).send_mail(auto_commit=auto_commit)
-- 
GitLab