diff --git a/account_credit_control/README.rst b/account_credit_control/README.rst index 0cb392146..534179fb3 100644 --- a/account_credit_control/README.rst +++ b/account_credit_control/README.rst @@ -65,13 +65,13 @@ Control Lines* menu. On each generated line, you have many choices: -- Send a email -- Print a letter -- Change the state (so you can ignore or reopen lines) -- Mark a line as Manually Overridden. The line will get the ignored - state when a second credit control run is done. -- Mark one line as Manual followup will also mark all the lines of the - partner. The partner will be visible in "Do Manual Follow-ups". +- Send a email +- Print a letter +- Change the state (so you can ignore or reopen lines) +- Mark a line as Manually Overridden. The line will get the ignored + state when a second credit control run is done. +- Mark one line as Manual followup will also mark all the lines of the + partner. The partner will be visible in "Do Manual Follow-ups". Once your lines are properly set up, go back to the "run" and click on *Run channel action* to massively generate and queue communication @@ -105,29 +105,29 @@ Authors Contributors ------------ -- Nicolas Bessi (Camptocamp) -- Guewen Baconnier (Camptocamp) -- Sylvain Van Hoof (Okia SPRL) -- Akim Juillerat (Camptocamp) -- Kinner Vachhani (Access Bookings Ltd) -- Raf Ven -- Quentin Groulard (ACSONE) -- `Tecnativa `__: - - - Vicent Cubells - - Manuel Calero - - Ernesto Tejeda - - Pedro M. Baeza - - Jairo Llopis - - João Marques - - César A. Sánchez - - Víctor Martínez - -- Enric Tobella -- Naglis Jonaitis (Versada UAB) -- `360ERP `__: - - - Andrea Stirpe +- Nicolas Bessi (Camptocamp) +- Guewen Baconnier (Camptocamp) +- Sylvain Van Hoof (Okia SPRL) +- Akim Juillerat (Camptocamp) +- Kinner Vachhani (Access Bookings Ltd) +- Raf Ven +- Quentin Groulard (ACSONE) +- `Tecnativa `__: + + - Vicent Cubells + - Manuel Calero + - Ernesto Tejeda + - Pedro M. Baeza + - Jairo Llopis + - João Marques + - César A. Sánchez + - Víctor Martínez + +- Enric Tobella +- Naglis Jonaitis (Versada UAB) +- `360ERP `__: + + - Andrea Stirpe Maintainers ----------- diff --git a/account_credit_control/models/credit_control_communication.py b/account_credit_control/models/credit_control_communication.py index 38cd0fc16..e9b9663ee 100644 --- a/account_credit_control/models/credit_control_communication.py +++ b/account_credit_control/models/credit_control_communication.py @@ -4,7 +4,9 @@ # Copyright 2020 Manuel Calero - Tecnativa # Copyright 2023 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import _, api, fields, models +import threading + +from odoo import _, api, fields, models, modules, registry, tools from odoo.tools.misc import format_amount, format_date @@ -225,12 +227,39 @@ def _generate_emails(self): comm.credit_control_line_ids.filtered( lambda line: line.state == "to_be_sent" ).write({"state": "queued"}) + comm._send_mails() + + def _send_mails(self): + # Launch process in new thread to improve the user speedup + if not tools.config["test_enable"] and not modules.module.current_test: + + @self.env.cr.postcommit.add + def _launch_print_thread(): + threaded_calculation = threading.Thread( + target=self.send_mails_threaded, + args=self.ids, + ) + threaded_calculation.start() + else: + self._send_communications_by_email() + + def send_mails_threaded(self, record_ids): + with registry(self._cr.dbname).cursor() as cr: + self = self.with_env(self.env(cr=cr)) + communications = self.browse(record_ids) + communications._send_communications_by_email() + + def _send_communications_by_email(self): + for comm in self: comm.message_mail_with_source( comm.policy_level_id.email_template_id, subtype_id=self.env["ir.model.data"]._xmlid_to_res_id( "account_credit_control.mt_request" ), ) + comm.credit_control_line_ids.filtered( + lambda line: line.state == "queued" + ).state = "sent" def _mark_credit_line_as_sent(self): lines = self.mapped("credit_control_line_ids") diff --git a/account_credit_control/models/credit_control_run.py b/account_credit_control/models/credit_control_run.py index 77816fb7b..e44d9bcd2 100644 --- a/account_credit_control/models/credit_control_run.py +++ b/account_credit_control/models/credit_control_run.py @@ -208,6 +208,16 @@ def run_channel_action(self): comm_obj = self.env["credit.control.communication"] comms = comm_obj._generate_comm_from_credit_lines(email_lines) comms._generate_emails() + # Notify user that the emails will be sent in background + self.env["bus.bus"]._sendone( + self.env.user.partner_id, + "simple_notification", + { + "type": "info", + "title": _("Notifications"), + "message": _("The emails will be sent in the background"), + }, + ) if letter_lines: wiz = self.env["credit.control.printer"].create( {"line_ids": letter_lines.ids} diff --git a/account_credit_control/tests/test_credit_control_run.py b/account_credit_control/tests/test_credit_control_run.py index d32d58eca..d0befba7c 100644 --- a/account_credit_control/tests/test_credit_control_run.py +++ b/account_credit_control/tests/test_credit_control_run.py @@ -249,6 +249,7 @@ def test_sent_email_invoice_detail(self): self.assertIn(self.invoice.name, new_communication.message_ids.body) # CASE 2: set the policy level to show invoice details = False control_lines.policy_level_id.mail_show_invoice_detail = False + control_lines.state = "to_be_sent" marker = self.env["credit.control.marker"].create( {"name": "to_be_sent", "line_ids": [(6, 0, control_lines.ids)]} )