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>
138 lines
5.6 KiB
Python
138 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from freezegun import freeze_time
|
|
|
|
from odoo.addons.test_crm_full.tests.common import TestCrmFullCommon
|
|
from odoo.tests.common import users, warmup, Form
|
|
from odoo.tests import tagged
|
|
|
|
|
|
@tagged('crm_performance', 'post_install', '-at_install')
|
|
class CrmPerformanceCase(TestCrmFullCommon):
|
|
|
|
def setUp(self):
|
|
super(CrmPerformanceCase, self).setUp()
|
|
# patch registry to simulate a ready environment
|
|
self.patch(self.env.registry, 'ready', True)
|
|
self._flush_tracking()
|
|
|
|
self.user_sales_leads.write({
|
|
'groups_id': [
|
|
(4, self.env.ref('event.group_event_user').id),
|
|
(4, self.env.ref('im_livechat.im_livechat_group_user').id),
|
|
]
|
|
})
|
|
|
|
def _flush_tracking(self):
|
|
""" Force the creation of tracking values notably, and ensure tests are
|
|
reproducible. """
|
|
self.env.flush_all()
|
|
self.cr.flush()
|
|
|
|
|
|
@tagged('crm_performance', 'post_install', '-at_install')
|
|
class TestCrmPerformance(CrmPerformanceCase):
|
|
|
|
@users('user_sales_leads')
|
|
@warmup
|
|
def test_lead_create_batch_mixed(self):
|
|
""" Test multiple lead creation (import) """
|
|
batch_size = 10
|
|
country_be = self.env.ref('base.be')
|
|
lang_be_id = self.env['res.lang']._lang_get_id('fr_BE')
|
|
|
|
with freeze_time(self.reference_now), self.assertQueryCount(user_sales_leads=196): # -1 tcf
|
|
self.env.cr._now = self.reference_now # force create_date to check schedulers
|
|
crm_values = [
|
|
{'country_id': country_be.id,
|
|
'email_from': 'address.email.%02d@test.example.com' % idx,
|
|
'function': 'Noisy Customer',
|
|
'lang_id': lang_be_id,
|
|
'mobile': '04551111%02d' % idx,
|
|
'name': 'Test Lead %02d' % idx,
|
|
'phone': '04550000%02d' % idx,
|
|
'street': 'Super Street, %092d' % idx,
|
|
'zip': '1400',
|
|
} for idx in range(batch_size)
|
|
]
|
|
crm_values += [
|
|
{'partner_id': self.partners[idx].id,
|
|
'name': 'Test Lead %02d' % idx,
|
|
} for idx in range(batch_size)
|
|
]
|
|
_leads = self.env['crm.lead'].create(crm_values)
|
|
|
|
@users('user_sales_leads')
|
|
@warmup
|
|
def test_lead_create_form_address(self):
|
|
""" Test a single lead creation using Form """
|
|
country_be = self.env.ref('base.be')
|
|
lang_be = self.env['res.lang']._lang_get('fr_BE')
|
|
|
|
with freeze_time(self.reference_now), self.assertQueryCount(user_sales_leads=179): # com 166 - -2 tcf
|
|
self.env.cr._now = self.reference_now # force create_date to check schedulers
|
|
with Form(self.env['crm.lead']) as lead_form:
|
|
lead_form.country_id = country_be
|
|
lead_form.email_from = 'address.email@test.example.com'
|
|
lead_form.function = 'Noisy Customer'
|
|
lead_form.lang_id = lang_be
|
|
lead_form.mobile = '0455111100'
|
|
lead_form.name = 'Test Lead'
|
|
lead_form.phone = '0455000011'
|
|
lead_form.street = 'Super Street, 00'
|
|
lead_form.zip = '1400'
|
|
|
|
_lead = lead_form.save()
|
|
|
|
@users('user_sales_leads')
|
|
@warmup
|
|
def test_lead_create_form_partner(self):
|
|
""" Test a single lead creation using Form with a partner """
|
|
with freeze_time(self.reference_now), self.assertQueryCount(user_sales_leads=187): # com 175 - -2 tcf
|
|
self.env.cr._now = self.reference_now # force create_date to check schedulers
|
|
with self.debug_mode():
|
|
# {'invisible': ['|', ('type', '=', 'opportunity'), ('is_partner_visible', '=', False)]}
|
|
# lead.is_partner_visible = bool(lead.type == 'opportunity' or lead.partner_id or is_debug_mode)
|
|
with Form(self.env['crm.lead']) as lead_form:
|
|
lead_form.partner_id = self.partners[0]
|
|
lead_form.name = 'Test Lead'
|
|
|
|
_lead = lead_form.save()
|
|
|
|
@users('user_sales_leads')
|
|
@warmup
|
|
def test_lead_create_single_address(self):
|
|
""" Test multiple lead creation (import) """
|
|
country_be = self.env.ref('base.be')
|
|
lang_be_id = self.env['res.lang']._lang_get_id('fr_BE')
|
|
|
|
with freeze_time(self.reference_now), self.assertQueryCount(user_sales_leads=33): # -1 tcf
|
|
self.env.cr._now = self.reference_now # force create_date to check schedulers
|
|
crm_values = [
|
|
{'country_id': country_be.id,
|
|
'email_from': 'address.email.00@test.example.com',
|
|
'function': 'Noisy Customer',
|
|
'lang_id': lang_be_id,
|
|
'mobile': '0455111100',
|
|
'name': 'Test Lead',
|
|
'phone': '0455000000',
|
|
'street': 'Super Street, 00',
|
|
'zip': '1400',
|
|
}
|
|
]
|
|
_lead = self.env['crm.lead'].create(crm_values)
|
|
|
|
@users('user_sales_leads')
|
|
@warmup
|
|
def test_lead_create_single_partner(self):
|
|
""" Test multiple lead creation (import) """
|
|
with freeze_time(self.reference_now), self.assertQueryCount(user_sales_leads=34): # -1 tcf
|
|
self.env.cr._now = self.reference_now # force create_date to check schedulers
|
|
crm_values = [
|
|
{'partner_id': self.partners[0].id,
|
|
'name': 'Test Lead',
|
|
}
|
|
]
|
|
_lead = self.env['crm.lead'].create(crm_values)
|