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>
157 lines
7.1 KiB
Python
157 lines
7.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
_inherit = 'account.move'
|
|
|
|
purchase_vendor_bill_id = fields.Many2one('purchase.bill.union', store=False, readonly=True,
|
|
states={'draft': [('readonly', False)]},
|
|
string='Auto-complete',
|
|
help="Auto-complete from a past bill / purchase order.")
|
|
purchase_id = fields.Many2one('purchase.order', store=False, readonly=True,
|
|
states={'draft': [('readonly', False)]},
|
|
string='Purchase Order',
|
|
help="Auto-complete from a past purchase order.")
|
|
purchase_order_count = fields.Integer(compute="_compute_origin_po_count", string='Purchase Order Count')
|
|
|
|
def _get_invoice_reference(self):
|
|
self.ensure_one()
|
|
vendor_refs = [ref for ref in set(self.line_ids.mapped('purchase_line_id.order_id.partner_ref')) if ref]
|
|
if self.ref:
|
|
return [ref for ref in self.ref.split(', ') if ref and ref not in vendor_refs] + vendor_refs
|
|
return vendor_refs
|
|
|
|
@api.onchange('purchase_vendor_bill_id', 'purchase_id')
|
|
def _onchange_purchase_auto_complete(self):
|
|
''' Load from either an old purchase order, either an old vendor bill.
|
|
|
|
When setting a 'purchase.bill.union' in 'purchase_vendor_bill_id':
|
|
* If it's a vendor bill, 'invoice_vendor_bill_id' is set and the loading is done by '_onchange_invoice_vendor_bill'.
|
|
* If it's a purchase order, 'purchase_id' is set and this method will load lines.
|
|
|
|
/!\ All this not-stored fields must be empty at the end of this function.
|
|
'''
|
|
if self.purchase_vendor_bill_id.vendor_bill_id:
|
|
self.invoice_vendor_bill_id = self.purchase_vendor_bill_id.vendor_bill_id
|
|
self._onchange_invoice_vendor_bill()
|
|
elif self.purchase_vendor_bill_id.purchase_order_id:
|
|
self.purchase_id = self.purchase_vendor_bill_id.purchase_order_id
|
|
self.purchase_vendor_bill_id = False
|
|
|
|
if not self.purchase_id:
|
|
return
|
|
|
|
# Copy data from PO
|
|
invoice_vals = self.purchase_id.with_company(self.purchase_id.company_id)._prepare_invoice()
|
|
invoice_vals['currency_id'] = self.invoice_line_ids and self.currency_id or invoice_vals.get('currency_id')
|
|
del invoice_vals['ref']
|
|
del invoice_vals['company_id'] # avoid recomputing the currency
|
|
self.update(invoice_vals)
|
|
|
|
# Copy purchase lines.
|
|
po_lines = self.purchase_id.order_line - self.line_ids.mapped('purchase_line_id')
|
|
for line in po_lines.filtered(lambda l: not l.display_type):
|
|
self.invoice_line_ids += self.env['account.move.line'].new(
|
|
line._prepare_account_move_line(self)
|
|
)
|
|
|
|
# Compute invoice_origin.
|
|
origins = set(self.line_ids.mapped('purchase_line_id.order_id.name'))
|
|
self.invoice_origin = ','.join(list(origins))
|
|
|
|
# Compute ref.
|
|
refs = self._get_invoice_reference()
|
|
self.ref = ', '.join(refs)
|
|
|
|
# Compute payment_reference.
|
|
if len(refs) == 1:
|
|
self.payment_reference = refs[0]
|
|
|
|
self.purchase_id = False
|
|
|
|
@api.onchange('partner_id', 'company_id')
|
|
def _onchange_partner_id(self):
|
|
res = super(AccountMove, self)._onchange_partner_id()
|
|
if self.partner_id and\
|
|
self.move_type in ['in_invoice', 'in_refund'] and\
|
|
self.currency_id != self.partner_id.property_purchase_currency_id and\
|
|
self.partner_id.property_purchase_currency_id.id:
|
|
if not self.env.context.get('default_journal_id'):
|
|
journal_domain = [
|
|
('type', '=', 'purchase'),
|
|
('company_id', '=', self.company_id.id),
|
|
('currency_id', '=', self.partner_id.property_purchase_currency_id.id),
|
|
]
|
|
default_journal_id = self.env['account.journal'].search(journal_domain, limit=1)
|
|
if default_journal_id:
|
|
self.journal_id = default_journal_id
|
|
if self.env.context.get('default_currency_id'):
|
|
self.currency_id = self.env.context['default_currency_id']
|
|
if self.partner_id.property_purchase_currency_id:
|
|
self.currency_id = self.partner_id.property_purchase_currency_id
|
|
return res
|
|
|
|
@api.depends('line_ids.purchase_line_id')
|
|
def _compute_origin_po_count(self):
|
|
for move in self:
|
|
move.purchase_order_count = len(move.line_ids.purchase_line_id.order_id)
|
|
|
|
def action_view_source_purchase_orders(self):
|
|
self.ensure_one()
|
|
source_orders = self.line_ids.purchase_line_id.order_id
|
|
result = self.env['ir.actions.act_window']._for_xml_id('purchase.purchase_form_action')
|
|
if len(source_orders) > 1:
|
|
result['domain'] = [('id', 'in', source_orders.ids)]
|
|
elif len(source_orders) == 1:
|
|
result['views'] = [(self.env.ref('purchase.purchase_order_form', False).id, 'form')]
|
|
result['res_id'] = source_orders.id
|
|
else:
|
|
result = {'type': 'ir.actions.act_window_close'}
|
|
return result
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
# OVERRIDE
|
|
moves = super(AccountMove, self).create(vals_list)
|
|
for move in moves:
|
|
if move.reversed_entry_id:
|
|
continue
|
|
purchases = move.line_ids.purchase_line_id.order_id
|
|
if not purchases:
|
|
continue
|
|
refs = [purchase._get_html_link() for purchase in purchases]
|
|
message = _("This vendor bill has been created from: %s") % ','.join(refs)
|
|
move.message_post(body=message)
|
|
return moves
|
|
|
|
def write(self, vals):
|
|
# OVERRIDE
|
|
old_purchases = [move.mapped('line_ids.purchase_line_id.order_id') for move in self]
|
|
res = super(AccountMove, self).write(vals)
|
|
for i, move in enumerate(self):
|
|
new_purchases = move.mapped('line_ids.purchase_line_id.order_id')
|
|
if not new_purchases:
|
|
continue
|
|
diff_purchases = new_purchases - old_purchases[i]
|
|
if diff_purchases:
|
|
refs = [purchase._get_html_link() for purchase in diff_purchases]
|
|
message = _("This vendor bill has been modified from: %s") % ','.join(refs)
|
|
move.message_post(body=message)
|
|
return res
|
|
|
|
|
|
class AccountMoveLine(models.Model):
|
|
""" Override AccountInvoice_line to add the link to the purchase order line it is related to"""
|
|
_inherit = 'account.move.line'
|
|
|
|
purchase_line_id = fields.Many2one('purchase.order.line', 'Purchase Order Line', ondelete='set null', index='btree_not_null')
|
|
purchase_order_id = fields.Many2one('purchase.order', 'Purchase Order', related='purchase_line_id.order_id', readonly=True)
|
|
|
|
def _copy_data_extend_business_fields(self, values):
|
|
# OVERRIDE to copy the 'purchase_line_id' field as well.
|
|
super(AccountMoveLine, self)._copy_data_extend_business_fields(values)
|
|
values['purchase_line_id'] = self.purchase_line_id.id
|