Skip to content

[18.0][FIX] payroll_account: company-aware accounts, real reversal on cancel, computed journal - #308

Open
nimarosa wants to merge 15 commits into
OCA:18.0from
nimarosa:18.0-fix-payroll_account
Open

nimarosa wants to merge 15 commits into
OCA:18.0from
nimarosa:18.0-fix-payroll_account

Conversation

@nimarosa

Copy link
Copy Markdown
Contributor
  • Debit/credit accounts, analytic account and taxes are now resolved in the
    payslip's own company instead of the confirming user's active company.
  • Cancelling a payslip reverses its posted entry instead of deleting it, and
    confirming twice no longer duplicates the accounting entry.
  • journal_id is a real computed/editable field (batch > contract) instead
    of a readonly mirror of the contract's journal.
  • Adds a README, a smart button to the journal entry, clearer error
    messages, and fixes a broken test fixture.

@OCA-git-bot

Copy link
Copy Markdown
Contributor

Hi @appstogrow,
some modules you are maintaining are being modified, check this out!

… company

`hr.salary.rule.account_debit` and `account_credit` are company-dependent
fields, but `action_payslip_done` read them off the bare recordset, i.e. in
`self.env.company`.  Confirming a payslip while working in another company
therefore silently produced an entry with the wrong accounts -- or, when the
rule has no value configured for that company, no accounting entry at all and
no error.

Every read of the rule's accounting configuration now goes through
`with_company(<the payslip's company>)`, and the generated `account.move`
carries that company explicitly instead of inheriting it from the environment.

Also tightened around the same problem:

* `hr.contract.journal_id` and `hr.payslip.run.journal_id` get `check_company`
  and a company domain, and the batch's default journal is looked up in the
  active company instead of taking the first `general` journal of any company.
* `hr.salary.rule.account_debit` / `account_credit` get `check_company`.
* `_get_tax_details` filters the `account.tax.repartition.line` lookups by
  company, and limits the one whose `.id` is read to a single record -- a tax
  spread over several repartition lines on the same account made that read
  raise `Expected singleton`.  Tax and tag values are built with `Command`.
…g it

Cancelling a payslip deleted its accounting entry outright -- posted or not --
with `force_delete=True`, unless the journal happened to have the hash table
restriction enabled. That destroys the audit trail: a posted entry disappears
from the books with no trace, and leaves a hole in the journal's sequence.
The deletion also ran BEFORE `super()`, which can still refuse the
cancellation, so a refused cancel could leave a confirmed payslip whose entry
had already been destroyed.

The entry is now always reversed, after `super()` has accepted the
cancellation, with an explicit reversal date (the entry's own date, or today
when the entry is backdated) so that nothing is posted before the entry it
reverses. An entry that was never posted is still simply deleted -- nothing
ever reached the books.

Confirming a payslip twice created a second entry and doubled every amount in
the books, because nothing checked for an existing `move_id`. It is now a
no-op, logged. So that cancelling and confirming again still generates a
fresh entry, the reversed pair is moved to a new `cancelled_move_ids` field,
which is also where the audit trail now lives.

Finally, a payslip that still carries a posted entry can no longer be
deleted: it has to be cancelled first, so that its entry is reversed.
…mirror of the contract

`hr.payslip.journal_id` was a stored *related* field on
`contract_id.journal_id`. Being related it is readonly, so the form -- which
marks it `required="1"` -- offered a field nobody could fill in: a payslip
whose contract has no journal could not be confirmed and had no way out.
Being related it was also recomputed from the contract, with three
consequences:

* the batch's journal was ignored. `hr.payslip.employees.compute_sheet`
  injects `default_journal_id` from the batch precisely so the payslips of a
  batch land in the batch's journal; the related field overwrote it with the
  contract's on the spot;
* the same happened to any journal set by hand or by another module;
* editing a contract's journal rewrote the journal of payslips that were
  already confirmed and posted, after the fact.

It becomes an ordinary stored computed field with `readonly=False`, depending
on `contract_id` and `payslip_run_id` themselves rather than on their journal:
it proposes a journal when the contract or the batch changes and never
overwrites the payslip afterwards. The batch's journal wins over the
contract's. `check_company` and a company domain were added while the field
was being redefined.

The `onchange_contract` override goes away: it existed to work around the
related field, and the compute now covers it.
…s and fixtures

* `readme/CONFIGURE.md` was mangled -- its list markers were escaped, so the
  whole thing rendered as one paragraph starting with a stray `#`. Rewritten,
  and it now says what the module actually does: debit/credit per rule and
  that both are company-dependent, the partner rule per account type, the
  analytic precedence (contract over rule), and the journal's default account
  as the adjustment plug.
