[18.0][FIX] sale_operating_unit: strict journal selection for OU sale orders#845
Open
[18.0][FIX] sale_operating_unit: strict journal selection for OU sale orders#845
Conversation
_compute_journal_id used to search with a domain that mixes operating_unit_id=<so.ou> and operating_unit_id=False in a single OR clause and then took limit=1 without an explicit order. When the database returned a no-OU journal first, the SO's journal_id was silently set to it, and the invoice created from that SO inherited the wrong journal, breaking account_operating_unit's _check_journal_operating_unit downstream. This change performs two separate searches: first prefer journals whose operating_unit_id exactly matches the SO's OU; only fall back to a no-OU journal if no matching one exists. The no-OU fallback is preserved for deployments that legitimately use a shared journal across OUs. Includes a TDD regression test that creates a B2B sale order in a multi-OU company where an OU1 sale journal would otherwise be picked first, and asserts that the resulting invoice journal belongs to B2B.
4a00957 to
1c034e9
Compare
IJOL
added a commit
to BITVAX/operating-unit
that referenced
this pull request
Apr 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
`sale_operating_unit._compute_journal_id` searches for the invoicing
journal with a domain that mixes `operating_unit_id = <so.ou>` and
`operating_unit_id = False` in a single OR clause, then takes
`limit=1` without an explicit order. When the database happens to
return a no-OU journal first, the sale order's `journal_id` is
silently set to it, and the invoice created from that SO inherits
the wrong journal. Downstream,
`account_operating_unit._check_journal_operating_unit` may trip with:
This can happen in perfectly configured multi-OU companies whenever
legacy no-OU journals coexist with OU-scoped journals.
Fix
Perform two separate searches:
the SO's OU.
The no-OU fallback is preserved for deployments that legitimately use
a shared journal across OUs.
Test
Includes a TDD regression test (`test_create_invoice_journal_ou.py`)
that:
in B2B (OU1 first, so its id is lower → it would be picked first
by the previous OR-based domain);
equals B2B.
Fails on upstream/18.0, passes with this fix.