From ae1aeea5f04d41b5a2732e31314631cc8148a102 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Wed, 11 Dec 2019 12:52:09 +0100 Subject: [PATCH] [IMP] account_voucher: Optimize computed field doing it via SQL --- .../migrations/9.0.1.0/post-migration.py | 33 +++++++++++++------ .../migrations/9.0.1.0/pre-migration.py | 21 ++++-------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/addons/account_voucher/migrations/9.0.1.0/post-migration.py b/addons/account_voucher/migrations/9.0.1.0/post-migration.py index 22ae37918db0..9b3ebad1b979 100644 --- a/addons/account_voucher/migrations/9.0.1.0/post-migration.py +++ b/addons/account_voucher/migrations/9.0.1.0/post-migration.py @@ -2,8 +2,6 @@ # © 2016 Therp BV # 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): @@ -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) diff --git a/addons/account_voucher/migrations/9.0.1.0/pre-migration.py b/addons/account_voucher/migrations/9.0.1.0/pre-migration.py index 3e657695abff..38ebdcc50d04 100644 --- a/addons/account_voucher/migrations/9.0.1.0/pre-migration.py +++ b/addons/account_voucher/migrations/9.0.1.0/pre-migration.py @@ -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 = { @@ -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'), + ], + )