Files
odoo_source/addons/account/tests/test_account_payment.py
T
william-andre d8d47f9ff8 [REF] accounting v16. Yeeeeaah
TLDR:
* invoices are implemented using computed methods instead of onchange
* the synchronization only happens when switching tabs in the Form view
  to improve perfs.

_______________________________________________________________________

The whole engine of the synchronization of Invoices to the Journal
Entries has been refactored
* by using computed fields instead of onchange functions
* by synchronizing only from invoice to journal entry in `create` and
  `write`
* by saving when switching tabs on the Invoice form, to synchronize
  before showing the values

This comes with numerous advantages:
* no need to call the onchange methods manually
* no need to use the Form emulator to build invoices (i.e. EDI, OCR,
  intercompany, ...)
* the performance for invoices with many lines improves drastically, going
  from 2 minutes to 4 seconds to create an invoice with 500 lines
* the model is more declarative, we can now see how the values are computed
  instead of having the values being copied from various places.
* remove the hack in `onchange` that disabled the recursivity of it,
  which was unexpected and needed to be managed manually in all the
  onchange methods

This means that:
* Some fields need to be exclusively computed on journal entries values
  or invoice values, more specifically the Tax Summary widget.
  It is now
    - computed from entry lines, when opening the view
    - computed from invoice lines when changing those, because the tax lines
      will need to be recomputed anyways, erasing previously set values
    - set with an inverse function when saving; after the sync has been done
* Some possible operations previously possible have been dropped.
  (i.e. look at the removed test test_in_invoice_line_onchange_accounting_fields_1)
  This is because such a behavior was undefined (how is changing the balance going
  to affect the unit price? How is the amount currency going to affect it?)

_______________________________________________________________________

Implementation Details
----------------------

The "dynamic lines", meaning the payment terms and the tax lines are now
only created in the `create` and `write` functions.
In order to reduce code duplication, it has been implemented using
context managers used in both `account.move` and `account.move.line`
These context managers help comparing the values before/after, acting
like a local `onchange`, but getting benefit from the dirty flags from
the `compute` dependences.
This is relying on computed fields on the move (`needed_terms`) and on
the lines (`compute_all_tax`) which contain the values needed for the
related move.
Depending on the needed values and the existing values (`term_key` and
`tax_key`, respectively) the context manager will determine what needs
to be created/updated/deleted.

Some related changes are to produce a `dict` instead of a `str` for the
`tax_totals` (previously `tax_totals_json`) fields, by simplicity to
reduce the complexity of IO, and simplicity of debugging, because the
logic of the field needed to change (cannot be computed at the same time
anymore since it needed the lines to be synced)

By simplicity, and also because it makes more sense, some boolean fields
have been merged into `display_type`:
* `is_rounding_line`
* `exclude_from_invoice_tab`
* `is_anglo_saxon_line`

The `price_unit`, `quantity` and other "invoice fields" are now not set
anymore on lines that are not product lines since it didn't make any
sense to have it.

Performances
------------

You have to keep in mind that a simple `create` didn't compute a lot of
fields, for instance not taxes were set, no payment terms,...
Now it does.

```python
import random
from timeit import timeit
from odoo import Command
domain = [('company_id', 'in', (False, self.env.company.id))]
products = self.env['product.product'].search(domain).ids
partners = self.env['res.partner'].search(domain).ids
taxes = self.env['account.tax'].search(domain).ids
def create(nmove, nline):
    self.env['account.move'].create([
        {
            'move_type': 'out_invoice',
            'partner_id': random.choice(partners),
            'invoice_line_ids': [
                Command.create({
                    'name': f'line{i}',
                    'product_id': random.choice(products),
                    'tax_ids': [Command.set([random.choice(taxes)])],
                })
                for i in range(nline)
            ]
        }
        for j in range(nmove)
    ])
                                                             # After  | Before
print(timeit("create(1, 1)", globals=globals(), number=1))   # 0.11   | 0.09
print(timeit("create(100, 1)", globals=globals(), number=1)) # 2.76   | 2.50
print(timeit("create(500, 1)", globals=globals(), number=1)) # 14.56  | 12.34
print(timeit("create(1, 100)", globals=globals(), number=1)) # 1.03   | 5.52
print(timeit("create(1, 500)", globals=globals(), number=1)) # 3.99   | 125.02
print(timeit("create(50, 50)", globals=globals(), number=1)) # 19.44  | 79.55
```

