diff --git a/rma/data/mail_data.xml b/rma/data/mail_data.xml index 0ecead99..5020344e 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 @@ -54,6 +54,62 @@

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

+ +
+
+ + + 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 b7495abb..0aaad7bd 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,21 @@ 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 e79d6e8e..6649aaa3 100644 --- a/rma/models/rma.py +++ b/rma/models/rma.py @@ -513,7 +513,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) + rmas = super().create(vals) + # 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 @api.multi def copy(self, default=None): @@ -529,6 +535,18 @@ class Rma(models.Model): _("You cannot delete RMAs that are not in draft state")) 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): @@ -541,6 +559,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() @@ -1203,6 +1233,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 de27c20e..99868b2c 100644 --- a/rma/models/stock_move.py +++ b/rma/models/stock_move.py @@ -72,7 +72,7 @@ class StockMove(models.Model): # if the stock user has no RMA permissions. to_be_received = move_done.sudo().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 f6029d7c..a69bd567 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -671,20 +671,46 @@ 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.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) + 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.subtype_id) + 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 ef05f2fe..477b4ffd 100644 --- a/rma/views/res_config_settings_views.xml +++ b/rma/views/res_config_settings_views.xml @@ -22,6 +22,38 @@ +
+
+ +
+
+
+
+
+
+ +
+
+
+