From 6a133f736d8c16965c140178993d480e4f6dffdf Mon Sep 17 00:00:00 2001 From: "lase@odoo.com" Date: Tue, 16 Jan 2024 10:26:12 +0000 Subject: [PATCH] [FIX] snailmail_account: generate snailmail Current behavior: If a customer has no email set, creating an invoice for that customer and clicking on send and print will not generate a snailmail for the invoice, even if, the checkbox (checkbox_send_by_post) is True. Expected behavior: A snailmail should be sent to the customer. Steps to reproduce: Using module_account from saas-16.2 to 17.0. Create a customer invoice for a customer without email > print and send > check "By Post" > send and print. Cause of the issue: The action action_send_and_print defined in account_move_send.py filters the moves that trigger a mail creation in the var "success". This variable filters out all moves without a partner_id.email. This makes perfect sense for emails but not for snailmails. However, creations of both types of mails are triggered by the _hook_if_success method taking "success" as an argument. Fix: To allow snailmail creations and correctly trigger email creation, we filter the moves with a partner email after the _hook_if_success method and only for email creation, not for snailmails. opw-3668487 closes odoo/odoo#152506 X-original-commit: b17a2c594aed08248c30842c62d2030de3087b51 Signed-off-by: Florian Gilbert (flg) Signed-off-by: Lancelot Semal (lase) --- addons/account/wizard/account_move_send.py | 4 +- addons/snailmail_account/tests/__init__.py | 1 + .../tests/test_snailmail_on_invoice.py | 46 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 addons/snailmail_account/tests/test_snailmail_on_invoice.py diff --git a/addons/account/wizard/account_move_send.py b/addons/account/wizard/account_move_send.py index 0661de1d9a8..00366a13c48 100644 --- a/addons/account/wizard/account_move_send.py +++ b/addons/account/wizard/account_move_send.py @@ -501,7 +501,7 @@ class AccountMoveSend(models.TransientModel): def _send_mails(self, moves_data): subtype = self.env.ref('mail.mt_comment') - for move, move_data in moves_data.items(): + for move, move_data in [(move, move_data) for move, move_data in moves_data.items() if move.partner_id.email]: mail_template = move_data['mail_template_id'] mail_lang = move_data['mail_lang'] mail_params = self._get_mail_params(move, move_data) @@ -659,7 +659,7 @@ class AccountMoveSend(models.TransientModel): self._generate_invoice_fallback_documents(errors) # Send mail. - success = {move: move_data for move, move_data in moves_data.items() if not move_data.get('error') and move.partner_id.email} + success = {move: move_data for move, move_data in moves_data.items() if not move_data.get('error')} if success: self._hook_if_success(success, from_cron=from_cron, allow_fallback_pdf=allow_fallback_pdf) diff --git a/addons/snailmail_account/tests/__init__.py b/addons/snailmail_account/tests/__init__.py index b81c3a2d614..b7f7b9f9004 100644 --- a/addons/snailmail_account/tests/__init__.py +++ b/addons/snailmail_account/tests/__init__.py @@ -1 +1,2 @@ from . import test_pingen_send +from . import test_snailmail_on_invoice diff --git a/addons/snailmail_account/tests/test_snailmail_on_invoice.py b/addons/snailmail_account/tests/test_snailmail_on_invoice.py new file mode 100644 index 00000000000..9b2f9979737 --- /dev/null +++ b/addons/snailmail_account/tests/test_snailmail_on_invoice.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.tests import TransactionCase, tagged +from odoo import Command + +@tagged('post_install', '-at_install') +class TestSnailmailOnInvoice(TransactionCase): + def test_snailmail_on_invoice_for_partner_without_email(self): + """" + Checks that a snailmail letter is created when one print and send by post a customer invoice + for a partner without email. + """ + + partner_without_email = self.env['res.partner'].create({ + 'name': 'Partner_without_email', + 'email': False, + }) + + product = self.env['product.product'].create({ + 'name': 'product', + 'sale_ok': True, + 'standard_price': 100.0, + }) + + invoice = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'partner_id': partner_without_email.id, + 'invoice_date': '2019-01-01', + 'invoice_line_ids': [Command.create({'product_id': product.id})], + }) + invoice.action_post() + + print_wiz = self.env['account.move.send'].create({ + 'move_ids': invoice.ids, + 'checkbox_download': False, + 'checkbox_send_mail': False, + 'checkbox_send_by_post': True, + }) + print_wiz.action_send_and_print() + + letter = self.env['snailmail.letter'].search([ + ('partner_id', '=', partner_without_email.id), + ('model', '=', invoice._name), + ('res_id', '=', invoice.id), + ], limit=1) + self.assertTrue(letter)