Skip to content

Commit

Permalink
[MIG] mail_composer_cc_bcc_account: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Mar 11, 2024
1 parent b77557c commit f964a6a
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 27 deletions.
1 change: 0 additions & 1 deletion mail_composer_cc_bcc/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"preloadable": True,
"depends": [
"mail",
"account",
],
"data": [
"views/res_company_views.xml",
Expand Down
12 changes: 6 additions & 6 deletions mail_composer_cc_bcc/tests/test_mail_cc_bcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def test_template_cc_bcc(self):
# Company default values
env.company.default_partner_cc_ids = self.partner_cc3
env.company.default_partner_bcc_ids = self.partner_cc2
# Product template values
tmpl_model = env["ir.model"].search([("model", "=", "product.template")])
# Partner template values
tmpl_model = env["ir.model"].search([("model", "=", "res.partner")])
partner_cc = self.partner_cc
partner_bcc = self.partner_bcc
vals = {
Expand All @@ -127,7 +127,7 @@ def test_template_cc_bcc(self):
(partner_bcc.name or "False", partner_bcc.email or "False")
),
}
prod_tmpl = env["mail.template"].create(vals)
partner_tmpl = env["mail.template"].create(vals)

# Open mail composer form and check for default values from company
form = self.open_mail_composer_form()
Expand All @@ -137,12 +137,12 @@ def test_template_cc_bcc(self):
self.assertEqual(composer.partner_bcc_ids, self.partner_cc2)

# Change email template and check for values from it
form.template_id = prod_tmpl
form.template_id = partner_tmpl
composer = form.save()

# Beside existing Cc and Bcc, add template's ones
form = Form(composer)
form.template_id = prod_tmpl
form.template_id = partner_tmpl
composer = form.save()
expecting = self.partner_cc3 + self.partner_cc

Expand All @@ -160,7 +160,7 @@ def test_template_cc_bcc(self):
form.save()
self.assertFalse(form.template_id) # no template

form.template_id = prod_tmpl
form.template_id = partner_tmpl
composer = form.save()

expecting = self.partner_cc3 + self.partner_cc
Expand Down
4 changes: 4 additions & 0 deletions mail_composer_cc_bcc_account/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
from . import wizards
5 changes: 2 additions & 3 deletions mail_composer_cc_bcc_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Email CC and BCC when sending invoice",
"summary": "This module enables sending mail to CC and BCC partners for invoices.",
"version": "16.0.2.0.0",
"version": "17.0.1.0.0",
"development_status": "Alpha",
"category": "Social",
"website": "https://github.com/OCA/social",
Expand All @@ -16,9 +16,8 @@
"preloadable": True,
"depends": [
"mail_composer_cc_bcc",
"account",
],
"data": [
"wizards/account_invoice_send_views.xml",
"wizards/account_move_send.xml",
],
}
1 change: 1 addition & 0 deletions mail_composer_cc_bcc_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import mail_thread
19 changes: 19 additions & 0 deletions mail_composer_cc_bcc_account/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class MailThread(models.AbstractModel):
_inherit = "mail.thread"

def _message_create(self, values_list):
context = self.env.context
res = super()._message_create(values_list)
partners_cc = context.get("partner_cc_ids", None)
if partners_cc:
res.recipient_cc_ids = partners_cc
partners_bcc = context.get("partner_bcc_ids", None)
if partners_bcc:
res.recipient_bcc_ids = partners_bcc
return res
1 change: 1 addition & 0 deletions mail_composer_cc_bcc_account/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Hai N. Le <[email protected]>
* Son Ho <[email protected]>
* Tris Doan <[email protected]>

* `Therp BV <https://therp.nl>`_:

Expand Down
39 changes: 29 additions & 10 deletions mail_composer_cc_bcc_account/tests/test_mail_cc_bcc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2023 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import date

from odoo.tests import Form

