[IMP] rma: auto send confirmation email

This commit is contained in:
david
2021-02-09 13:50:02 +01:00
parent 9e3c8dd7aa
commit c36610357e
7 changed files with 97 additions and 1 deletions

View File

@@ -34,6 +34,7 @@
"views/rma_views.xml", "views/rma_views.xml",
"views/stock_picking_views.xml", "views/stock_picking_views.xml",
"views/stock_warehouse_views.xml", "views/stock_warehouse_views.xml",
"views/res_config_settings_views.xml",
], ],
'post_init_hook': 'post_init_hook', 'post_init_hook': 'post_init_hook',
"application": True, "application": True,

View File

@@ -5,6 +5,7 @@ from . import rma
from . import rma_operation from . import rma_operation
from . import rma_team from . import rma_team
from . import res_company from . import res_company
from . import res_config_settings
from . import res_partner from . import res_partner
from . import res_users from . import res_users
from . import stock_move from . import stock_move

View File

@@ -1,12 +1,31 @@
# Copyright 2020 Tecnativa - Ernesto Tejeda # Copyright 2020 Tecnativa - Ernesto Tejeda
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # 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): class Company(models.Model):
_inherit = "res.company" _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 @api.model
def create(self, vals): def create(self, vals):
company = super(Company, self).create(vals) company = super(Company, self).create(vals)

View File

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

View File

@@ -528,10 +528,23 @@ class Rma(models.Model):
_("You cannot delete RMAs that are not in draft state")) _("You cannot delete RMAs that are not in draft state"))
return super().unlink() 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 # Action methods
def action_rma_send(self): def action_rma_send(self):
self.ensure_one() self.ensure_one()
template = self.env.ref('rma.mail_template_rma_notification', False) 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) form = self.env.ref('mail.email_compose_message_wizard_form', False)
ctx = { ctx = {
'default_model': 'rma', 'default_model': 'rma',
@@ -569,6 +582,7 @@ class Rma(models.Model):
}) })
if self.partner_id not in self.message_partner_ids: if self.partner_id not in self.message_partner_ids:
self.message_subscribe([self.partner_id.id]) self.message_subscribe([self.partner_id.id])
self._send_confirmation_email()
def action_refund(self): def action_refund(self):
"""Invoked when 'Refund' button in rma form view is clicked """Invoked when 'Refund' button in rma form view is clicked

View File

@@ -669,3 +669,20 @@ class TestRma(SavepointCase):
rma.reception_move_id.quantity_done = 10 rma.reception_move_id.quantity_done = 10
rma.reception_move_id.picking_id.action_done() rma.reception_move_id.picking_id.action_done()
self.assertEqual(rma.product_id.qty_available, 0) 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,28 @@
<?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>