Jairo Llopis 173b2b2db2 [10.0][IMP] contract: Performance boost 🚀
With this patch we save about 83% of the execution time when generating invoices in batch.

# Optimizations made

## Recompute once at the end of the batch

This part avoids recomputing many fields per record. Instead, global recomputations are triggered at the end of the block:

```python
with _self.env.norecompute():
    ...
    invoices.compute_taxes()
_self.recompute()
```

Notice the explicit call to `compute_taxes()`, which was explicit before also, but it was done once per invoice, losing batch-computing boost.

## Disabling prefetch for extra fields

It's done in this part:

```python
_self = self.with_context(prefetch_fields=False)
```

Prefetching makes sense when we are going to use a lot of fields for a model that has only a few.

In our case, we are using not much fields, but the models involved have lots of them.

This produces more queries to get those fields, but the queries are noticeably smaller. At the end of the day, it saves a lot of time, which is what matters.

## Disabling track mail creation

This part does it:

```diff
         ctx.update({
+            'mail_notrack': True,
             'next_date': next_date,
```

It makes that when creating invoices, we don't create the "Invoice created" message.

## Precomputing price

Obtaining `price_unit` from `contract.recurring_invoice_line_ids` was quite expenisve in terms of CPU, and it was being made once per line, each one in a different context, which means also a different cache.

Instead of that, lines now share a single context, and are computed before starting the batch.

This code precomputes stuff:

```python
# Precompute expensive computed fields in batch
recurring_lines = _self.mapped("recurring_invoice_line_ids")
recurring_lines._fields["price_unit"].determine_value(recurring_lines)
```

And the usage of 2 different environments done inside `_create_invoice()` (`self` and `_self`) guarantee that the invoices are filled with the correct data, but also that the lines use the cached precomputed value instead of having to compute it each time.

# Performance gain

According to my tests, generating 10 invoices took 62 seconds before, and it takes about 18 seconds now.
2019-01-24 13:30:49 +00:00
2018-04-19 04:42:38 +02:00
2015-08-30 16:15:14 +00:00
2015-08-30 16:15:14 +00:00
2015-08-30 16:15:14 +00:00
2019-01-15 10:02:35 +00:00

Runbot Status Build Status Coverage Status

Contracts

Odoo Contracts are special types of Analytic Accounts. This repository provides features extending Contracts.

Dependencies on Project fetures should not be required. For Project related extensions please see the OCA/Project repository.

Translation Status

Transifex Status

Available addons

addon version summary
agreement_account 10.0.1.0.0 Adds an agreement object linked to an invoice
agreement_sale 10.0.1.0.1 Link an agreement to a sale order and copy to invoice
contract 10.0.4.3.0 Contracts Management - Recurring
contract_digitized_signature 10.0.1.0.0 Contract Digitized Signature
contract_payment_auto 10.0.1.0.1 Adds automatic payments to contracts.
contract_payment_mode 10.0.1.0.1 Payment mode in contracts and their invoices
contract_recurring_analytic_distribution 10.0.1.0.0 Analytic plans on contracts recurring invoices
contract_sale 10.0.1.0.0 Contract from Sale
contract_sale_generation 10.0.3.0.0 Contracts Management - Recurring Sales
contract_show_invoice 10.0.1.0.1 Button in contracts to show their invoices
contract_variable_quantity 10.0.1.2.0 Variable quantity in contract recurrent invoicing
product_contract 10.0.1.0.0 Product Contract
website_portal_contract 10.0.1.0.0 Extends website portal with contracts.
Description
No description provided
Readme 66 MiB
Languages
Python 60.4%
HTML 39%
JavaScript 0.4%
SCSS 0.2%