Merge pull request #393 from Tecnativa/9.0-account_payment_mode-ou_fix

[IMP] account_payment_*: Complete migration scripts
This commit is contained in:
Pedro M. Baeza
2017-09-21 02:09:55 +02:00
committed by GitHub
2 changed files with 78 additions and 0 deletions

View File

@@ -21,6 +21,8 @@ def migrate_from_8(cr):
cr.execute('ALTER TABLE payment_mode RENAME TO account_payment_mode')
cr.execute('ALTER SEQUENCE payment_mode_id_seq '
'RENAME TO account_payment_mode_id_seq')
# Set fixed bank account in all, which is the equivalent behavior in v8
cr.execute("UPDATE account_payment_mode SET bank_account_link='fixed'")
cr.execute(
'ALTER TABLE account_payment_mode '
'RENAME COLUMN journal TO fixed_journal_id'

View File

@@ -59,6 +59,25 @@ column_renames_payment_transfer = {
],
}
# They are being handled after module rename, so source module name is correct
EXISTING_PAYMENT_MODE_TYPE_XML_IDS = {
'account_payment_order.manual_bank_tranfer': (
'account.account_payment_method_manual_out',
),
'account_payment_order.manual_direct_debit': (
'account.account_payment_method_manual_in',
),
}
TO_CREATE_PAYMENT_MODE_TYPE_XML_IDS = {
'account_banking_sepa_direct_debit.export_sdd_008_001_02': (
'account_banking_sepa_direct_debit.sepa_direct_debit',
),
'account_banking_sepa_credit_transfer.export_sepa_sct_001_001_03': (
'account_banking_sepa_credit_transfer.sepa_credit_transfer',
),
}
def install_new_modules(cr):
sql = """
@@ -69,6 +88,62 @@ def install_new_modules(cr):
openupgrade.logged_query(cr, sql)
def migrate_payment_mode_types(env):
"""Create payment methods for each old payment type defined, and assign it
on payment modes."""
payment_method_obj = env['account.payment.method']
imr_obj = env['ir.model.data']
# Add a column in payment method for storing old payment_mode_type ID
old_column_name = openupgrade.get_legacy_name('payment_mode_type_id')
env.cr.execute(
"ALTER TABLE account_payment_method ADD %s INTEGER" % old_column_name
)
env.cr.execute(
"""SELECT id, name, code, payment_order_type, active
FROM payment_mode_type
"""
)
for row in env.cr.fetchall():
# Look if this type has a key XML-ID for not duplicating records with
# the same goal
imr = imr_obj.search([
('res_id', '=', row[0]),
('model', '=', 'payment_mode_type'),
])
xml_id = "%s.%s" % (imr.module, imr.name)
if xml_id in EXISTING_PAYMENT_MODE_TYPE_XML_IDS:
method = env.ref(EXISTING_PAYMENT_MODE_TYPE_XML_IDS[xml_id])
else:
method = payment_method_obj.create({
'name': row[1],
'code': row[2],
'payment_type': (
'outbound' if row[3] == 'payment' else 'outbound'
),
'active': row[4],
})
env.cr.execute(
"UPDATE account_payment_method SET %s = %%s" % old_column_name,
(method.id, ),
)
if xml_id in TO_CREATE_PAYMENT_MODE_TYPE_XML_IDS:
# Create XML-ID for this yet non existing records
mod, name = TO_CREATE_PAYMENT_MODE_TYPE_XML_IDS[xml_id].split('.')
imr_obj.create({
'module': mod,
'name': name,
'res_id': method.id,
'model': method._name,
})
openupgrade.logged_query(
env.cr,
"""UPDATE account_payment_mode
SET payment_method_id = %s
WHERE type = %s""",
(method.id, row[0]),
)
@openupgrade.migrate(use_env=True)
def migrate(env, version):
install_new_modules(env.cr)
@@ -87,6 +162,7 @@ def migrate(env, version):
[('account_banking_payment_export', 'account_payment_order')],
merge_modules=True)
openupgrade.rename_columns(env.cr, column_renames_payment_export)
migrate_payment_mode_types(env)
if openupgrade.is_module_installed(
env.cr, 'account_banking_payment_transfer'):