[FIX] account: seek for lines for old outstanding accounts

When having reconciled a payment with an invoice
after having changed the outstanding receipts account
on the bank journal, we get an error when unreconciliing
the payment and the move.

Steps:

- Create two new outstanding receipts accounts X and Y
- Set X as outstandings receipts account on the Bank journal
- Create a manual customer payment P for $100, confirm
- On Bank journal, change the outstanding receipts account from
  X to Y
- Create and confirm an invoice for $100
- Reconcile with payment P from the widget
- Unreconcile or reset invoice to draft
-> Error: "Journal Entry %s is not valid. In order to proceed,
the journal items must include one and only one outstanding
payments/receipts account."

opw-3659092

closes odoo/odoo#155558

Signed-off-by: Laurent Smet (las) <las@odoo.com>
This commit is contained in:
guva-odoo
2024-03-04 09:52:02 +00:00
parent 39ba96b0ac
commit f648aedd2b
2 changed files with 44 additions and 9 deletions
+15 -9
View File
@@ -182,20 +182,26 @@ class AccountPayment(models.Model):
'''
self.ensure_one()
liquidity_lines = self.env['account.move.line']
counterpart_lines = self.env['account.move.line']
writeoff_lines = self.env['account.move.line']
# liquidity_lines, counterpart_lines, writeoff_lines
lines = [self.env['account.move.line'] for _dummy in range(3)]
valid_account_types = self._get_valid_payment_account_types()
for line in self.move_id.line_ids:
if line.account_id in self._get_valid_liquidity_accounts():
liquidity_lines += line
elif line.account_id.account_type in valid_account_types or line.account_id == self.company_id.transfer_account_id:
counterpart_lines += line
lines[0] += line # liquidity_lines
elif line.account_id.account_type in valid_account_types or line.account_id == line.company_id.transfer_account_id:
lines[1] += line # counterpart_lines
else:
writeoff_lines += line
lines[2] += line # writeoff_lines
return liquidity_lines, counterpart_lines, writeoff_lines
# In some case, there is no liquidity or counterpart line (after changing an outstanding account on the journal for example)
# In that case, and if there is one writeoff line, we take this line and set it as liquidity/counterpart line
if len(lines[2]) == 1:
for i in (0, 1):
if not lines[i]:
lines[i] = lines[2]
lines[2] -= lines[2]
return lines
def _get_valid_liquidity_accounts(self):
return (
@@ -1002,3 +1002,32 @@ class TestAccountPayment(AccountTestInvoicingCommon):
{'account_id': bank_2.inbound_payment_method_line_ids.payment_account_id.id},
{'account_id': transfer_account.id},
])
def test_reconciliation_with_old_oustanding_account(self):
"""
Test the reconciliation of an invoice with a payment after changing the outstanding account of the journal.
"""
outstanding_account_1 = self.company_data['company'].account_journal_payment_debit_account_id.copy()
outstanding_account_2 = outstanding_account_1.copy()
self.company_data['default_journal_bank'].inbound_payment_method_line_ids.payment_account_id = outstanding_account_1
payment = self.env['account.payment'].create({
'payment_type': 'inbound',
'partner_type': 'customer',
'partner_id': self.partner_a.id,
'journal_id': self.company_data['default_journal_bank'].id,
'amount': 1150,
})
payment.action_post()
self.company_data['default_journal_bank'].inbound_payment_method_line_ids.payment_account_id = outstanding_account_2
invoice = self.init_invoice('out_invoice', post=True, amounts=[1000.0], taxes=self.env['account.tax'])
credit_line = payment.line_ids.filtered(lambda l: l.credit and l.account_id == self.company_data['default_account_receivable'])
invoice.js_assign_outstanding_line(credit_line.id)
self.assertTrue(invoice.payment_state in ('in_payment', 'paid'), "Invoice should be paid")
invoice.button_draft()
self.assertTrue(invoice.payment_state == 'not_paid', "Invoice should'nt be paid anymore")
self.assertTrue(invoice.state == 'draft', "Invoice should be draft")