[FIX] account,snailmail_account: fix methods depends on env

Some methods were using the `self.env` to decide users/partners while
those methods can also be called from Cron, thus the user would end up to
be OdooBot instead of the partner that initiated the async sending.

closes odoo/odoo#157991

Signed-off-by: Laurent Smet (las) <las@odoo.com>
This commit is contained in:
Claire Bretton (clbr)
2024-03-20 09:13:27 +00:00
parent 3932c46f91
commit 6efbcae063
2 changed files with 18 additions and 14 deletions
+6 -2
View File
@@ -142,6 +142,8 @@ class AccountMoveSend(models.TransientModel):
self.ensure_one()
return {
'mail_template_id': self.mail_template_id.id,
'sp_partner_id': self.env.user.partner_id.id,
'sp_user_id': self.env.user.id,
'download': self.checkbox_download,
'send_mail': self.checkbox_send_mail,
}
@@ -452,11 +454,12 @@ class AccountMoveSend(models.TransientModel):
def _send_mail(self, move, mail_template, **kwargs):
""" Send the journal entry passed as parameter by mail. """
partner_ids = kwargs.get('partner_ids', [])
author_id = kwargs.pop('author_id')
new_message = move\
.with_context(
no_new_invoice=True,
mail_notify_author=self.env.user.partner_id.id in partner_ids,
mail_notify_author=author_id in partner_ids,
).message_post(
message_type='comment',
**kwargs,
@@ -503,6 +506,7 @@ class AccountMoveSend(models.TransientModel):
'subject': move_data['mail_subject'],
'partner_ids': move_data['mail_partner_ids'].ids,
'attachments': mail_attachments,
'author_id': move_data['sp_partner_id'],
}
@api.model
@@ -707,7 +711,7 @@ class AccountMoveSend(models.TransientModel):
if process_later:
# Set sending information on moves
for move in self.move_ids:
move.send_and_print_values = {'sp_partner_id': self.env.user.partner_id.id, **self._get_wizard_values()}
move.send_and_print_values = self._get_wizard_values()
self.env.ref('account.ir_cron_account_move_send')._trigger()
return {
'type': 'ir.actions.client',
@@ -67,7 +67,6 @@ class AccountMoveSend(models.TransientModel):
'partner_id': move.partner_id.id,
'model': 'account.move',
'res_id': move.id,
'user_id': self.env.user.id,
'company_id': move.company_id.id,
'report_template': self.env['ir.actions.report']._get_report('account.account_invoices').id
}
@@ -77,16 +76,17 @@ class AccountMoveSend(models.TransientModel):
# EXTENDS 'account'
super()._hook_if_success(moves_data, from_cron=from_cron, allow_fallback_pdf=allow_fallback_pdf)
moves = self.env['account.move']
for move, move_data in moves_data.items():
if move_data.get('send_by_post') and move.invoice_pdf_report_id:
moves |= move
if not moves:
return
self.env['snailmail.letter']\
.create([
self._prepare_snailmail_letter_values(move)
for move in moves
to_send = {
move: move_data
for move, move_data in moves_data.items()
if move_data.get('send_by_post') and move.invoice_pdf_report_id
}
if to_send:
self.env['snailmail.letter'].create([
{
'user_id': move_data.get('sp_user_id', self.env.user.id),
**self._prepare_snailmail_letter_values(move),
}
for move, move_data in to_send.items()
])\
._snailmail_print(immediate=False)