diff --git a/models/account_payment.py b/models/account_payment.py index 1c0b448..cd7568b 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -124,4 +124,45 @@ class AccountPayment(models.Model): payment.realization_id.expense_sheet_id.invalidate_recordset(['state']) return res - + def _seek_for_lines(self): + """ + Override _seek_for_lines to explicitly force the destination_account_id + to be recognized as the counterpart_line. + + Native Odoo sometimes misclassifies the advance/expense account (e.g. 118101) + as a write-off line if it's not strictly a receivable/payable account type. + Simultaneously, if a tax deduction account (e.g. 217103 PPh 23) is a payable account type, + Odoo erroneously classifies the deduction as the counterpart. + This causes duplicate lines when regenerating the journal entries. + """ + self.ensure_one() + + # Capture the original output + liquidity_lines, counterpart_lines, writeoff_lines = super()._seek_for_lines() + + # If there's only one line in write-off, Odoo native might have forcefully moved it + # to counterpart. But we need to make sure the correct counterpart is identified based + # on the configured destination account. + + if self.destination_account_id: + # We want to re-evaluate counterpart vs writeoff based purely on destination_account_id + + # Reset counterpart and writeoff + all_non_liquidity = counterpart_lines + writeoff_lines + new_counterpart = self.env['account.move.line'] + new_writeoff = self.env['account.move.line'] + + for line in all_non_liquidity: + # The primary destination account is the counterpart + if line.account_id == self.destination_account_id: + new_counterpart += line + else: + new_writeoff += line + + # In edge cases where Odoo couldn't find ANY counterpart, let's preserve Odoo's fallback + # if our logic resulted in empty counterpart but native didn't. + # However, typically the destination_account_id *must* be the counterpart. + if new_counterpart: + return liquidity_lines, new_counterpart, new_writeoff + + return liquidity_lines, counterpart_lines, writeoff_lines