* Added `readme/USAGE.md`: one entry per payslip, confirming twice is a no-op,
  cancel reverses rather than deletes, refunds invert the sides.
* The payslip form showed the accounting entry as a bare readonly Many2one.
  It is a smart button now, next to the payslip lines one.
* The two "no default account" errors named an *Expense Journal* and a
  *Credit/Debit Account* that exist nowhere in the module. They now name the
  salary journal and its default account, and say what the account is used
  for.
* `logger.info` was called with an f-string; it takes lazy arguments now.
* Dropped the `_description` on the `hr.contract` extension: `_inherit`
  already carries the base model's.
* Test fixtures: `hr.employee.bank_account_id` was given the id of a
  `res.bank`, not of the `res.partner.bank` created right above it; the
  employee's bank account was therefore an unrelated record, and the
  bank-account branch of `_get_partner_id` was never really exercised. Fixed,
  and covered by a test. The fixture also moved to `setUpClass`.
@nimarosa
nimarosa force-pushed the 18.0-fix-payroll_account branch from 59d71a6 to 6af5ec9 Compare September 22, 2026 16:25
@nimarosa

nimarosa commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping, could someone take a look at this one? It's been a couple of weeks. Thanks.

@mileo

mileo commented Oct 22, 2026

Copy link
Copy Markdown
Member

Thanks for this, it fixes the empty journal we hit on 18.0 when the payslip is created from the form.

One case is still left: a payslip with no batch and a contract without journal_id keeps journal_id empty, and
confirming it fails later in payroll_account. In 16.0 the default fell back to the first general journal of the
company (_get_default_journal in payroll_account/models/hr_payslip.py). Would you add that as the last step of
_compute_journal_id, something like
or self.env["account.journal"].search([("type", "=", "general"), ("company_id", "=", payslip.company_id.id)], limit=1)?
Happy to open a follow-up PR on top of this one if you prefer to keep the scope.

@nimarosa

nimarosa commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @mileo, good catch. Added here instead of a follow-up, in bf82060:

  • _compute_journal_id now falls back to the first general journal of the payslip's own company (same default the batch already uses), after the batch and the contract. A journal of another company is not kept when the payslip's company changes.
  • Confirming a payslip that still has no journal (company without any general journal) raises a clear UserError before building the entry, instead of failing on account.move.
  • Three new tests: no batch and contract without journal, the multi-company case, and the company without a general journal. readme/CONFIGURE.md lists the fallback as the last step of the journal precedence.

Small note for the record: in 16.0 this was an inline default= on the field, without a company filter, not a _get_default_journal method.

@mileo @appstogrow @CristianoMafraJunior @SirPyTech @marcelsavegnago @dreispt a review would be much appreciated. Thanks!

@nimarosa

Copy link
Copy Markdown
Contributor Author

@dreispt @mileo can you review and approve? We need to merge this before migrating modules to other versions.

@dreispt dreispt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have concerns with:

  • Possibly unneeded message rewrites, breaking translations.
  • Excessive low level uses of with_company triggering inefficient records rebrowse

Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/models/hr_payslip.py Resolved
Comment thread payroll_account/__manifest__.py Resolved
A payslip with no batch and a contract without journal kept journal_id
empty, and confirming it failed later when creating the account.move.

When neither the batch nor the contract has a journal, propose the first
general journal of the payslip's own company, as the batch already does
by default. A journal of another company is no longer kept when the
payslip's company changes. If the payslip still has no journal when it
is confirmed, raise a clear UserError before building the entry.
… entry to post

The check added to action_payslip_done raised whenever the payslip had no
journal, before the move lines were built. A company without accounting
(no chart of accounts, no general journal) whose salary rules have no
accounts could no longer confirm any payslip, although there is nothing to
post.

Raise the "no salary journal" error only when the payslip generated move
lines; the adjustment lines that need the journal's default account can
only exist then too. A payslip without journal and without accounted lines
confirms again and creates no entry.
When neither the batch nor the contract has a journal, the payslip falls
back to the first general journal of its company. That fallback was
inlined in _compute_journal_id, so a submodule that lets each company
configure its salary journal could only fill the gap after super() had
already chosen a general journal, and never got a say.

