2
0

[MIG] account_asset_management: Handle account_asset_disposal migration

This commit is contained in:
Pedro M. Baeza 2019-11-07 21:28:41 +01:00 committed by Rodrigo
parent 6673fcab25
commit e236708977
2 changed files with 52 additions and 0 deletions

View File

@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from psycopg2 import sql
from odoo.tools import float_is_zero
def adjust_asset_values(env):
@ -63,6 +64,31 @@ def adjust_aml_values(env):
)
def handle_account_asset_disposal_migration(env):
"""Take care of potentially installed `account_asset_disposal` module.
In this phase we set the last asset line to the type remove on the
corresponding assets.
"""
column_name = openupgrade.get_legacy_name('disposal_move_id')
if not openupgrade.column_exists(env.cr, 'account_asset', column_name):
return
env.cr.execute(
sql.SQL(
"SELECT id FROM account_asset WHERE {col} IS NOT NULL"
).format(col=sql.Identifier(column_name))
)
assets = env['account.asset'].with_context(
allow_asset_line_update=True,
).browse(x[0] for x in env.cr.fetchall())
assets.mapped('depreciation_line_ids').filtered(
lambda x: float_is_zero(
x.remaining_value,
precision_rounding=x.asset_id.company_currency_id.rounding,
)
).write({'type': 'remove'})
@openupgrade.migrate()
def migrate(env, version):
copied_column = openupgrade.get_legacy_name('method_time')
@ -71,3 +97,4 @@ def migrate(env, version):
return
adjust_asset_values(env)
adjust_aml_values(env)
handle_account_asset_disposal_migration(env)

View File

@ -48,6 +48,30 @@ _field_renames = [
]
def handle_account_asset_disposal_migration(env):
"""Take care of potentially installed `account_asset_disposal` module.
In this phase we rename stuff for adapting to the new data structure.
"""
cr = env.cr
if not openupgrade.column_exists(cr, 'account_asset', 'disposal_move_id'):
return
openupgrade.copy_columns(cr, {'account_asset': [('state', None, None)]})
openupgrade.rename_columns(
cr, {'account_asset': [('disposal_move_id', None)]})
openupgrade.map_values(
cr, openupgrade.get_legacy_name('state'), 'state',
[('disposed', 'removed')], table='account_asset',
)
openupgrade.rename_fields(
env, [
('account.asset', 'account_asset', 'disposal_date', 'date_remove'),
('account.asset.profile', 'account_asset_profile',
'account_loss_id', 'account_residual_value_id'),
],
)
@openupgrade.migrate()
def migrate(env, version):
cr = env.cr
@ -63,3 +87,4 @@ def migrate(env, version):
openupgrade.copy_columns(cr, _column_copies)
openupgrade.rename_columns(cr, _column_renames)
openupgrade.rename_fields(env, _field_renames)
handle_account_asset_disposal_migration(env)