Skip to content

Commit a10f3b8

Browse files
committed
[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#158340 X-original-commit: 6efbcae Signed-off-by: Laurent Smet (las) <las@odoo.com> Signed-off-by: Claire Bretton (clbr) <clbr@odoo.com>
1 parent f042f60 commit a10f3b8

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

addons/account/wizard/account_move_send.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def _get_wizard_values(self):
142142
self.ensure_one()
143143
return {
144144
'mail_template_id': self.mail_template_id.id,
145+
'sp_partner_id': self.env.user.partner_id.id,
146+
'sp_user_id': self.env.user.id,
145147
'download': self.checkbox_download,
146148
'send_mail': self.checkbox_send_mail,
147149
}
@@ -452,11 +454,12 @@ def _hook_if_success(self, moves_data, from_cron=False, allow_fallback_pdf=False
452454
def _send_mail(self, move, mail_template, **kwargs):
453455
""" Send the journal entry passed as parameter by mail. """
454456
partner_ids = kwargs.get('partner_ids', [])
457+
author_id = kwargs.pop('author_id')
455458

456459
new_message = move\
457460
.with_context(
458461
no_new_invoice=True,
459-
mail_notify_author=self.env.user.partner_id.id in partner_ids,
462+
mail_notify_author=author_id in partner_ids,
460463
).message_post(
461464
message_type='comment',
462465
**kwargs,
@@ -503,6 +506,7 @@ def _get_mail_params(self, move, move_data):
503506
'subject': move_data['mail_subject'],
504507
'partner_ids': move_data['mail_partner_ids'].ids,
505508
'attachments': mail_attachments,
509+
'author_id': move_data['sp_partner_id'],
506510
}
507511

508512
@api.model
@@ -707,7 +711,7 @@ def action_send_and_print(self, force_synchronous=False, allow_fallback_pdf=Fals
707711
if process_later:
708712
# Set sending information on moves
709713
for move in self.move_ids:
710-
move.send_and_print_values = {'sp_partner_id': self.env.user.partner_id.id, **self._get_wizard_values()}
714+
move.send_and_print_values = self._get_wizard_values()
711715
self.env.ref('account.ir_cron_account_move_send')._trigger()
712716
return {
713717
'type': 'ir.actions.client',

addons/snailmail_account/wizard/account_move_send.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def _prepare_snailmail_letter_values(self, move):
6767
'partner_id': move.partner_id.id,
6868
'model': 'account.move',
6969
'res_id': move.id,
70-
'user_id': self.env.user.id,
7170
'company_id': move.company_id.id,
7271
'report_template': self.env['ir.actions.report']._get_report('account.account_invoices').id
7372
}
@@ -77,16 +76,17 @@ def _hook_if_success(self, moves_data, from_cron=False, allow_fallback_pdf=False
7776
# EXTENDS 'account'
7877
super()._hook_if_success(moves_data, from_cron=from_cron, allow_fallback_pdf=allow_fallback_pdf)
7978

80-
moves = self.env['account.move']
81-
for move, move_data in moves_data.items():
82-
if move_data.get('send_by_post') and move.invoice_pdf_report_id:
83-
moves |= move
84-
if not moves:
85-
return
86-
87-
self.env['snailmail.letter']\
88-
.create([
89-
self._prepare_snailmail_letter_values(move)
90-
for move in moves
79+
to_send = {
80+
move: move_data
81+
for move, move_data in moves_data.items()
82+
if move_data.get('send_by_post') and move.invoice_pdf_report_id
83+
}
84+
if to_send:
85+
self.env['snailmail.letter'].create([
86+
{
87+
'user_id': move_data.get('sp_user_id', self.env.user.id),
88+
**self._prepare_snailmail_letter_values(move),
89+
}
90+
for move, move_data in to_send.items()
9191
])\
9292
._snailmail_print(immediate=False)

0 commit comments

Comments
 (0)