From 5eb8e8443eb0dc7b9d339030cb7611b71d8a7ed5 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 19 May 2021 14:49:54 +0200 Subject: [PATCH] [IMP] rma: notify reception to customer Now we can configure if an automatic notification should be sent when we receive the goods from an RMA in our warehouse If we've got `rma_sale` or `website_rma` we can also configure draft notifications so when the customer places an RMA from the portal the receive an acknowledge email. TT29595 --- rma/data/mail_data.xml | 60 ++++++++++++++++++- rma/models/res_company.py | 35 ++++++++++++ rma/models/res_config_settings.py | 16 ++++++ rma/models/rma.py | 40 ++++++++++++- rma/models/stock_move.py | 2 +- rma/tests/test_rma.py | 47 ++++++++++++--- rma/views/res_config_settings_views.xml | 76 +++++++++++++++++++++++++ 7 files changed, 265 insertions(+), 11 deletions(-) diff --git a/rma/data/mail_data.xml b/rma/data/mail_data.xml index b30d4b3a..4293a84b 100644 --- a/rma/data/mail_data.xml +++ b/rma/data/mail_data.xml @@ -8,7 +8,7 @@ RMA in draft state - RMA Notificatoin + RMA Notification rma RMA automatic customer notifications @@ -58,4 +58,62 @@ + + + RMA Receipt Notification + + ${object.user_id.email_formatted |safe} + ${object.partner_id.id} + ${object.company_id.name} RMA (Ref ${object.name or 'n/a' }) products received + + ${(object.name or '')} + ${object.partner_id.lang} + + +
+

+ Dear ${object.partner_id.name} + % if object.partner_id.parent_id: + (${object.partner_id.parent_id.name}) + % endif +

+ The products for your RMA ${object.name} + from ${object.company_id.name} have been received in our warehouse. +

+ Do not hesitate to contact us if you have any question. +

+
+
+
+ + RMA Draft Notification + + ${object.user_id.email_formatted |safe} + ${object.partner_id.id} + ${object.company_id.name} Your RMA has been succesfully created (Ref ${object.name or 'n/a' }) + + ${(object.name or '')} + ${object.partner_id.lang} + + +
+

+ Dear ${object.partner_id.name} + % if object.partner_id.parent_id: + (${object.partner_id.parent_id.name}) + % endif +

+ You've succesfully placed your RMA ${object.name} + on ${object.company_id.name}. Our team will check it and will validate + it as soon as possible. +

+ Do not hesitate to contact us if you have any question. +

