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>
175 lines
10 KiB
Python
175 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
from odoo.exceptions import ValidationError
|
|
from odoo import models, fields, api, _
|
|
from odoo.tools.misc import formatLang
|
|
|
|
SII_VAT = '60805000-0'
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
_inherit = "account.move"
|
|
|
|
partner_id_vat = fields.Char(related='partner_id.vat', string='VAT No')
|
|
l10n_latam_internal_type = fields.Selection(
|
|
related='l10n_latam_document_type_id.internal_type', string='L10n Latam Internal Type')
|
|
|
|
def _get_l10n_latam_documents_domain(self):
|
|
self.ensure_one()
|
|
if self.journal_id.company_id.account_fiscal_country_id != self.env.ref('base.cl') or not \
|
|
self.journal_id.l10n_latam_use_documents:
|
|
return super()._get_l10n_latam_documents_domain()
|
|
if self.journal_id.type == 'sale':
|
|
if self.move_type == 'out_refund':
|
|
internal_types_domain = ('internal_type', '=', 'credit_note')
|
|
else:
|
|
internal_types_domain = ('internal_type', 'in', ['invoice', 'debit_note'])
|
|
domain = [('country_id.code', '=', 'CL'), internal_types_domain]
|
|
if self.company_id.partner_id.l10n_cl_sii_taxpayer_type == '1':
|
|
domain += [('code', '!=', '71')] # Companies with VAT Affected doesn't have "Boleta de honorarios Electrónica"
|
|
return domain
|
|
if self.move_type == 'in_refund':
|
|
internal_types_domain = ('internal_type', '=', 'credit_note')
|
|
else:
|
|
internal_types_domain = ('internal_type', 'in', ['invoice', 'debit_note', 'credit_note', 'invoice_in'])
|
|
domain = [
|
|
('country_id.code', '=', 'CL'),
|
|
internal_types_domain,
|
|
]
|
|
if self.partner_id.l10n_cl_sii_taxpayer_type == '1' and self.partner_id_vat != '60805000-0':
|
|
domain += [('code', 'not in', ['39', '70', '71', '914', '911'])]
|
|
elif self.partner_id.l10n_cl_sii_taxpayer_type == '1' and self.partner_id_vat == '60805000-0':
|
|
domain += [('code', 'not in', ['39', '70', '71'])]
|
|
if self.move_type == 'in_invoice':
|
|
domain += [('internal_type', '!=', 'credit_note')]
|
|
elif self.partner_id.l10n_cl_sii_taxpayer_type == '2':
|
|
domain += [('code', 'in', ['70', '71', '56', '61'])]
|
|
elif self.partner_id.l10n_cl_sii_taxpayer_type == '3':
|
|
domain += [('code', 'in', ['35', '38', '39', '41', '56', '61'])]
|
|
elif not self.partner_id.l10n_cl_sii_taxpayer_type or self.partner_id.country_id != self.env.ref(
|
|
'base.cl') or self.partner_id.l10n_cl_sii_taxpayer_type == '4':
|
|
domain += [('code', 'in', [])]
|
|
return domain
|
|
|
|
def _check_document_types_post(self):
|
|
for rec in self.filtered(
|
|
lambda r: r.company_id.account_fiscal_country_id.code == "CL" and
|
|
r.journal_id.type in ['sale', 'purchase']):
|
|
tax_payer_type = rec.partner_id.l10n_cl_sii_taxpayer_type
|
|
vat = rec.partner_id.vat
|
|
country_id = rec.partner_id.country_id
|
|
latam_document_type_code = rec.l10n_latam_document_type_id.code
|
|
if (not tax_payer_type or not vat) and (country_id.code == "CL" and latam_document_type_code
|
|
and latam_document_type_code not in ['35', '38', '39', '41']):
|
|
raise ValidationError(_('Tax payer type and vat number are mandatory for this type of '
|
|
'document. Please set the current tax payer type of this customer'))
|
|
if rec.journal_id.type == 'sale' and rec.journal_id.l10n_latam_use_documents:
|
|
if country_id.code != "CL":
|
|
if not ((tax_payer_type == '4' and latam_document_type_code in ['110', '111', '112']) or (
|
|
tax_payer_type == '3' and latam_document_type_code in ['39', '41', '61', '56'])):
|
|
raise ValidationError(_(
|
|
'Document types for foreign customers must be export type (codes 110, 111 or 112) or you should define the customer as an end consumer and use receipts (codes 39 or 41)'))
|
|
if rec.journal_id.type == 'purchase' and rec.journal_id.l10n_latam_use_documents:
|
|
if vat != SII_VAT and latam_document_type_code == '914':
|
|
raise ValidationError(_('The DIN document is intended to be used only with RUT 60805000-0'
|
|
' (Tesorería General de La República)'))
|
|
if not tax_payer_type or not vat:
|
|
if country_id.code == "CL" and latam_document_type_code not in [
|
|
'35', '38', '39', '41']:
|
|
raise ValidationError(_('Tax payer type and vat number are mandatory for this type of '
|
|
'document. Please set the current tax payer type of this supplier'))
|
|
if tax_payer_type == '2' and latam_document_type_code not in ['70', '71', '56', '61']:
|
|
raise ValidationError(_('The tax payer type of this supplier is incorrect for the selected type'
|
|
' of document.'))
|
|
if tax_payer_type in ['1', '3']:
|
|
if latam_document_type_code in ['70', '71']:
|
|
raise ValidationError(_('The tax payer type of this supplier is not entitled to deliver '
|
|
'fees documents'))
|
|
if latam_document_type_code in ['110', '111', '112']:
|
|
raise ValidationError(_('The tax payer type of this supplier is not entitled to deliver '
|
|
'imports documents'))
|
|
if tax_payer_type == '4' or country_id.code != "CL":
|
|
raise ValidationError(_('You need a journal without the use of documents for foreign '
|
|
'suppliers'))
|
|
|
|
@api.onchange('journal_id')
|
|
def _l10n_cl_onchange_journal(self):
|
|
if self.company_id.country_id.code == 'CL':
|
|
self.l10n_latam_document_type_id = False
|
|
|
|
def _post(self, soft=True):
|
|
self._check_document_types_post()
|
|
return super()._post(soft)
|
|
|
|
def _l10n_cl_get_formatted_sequence(self, number=0):
|
|
return '%s %06d' % (self.l10n_latam_document_type_id.doc_code_prefix, number)
|
|
|
|
def _get_starting_sequence(self):
|
|
""" If use documents then will create a new starting sequence using the document type code prefix and the
|
|
journal document number with a 6 padding number """
|
|
if self.journal_id.l10n_latam_use_documents and self.company_id.account_fiscal_country_id.code == "CL":
|
|
if self.l10n_latam_document_type_id:
|
|
return self._l10n_cl_get_formatted_sequence()
|
|
return super()._get_starting_sequence()
|
|
|
|
def _get_last_sequence_domain(self, relaxed=False):
|
|
where_string, param = super(AccountMove, self)._get_last_sequence_domain(relaxed)
|
|
if self.company_id.account_fiscal_country_id.code == "CL" and self.l10n_latam_use_documents:
|
|
where_string = where_string.replace('journal_id = %(journal_id)s AND', '')
|
|
where_string += ' AND l10n_latam_document_type_id = %(l10n_latam_document_type_id)s AND ' \
|
|
'company_id = %(company_id)s AND move_type IN %(move_type)s'
|
|
|
|
param['company_id'] = self.company_id.id or False
|
|
param['l10n_latam_document_type_id'] = self.l10n_latam_document_type_id.id or 0
|
|
param['move_type'] = (('in_invoice', 'in_refund') if
|
|
self.l10n_latam_document_type_id._is_doc_type_vendor() else ('out_invoice', 'out_refund'))
|
|
return where_string, param
|
|
|
|
def _get_name_invoice_report(self):
|
|
self.ensure_one()
|
|
if self.l10n_latam_use_documents and self.company_id.account_fiscal_country_id.code == 'CL':
|
|
return 'l10n_cl.report_invoice_document'
|
|
return super()._get_name_invoice_report()
|
|
|
|
def _l10n_cl_get_invoice_totals_for_report(self):
|
|
self.ensure_one()
|
|
include_sii = self._l10n_cl_include_sii()
|
|
|
|
base_lines = self.line_ids.filtered(lambda x: x.display_type == 'product')
|
|
tax_lines = self.line_ids.filtered(lambda x: x.display_type == 'tax')
|
|
|
|
base_line_vals_list = [x._convert_to_tax_base_line_dict() for x in base_lines]
|
|
if include_sii:
|
|
for vals in base_line_vals_list:
|
|
vals['taxes'] = vals['taxes'].flatten_taxes_hierarchy().filtered(lambda tax: tax.l10n_cl_sii_code != 14)
|
|
|
|
tax_line_vals_list = [x._convert_to_tax_line_dict() for x in tax_lines]
|
|
if include_sii:
|
|
tax_line_vals_list = [x for x in tax_line_vals_list if x['tax_repartition_line'].tax_id.l10n_cl_sii_code != 14]
|
|
|
|
tax_totals = self.env['account.tax']._prepare_tax_totals(
|
|
base_line_vals_list,
|
|
self.currency_id,
|
|
tax_lines=tax_line_vals_list,
|
|
)
|
|
|
|
if include_sii:
|
|
tax_totals['amount_total'] = self.amount_total
|
|
tax_totals['amount_untaxed'] = self.currency_id.round(
|
|
tax_totals['amount_total'] - sum([x['tax_amount'] for x in tax_line_vals_list if 'tax_amount' in x]))
|
|
tax_totals['formatted_amount_total'] = formatLang(self.env, tax_totals['amount_total'], currency_obj=self.currency_id)
|
|
tax_totals['formatted_amount_untaxed'] = formatLang(self.env, tax_totals['amount_untaxed'], currency_obj=self.currency_id)
|
|
if tax_totals['subtotals']:
|
|
tax_totals['subtotals'][0]['formatted_amount'] = tax_totals['formatted_amount_untaxed']
|
|
|
|
return tax_totals
|
|
|
|
def _l10n_cl_include_sii(self):
|
|
self.ensure_one()
|
|
return self.l10n_latam_document_type_id.code in ['39', '41', '110', '111', '112', '34']
|
|
|
|
def _is_manual_document_number(self):
|
|
if self.journal_id.company_id.country_id.code == 'CL':
|
|
return self.journal_id.type == 'purchase' and not self.l10n_latam_document_type_id._is_doc_type_vendor()
|
|
return super()._is_manual_document_number()
|