Expand All @@ -9,31 +10,49 @@
class TestMailCcBccInvoice(TestMailCcBcc):
def open_invoice_mail_composer_form(self):
# Use form to populate data
for_name = [("name", "like", "%INV/20__/00003")]
self.test_invoice = test_record = self.env["account.move"].search(for_name)
# init invoice data
self.test_invoice = test_record = self.test_account_move = self.env[
"account.move"
].create(
{
"invoice_date": date(2024, 3, 2),
"invoice_date_due": date(2024, 3, 10),
"invoice_line_ids": [
(0, 0, {"name": "Line1", "price_unit": 100.0}),
(0, 0, {"name": "Line2", "price_unit": 200.0}),
],
"move_type": "out_invoice",
"name": "invoice test",
"partner_id": self.env.ref("base.res_partner_2").id,
}
)

self.assertTrue(
self.test_invoice,
"Test setup did not succeeed. Invoice not found.",
"Test setup did not succeed. Invoice not found.",
)
self.test_invoice.write({"state": "posted"})

ctx = {
"active_ids": test_record.ids,
"default_model": "account.move",
"default_res_id": test_record.id,
"mail_notify_force_send": True,
}
form = Form(self.env["account.invoice.send"].with_context(**ctx))
form.body = "<p>Hello</p>"
form = Form(self.env["account.move.send"].with_context(**ctx))
form.mail_body = "<p>Hello</p>"
return form

def test_invoice_mail_cc_bcc(self):
self.set_company()
form = self.open_invoice_mail_composer_form()
form.subject = "Hello"
form.mail_subject = "Hello"
composer = form.save()
with self.mock_mail_gateway():
composer._send_email()
composer.action_send_and_print()
message = self.test_invoice.message_ids[0]
self.assertEqual(len(message.mail_ids), 1)
# Only 4 partners notified
self.assertEqual(len(message.notified_partner_ids), 4)
self.assertEqual(len(message.notification_ids), 4)

# Only 2 partners (from default_cc/bcc of company) notified
self.assertEqual(len(message.notified_partner_ids), 2)
self.assertEqual(len(message.notification_ids), 2)
1 change: 1 addition & 0 deletions mail_composer_cc_bcc_account/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_move_send
139 changes: 139 additions & 0 deletions mail_composer_cc_bcc_account/wizards/account_move_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import Command, api, fields, models, tools


class AccountMoveSend(models.TransientModel):
_inherit = "account.move.send"

partner_cc_ids = fields.Many2many(
"res.partner",
"account_move_send_res_partner_cc_rel",
"wizard_id",
"partner_id",
string="Cc",
compute="_compute_mail_partner_cc_bcc_ids",
store=True,
readonly=False,
)
partner_bcc_ids = fields.Many2many(
"res.partner",
"account_move_send_res_partner_bcc_rel",
"wizard_id",
"partner_id",
string="Bcc",
compute="_compute_mail_partner_cc_bcc_ids",
store=True,
readonly=False,
)

def _get_default_mail_partner_cc_ids(self, move, mail_template):
partners = self.env["res.partner"].with_company(move.company_id)
if mail_template.email_cc:
for mail_data in tools.email_split(mail_template.email_cc):
partners |= partners.find_or_create(mail_data)
return partners

def _get_default_mail_partner_bcc_ids(self, move, mail_template):
partners = self.env["res.partner"].with_company(move.company_id)
if mail_template.email_bcc:
for mail_data in tools.email_split(mail_template.email_bcc):
partners |= partners.find_or_create(mail_data)
return partners

@api.model
def default_get(self, fields_list):
company = self.env.company
res = super().default_get(fields_list)
partner_cc = company.default_partner_cc_ids
if partner_cc:
res["partner_cc_ids"] = [Command.set(partner_cc.ids)]
partner_bcc = company.default_partner_bcc_ids
if partner_bcc:
res["partner_bcc_ids"] = [Command.set(partner_bcc.ids)]
return res