+
+
+
diff --git a/rma/models/res_company.py b/rma/models/res_company.py index 0a270769..479e7e7c 100644 --- a/rma/models/res_company.py +++ b/rma/models/res_company.py @@ -13,11 +13,32 @@ class Company(models.Model): except ValueError: return False + def _default_rma_mail_receipt_template(self): + try: + return self.env.ref("rma.mail_template_rma_receipt_notification").id + except ValueError: + return False + + def _default_rma_mail_draft_template(self): + try: + return self.env.ref("rma.mail_template_rma_draft_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.", ) + send_rma_receipt_confirmation = fields.Boolean( + string="Send RMA Receipt Confirmation", + help="When the RMA receipt is confirmed, send a confirmation email " + "to the customer.", + ) + send_rma_draft_confirmation = fields.Boolean( + string="Send RMA draft Confirmation", + help="When a customer places an RMA, send a notification with it", + ) rma_mail_confirmation_template_id = fields.Many2one( comodel_name="mail.template", string="Email Template confirmation for RMA", @@ -25,6 +46,20 @@ class Company(models.Model): default=_default_rma_mail_confirmation_template, help="Email sent to the customer once the RMA is confirmed.", ) + rma_mail_receipt_confirmation_template_id = fields.Many2one( + comodel_name="mail.template", + string="Email Template receipt confirmation for RMA", + domain="[('model', '=', 'rma')]", + default=_default_rma_mail_receipt_template, + help="Email sent to the customer once the RMA products are received.", + ) + rma_mail_draft_confirmation_template_id = fields.Many2one( + comodel_name="mail.template", + string="Email Template draft notification for RMA", + domain="[('model', '=', 'rma')]", + default=_default_rma_mail_draft_template, + help="Email sent to the customer when they place " "an RMA from the portal", + ) @api.model def create(self, vals): diff --git a/rma/models/res_config_settings.py b/rma/models/res_config_settings.py index 303044cb..c48d324e 100644 --- a/rma/models/res_config_settings.py +++ b/rma/models/res_config_settings.py @@ -14,3 +14,19 @@ class ResConfigSettings(models.TransientModel): related="company_id.rma_mail_confirmation_template_id", readonly=False, ) + send_rma_receipt_confirmation = fields.Boolean( + related="company_id.send_rma_receipt_confirmation", + readonly=False, + ) + rma_mail_receipt_confirmation_template_id = fields.Many2one( + related="company_id.rma_mail_receipt_confirmation_template_id", + readonly=False, + ) + send_rma_draft_confirmation = fields.Boolean( + related="company_id.send_rma_draft_confirmation", + readonly=False, + ) + rma_mail_draft_confirmation_template_id = fields.Many2one( + related="company_id.rma_mail_draft_confirmation_template_id", + readonly=False, + ) diff --git a/rma/models/rma.py b/rma/models/rma.py index e59bf9f8..b825c576 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -511,7 +511,13 @@ class Rma(models.Model): # Assign a default team_id which will be the first in the sequence if "team_id" not in vals: vals["team_id"] = self.env["rma.team"].search([], limit=1).id - return super().create(vals_list) + rmas = super().create(vals_list) + # Send acknowledge when the RMA is created from the portal and the + # company has the proper setting active. This context is set by the + # `rma_sale` module. + if self.env.context.get("from_portal"): + rmas._send_draft_email() + return rmas def copy(self, default=None): team = super().copy(default) @@ -529,6 +535,16 @@ class Rma(models.Model): ) return super().unlink() + def _send_draft_email(self): + """Send customer notifications they place the RMA from the portal""" + for rma in self.filtered("company_id.send_rma_draft_confirmation"): + rma_template_id = rma.company_id.rma_mail_draft_confirmation_template_id.id + rma.with_context( + force_send=True, + mark_rma_as_sent=True, + default_subtype_id=self.env.ref("rma.mt_rma_notification").id, + ).message_post_with_template(rma_template_id) + def _send_confirmation_email(self): """Auto send notifications""" for rma in self.filtered(lambda p: p.company_id.send_rma_confirmation): @@ -539,6 +555,18 @@ class Rma(models.Model): default_subtype_id=self.env.ref("rma.mt_rma_notification").id, ).message_post_with_template(rma_template_id) + def _send_receipt_confirmation_email(self): + """Send customer notifications when the products are received""" + for rma in self.filtered("company_id.send_rma_receipt_confirmation"): + rma_template_id = ( + rma.company_id.rma_mail_receipt_confirmation_template_id.id + ) + rma.with_context( + force_send=True, + mark_rma_as_sent=True, + default_subtype_id=self.env.ref("rma.mt_rma_notification").id, + ).message_post_with_template(rma_template_id) + # Action methods def action_rma_send(self): self.ensure_one() @@ -1260,6 +1288,16 @@ class Rma(models.Model): return "RMA Report - %s" % self.name # Other business methods + + def update_received_state_on_reception(self): + """Invoked by: + [stock.move]._action_done + Here we can attach methods to trigger when the customer products + are received on the RMA location, such as automatic notifications + """ + self.write({"state": "received"}) + self._send_receipt_confirmation_email() + def update_received_state(self): """Invoked by: [stock.move].unlink diff --git a/rma/models/stock_move.py b/rma/models/stock_move.py index e6ca9df3..fc22806b 100644 --- a/rma/models/stock_move.py +++ b/rma/models/stock_move.py @@ -75,7 +75,7 @@ class StockMove(models.Model): .mapped("rma_receiver_ids") .filtered(lambda r: r.state == "confirmed") ) - to_be_received.write({"state": "received"}) + to_be_received.update_received_state_on_reception() # Set RMAs as delivered move_done.mapped("rma_id").update_replaced_state() move_done.mapped("rma_id").update_returned_state() diff --git a/rma/tests/test_rma.py b/rma/tests/test_rma.py index d0680ce2..d38cd3e1 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -676,19 +676,50 @@ class TestRma(SavepointCase): 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( + self.company.send_rma_confirmation = True + self.company.send_rma_receipt_confirmation = True + self.company.send_rma_draft_confirmation = True + self.company.rma_mail_confirmation_template_id = self.env.ref( "rma.mail_template_rma_notification" ) + self.company.rma_mail_receipt_confirmation_template_id = self.env.ref( + "rma.mail_template_rma_receipt_notification" + ) + self.company.rma_mail_draft_confirmation_template_id = self.env.ref( + "rma.mail_template_rma_draft_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( + # Force the context to mock an RMA created from the portal, which is + # feature that we get on `rma_sale`. We drop it after the RMA creation + # to avoid uncontrolled side effects + ctx = self.env.context + self.env.context = dict(ctx, from_portal=True) + rma = self._create_rma(self.partner, self.product, 10, self.rma_loc) + self.env.context = ctx + mail_draft = 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) - self.assertEqual(self.env.ref("rma.mt_rma_notification"), mail.subtype_id) + rma.action_confirm() + mail_confirm = ( + self.env["mail.message"].search([("partner_ids", "in", self.partner.ids)]) + - mail_draft + ) + self.assertTrue(rma.name in mail_confirm.subject) + self.assertTrue(rma.name in mail_confirm.body) + self.assertEqual( + self.env.ref("rma.mt_rma_notification"), mail_confirm.subtype_id + ) + # Now we'll confirm the incoming goods picking and the automatic + # reception notification should be sent + rma.reception_move_id.quantity_done = rma.product_uom_qty + rma.reception_move_id.picking_id.button_validate() + mail_receipt = ( + self.env["mail.message"].search([("partner_ids", "in", self.partner.ids)]) + - mail_draft + - mail_confirm + ) + self.assertTrue(rma.name in mail_receipt.subject) + self.assertTrue("products received" in mail_receipt.subject) diff --git a/rma/views/res_config_settings_views.xml b/rma/views/res_config_settings_views.xml index 744da02b..423dff2d 100644 --- a/rma/views/res_config_settings_views.xml +++ b/rma/views/res_config_settings_views.xml @@ -46,6 +46,82 @@ +
+
+ +
+
+
+
+
+
+ +
+
+
+