Skip to content

Commit

Permalink
Merge pull request #1 from tafaRU/12.0-fix-l10n_it_fatturapa_in-migr-1.6
Browse files Browse the repository at this point in the history
l10n_it_fatturapa_in: avoid duplication in the code of post migration script (DRY principle)
  • Loading branch information
sergiocorato authored Dec 10, 2020
2 parents ccb0e4c + e15f365 commit 3b72693
Showing 1 changed file with 31 additions and 68 deletions.
99 changes: 31 additions & 68 deletions l10n_it_fatturapa_in/migrations/12.0.2.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,41 @@
# Copyright 2020 Sergio Corato <https://github.com/sergiocorato>
# Copyright 2020 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from psycopg2 import sql


def create_withholding_data_lines(env):
# create ftpa_withholding_ids from ftpa_withholding_type and ftpa_withholding_amount
column_name = openupgrade.get_legacy_name('ftpa_withholding_amount')
if openupgrade.column_exists(env.cr, 'account_invoice', column_name):
openupgrade.logged_query(
env.cr, sql.SQL(
"""
INSERT INTO withholding_data_line
(
name,
amount,
invoice_id,
create_uid,
create_date,
write_date,
write_uid
)
SELECT
ai.{ftpa_withholding_type},
ai.{ftpa_withholding_amount},
ai.id,
ai.create_uid,
ai.create_date,
ai.write_date,
ai.write_uid
FROM account_invoice ai
WHERE ai.{ftpa_withholding_type} IS NOT NULL;
"""
).format(
ftpa_withholding_type=sql.Identifier(
openupgrade.get_legacy_name(
'ftpa_withholding_type')
),
ftpa_withholding_amount=sql.Identifier(
openupgrade.get_legacy_name(
'ftpa_withholding_amount')
),
)
)
else:
openupgrade.logged_query(
env.cr, sql.SQL(
"""
INSERT INTO withholding_data_line
(
name,
invoice_id,
create_uid,
create_date,
write_date,
write_uid
)
SELECT
ai.{ftpa_withholding_type},
ai.id,
ai.create_uid,
ai.create_date,
ai.write_date,
ai.write_uid
FROM account_invoice ai
WHERE ai.{ftpa_withholding_type} IS NOT NULL;
"""
).format(
ftpa_withholding_type=sql.Identifier(
openupgrade.get_legacy_name(
'ftpa_withholding_type')
),
)
)
"""
Create ftpa_withholding_ids from ftpa_withholding_type
and ftpa_withholding_amount
"""
column_wht_amount = openupgrade.get_legacy_name('ftpa_withholding_amount')
column_wht_type = openupgrade.get_legacy_name('ftpa_withholding_type')
exists = openupgrade.column_exists(env.cr, 'account_invoice', column_wht_amount)
mapping = {
'name': 'ai.{ftpa_withholding_type}'.format(
ftpa_withholding_type=column_wht_type),
'invoice_id': 'ai.id',
'create_uid': 'ai.create_uid',
'create_date': 'ai.create_date',
'write_date': 'ai.write_date',
'write_uid': 'ai.write_uid',
}
if exists:
mapping.update(
{'amount': 'ai.{ftpa_withholding_amount}'.format(
ftpa_withholding_amount=column_wht_amount)})
query = """
INSERT INTO withholding_data_line
({columns})
SELECT {values}
FROM account_invoice AS ai
WHERE ai.{ftpa_withholding_type} IS NOT NULL;""".format(
columns=','.join(mapping.keys()),
values=','.join(mapping.values()),
ftpa_withholding_type=column_wht_type)
openupgrade.logged_query(env.cr, sql.SQL(query))


@openupgrade.migrate()
Expand Down

0 comments on commit 3b72693

Please sign in to comment.