Skip to content

Commit

Permalink
[IMP] account_voucher: Optimize computed field doing it via SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobaeza committed Dec 16, 2019
1 parent 7a49881 commit ae1aeea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
33 changes: 23 additions & 10 deletions addons/account_voucher/migrations/9.0.1.0/post-migration.py
Expand Up @@ -2,8 +2,6 @@
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from openerp.addons.account_voucher.account_voucher import \
account_voucher_line


def create_payments_from_vouchers(env):
Expand Down Expand Up @@ -113,16 +111,31 @@ def create_voucher_line_tax_lines(env):
"where v.tax_id is not null")


@openupgrade.logging()
def set_voucher_line_amount(env):
"""We replicate here the code of the compute function and set the value
finally via SQL for avoiding the trigger of the rest of the computed
fields that depends on this field.
"""
lines = env['account.voucher.line'].search([])
for line in openupgrade.chunked(lines):
if line.tax_ids:
taxes = line.tax_ids.compute_all(
line.price_unit, line.voucher_id.currency_id, line.quantity,
product=line.product_id, partner=line.voucher_id.partner_id)
price_subtotal = taxes['total_excluded']
else:
price_subtotal = line.quantity * line.price_unit
env.cr.execute(
"""UPDATE account_voucher_line
SET price_subtotal = %s
WHERE id = %s""", (price_subtotal, line.id),
)


@openupgrade.migrate(use_env=True)
def migrate(env, version):
"""Control function for account_voucher migration."""
create_payments_from_vouchers(env)
create_voucher_line_tax_lines(env)

if 'price_subtotal' in account_voucher_line._openupgrade_recompute_fields_blacklist:
# This means we have no taxes and we can update the price_subtotal
# quite simply.
openupgrade.logged_query(
env.cr,
'UPDATE account_voucher_line SET price_subtotal = quantity * price_unit'
)
set_voucher_line_amount(env)
21 changes: 6 additions & 15 deletions addons/account_voucher/migrations/9.0.1.0/pre-migration.py
Expand Up @@ -4,10 +4,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade
from openerp.addons.account_voucher.account_voucher import \
account_voucher_line

account_voucher_line._openupgrade_recompute_fields_blacklist = []


column_copies = {
Expand Down Expand Up @@ -41,14 +37,9 @@ def migrate(env, version):
openupgrade.copy_columns(cr, column_copies)
openupgrade.rename_fields(env, field_renames)
delete_payment_views(cr)
cr.execute('SELECT count(*) FROM account_voucher WHERE tax_id IS NOT NULL')
taxed_vouchers = cr.fetchone()[0]
if not taxed_vouchers:
# If you have a DB where all account vouchers have no tax applied (we
# do), the new field price_subtotal can be computed from price_unit
# (amount) and quantity (1).
#
# See the post migration script to have the full idea.
account_voucher_line._openupgrade_recompute_fields_blacklist.append(
'price_subtotal'
)
openupgrade.add_fields(
env, [
('price_subtotal', 'account.voucher.line', 'account_voucher_line',
'monetary', False, 'account_voucher'),
],
)

0 comments on commit ae1aeea

Please sign in to comment.