Skip to content

Commit 554dbbf

Browse files
committed
[FIX] account_operating_unit: filter payment register journals by OU
When registering a payment from the invoice form, the payment wizard shows journals from all operating units instead of filtering by the invoice's operating unit. This causes AccessError when users with restricted OU access try to create payments. Override _get_batch_available_journals in account.payment.register to filter journals by the invoice's operating unit when all invoices in the batch belong to the same OU.
1 parent 8a6ec74 commit 554dbbf

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

account_operating_unit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
from . import models
44
from . import report
5+
from . import wizard

account_operating_unit/tests/test_account_operating_unit.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# © 2019 Serpent Consulting Services Pvt. Ltd.
33
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
44

5+
from odoo import fields
56
from odoo.models import Command
67
from odoo.tests import tagged
78

@@ -181,3 +182,48 @@ def _prepare_invoice(self, operating_unit_id, name="Test Supplier Invoice"):
181182
"invoice_line_ids": lines,
182183
}
183184
return inv_vals
185+
186+
def test_payment_register_journals_filtered_by_ou(self):
187+
"""Payment register wizard should only show journals matching the
188+
invoice's operating unit. A user with access to a single OU must
189+
not see journals from other OUs, and creating a payment must not
190+
raise AccessError."""
191+
inv_vals = self._prepare_invoice(self.b2b.id, name="Test B2B Invoice")
192+
inv_vals["invoice_date"] = fields.Date.today()
193+
invoice = (
194+
self.move_model.with_user(self.user1)
195+
.with_context(default_move_type="in_invoice")
196+
.create(inv_vals)
197+
)
198+
invoice.action_post()
199+
200+
ctx = {
201+
"active_model": "account.move",
202+
"active_ids": invoice.ids,
203+
}
204+
wizard = (
205+
self.register_payments_model.with_user(self.user1)
206+
.with_context(**ctx)
207+
.create({"journal_id": self.cash2_journal_b2b.id})
208+
)
209+
210+
available_journals = wizard.available_journal_ids
211+
for journal in available_journals:
212+
self.assertTrue(
213+
not journal.operating_unit_id
214+
or journal.operating_unit_id == self.b2b,
215+
"Journal '%s' (OU=%s) should not be available for OU %s"
216+
% (
217+
journal.name,
218+
journal.operating_unit_id.name,
219+
self.b2b.name,
220+
),
221+
)
222+
223+
self.assertNotIn(
224+
self.cash_journal_ou1,
225+
available_journals,
226+
"OU1 journal should not be available when paying a B2B invoice",
227+
)
228+
229+
wizard.action_create_payments()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import account_payment_register
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
2+
3+
from odoo import api, models
4+
5+
6+
class AccountPaymentRegister(models.TransientModel):
7+
_inherit = "account.payment.register"
8+
9+
@api.model
10+
def _get_batch_available_journals(self, batch_result):
11+
"""Filter available journals by the operating unit of the invoices."""
12+
journals = super()._get_batch_available_journals(batch_result)
13+
lines = batch_result.get("lines")
14+
if lines:
15+
invoice_ous = lines.move_id.operating_unit_id
16+
if invoice_ous and len(invoice_ous) == 1:
17+
journals = journals.filtered(
18+
lambda j: not j.operating_unit_id
19+
or j.operating_unit_id == invoice_ous
20+
)
21+
return journals

0 commit comments

Comments
 (0)