[IMP] rma: auto send confirmation email

This commit is contained in:
david
2021-02-09 13:50:02 +01:00
committed by Pedro M. Baeza
parent 02a2694f01
commit 8e6a869aca
7 changed files with 115 additions and 1 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -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)

View File

@@ -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,
)

View File

@@ -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

View File

@@ -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)

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="stock.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath
expr="//div[@data-key='stock']/div[hasclass('o_settings_container')]"
position="inside"
>
<div
class="col-12 col-lg-6 o_setting_box"
title="Send automatic RMA info to customer"
>
<div class="o_setting_left_pane">
<field name="send_rma_confirmation" />
</div>
<div class="o_setting_right_pane">
<label
for="send_rma_confirmation"
string="RMA Confirmation Email"
/>
<span
class="fa fa-lg fa-building-o"
title="Values set here are company-specific."
groups="base.group_multi_company"
/>
<div class="text-muted">
When the RMA is confirmed, send an automatic information email.
</div>
<div
class="row mt16"
attrs="{'invisible': [('send_rma_confirmation', '=', False)]}"
>
<label
for="rma_mail_confirmation_template_id"
string="Email Template"
class="col-lg-4 o_light_label"
/>
<field
name="rma_mail_confirmation_template_id"
class="oe_inline"
attrs="{'required': [('send_rma_confirmation', '=', True)]}"
context="{'default_model': 'rma'}"
/>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>