Browse Source
[FIX+IMP+MIG] contract: Several refinements:
[FIX+IMP+MIG] contract: Several refinements:
* Remove incorrect oldname attributes. * Add filter on partners for running contracts (+ a support o2m field for that). * Cover more tables in model renaming + cleaner code using a loop. * Don't copy contract lines, but rename table + copy contract records on pre. * Contract code is now populated to "Reference/Description" field in invoice. * Order on new contract model has been restored to the same as old analytic accounts.pull/207/head
Pedro M. Baeza
5 years ago
committed by
sbejaoui
8 changed files with 122 additions and 268 deletions
-
163contract/migrations/12.0.4.0.0/post-migration.py
-
194contract/migrations/12.0.4.0.0/pre-migration.py
-
1contract/models/abstract_contract_line.py
-
4contract/models/contract.py
-
1contract/models/contract_line.py
-
1contract/models/contract_template.py
-
5contract/models/res_partner.py
-
21contract/views/res_partner_view.xml
@ -1,113 +1,113 @@ |
|||
# Copyright 2019 ACSONE SA/NV |
|||
# Copyright 2019 Tecnativa 2019 - Pedro M. Baeza |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
|
|||
import logging |
|||
|
|||
from openupgradelib import openupgrade |
|||
from psycopg2 import sql |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
models_to_rename = [ |
|||
# Contract Line Wizard |
|||
('account.analytic.invoice.line.wizard', 'contract.line.wizard'), |
|||
# Abstract Contract |
|||
('account.abstract.analytic.contract', 'contract.abstract.contract'), |
|||
# Abstract Contract Line |
|||
( |
|||
'account.abstract.analytic.contract.line', |
|||
'contract.abstract.contract.line', |
|||
), |
|||
# Contract Line |
|||
('account.analytic.invoice.line', 'contract.line'), |
|||
# Contract Template |
|||
('account.analytic.contract', 'contract.template'), |
|||
# Contract Template Line |
|||
('account.analytic.contract.line', 'contract.template.line'), |
|||
] |
|||
tables_to_rename = [ |
|||
# Contract Line |
|||
('account_analytic_invoice_line', 'contract_line'), |
|||
# Contract Template |
|||
('account_analytic_contract', 'contract_template'), |
|||
# Contract Template Line |
|||
('account_analytic_contract_line', 'contract_template_line'), |
|||
] |
|||
columns_to_copy = { |
|||
'contract_line': [ |
|||
('analytic_account_id', 'contract_id', None), |
|||
], |
|||
} |
|||
xmlids_to_rename = [ |
|||
( |
|||
'contract.account_analytic_cron_for_invoice', |
|||
'contract.contract_cron_for_invoice', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_manager', |
|||
'contract.contract_template_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_user', |
|||
'contract.contract_template_user', |
|||
), |
|||
( |
|||
'contract.account_analytic_invoice_line_manager', |
|||
'contract.contract_line_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_invoice_line_user', |
|||
'contract.contract_line_user', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_line_manager', |
|||
'contract.contract_template_line_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_line_user', |
|||
'contract.contract_template_line_user', |
|||
), |
|||
] |
|||
|
|||
|
|||
def _get_contract_field_name(cr): |
|||
""" |
|||
Contract field changed the name from analytic_account_id to contract_id |
|||
in 12.0.2.0.0. This method used to get the contract field name in |
|||
account_analytic_invoice_line""" |
|||
return ( |
|||
'contract_id' |
|||
if openupgrade.column_exists( |
|||
cr, 'account_analytic_invoice_line', 'contract_id' |
|||
) |
|||
else 'analytic_account_id' |
|||
) |
|||
|
|||
|
|||
def create_contract_records(cr): |
|||
contract_field_name = _get_contract_field_name(cr) |
|||
openupgrade.logged_query( |
|||
cr, """ |
|||
CREATE TABLE contract_contract |
|||
(LIKE account_analytic_account INCLUDING ALL)""", |
|||
) |
|||
openupgrade.logged_query( |
|||
cr, sql.SQL(""" |
|||
INSERT INTO contract_contract |
|||
SELECT * FROM account_analytic_account |
|||
WHERE id IN (SELECT DISTINCT {} FROM contract_line) |
|||
""").format( |
|||
sql.Identifier(contract_field_name), |
|||
), |
|||
) |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
_logger.info(">> Pre-Migration 12.0.4.0.0") |
|||
cr = env.cr |
|||
|
|||
models_to_rename = [ |
|||
# Contract Line Wizard |
|||
('account.analytic.invoice.line.wizard', 'contract.line.wizard'), |
|||
# Abstract Contract |
|||
('account.abstract.analytic.contract', 'contract.abstract.contract'), |
|||
# Abstract Contract Line |
|||
( |
|||
'account.abstract.analytic.contract.line', |
|||
'contract.abstract.contract.line', |
|||
), |
|||
# Contract Line |
|||
('account.analytic.invoice.line', 'contract.line'), |
|||
# Contract Template |
|||
('account.analytic.contract', 'contract.template'), |
|||
# Contract Template Line |
|||
('account.analytic.contract.line', 'contract.template.line'), |
|||
] |
|||
tables_to_rename = [ |
|||
# Contract Template |
|||
('account_analytic_contract', 'contract_template'), |
|||
# Contract Template Line |
|||
('account_analytic_contract_line', 'contract_template_line'), |
|||
] |
|||
xmlids_to_rename = [ |
|||
( |
|||
'contract.account_analytic_cron_for_invoice', |
|||
'contract.contract_cron_for_invoice', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_manager', |
|||
'contract.contract_template_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_user', |
|||
'contract.contract_template_user', |
|||
), |
|||
( |
|||
'contract.account_analytic_invoice_line_manager', |
|||
'contract.contract_line_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_invoice_line_user', |
|||
'contract.contract_line_user', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_line_manager', |
|||
'contract.contract_template_line_manager', |
|||
), |
|||
( |
|||
'contract.account_analytic_contract_line_user', |
|||
'contract.contract_template_line_user', |
|||
), |
|||
] |
|||
openupgrade.rename_models(cr, models_to_rename) |
|||
openupgrade.rename_tables(cr, tables_to_rename) |
|||
openupgrade.rename_xmlids(cr, xmlids_to_rename) |
|||
# A temporary column is needed to avoid breaking the foreign key constraint |
|||
# The temporary column is dropped in the post-migration script |
|||
openupgrade.logged_query( |
|||
cr, |
|||
""" |
|||
ALTER TABLE account_invoice_line |
|||
ADD COLUMN contract_line_id_tmp INTEGER |
|||
""", |
|||
) |
|||
if openupgrade.column_exists( |
|||
cr, 'account_invoice_line', 'contract_line_id' |
|||
): |
|||
openupgrade.logged_query( |
|||
cr, |
|||
""" |
|||
UPDATE account_invoice_line |
|||
SET contract_line_id_tmp = contract_line_id |
|||
""", |
|||
) |
|||
openupgrade.logged_query( |
|||
cr, |
|||
""" |
|||
UPDATE account_invoice_line SET contract_line_id = NULL |
|||
""", |
|||
) |
|||
if not openupgrade.column_exists( |
|||
cr, 'account_invoice', 'old_contract_id' |
|||
): |
|||
openupgrade.logged_query( |
|||
cr, |
|||
""" |
|||
ALTER TABLE account_invoice |
|||
ADD COLUMN old_contract_id_tmp INTEGER |
|||
""", |
|||
) |
|||
openupgrade.logged_query( |
|||
cr, |
|||
""" |
|||
UPDATE account_invoice |
|||
SET old_contract_id_tmp = contract_id |
|||
""", |
|||
) |
|||
openupgrade.copy_columns(cr, columns_to_copy) |
|||
create_contract_records(cr) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue