|
| 1 | +# © 2026 BITVAX |
| 2 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). |
| 3 | +"""Bug: account.payment.register wizard's available_journal_ids |
| 4 | +includes journals from operating units the invoice does not belong to. |
| 5 | +
|
| 6 | +A B2B invoice should only offer journals whose operating_unit_id |
| 7 | +matches B2B (or no OU). On upstream/18.0 the wizard pulls journals via |
| 8 | +``account.journal.search()`` without OU context, so it returns every |
| 9 | +sale/cash journal of the company regardless of OU. |
| 10 | +
|
| 11 | +The fix is to override |
| 12 | +``account.payment.register._get_batch_available_journals`` and filter |
| 13 | +the returned set by the operating_unit_id of the invoices in the |
| 14 | +batch. |
| 15 | +""" |
| 16 | + |
| 17 | +from odoo.models import Command |
| 18 | +from odoo.tests import tagged |
| 19 | + |
| 20 | +from odoo.addons.account.tests.common import AccountTestInvoicingCommon |
| 21 | +from odoo.addons.operating_unit.tests.common import OperatingUnitCommon |
| 22 | + |
| 23 | + |
| 24 | +@tagged("post_install", "-at_install") |
| 25 | +class TestPaymentRegisterJournalOu(AccountTestInvoicingCommon, OperatingUnitCommon): |
| 26 | + @classmethod |
| 27 | + def setUpClass(cls): |
| 28 | + super().setUpClass() |
| 29 | + |
| 30 | + (cls.ou1 | cls.b2b | cls.b2c).sudo().write({"company_id": cls.company.id}) |
| 31 | + |
| 32 | + cls.env.user.sudo().write( |
| 33 | + { |
| 34 | + "groups_id": [ |
| 35 | + Command.link( |
| 36 | + cls.env.ref("operating_unit.group_manager_operating_unit").id |
| 37 | + ), |
| 38 | + ], |
| 39 | + "operating_unit_ids": [ |
| 40 | + Command.link(cls.ou1.id), |
| 41 | + Command.link(cls.b2b.id), |
| 42 | + ], |
| 43 | + "default_operating_unit_id": cls.ou1.id, |
| 44 | + "company_ids": [Command.link(cls.company.id)], |
| 45 | + "company_id": cls.company.id, |
| 46 | + } |
| 47 | + ) |
| 48 | + |
| 49 | + cls.user1.write( |
| 50 | + { |
| 51 | + "groups_id": [ |
| 52 | + ( |
| 53 | + 3, |
| 54 | + cls.env.ref("operating_unit.group_manager_operating_unit").id, |
| 55 | + ), |
| 56 | + Command.link( |
| 57 | + cls.env.ref("operating_unit.group_multi_operating_unit").id |
| 58 | + ), |
| 59 | + Command.link(cls.env.ref("account.group_account_invoice").id), |
| 60 | + ], |
| 61 | + "assigned_operating_unit_ids": [(6, 0, [cls.b2b.id])], |
| 62 | + "default_operating_unit_id": cls.b2b.id, |
| 63 | + "company_id": cls.company.id, |
| 64 | + "company_ids": [Command.link(cls.company.id)], |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + Journal = cls.env["account.journal"].sudo() |
| 69 | + cls.purchase_journal_b2b = Journal.create( |
| 70 | + { |
| 71 | + "name": "Vendor Bills B2B (test_pay_reg)", |
| 72 | + "code": "TPRPB", |
| 73 | + "type": "purchase", |
| 74 | + "company_id": cls.company.id, |
| 75 | + "operating_unit_id": cls.b2b.id, |
| 76 | + } |
| 77 | + ) |
| 78 | + cls.cash_journal_ou1 = Journal.create( |
| 79 | + { |
| 80 | + "name": "Cash OU1 (test_pay_reg)", |
| 81 | + "code": "TPRC1", |
| 82 | + "type": "cash", |
| 83 | + "company_id": cls.company.id, |
| 84 | + "operating_unit_id": cls.ou1.id, |
| 85 | + } |
| 86 | + ) |
| 87 | + cls.cash_journal_b2b = Journal.create( |
| 88 | + { |
| 89 | + "name": "Cash B2B (test_pay_reg)", |
| 90 | + "code": "TPRCB", |
| 91 | + "type": "cash", |
| 92 | + "company_id": cls.company.id, |
| 93 | + "operating_unit_id": cls.b2b.id, |
| 94 | + } |
| 95 | + ) |
| 96 | + cls.expense_account = cls.env["account.account"].search( |
| 97 | + [ |
| 98 | + ("account_type", "=", "expense"), |
| 99 | + ("company_ids", "in", cls.company.ids), |
| 100 | + ], |
| 101 | + limit=1, |
| 102 | + ) |
| 103 | + |
| 104 | + def test_payment_register_filters_journals_by_invoice_ou(self): |
| 105 | + invoice = ( |
| 106 | + self.env["account.move"] |
| 107 | + .with_context(default_move_type="in_invoice") |
| 108 | + .create( |
| 109 | + { |
| 110 | + "partner_id": self.partner1.id, |
| 111 | + "operating_unit_id": self.b2b.id, |
| 112 | + "invoice_date": "2026-01-01", |
| 113 | + "journal_id": self.purchase_journal_b2b.id, |
| 114 | + "invoice_line_ids": [ |
| 115 | + ( |
| 116 | + 0, |
| 117 | + 0, |
| 118 | + { |
| 119 | + "name": "Line", |
| 120 | + "quantity": 1, |
| 121 | + "price_unit": 100.0, |
| 122 | + "account_id": self.expense_account.id, |
| 123 | + "tax_ids": [], |
| 124 | + }, |
| 125 | + ) |
| 126 | + ], |
| 127 | + } |
| 128 | + ) |
| 129 | + ) |
| 130 | + invoice.action_post() |
| 131 | + |
| 132 | + wizard = ( |
| 133 | + self.env["account.payment.register"] |
| 134 | + .with_user(self.user1) |
| 135 | + .with_context( |
| 136 | + active_model="account.move", |
| 137 | + active_ids=invoice.ids, |
| 138 | + ) |
| 139 | + .create({"journal_id": self.cash_journal_b2b.id}) |
| 140 | + ) |
| 141 | + available = wizard.available_journal_ids |
| 142 | + self.assertIn( |
| 143 | + self.cash_journal_b2b, |
| 144 | + available, |
| 145 | + "B2B cash journal must be available to pay a B2B invoice.", |
| 146 | + ) |
| 147 | + self.assertNotIn( |
| 148 | + self.cash_journal_ou1, |
| 149 | + available, |
| 150 | + "OU1 cash journal must NOT be available when paying a " "B2B invoice.", |
| 151 | + ) |
0 commit comments