[18.0][FIX] payroll_account: company-aware accounts, real reversal on cancel, computed journal #308
Hi @appstogrow,
some modules you are maintaining are being modified, check this out!
Friendly ping, could someone take a look at this one? It's been a couple of weeks. Thanks.
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 likeor 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.
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.UserError before building the entry, instead of failing on account.move.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!
@dreispt @mileo can you review and approve? We need to merge this before migrating modules to other versions.
@api.depends("payslip_run_id.journal_id", "contract_id.journal_id", "company_id")
Seems overcomplicated.
I expect the payslip to have benn browsed with the right company context, and not need this.
self._cancel_accounting_entry()
And have
_cancel_accounting_entry() be recorset aware and behave properly for records without move_idThis sounds like a silent data error - the old move_id would have different values from the current payslip.
I understand this would be an exception case, and it would be reasonable to error and request manual correction.
It would be more efficient to just have this as a field on the payslip.
Use the _prepare_method pattern
The per line with_company causes a reread of the record data and is inefficient.
Favor a single top level with_company operation on the recordset instead (when starting computation, for example)
Avoid changing translatable terms, unless it is really important, because it breaks existing translations.
Leave version to be bumped by the ocabot.
I have concerns with:
with_company triggering inefficient records rebrowseThanks 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.
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](payroll_account/models/hr_payslip_line.py:34-38) 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](payroll_account/models/hr_payslip_line.py:34-38) 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.
Approved these changes.
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.
Code review only.
This is a comprehensive and well-thought-out set of fixes for payroll_account:
with_company(payslip.company_id) prevents silent multi-company posting bugs.cancelled_move_ids) and blocking double-confirmation or deletion of posted entries properly preserves the accounting audit trail.journal_id a stored computed field with the fallback chain (batch > contract > default general journal, keeping manual selections intact) and only requiring it when an entry actually needs to be posted addresses the earlier review feedback cleanly.The added hook for submodules, cross-company partner tests, and updated documentation look very solid.
This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖
/ocabot merge minor
Hey, thanks for contributing! Proceeding to merge this for you.
Prepared branch 18.0-ocabot-merge-pr-308-by-dreispt-bump-minor, awaiting test results.
Approved these changes.
Congratulations, your PR was merged at ecda16882f24fffa58f8ca42ae09e280d889ab68. Thanks a lot for contributing to OCA. ❤️
dreispt
nimarosa
payslip's own company instead of the confirming user's active company.
confirming twice no longer duplicates the accounting entry.
journal_idis a real computed/editable field (batch > contract) insteadof a readonly mirror of the contract's journal.
messages, and fixes a broken test fixture.