diff --git a/rma/__manifest__.py b/rma/__manifest__.py index 9e065fe7..4ab56322 100644 --- a/rma/__manifest__.py +++ b/rma/__manifest__.py @@ -34,6 +34,7 @@ "views/rma_views.xml", "views/stock_picking_views.xml", "views/stock_warehouse_views.xml", + "views/res_config_settings_views.xml", ], 'post_init_hook': 'post_init_hook', "application": True, diff --git a/rma/models/__init__.py b/rma/models/__init__.py index 134da114..b146eaaf 100644 --- a/rma/models/__init__.py +++ b/rma/models/__init__.py @@ -5,6 +5,7 @@ from . import rma from . import rma_operation from . import rma_team from . import res_company +from . import res_config_settings from . import res_partner from . import res_users from . import stock_move diff --git a/rma/models/res_company.py b/rma/models/res_company.py index cb65960c..b7495abb 100644 --- a/rma/models/res_company.py +++ b/rma/models/res_company.py @@ -1,12 +1,31 @@ # Copyright 2020 Tecnativa - Ernesto Tejeda # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import api, models, _ +from odoo import api, fields, models, _ class Company(models.Model): _inherit = "res.company" + def _default_rma_mail_confirmation_template(self): + try: + return self.env.ref("rma.mail_template_rma_notification").id + except ValueError: + return False + + send_rma_confirmation = fields.Boolean( + string="Send RMA Confirmation", + help="When the delivery is confirmed, send a confirmation email " + "to the customer.", + ) + rma_mail_confirmation_template_id = fields.Many2one( + comodel_name="mail.template", + string="Email Template confirmation for RMA", + domain="[('model', '=', 'rma')]", + default=_default_rma_mail_confirmation_template, + help="Email sent to the customer once the RMA is confirmed.", + ) + @api.model def create(self, vals): company = super(Company, self).create(vals) diff --git a/rma/models/res_config_settings.py b/rma/models/res_config_settings.py new file mode 100644 index 00000000..303044cb --- /dev/null +++ b/rma/models/res_config_settings.py @@ -0,0 +1,16 @@ +# Copyright 2021 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + send_rma_confirmation = fields.Boolean( + related="company_id.send_rma_confirmation", + readonly=False, + ) + rma_mail_confirmation_template_id = fields.Many2one( + related="company_id.rma_mail_confirmation_template_id", + readonly=False, + ) diff --git a/rma/models/rma.py b/rma/models/rma.py index a2c77803..0b21d868 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -528,10 +528,23 @@ class Rma(models.Model): _("You cannot delete RMAs that are not in draft state")) return super().unlink() + def _send_confirmation_email(self): + """Auto send notifications""" + for rma in self.filtered(lambda p: p.company_id.send_rma_confirmation): + rma_template_id = ( + rma.company_id.rma_mail_confirmation_template_id.id + ) + rma.with_context( + force_send=True, + mark_rma_as_sent=True + ).message_post_with_template(rma_template_id) + # Action methods def action_rma_send(self): self.ensure_one() template = self.env.ref('rma.mail_template_rma_notification', False) + template = ( + self.company_id.rma_mail_confirmation_template_id or template) form = self.env.ref('mail.email_compose_message_wizard_form', False) ctx = { 'default_model': 'rma', @@ -569,6 +582,7 @@ class Rma(models.Model): }) if self.partner_id not in self.message_partner_ids: self.message_subscribe([self.partner_id.id]) + self._send_confirmation_email() def action_refund(self): """Invoked when 'Refund' button in rma form view is clicked diff --git a/rma/tests/test_rma.py b/rma/tests/test_rma.py index 03463721..dffbf152 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -669,3 +669,20 @@ class TestRma(SavepointCase): rma.reception_move_id.quantity_done = 10 rma.reception_move_id.picking_id.action_done() self.assertEqual(rma.product_id.qty_available, 0) + + def test_autoconfirm_email(self): + rma = self._create_rma(self.partner, self.product, 10, self.rma_loc) + rma.company_id.send_rma_confirmation = True + rma.company_id.rma_mail_confirmation_template_id = ( + self.env.ref("rma.mail_template_rma_notification") + ) + previous_mails = self.env["mail.mail"].search( + [("partner_ids", "in", self.partner.ids)] + ) + self.assertFalse(previous_mails) + rma.action_confirm() + mail = self.env["mail.message"].search( + [("partner_ids", "in", self.partner.ids)] + ) + self.assertTrue(rma.name in mail.subject) + self.assertTrue(rma.name in mail.body) diff --git a/rma/views/res_config_settings_views.xml b/rma/views/res_config_settings_views.xml new file mode 100644 index 00000000..ef05f2fe --- /dev/null +++ b/rma/views/res_config_settings_views.xml @@ -0,0 +1,28 @@ + + + + + res.config.settings + + + +
+
+ +
+
+
+
+
+
+
+