Move the fallback to _get_default_salary_journal, which submodules can
override to propose a company-configured journal and call super() when
there is none.
Rewording the two "no default account" errors broke their existing
translations. Restore the original strings.
Calling `with_company` on every salary rule of every line re-read the
records each time. The payslip is now switched to its accounting company
once, at the start of the loop in `action_payslip_done`, so its lines and
their salary rules are read in that company, and the line helpers no longer
switch it themselves.

The values of the `account.move` are built by a new
`_prepare_account_move_vals` hook.
It no longer requires a single record: it skips the payslips without an
entry, reverses or drops the others, and returns the reversals it created.
`action_payslip_cancel` just calls it on `self`.
…n entry

Confirming a payslip that still carries an accounting entry was logged and
skipped. That entry may not match what the payslip computes now, so keeping
it silently hides a difference in the books. Raise a UserError instead, and
let the user cancel the payslip (which reverses the entry) or correct it by
hand.
The journal is the batch's, else the contract's, else the one proposed by
`_get_default_salary_journal`. The step that kept the payslip's current
journal when it belonged to the payslip's company is gone: the compute only
runs when the batch, the contract or the company changes.
@nimarosa
nimarosa force-pushed the 18.0-fix-payroll_account branch from a925f7e to 886bf9b Compare November 11, 2026 14:06
@nimarosa

Copy link
Copy Markdown
Contributor Author

Thanks for the review @dreispt. Pushed the changes:

  1. Translations: restored the original wording of the two journal messages.
  2. with_company: one with_company per payslip at the start of action_payslip_done, and none in the line helpers.
  3. _cancel_accounting_entry: works on recordsets now and skips payslips without an entry.
  4. Confirming a payslip that already has an entry: it raises instead of skipping.
  5. Move values: moved to a _prepare_account_move_vals() method.
  6. Version: reverted, I leave the bump to the bot.
  7. Journal compute: dropped the "keep the current journal" step, it is simpler.
  8. @api.depends: I would keep it on the records, not on their journal. With contract_id.journal_id there, changing the journal on a contract rewrites it on all its payslips, including confirmed ones. That is one of the bugs this PR fixes.
  9. _get_accounting_company: it is just company_id with a fallback, no extra reads. I would keep it as a small hook since another module uses it, but I can inline company_id if you prefer.

@nimarosa
nimarosa requested a review from dreispt November 11, 2026 14:15
@dreispt

dreispt commented Nov 11, 2026

Copy link
Copy Markdown
Member

On 8. good point, you're right.
I also see the below flag on my end that can be worth checking:

payroll_account/models/hr_payslip.py:R65-68

Custom salary journal silently replaced

When a payslip changes contracts or batches without a configured journal, _compute_journal_id replaces its manually selected journal with the general fallback. Confirming the payslip then posts its entry to the wrong journal.

I see one more thing worth investigating:

payroll_account/models/hr_payslip.py:R178

Wrong partner on cross-company payroll entries

When account types differ across companies, salary_rule supplies the payslip company's account but partner selection uses the active company's account. The partner helper reads the rule without switching companies, leaving payable items without their intended partner.

A salary rule has company-dependent debit and credit accounts. The entry builder now reads them with the payslip's company, but _get_partner_id reads the same fields from the original payslip line in the active company. Partner selection therefore can disagree with the account actually posted. This matters for payable, receivable, and current-liability accounts, whose partner is chosen from either the contribution register or the employee.

Example: Company A's credit account is an expense and company B's credit account is payable. A user active in A confirms a B payslip; its move uses B's payable account, but partner selection sees A's expense account and assigns no register partner.

…as none

Reverts the simplification of the journal compute: without the step that
keeps the payslip's current journal, moving a payslip to a contract without
a journal replaced a journal chosen by hand with the company's fallback
journal, silently, and the entry was then posted in the wrong journal.

The precedence is again: the batch's journal, the contract's, the payslip's
current journal when it belongs to the payslip's company, and last
`_get_default_salary_journal`. Tests cover the hand-picked journal and the
journal of another company that must not be kept.
…ntry

The partner of each journal item depends on the type of its account, and
the debit/credit accounts are company-dependent. Cover a payslip of one
company confirmed from another, with different account types per company:
the register and employee partners follow the payslip's company.
@nimarosa

Copy link
Copy Markdown
Contributor Author

Thanks @dreispt!

  1. Journal replaced: that is what the step I dropped in point 7 was for. It kept a journal picked by hand when the new contract has none. I put it back, with a test.
  2. Partner: this one is fine. After the with_company change the lines come from slip.line_ids in the payslip's company, so _get_partner_id reads the rule accounts in that company. I added a test for the case you describe and it passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants