From d93139d1476f9b2c10691d8e19c80ce03291cf6c Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Mon, 15 Jul 2019 19:28:04 +0200 Subject: [PATCH] [IMP] - Make one migration from 12.0.1 to 12.0.4 [RMV] - Remove empty file [IMP] - Update no_update cron after migration [IMP] - move contract template recurrence info to line level [FIX] - Fix contract line model description [IMP] - Link contracts to analytic accounts [FIX] - Fix pylint [IMP] - Move chatter and attachments from analytic account to contract [IMP] - Move account_analytic_id to contract line level [IMP] - Improve version check in migration script [IMP] - Move contracts followers from analytic accounts [ADD] - Add mail.activity.mixin to contract.contract model remove data drop from migration scripts [12.0][FIX] - Fix _init_last_date_invoiced fix flake8 [ADD] - Update contributors list --- .../migrations/12.0.2.0.0/post-migration.py | 30 -- .../migrations/12.0.2.0.0/pre-migration.py | 92 +++++- .../migrations/12.0.3.0.0/post-migration.py | 16 - .../migrations/12.0.4.0.0/post-migration.py | 308 +++++++++++------- .../migrations/12.0.4.0.0/pre-migration.py | 103 ++++-- contract/models/account_invoice.py | 4 +- contract/models/contract.py | 11 +- contract/models/contract_line.py | 10 +- contract/readme/CONTRIBUTORS.rst | 3 +- contract/views/account_invoice_view.xml | 0 contract/views/contract.xml | 4 +- contract/views/contract_line.xml | 6 + 12 files changed, 357 insertions(+), 230 deletions(-) delete mode 100644 contract/migrations/12.0.2.0.0/post-migration.py delete mode 100644 contract/migrations/12.0.3.0.0/post-migration.py delete mode 100644 contract/views/account_invoice_view.xml diff --git a/contract/migrations/12.0.2.0.0/post-migration.py b/contract/migrations/12.0.2.0.0/post-migration.py deleted file mode 100644 index 9fa47c68..00000000 --- a/contract/migrations/12.0.2.0.0/post-migration.py +++ /dev/null @@ -1,30 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2018 ACSONE SA/NV -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -import logging -from odoo import SUPERUSER_ID, api - - -_logger = logging.getLogger(__name__) - - -def migrate(cr, version): - """Copy recurrence info from contract to contract lines and compute - last_date_invoiced""" - _logger.info(">> Post-Migration 12.0.2.0.0") - cr.execute( - """UPDATE account_analytic_invoice_line AS contract_line - SET recurring_rule_type=contract.recurring_rule_type, - recurring_invoicing_type=contract.recurring_invoicing_type, - recurring_interval=contract.recurring_interval, - recurring_next_date=contract.recurring_next_date, - date_start=contract.date_start, - date_end=contract.date_end - FROM account_analytic_account AS contract - WHERE contract.id=contract_line.contract_id""" - ) - env = api.Environment(cr, SUPERUSER_ID, {}) - contract_lines = env["account.analytic.invoice.line"].search( - [("recurring_next_date", "!=", False)] - ) - contract_lines._init_last_date_invoiced() diff --git a/contract/migrations/12.0.2.0.0/pre-migration.py b/contract/migrations/12.0.2.0.0/pre-migration.py index 0f616ab6..87de6704 100644 --- a/contract/migrations/12.0.2.0.0/pre-migration.py +++ b/contract/migrations/12.0.2.0.0/pre-migration.py @@ -3,22 +3,92 @@ import logging -from odoo import SUPERUSER_ID, api +from openupgradelib import openupgrade _logger = logging.getLogger(__name__) -def migrate(cr, version): +def _set_finished_contract(cr): + _logger.info("set recurring_next_date to false for finished contract") + openupgrade.logged_query( + cr, + """ + UPDATE account_analytic_account + SET recurring_next_date=NULL + WHERE recurring_next_date > date_end + """, + ) + + +def _move_contract_recurrence_info_to_contract_line(cr): + _logger.info("Move contract data to line level") + openupgrade.logged_query( + cr, + """ + ALTER TABLE account_analytic_invoice_line + ADD COLUMN IF NOT EXISTS recurring_rule_type VARCHAR(255), + ADD COLUMN IF NOT EXISTS recurring_invoicing_type VARCHAR(255), + ADD COLUMN IF NOT EXISTS recurring_interval INTEGER, + ADD COLUMN IF NOT EXISTS recurring_next_date DATE, + ADD COLUMN IF NOT EXISTS date_start DATE, + ADD COLUMN IF NOT EXISTS date_end DATE + """, + ) + + openupgrade.logged_query( + cr, + """ + UPDATE account_analytic_invoice_line AS contract_line + SET + recurring_rule_type=contract.recurring_rule_type, + recurring_invoicing_type=contract.recurring_invoicing_type, + recurring_interval=contract.recurring_interval, + recurring_next_date=contract.recurring_next_date, + date_start=contract.date_start, + date_end=contract.date_end + FROM + account_analytic_account AS contract + WHERE + contract.id=contract_line.analytic_account_id + """, + ) + + +def _move_contract_template_recurrence_info_to_contract_template_line(cr): + _logger.info("Move contract template data to line level") + openupgrade.logged_query( + cr, + """ + ALTER TABLE account_analytic_contract_line + ADD COLUMN IF NOT EXISTS recurring_rule_type VARCHAR(255), + ADD COLUMN IF NOT EXISTS recurring_invoicing_type VARCHAR(255), + ADD COLUMN IF NOT EXISTS recurring_interval INTEGER + """, + ) + + openupgrade.logged_query( + cr, + """ + UPDATE account_analytic_contract_line AS contract_template_line + SET + recurring_rule_type=contract_template.recurring_rule_type, + recurring_invoicing_type=contract_template.recurring_invoicing_type, + recurring_interval=contract_template.recurring_interval + FROM + account_analytic_contract AS contract_template + WHERE + contract_template.id=contract_template_line.analytic_account_id + """, + ) + + +@openupgrade.migrate() +def migrate(env, version): """ set recurring_next_date to false for finished contract """ _logger.info(">> Pre-Migration 12.0.2.0.0") - with api.Environment(cr, SUPERUSER_ID, {}) as env: - contracts = env["account.analytic.account"].search([]) - finished_contract = contracts.filtered( - lambda c: not c.create_invoice_visibility - ) - cr.execute( - "UPDATE account_analytic_account set recurring_next_date=null " - "where id in (%)" % ','.join(finished_contract.ids) - ) + cr = env.cr + _set_finished_contract(cr) + _move_contract_recurrence_info_to_contract_line(cr) + _move_contract_template_recurrence_info_to_contract_template_line(cr) diff --git a/contract/migrations/12.0.3.0.0/post-migration.py b/contract/migrations/12.0.3.0.0/post-migration.py deleted file mode 100644 index 9c2f3fd8..00000000 --- a/contract/migrations/12.0.3.0.0/post-migration.py +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright 2019 ACSONE SA/NV -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - -import logging - -from odoo import SUPERUSER_ID, api - -_logger = logging.getLogger(__name__) - - -def migrate(cr, version): - _logger.info(">> Post-Migration 12.0.3.0.0") - _logger.info("Populate invoicing partner field on contracts") - env = api.Environment(cr, SUPERUSER_ID, {}) - contracts = env["account.analytic.account"].search([]) - contracts._inverse_partner_id() diff --git a/contract/migrations/12.0.4.0.0/post-migration.py b/contract/migrations/12.0.4.0.0/post-migration.py index ef5691a6..23cea8e7 100644 --- a/contract/migrations/12.0.4.0.0/post-migration.py +++ b/contract/migrations/12.0.4.0.0/post-migration.py @@ -4,149 +4,207 @@ import logging from openupgradelib import openupgrade +from odoo.tools import parse_version _logger = logging.getLogger(__name__) +CONTRACT_FIELDS = [ + 'id', + 'name', + 'partner_id', + 'pricelist_id', + 'contract_type', + 'journal_id', + 'company_id', + 'active', + 'code', + 'group_id', + 'contract_template_id', + 'user_id', + 'recurring_next_date', + 'date_end', + 'message_main_attachment_id', + 'create_uid', + 'create_date', + 'write_uid', + 'write_date', + 'payment_term_id', + 'fiscal_position_id', + 'invoice_partner_id', +] -@openupgrade.migrate() -def migrate(env, version): - cr = env.cr +CONTRACT_LINE_FIELDS = [ + 'id', + 'product_id', + 'name', + 'quantity', + 'uom_id', + 'automatic_price', + 'specific_price', + 'discount', + 'recurring_rule_type', + 'recurring_invoicing_type', + 'recurring_interval', + 'sequence', + 'date_start', + 'date_end', + 'recurring_next_date', + 'last_date_invoiced', + 'termination_notice_date', + 'successor_contract_line_id', + 'predecessor_contract_line_id', + 'manual_renew_needed', + 'create_uid', + 'create_date', + 'write_uid', + 'write_date', +] + + +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 _copy_contract_table(cr): + contract_fields = [] + for field in CONTRACT_FIELDS: + if openupgrade.column_exists(cr, 'account_analytic_account', field): + contract_fields.append(field) + contract_field_name = _get_contract_field_name(cr) openupgrade.logged_query( cr, - """ - INSERT INTO contract_contract ( - id, - name, - partner_id, - pricelist_id, - contract_type, - journal_id, - company_id, - analytic_account_id, - active, - code, - group_id, - contract_template_id, - user_id, - recurring_next_date, - date_end, - payment_term_id, - fiscal_position_id, - invoice_partner_id, - message_main_attachment_id, - create_uid, - create_date, - write_uid, - write_date - ) - SELECT id, - name, - partner_id, - pricelist_id, - contract_type, - journal_id, - company_id, - id, - active, - code, - group_id, - contract_template_id, - user_id, - recurring_next_date, - date_end, - payment_term_id, - fiscal_position_id, - invoice_partner_id, - message_main_attachment_id, - create_uid, - create_date, - write_uid, - write_date - FROM account_analytic_account - WHERE id in ( - SELECT DISTINCT contract_id FROM account_analytic_invoice_line + "INSERT INTO contract_contract (" + + ', '.join(contract_fields) + + ") " + + "SELECT " + + ', '.join(contract_fields) + + " FROM account_analytic_account " + + "WHERE id in ( SELECT DISTINCT " + + contract_field_name + + " FROM " + + "account_analytic_invoice_line)", + ) + if openupgrade.column_exists(cr, 'account_invoice', 'old_contract_id_tmp'): + openupgrade.logged_query( + cr, + """ + UPDATE account_invoice + SET old_contract_id = old_contract_id_tmp + """, ) - """ + + # Move contract attachments + openupgrade.logged_query( + cr, + "UPDATE ir_attachment SET res_model='contract.contract'" + + "WHERE res_model='account.analytic.account' " + + "AND res_id IN ( SELECT DISTINCT " + + contract_field_name + + " FROM account_analytic_invoice_line)", ) + # Move contract messages openupgrade.logged_query( cr, - """ - INSERT INTO contract_line ( - id, - product_id, - name, - quantity, - uom_id, - automatic_price, - specific_price, - discount, - recurring_rule_type, - recurring_invoicing_type, - recurring_interval, - sequence, - contract_id, - date_start, - date_end, - recurring_next_date, - last_date_invoiced, - termination_notice_date, - successor_contract_line_id, - predecessor_contract_line_id, - manual_renew_needed, - active, - create_uid, - create_date, - write_uid, - write_date - ) - SELECT id, - product_id, - name, - quantity, - uom_id, - automatic_price, - specific_price, - discount, - recurring_rule_type, - recurring_invoicing_type, - recurring_interval, - sequence, - contract_id, - date_start, - date_end, - recurring_next_date, - last_date_invoiced, - termination_notice_date, - successor_contract_line_id, - predecessor_contract_line_id, - manual_renew_needed, - active, - create_uid, - create_date, - write_uid, - write_date - FROM account_analytic_invoice_line - """ + "UPDATE mail_message SET model='contract.contract'" + + "WHERE model='account.analytic.account' " + + "AND res_id IN ( SELECT DISTINCT " + + contract_field_name + + " FROM account_analytic_invoice_line)", ) - openupgrade.rename_models(cr, [('account.analytic.invoice.line', - 'contract.line')]) + # Move contract followers openupgrade.logged_query( cr, - """ - DROP TABLE account_analytic_invoice_line - """ + "UPDATE mail_followers SET res_model='contract.contract'" + + "WHERE res_model='account.analytic.account' " + + "AND res_id IN ( SELECT DISTINCT " + + contract_field_name + + " FROM account_analytic_invoice_line)", ) + + +def _copy_contract_line_table(cr): + contract_line_fields = [] + contract_field_name = _get_contract_field_name(cr) + for field in CONTRACT_LINE_FIELDS: + if openupgrade.column_exists( + cr, 'account_analytic_invoice_line', field + ): + contract_line_fields.append(field) + account_analytic_invoice_line_fields = contract_line_fields.copy() + contract_line_fields.append('contract_id') + account_analytic_invoice_line_fields.append(contract_field_name) + contract_line_fields.append('analytic_account_id') + account_analytic_invoice_line_fields.append(contract_field_name) + contract_line_fields.append('active') + account_analytic_invoice_line_fields.append('true') + openupgrade.logged_query( cr, - """ - UPDATE account_invoice_line - SET contract_line_id = contract_line_id_tmp - """ + "INSERT INTO contract_line (" + + ', '.join(contract_line_fields) + + ") " + + "SELECT " + + ', '.join(account_analytic_invoice_line_fields) + + " FROM account_analytic_invoice_line ", ) + openupgrade.logged_query( cr, """ - ALTER TABLE account_invoice_line - DROP COLUMN contract_line_id_tmp - """ + UPDATE account_invoice_line + SET contract_line_id = contract_line_id_tmp + """, + ) + + +def _update_no_update_ir_cron(env): + # Update ir.cron + env.ref('contract.contract_cron_for_invoice').model_id = env.ref( + 'contract.model_contract_contract' + ) + env.ref('contract.contract_line_cron_for_renew').model_id = env.ref( + 'contract.model_contract_line' + ) + env.ref('contract.email_contract_template').model_id = env.ref( + 'contract.model_contract_contract' ) + + +def _init_last_date_invoiced_on_contract_lines(env): + _logger.info("init last_date_invoiced field for contract lines") + contract_lines = env["contract.line"].search( + [("recurring_next_date", "!=", False)] + ) + contract_lines._init_last_date_invoiced() + + +def _init_invoicing_partner_id_on_contracts(env): + _logger.info("Populate invoicing partner field on contracts") + contracts = env["contract.contract"].search([]) + contracts._inverse_partner_id() + + +@openupgrade.migrate() +def migrate(env, version): + cr = env.cr + + _copy_contract_table(cr) + _copy_contract_line_table(cr) + _update_no_update_ir_cron(env) + + if parse_version(version) < parse_version('12.0.2.0.0'): + # We check the version here as this post-migration script was in + # 12.0.2.0.0 and already done for those who used the module when + # it was a PR + _init_last_date_invoiced_on_contract_lines(env) + _init_invoicing_partner_id_on_contracts(env) diff --git a/contract/migrations/12.0.4.0.0/pre-migration.py b/contract/migrations/12.0.4.0.0/pre-migration.py index 2c00d700..01fcbe32 100644 --- a/contract/migrations/12.0.4.0.0/pre-migration.py +++ b/contract/migrations/12.0.4.0.0/pre-migration.py @@ -12,20 +12,17 @@ _logger = logging.getLogger(__name__) def migrate(env, version): _logger.info(">> Pre-Migration 12.0.4.0.0") cr = env.cr - openupgrade.logged_query( - cr, - """ - DROP TABLE IF EXISTS account_analytic_invoice_line_wizard - """ - ) + 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'), + ( + 'account.abstract.analytic.contract.line', + 'contract.abstract.contract.line', + ), # Contract Line ('account.analytic.invoice.line', 'contract.line'), # Contract Template @@ -40,20 +37,34 @@ def migrate(env, version): ('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'), + ( + '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) @@ -65,18 +76,38 @@ def migrate(env, version): """ ALTER TABLE account_invoice_line ADD COLUMN contract_line_id_tmp INTEGER - """ - ) - 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 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 + """, + ) diff --git a/contract/models/account_invoice.py b/contract/models/account_invoice.py index fa2208f7..f7608020 100644 --- a/contract/models/account_invoice.py +++ b/contract/models/account_invoice.py @@ -8,6 +8,4 @@ class AccountInvoice(models.Model): _inherit = 'account.invoice' # We keep this field for migration purpose - old_contract_id = fields.Many2one( - 'contract.contract', oldname="contract_id" - ) + old_contract_id = fields.Many2one('contract.contract') diff --git a/contract/models/contract.py b/contract/models/contract.py index 7aab3e05..906af061 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -13,7 +13,12 @@ from odoo.tools.translate import _ class ContractContract(models.Model): _name = 'contract.contract' - _inherit = ['mail.thread', 'contract.abstract.contract'] + _description = "Contract" + _inherit = [ + 'mail.thread', + 'mail.activity.mixin', + 'contract.abstract.contract', + ] active = fields.Boolean( default=True, @@ -26,10 +31,6 @@ class ContractContract(models.Model): comodel_name='account.analytic.account', ondelete='restrict', ) - analytic_account_id = fields.Many2one( - string="Analytic account", - comodel_name='account.analytic.account', - ) currency_id = fields.Many2one( related="company_id.currency_id", string="Currency", diff --git a/contract/models/contract_line.py b/contract/models/contract_line.py index ee016688..61f78a92 100644 --- a/contract/models/contract_line.py +++ b/contract/models/contract_line.py @@ -13,6 +13,7 @@ from .contract_line_constraints import get_allowed class ContractLine(models.Model): _name = 'contract.line' + _description = "Contract Line" _inherit = 'contract.abstract.contract.line' sequence = fields.Integer( @@ -26,6 +27,10 @@ class ContractLine(models.Model): ondelete='cascade', oldname='analytic_account_id', ) + analytic_account_id = fields.Many2one( + string="Analytic account", + comodel_name='account.analytic.account', + ) date_start = fields.Date( string='Date Start', required=True, @@ -518,7 +523,7 @@ class ContractLine(models.Model): invoice_line_vals.update( { 'name': name, - 'account_analytic_id': self.contract_id.analytic_account_id.id, + 'account_analytic_id': self.analytic_account_id.id, 'price_unit': self.price_unit, } ) @@ -597,6 +602,9 @@ class ContractLine(models.Model): def _init_last_date_invoiced(self): """Used to init last_date_invoiced for migration purpose""" for rec in self: + last_date_invoiced = rec.recurring_next_date - relativedelta( + days=1 + ) if rec.recurring_rule_type == 'monthlylastday': last_date_invoiced = ( rec.recurring_next_date diff --git a/contract/readme/CONTRIBUTORS.rst b/contract/readme/CONTRIBUTORS.rst index 4a73f3d0..054e1a8c 100644 --- a/contract/readme/CONTRIBUTORS.rst +++ b/contract/readme/CONTRIBUTORS.rst @@ -6,4 +6,5 @@ * Miquel Raïch * Souheil Bejaoui * Thomas Binsfeld - +* Rafael Blasco +* Guillaume Vandamme diff --git a/contract/views/account_invoice_view.xml b/contract/views/account_invoice_view.xml deleted file mode 100644 index e69de29b..00000000 diff --git a/contract/views/contract.xml b/contract/views/contract.xml index 5c590866..ee810e87 100644 --- a/contract/views/contract.xml +++ b/contract/views/contract.xml @@ -105,8 +105,8 @@
- + +
diff --git a/contract/views/contract_line.xml b/contract/views/contract_line.xml index 08abea47..2d14a81f 100644 --- a/contract/views/contract_line.xml +++ b/contract/views/contract_line.xml @@ -35,6 +35,11 @@ + + + + + @@ -79,6 +84,7 @@ +