diff --git a/rma/__manifest__.py b/rma/__manifest__.py index aac3ec2c..9abf748e 100644 --- a/rma/__manifest__.py +++ b/rma/__manifest__.py @@ -29,6 +29,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 f3c5b7de..66a957a6 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 a7129391..0a270769 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..d18ce044 --- /dev/null +++ b/rma/models/res_config_settings.py @@ -0,0 +1,14 @@ +# 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 8e7ddf4c..3ce4cb3b 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -498,10 +498,19 @@ class Rma(models.Model): ) 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", @@ -536,6 +545,7 @@ class Rma(models.Model): self.write({"reception_move_id": reception_move.id, "state": "confirmed"}) 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 b917f269..27cb010f 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -656,3 +656,20 @@ class TestRma(SavepointCase): def test_quantities_on_hand(self): rma = self._create_confirm_receive(self.partner, self.product, 10, self.rma_loc) 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..744da02b --- /dev/null +++ b/rma/views/res_config_settings_views.xml @@ -0,0 +1,52 @@ + + + + res.config.settings + + + +
+
+ +
+
+
+
+
+
+
+