@api.depends("mail_template_id")
def _compute_mail_partner_cc_bcc_ids(self):
for wizard in self:
if wizard.mode == "invoice_single" and wizard.mail_template_id:
wizard.partner_cc_ids = self._get_default_mail_partner_cc_ids(
self.move_ids, wizard.mail_template_id
)
wizard.partner_bcc_ids = self._get_default_mail_partner_bcc_ids(
self.move_ids, wizard.mail_template_id
)
else:
wizard.partner_cc_ids = None
wizard.partner_bcc_ids = None

def _get_mail_move_values(self, move, wizard=None):
mail_template_id = (
move.send_and_print_values
and move.send_and_print_values.get("mail_template_id")
)
mail_template = (
wizard
and wizard.mail_template_id
or self.env["mail.template"].browse(mail_template_id)
)
mail_lang = self._get_default_mail_lang(move, mail_template)
return {
"mail_template_id": mail_template,
"mail_lang": mail_lang,
"mail_body": wizard
and wizard.mail_body
or self._get_default_mail_body(move, mail_template, mail_lang),
"mail_subject": wizard
and wizard.mail_subject
or self._get_default_mail_subject(move, mail_template, mail_lang),
"mail_partner_ids": wizard
and wizard.mail_partner_ids
or self._get_default_mail_partner_ids(move, mail_template, mail_lang),
"mail_attachments_widget": wizard
and wizard.mail_attachments_widget
or self._get_default_mail_attachments_widget(move, mail_template),
"partner_cc_ids": wizard
and wizard.partner_cc_ids
or self._get_default_mail_partner_cc_ids(move, mail_template),
"partner_bcc_ids": wizard
and wizard.partner_bcc_ids
or self._get_default_mail_partner_bcc_ids(move, mail_template),
}

# -------------------------------------------------------------------------
# BUSINESS ACTIONS
# -------------------------------------------------------------------------

@api.model
def _send_mail(self, move, mail_template, **kwargs):
"""Send the journal entry passed as parameter by mail."""
partner_ids = kwargs.get("partner_ids", [])

new_message = move.with_context(
no_new_invoice=True,
mail_notify_author=self.env.user.partner_id.id in partner_ids,
is_from_composer=True,
partner_cc_ids=self.partner_cc_ids,
partner_bcc_ids=self.partner_bcc_ids,
).message_post(
message_type="comment",
**kwargs,
**{
"email_layout_xmlid": "mail.mail_notification_layout_with_responsible_signature",
"email_add_signature": not mail_template,
"mail_auto_delete": mail_template.auto_delete,
"mail_server_id": mail_template.mail_server_id.id,
"reply_to_force_new": False,
},
)

# Prevent duplicated attachments linked to the invoice.
new_message.attachment_ids.write(
{
"res_model": new_message._name,
"res_id": new_message.id,
}
)
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="account_invoice_send_wizard_form_inherit" model="ir.ui.view">
<field name="name">account.invoice.send.form.inherit</field>
<field name="model">account.invoice.send</field>
<field name="inherit_id" ref="account.account_invoice_send_wizard_form" />
<record id="account_move_send_form_inherit" model="ir.ui.view">
<field name="name">account.move.send.form.inherit</field>
<field name="model">account.move.send</field>
<field name="inherit_id" ref="account.account_move_send_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_ids']/.." position="after">
<xpath expr="//field[@name='mail_partner_ids']/.." position="after">
<field name='mode' invisible="1" />
<field
name="partner_cc_ids"
widget="many2many_tags_email"
context="{'force_email':True, 'show_email':True}"
attrs="{'invisible': [('composition_mode', '!=', 'comment')]}"
invisible="mode == 'invoice_multi'"
/>
<field
name="partner_bcc_ids"
widget="many2many_tags_email"
context="{'force_email':True, 'show_email':True}"
attrs="{'invisible': [('composition_mode', '!=', 'comment')]}"
invisible="mode == 'invoice_multi'"
/>
</xpath>
</field>
Expand Down

0 comments on commit f964a6a

Please sign in to comment.