Another metric that can be used is running the test suite with
`--test-tags=/account` (only `account` installed)
* before: 404s, 267127 queries (366 tests)
* after: 318s, 232125 queries (362 tests)

Why this commit title?
----------------------

Someone told me that this was the perfect way of naming your commits.
c04065abd8

task-2711317

closes odoo/odoo#96134

Related: odoo/upgrade#3715
Related: odoo/enterprise#29758
Signed-off-by: Laurent Smet <las@odoo.com>
2022-08-03 13:44:49 +02:00

919 lines
35 KiB
Python

# -*- coding: utf-8 -*-
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged, new_test_user
from odoo.tests.common import Form
@tagged('post_install', '-at_install')
class TestAccountPayment(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
company = cls.company_data['default_journal_bank'].company_id
cls.payment_debit_account_id = cls.copy_account(company.account_journal_payment_debit_account_id)
cls.payment_credit_account_id = cls.copy_account(company.account_journal_payment_credit_account_id)
cls.bank_journal_1 = cls.company_data['default_journal_bank']
cls.bank_journal_2 = cls.company_data['default_journal_bank'].copy()
cls.partner_bank_account1 = cls.env['res.partner.bank'].create({
'acc_number': "0123456789",
'partner_id': cls.partner_a.id,
'acc_type': 'bank',
})
cls.partner_bank_account2 = cls.env['res.partner.bank'].create({
'acc_number': "9876543210",
'partner_id': cls.partner_a.id,
'acc_type': 'bank',
})
cls.comp_bank_account1 = cls.env['res.partner.bank'].create({
'acc_number': "985632147",
'partner_id': cls.env.company.partner_id.id,
'acc_type': 'bank',
})
cls.comp_bank_account2 = cls.env['res.partner.bank'].create({
'acc_number': "741258963",
'partner_id': cls.env.company.partner_id.id,
'acc_type': 'bank',
})
company.write({
'account_journal_payment_debit_account_id': cls.payment_debit_account_id.id,
'account_journal_payment_credit_account_id': cls.payment_credit_account_id.id
})
def test_payment_move_sync_create_write(self):
copy_receivable = self.copy_account(self.company_data['default_account_receivable'])
payment = self.env['account.payment'].create({
'amount': 50.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'destination_account_id': copy_receivable.id,
})
expected_payment_values = {
'amount': 50.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'payment_reference': False,
'is_reconciled': False,
'currency_id': self.company_data['currency'].id,
'partner_id': False,
'destination_account_id': copy_receivable.id,
'payment_method_line_id': self.inbound_payment_method_line.id,
}
expected_move_values = {
'currency_id': self.company_data['currency'].id,
'partner_id': False,
}
expected_liquidity_line = {
'debit': 50.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.payment_debit_account_id.id,
}
expected_counterpart_line = {
'debit': 0.0,
'credit': 50.0,
'amount_currency': -50.0,
'currency_id': self.company_data['currency'].id,
'account_id': copy_receivable.id,
}
self.assertRecordValues(payment, [expected_payment_values])
self.assertRecordValues(payment.move_id, [expected_move_values])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
expected_counterpart_line,
expected_liquidity_line,
])
# ==== Check editing the account.payment ====
payment.write({
'partner_type': 'supplier',
'currency_id': self.currency_data['currency'].id,
'partner_id': self.partner_a.id,
})
self.assertRecordValues(payment, [{
**expected_payment_values,
'partner_type': 'supplier',
'destination_account_id': self.partner_a.property_account_payable_id.id,
'currency_id': self.currency_data['currency'].id,
'partner_id': self.partner_a.id,
}])
self.assertRecordValues(payment.move_id, [{
**expected_move_values,
'currency_id': self.currency_data['currency'].id,
'partner_id': self.partner_a.id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
**expected_counterpart_line,
'debit': 0.0,
'credit': 25.0,
'amount_currency': -50.0,
'currency_id': self.currency_data['currency'].id,
'account_id': self.partner_a.property_account_payable_id.id,
},
{
**expected_liquidity_line,
'debit': 25.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.currency_data['currency'].id,
},
])
# ==== Check editing the account.move.line ====
liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, counterpart_lines.id, {
'debit': 0.0,
'credit': 75.0,
'amount_currency': -75.0,
'currency_id': self.company_data['currency'].id,
'account_id': copy_receivable.id,
'partner_id': self.partner_b.id,
}),
(1, liquidity_lines.id, {
'debit': 100.0,
'credit': 0.0,
'amount_currency': 100.0,
'currency_id': self.company_data['currency'].id,
'partner_id': self.partner_b.id,
}),
# Additional write-off:
(0, 0, {
'debit': 0.0,
'credit': 25.0,
'amount_currency': -25.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['default_account_revenue'].id,
'partner_id': self.partner_b.id,
}),
]
})
self.assertRecordValues(payment, [{
**expected_payment_values,
'amount': 100.0,
'partner_id': self.partner_b.id,
}])
self.assertRecordValues(payment.move_id, [{
**expected_move_values,
'partner_id': self.partner_b.id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
**expected_counterpart_line,
'debit': 0.0,
'credit': 75.0,
'amount_currency': -75.0,
'partner_id': self.partner_b.id,
},
{
'debit': 0.0,
'credit': 25.0,
'amount_currency': -25.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['default_account_revenue'].id,
'partner_id': self.partner_b.id,
},
{
**expected_liquidity_line,
'debit': 100.0,
'credit': 0.0,
'amount_currency': 100.0,
'account_id': self.payment_debit_account_id.id,
'partner_id': self.partner_b.id,
},
])
def test_payment_move_sync_onchange(self):
pay_form = Form(self.env['account.payment'].with_context(
default_journal_id=self.company_data['default_journal_bank'].id,
# The `partner_type` is set through the window action context in the web client
# the field is otherwise invisible in the form.
default_partner_type='customer',
))
pay_form.amount = 50.0
pay_form.payment_type = 'inbound'
pay_form.partner_id = self.partner_a
payment = pay_form.save()
expected_payment_values = {
'amount': 50.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'payment_reference': False,
'is_reconciled': False,
'currency_id': self.company_data['currency'].id,
'partner_id': self.partner_a.id,
'destination_account_id': self.partner_a.property_account_receivable_id.id,
'payment_method_line_id': self.inbound_payment_method_line.id,
}
expected_move_values = {
'currency_id': self.company_data['currency'].id,
'partner_id': self.partner_a.id,
}
expected_liquidity_line = {
'debit': 50.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.payment_debit_account_id.id,
}
expected_counterpart_line = {
'debit': 0.0,
'credit': 50.0,
'amount_currency': -50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['default_account_receivable'].id,
}
self.assertRecordValues(payment, [expected_payment_values])
self.assertRecordValues(payment.move_id, [expected_move_values])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
expected_counterpart_line,
expected_liquidity_line,
])
# ==== Check editing the account.payment ====
# `partner_type` on payment is always invisible. It's supposed to be set through a context `default_` key
# In this case the goal of the test is to take an existing customer payment and change it to a supplier payment,
# which is not supposed to be possible through the web interface.
# So, change the payment partner_type beforehand rather than in the form view.
payment.partner_type = 'supplier'
pay_form = Form(payment)
pay_form.currency_id = self.currency_data['currency']
pay_form.partner_id = self.partner_a
payment = pay_form.save()
self.assertRecordValues(payment, [{
**expected_payment_values,
'partner_type': 'supplier',
'destination_account_id': self.partner_a.property_account_payable_id.id,
'currency_id': self.currency_data['currency'].id,
'partner_id': self.partner_a.id,
}])
self.assertRecordValues(payment.move_id, [{
**expected_move_values,
'currency_id': self.currency_data['currency'].id,
'partner_id': self.partner_a.id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
**expected_counterpart_line,
'debit': 0.0,
'credit': 25.0,
'amount_currency': -50.0,
'currency_id': self.currency_data['currency'].id,
'account_id': self.partner_a.property_account_payable_id.id,
},
{
**expected_liquidity_line,
'debit': 25.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.currency_data['currency'].id,
},
])
# ==== Check editing the account.move.line ====
move_form = Form(payment.move_id)
with move_form.line_ids.edit(0) as line_form:
line_form.currency_id = self.company_data['currency']
line_form.amount_currency = 100.0
line_form.partner_id = self.partner_b
with move_form.line_ids.edit(1) as line_form:
line_form.currency_id = self.company_data['currency']
line_form.amount_currency = -75.0
line_form.partner_id = self.partner_b
line_form.account_id = self.company_data['default_account_receivable']
with move_form.line_ids.new() as line_form:
line_form.currency_id = self.company_data['currency']
line_form.amount_currency = -25.0
line_form.partner_id = self.partner_b
line_form.account_id = self.company_data['default_account_revenue']
move_form.save()
self.assertRecordValues(payment, [{
**expected_payment_values,
'amount': 100.0,
'partner_id': self.partner_b.id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
**expected_counterpart_line,
'debit': 0.0,
'credit': 75.0,
'amount_currency': -75.0,
'partner_id': self.partner_b.id,
},
{
'debit': 0.0,
'credit': 25.0,
'amount_currency': -25.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['default_account_revenue'].id,
'partner_id': self.partner_b.id,
},
{
**expected_liquidity_line,
'debit': 100.0,
'credit': 0.0,
'amount_currency': 100.0,
'account_id': self.payment_debit_account_id.id,
'partner_id': self.partner_b.id,
},
])
def test_inbound_payment_sync_writeoff_debit_sign(self):
payment = self.env['account.payment'].create({
'amount': 100.0,
'payment_type': 'inbound',
'partner_type': 'customer',
})
# ==== Edit the account.move.line ====
liquidity_lines, counterpart_lines, dummy = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, liquidity_lines.id, {'debit': 100.0}),
(1, counterpart_lines.id, {'credit': 125.0}),
(0, 0, {'debit': 25.0, 'account_id': self.company_data['default_account_revenue'].id}),
],
})
self.assertRecordValues(payment, [{
'payment_type': 'inbound',
'partner_type': 'customer',
'amount': 100.0,
}])
# ==== Edit the account.payment amount ====
payment.write({
'partner_type': 'supplier',
'amount': 100.1,
'destination_account_id': self.company_data['default_account_payable'].id,
})
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
'debit': 0.0,
'credit': 125.1,
'account_id': self.company_data['default_account_payable'].id,
},
{
'debit': 25.0,
'credit': 0.0,
'account_id': self.company_data['default_account_revenue'].id,
},
{
'debit': 100.1,
'credit': 0.0,
'account_id': self.payment_debit_account_id.id,
},
])
def test_inbound_payment_sync_writeoff_credit_sign(self):
payment = self.env['account.payment'].create({
'amount': 100.0,
'payment_type': 'inbound',
'partner_type': 'customer',
})
# ==== Edit the account.move.line ====
liquidity_lines, counterpart_lines, dummy = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, liquidity_lines.id, {'debit': 100.0}),
(1, counterpart_lines.id, {'credit': 75.0}),
(0, 0, {'credit': 25.0, 'account_id': self.company_data['default_account_revenue'].id}),
],
})
self.assertRecordValues(payment, [{
'payment_type': 'inbound',
'partner_type': 'customer',
'amount': 100.0,
}])
# ==== Edit the account.payment amount ====
payment.write({
'partner_type': 'supplier',
'amount': 100.1,
'destination_account_id': self.company_data['default_account_payable'].id,
})
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
'debit': 0.0,
'credit': 75.1,
'account_id': self.company_data['default_account_payable'].id,
},
{
'debit': 0.0,
'credit': 25.0,
'account_id': self.company_data['default_account_revenue'].id,
},
{
'debit': 100.1,
'credit': 0.0,
'account_id': self.payment_debit_account_id.id,
},
])
def test_outbound_payment_sync_writeoff_debit_sign(self):
payment = self.env['account.payment'].create({
'amount': 100.0,
'payment_type': 'outbound',
'partner_type': 'supplier',
})
# ==== Edit the account.move.line ====
liquidity_lines, counterpart_lines, dummy = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, liquidity_lines.id, {'credit': 100.0}),
(1, counterpart_lines.id, {'debit': 75.0}),
(0, 0, {'debit': 25.0, 'account_id': self.company_data['default_account_revenue'].id}),
],
})
self.assertRecordValues(payment, [{
'payment_type': 'outbound',
'partner_type': 'supplier',
'amount': 100.0,
}])
# ==== Edit the account.payment amount ====
payment.write({
'partner_type': 'customer',
'amount': 100.1,
'destination_account_id': self.company_data['default_account_receivable'].id,
})
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
'debit': 0.0,
'credit': 100.1,
'account_id': self.payment_credit_account_id.id,
},
{
'debit': 25.0,
'credit': 0.0,
'account_id': self.company_data['default_account_revenue'].id,
},
{
'debit': 75.1,
'credit': 0.0,
'account_id': self.company_data['default_account_receivable'].id,
},
])
def test_outbound_payment_sync_writeoff_credit_sign(self):
payment = self.env['account.payment'].create({
'amount': 100.0,
'payment_type': 'outbound',
'partner_type': 'supplier',
})
# ==== Edit the account.move.line ====
liquidity_lines, counterpart_lines, dummy = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, liquidity_lines.id, {'credit': 100.0}),
(1, counterpart_lines.id, {'debit': 125.0}),
(0, 0, {'credit': 25.0, 'account_id': self.company_data['default_account_revenue'].id}),
],
})
self.assertRecordValues(payment, [{
'payment_type': 'outbound',
'partner_type': 'supplier',
'amount': 100.0,
}])
# ==== Edit the account.payment amount ====
payment.write({
'partner_type': 'customer',
'amount': 100.1,
'destination_account_id': self.company_data['default_account_receivable'].id,
})
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
'debit': 0.0,
'credit': 100.1,
'account_id': self.payment_credit_account_id.id,
},
{
'debit': 0.0,
'credit': 25.0,
'account_id': self.company_data['default_account_revenue'].id,
},
{
'debit': 125.1,
'credit': 0.0,
'account_id': self.company_data['default_account_receivable'].id,
},
])
def test_internal_transfer(self):
copy_receivable = self.copy_account(self.company_data['default_account_receivable'])
payment = self.env['account.payment'].create({
'amount': 50.0,
'is_internal_transfer': True,
'destination_journal_id': self.company_data['default_journal_cash'].id,
})
expected_payment_values = {
'amount': 50.0,
'payment_type': 'inbound',
'currency_id': self.company_data['currency'].id,
'partner_id': self.company_data['company'].partner_id.id,
'destination_account_id': self.company_data['company'].transfer_account_id.id,
}
expected_move_values = {
'currency_id': self.company_data['currency'].id,
'partner_id': self.company_data['company'].partner_id.id,
}
expected_liquidity_line = {
'debit': 50.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.payment_debit_account_id.id,
}
expected_counterpart_line = {
'debit': 0.0,
'credit': 50.0,
'amount_currency': -50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['company'].transfer_account_id.id,
}
self.assertRecordValues(payment, [expected_payment_values])
self.assertRecordValues(payment.move_id, [expected_move_values])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
expected_counterpart_line,
expected_liquidity_line,
])
# ==== Check editing the account.payment ====
payment.write({
'partner_type': 'customer',
'partner_id': self.partner_a.id,
'destination_account_id': copy_receivable.id,
})
self.assertRecordValues(payment, [{
**expected_payment_values,
'partner_type': 'customer',
'destination_account_id': copy_receivable.id,
'partner_id': self.partner_a.id,
'is_internal_transfer': False,
}])
self.assertRecordValues(payment.move_id, [{
**expected_move_values,
'partner_id': self.partner_a.id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
**expected_counterpart_line,
'account_id': copy_receivable.id,
},
expected_liquidity_line,
])
# ==== Check editing the account.move.line ====
liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines()
payment.move_id.write({
'line_ids': [
(1, counterpart_lines.id, {
'account_id': self.company_data['company'].transfer_account_id.id,
'partner_id': self.company_data['company'].partner_id.id,
}),
(1, liquidity_lines.id, {
'partner_id': self.company_data['company'].partner_id.id,
}),
]
})
self.assertRecordValues(payment, [expected_payment_values])
self.assertRecordValues(payment.move_id, [expected_move_values])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
expected_counterpart_line,
expected_liquidity_line,
])
# ==== Check creation of paired internal transfer payment ====
payment = self.env['account.payment'].create({
'amount': 50.0,
'is_internal_transfer': True,
'payment_type': 'inbound',
'destination_journal_id': self.company_data['default_journal_cash'].id,
})
payment.action_post()
paired_payment = self.env['account.payment'].search([('payment_type', '=', 'outbound')])
expected_payment_values = {
'amount': 50.0,
'payment_type': 'outbound',
'currency_id': self.company_data['currency'].id,
'partner_id': self.company_data['company'].partner_id.id,
'destination_account_id': self.company_data['company'].transfer_account_id.id,
}
expected_move_values = {
'currency_id': self.company_data['currency'].id,
'partner_id': self.company_data['company'].partner_id.id,
}
expected_liquidity_line = {
'debit': 0.0,
'credit': 50.0,
'amount_currency': -50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['default_journal_cash'].company_id.account_journal_payment_credit_account_id.id,
}
expected_counterpart_line = {
'debit': 50.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.company_data['currency'].id,
'account_id': self.company_data['company'].transfer_account_id.id,
}
self.assertRecordValues(paired_payment, [expected_payment_values])
self.assertRecordValues(paired_payment.move_id, [expected_move_values])
self.assertRecordValues(paired_payment.line_ids.sorted('balance'), [
expected_liquidity_line,
expected_counterpart_line,
])
def test_compute_currency_id(self):
''' When creating a new account.payment without specifying a currency, the default currency should be the one
set on the journal.
'''
self.company_data['default_journal_bank'].write({
'currency_id': self.currency_data['currency'].id,
})
payment = self.env['account.payment'].create({
'amount': 50.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'partner_id': self.partner_a.id,
'journal_id': self.company_data['default_journal_bank'].id,
})
self.assertRecordValues(payment, [{
'currency_id': self.currency_data['currency'].id,
}])
self.assertRecordValues(payment.move_id, [{
'currency_id': self.currency_data['currency'].id,
}])
self.assertRecordValues(payment.line_ids.sorted('balance'), [
{
'debit': 0.0,
'credit': 25.0,
'amount_currency': -50.0,
'currency_id': self.currency_data['currency'].id,
},
{
'debit': 25.0,
'credit': 0.0,
'amount_currency': 50.0,
'currency_id': self.currency_data['currency'].id,
},
])
def test_reconciliation_payment_states(self):
payment = self.env['account.payment'].create({
'amount': 50.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'destination_account_id': self.company_data['default_account_receivable'].id,
})
liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines()
self.assertRecordValues(payment, [{
'is_reconciled': False,
'is_matched': False,
}])
invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.partner_a.id,
'invoice_line_ids': [(0, 0, {
'name': '50 to pay',
'price_unit': 50.0,
'quantity': 1,
'account_id': self.company_data['default_account_revenue'].id,
})],
})
payment.action_post()
invoice.action_post()
(counterpart_lines + invoice.line_ids.filtered(lambda line: line.account_type == 'asset_receivable'))\
.reconcile()
self.assertRecordValues(payment, [{
'is_reconciled': True,
'is_matched': False,
}])
statement = self.env['account.bank.statement'].create({
'name': 'test_statement',
'journal_id': self.company_data['default_journal_bank'].id,
'line_ids': [
(0, 0, {
'payment_ref': '50 to pay',
'partner_id': self.partner_a.id,
'amount': 50.0,
}),
],
})
statement.button_post()
statement_line = statement.line_ids
# Reconcile without the bank reconciliation widget since the widget is in enterprise.
_st_liquidity_lines, st_suspense_lines, _st_other_lines = statement_line\
.with_context(skip_account_move_synchronization=True)\
._seek_for_lines()
st_suspense_lines.account_id = liquidity_lines.account_id
(st_suspense_lines + liquidity_lines).reconcile()
self.assertRecordValues(payment, [{
'is_reconciled': True,
'is_matched': True,
}])
def test_payment_name(self):
AccountPayment = self.env['account.payment']
AccountPayment.search([]).unlink()
payment = AccountPayment.create({
'journal_id': self.company_data['default_journal_bank'].id,
})
self.assertRegex(payment.name, r'BNK1/\d{4}/\d{2}/0001')
with Form(AccountPayment.with_context(default_move_journal_types=('bank', 'cash'))) as payment_form:
self.assertEqual(payment_form._values['name'], '/')
payment_form.journal_id = self.company_data['default_journal_cash']
self.assertRegex(payment_form._values['name'], r'CSH1/\d{4}/\d{2}/0001')
payment_form.journal_id = self.company_data['default_journal_bank']
payment = payment_form.save()
self.assertEqual(payment.name, '/')
payment.action_post()
self.assertRegex(payment.name, r'BNK1/\d{4}/\d{2}/0002')
def test_payment_without_default_company_account(self):
""" The purpose of this test is to check the specific behavior when duplicating an inbound payment, then change
the copy to an outbound payment when we set the outstanding accounts (payments and receipts) on a journal but
not on the company level.
"""
company = self.company_data['company']
bank_journal = self.company_data['default_journal_bank']
bank_journal.outbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_credit_account_id
bank_journal.inbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_debit_account_id
company.account_journal_payment_debit_account_id = False
company.account_journal_payment_credit_account_id = False
payment = self.env['account.payment'].create({
'amount': 5.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'journal_id': bank_journal.id,
})
self.assertRecordValues(payment, [{
'amount': 5.0,
'payment_type': 'inbound',
'partner_type': 'customer',
'payment_reference': False,
'is_reconciled': False,
'currency_id': self.company_data['currency'].id,
'partner_id': False,
'payment_method_line_id': self.inbound_payment_method_line.id,
}])
payment.payment_type = 'outbound'
self.assertRecordValues(payment, [{
'amount': 5.0,
'payment_type': 'outbound',
'partner_type': 'customer',
'payment_reference': False,
'is_reconciled': False,
'currency_id': self.company_data['currency'].id,
'partner_id': False,
'payment_method_line_id': self.outbound_payment_method_line.id,
}])
def test_suggested_default_partner_bank(self):
""" Ensure the 'partner_bank_id' is well computed on payments. When the payment is inbound, the money must be
received by a bank account linked to the company. In case of outbound payment, the bank account must be found
on the partner.
"""
payment = self.env['account.payment'].create({
'journal_id': self.bank_journal_1.id,
'amount': 50.0,
'payment_type': 'outbound',
'partner_type': 'supplier',
'partner_id': self.partner_a.id,
})
self.assertRecordValues(payment, [{
'available_partner_bank_ids': self.partner_a.bank_ids.ids,
'partner_bank_id': self.partner_bank_account1.id,
}])
payment.payment_type = 'inbound'
self.assertRecordValues(payment, [{
'available_partner_bank_ids': [],
'partner_bank_id': False,
}])
self.bank_journal_2.bank_account_id = self.comp_bank_account2
# A sequence is automatically added on the first move. We need to clean it before changing the journal.
payment.name = False
payment.journal_id = self.bank_journal_2
self.assertRecordValues(payment, [{
'available_partner_bank_ids': self.comp_bank_account2.ids,
'partner_bank_id': self.comp_bank_account2.id,
}])
def test_internal_transfer_right_accounts(self):
"""The purpose of this test is to check that the right accounts are computed when making an internal bank transfer"""
company = self.env.company
transfer_account = company.transfer_account_id
bank = self.bank_journal_1
bank_2 = self.bank_journal_2
payment = self.env['account.payment'].create({
'is_internal_transfer': True,
'payment_type': 'outbound',
'amount': 100.0,
'journal_id': bank.id,
'destination_journal_id': bank_2.id,
})
payment.action_post()
self.assertRecordValues(payment.line_ids, [
{'account_id': company.account_journal_payment_credit_account_id.id},
{'account_id': transfer_account.id},
])
self.assertRecordValues(payment.paired_internal_transfer_payment_id.line_ids, [
{'account_id': company.account_journal_payment_debit_account_id.id},
{'account_id': transfer_account.id},
])
# We check the behavior when setting specific receipts/payments account on bank journals
bank.inbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_debit_account_id.copy({'code': 1001})
bank.outbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_credit_account_id.copy({'code': 1002})
bank_2.inbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_debit_account_id.copy({'code': 2001})
bank_2.outbound_payment_method_line_ids.payment_account_id = company.account_journal_payment_credit_account_id.copy({'code': 2002})
payment = self.env['account.payment'].create({
'is_internal_transfer': True,
'payment_type': 'outbound',
'amount': 100.0,
'journal_id': bank.id,
'destination_journal_id': bank_2.id,
})
payment.action_post()
self.assertRecordValues(payment.line_ids, [
{'account_id': bank.outbound_payment_method_line_ids.payment_account_id.id,},
{'account_id': transfer_account.id},
])
self.assertRecordValues(payment.paired_internal_transfer_payment_id.line_ids, [
{'account_id': bank_2.inbound_payment_method_line_ids.payment_account_id.id},
{'account_id': transfer_account.id},
])