Skip to content

Commit

Permalink
Merge PR #2114 into 9.0
Browse files Browse the repository at this point in the history
Signed-off-by StefanRijnhart
  • Loading branch information
OCA-git-bot committed Dec 16, 2019
2 parents bad7259 + ae1aeea commit 967d9d2
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 967d9d2

Please sign in to comment.