mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
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.
111 lines
4.0 KiB
ReStructuredText
111 lines
4.0 KiB
ReStructuredText
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
|
|
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
|
|
:alt: License: AGPL-3
|
|
|
|
=================================
|
|
Contracts for recurrent invoicing
|
|
=================================
|
|
|
|
This module forward-port to v10 the contracts management with recurring
|
|
invoicing functions. Also you can print and send by email contract report.
|
|
|
|
In upstream Odoo, this functionality was moved into the Enterprise edition.
|
|
|
|
Configuration
|
|
=============
|
|
|
|
To view discount field set *Discount on lines* in user access rights.
|
|
|
|
You might find that generating all pending invoices at once takes too much
|
|
time and produces some performance problems, mostly in cases where you
|
|
generate a lot of invoices in little time (i.e. when invoicing thousands
|
|
of contracts yearly, and you get to January 1st of the next year). To avoid
|
|
this bottleneck, the trick is to **increase the cron frequence and decrease
|
|
the contracts batch size**. The counterpart is that some invoices could not
|
|
be generated in the exact day you expected. To configure that:
|
|
|
|
#. Go to *Settings > Activate the developer mode*.
|
|
#. Go to *Settings > Technical > Automation > Scheduled Actions >
|
|
Generate Recurring Invoices from Contracts > Edit > Information*.
|
|
#. Set a lower interval. For example, change *Interval Unit* to *Hours*.
|
|
#. Go to the *Technical Data* tab, and add a batch size in *Arguments*.
|
|
For example, it should look like ``(20,)``.
|
|
#. Save.
|
|
|
|
That's it! From now on, only 20 invoices per hour will be generated.
|
|
That should take only a few seconds each hour, and shouln't block other users.
|
|
|
|
Usage
|
|
=====
|
|
|
|
To use this module, you need to:
|
|
|
|
#. Go to Accounting -> Contracts and select or create a new contract.
|
|
#. Check *Generate recurring invoices automatically*.
|
|
#. Fill fields for selecting the recurrency and invoice parameters:
|
|
|
|
* Journal
|
|
* Pricelist
|
|
* Period. It can be any interval of days, weeks, months, months last day or
|
|
years.
|
|
* Start date and next invoice date.
|
|
* Invoicing type: pre-paid or post-paid.
|
|
#. Add the lines to be invoiced with the product, description, quantity and
|
|
price.
|
|
#. You can mark Auto-price? for having a price automatically obtained applying
|
|
the pricelist to the product price.
|
|
#. You have the possibility to use the markers #START# or #END# in the
|
|
description field to show the start and end date of the invoiced period.
|
|
#. Choosing between pre-paid and post-paid, you modify the dates that are shown
|
|
with the markers.
|
|
#. A cron is created with daily interval, but if you are in debug mode, you can
|
|
click on *Create invoices* to force this action.
|
|
#. Click *Show recurring invoices* link to show all invoices created by the
|
|
contract.
|
|
#. Click on *Print > Contract* menu to print contract report.
|
|
#. Click on *Send by Email* button to send contract by email.
|
|
|
|
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
|
|
:alt: Try me on Runbot
|
|
:target: https://runbot.odoo-community.org/runbot/110/10.0
|
|
|
|
Known issues / Roadmap
|
|
======================
|
|
|
|
* Recover states and others functional fields in Contracts.
|
|
|
|
Bug Tracker
|
|
===========
|
|
|
|
Bugs are tracked on `GitHub Issues
|
|
<https://github.com/OCA/contract/issues>`_. In case of trouble, please
|
|
check there if your issue has already been reported. If you spotted it first,
|
|
help us smashing it by providing a detailed and welcomed feedback.
|
|
|
|
Credits
|
|
=======
|
|
|
|
Contributors
|
|
------------
|
|
|
|
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
|
* Carlos Dauden <carlos.dauden@tecnativa.com>
|
|
* Angel Moya <angel.moya@domatix.com>
|
|
* Dave Lasley <dave@laslabs.com>
|
|
* Vicent Cubells <vicent.cubells@tecnativa.com>
|
|
|
|
Maintainer
|
|
----------
|
|
|
|
.. image:: https://odoo-community.org/logo.png
|
|
:alt: Odoo Community Association
|
|
:target: https://odoo-community.org
|
|
|
|
This module is maintained by the OCA.
|
|
|
|
OCA, or the Odoo Community Association, is a nonprofit organization whose
|
|
mission is to support the collaborative development of Odoo features and
|
|
promote its widespread use.
|
|
|
|
To contribute to this module, please visit https://odoo-community.org.
|