Browse Source

[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
pull/207/head
sbejaoui 5 years ago
parent
commit
d93139d147
  1. 30
      contract/migrations/12.0.2.0.0/post-migration.py
  2. 92
      contract/migrations/12.0.2.0.0/pre-migration.py
  3. 16
      contract/migrations/12.0.3.0.0/post-migration.py
  4. 304
      contract/migrations/12.0.4.0.0/post-migration.py
  5. 79
      contract/migrations/12.0.4.0.0/pre-migration.py
  6. 4
      contract/models/account_invoice.py
  7. 11
      contract/models/contract.py
  8. 10
      contract/models/contract_line.py
  9. 3
      contract/readme/CONTRIBUTORS.rst
  10. 0
      contract/views/account_invoice_view.xml
  11. 4
      contract/views/contract.xml
  12. 6
      contract/views/contract_line.xml

30
contract/migrations/12.0.2.0.0/post-migration.py

@ -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()

92
contract/migrations/12.0.2.0.0/pre-migration.py

@ -3,22 +3,92 @@
import logging import logging
from odoo import SUPERUSER_ID, api
from openupgradelib import openupgrade
_logger = logging.getLogger(__name__) _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,
""" """
set recurring_next_date to false for finished contract
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,
""" """
_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
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
""",
) )
cr.execute(
"UPDATE account_analytic_account set recurring_next_date=null "
"where id in (%)" % ','.join(finished_contract.ids)
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")
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)

16
contract/migrations/12.0.3.0.0/post-migration.py

@ -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()

304
contract/migrations/12.0.4.0.0/post-migration.py

@ -4,149 +4,207 @@
import logging import logging
from openupgradelib import openupgrade from openupgradelib import openupgrade
from odoo.tools import parse_version
_logger = logging.getLogger(__name__) _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
openupgrade.logged_query(
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):
""" """
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
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'
) )
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
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 ("
+ ', '.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( openupgrade.logged_query(
cr, 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
UPDATE account_invoice
SET old_contract_id = old_contract_id_tmp
""",
) )
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
"""
# 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)",
) )
openupgrade.rename_models(cr, [('account.analytic.invoice.line',
'contract.line')])
# Move contract messages
openupgrade.logged_query( openupgrade.logged_query(
cr, cr,
"""
DROP TABLE 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)",
) )
# Move contract followers
openupgrade.logged_query( openupgrade.logged_query(
cr, cr,
"""
UPDATE account_invoice_line
SET contract_line_id = contract_line_id_tmp
"""
"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,
"INSERT INTO contract_line ("
+ ', '.join(contract_line_fields)
+ ") "
+ "SELECT "
+ ', '.join(account_analytic_invoice_line_fields)
+ " FROM account_analytic_invoice_line ",
)
openupgrade.logged_query( openupgrade.logged_query(
cr, 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)

79
contract/migrations/12.0.4.0.0/pre-migration.py

@ -12,20 +12,17 @@ _logger = logging.getLogger(__name__)
def migrate(env, version): def migrate(env, version):
_logger.info(">> Pre-Migration 12.0.4.0.0") _logger.info(">> Pre-Migration 12.0.4.0.0")
cr = env.cr cr = env.cr
openupgrade.logged_query(
cr,
"""
DROP TABLE IF EXISTS account_analytic_invoice_line_wizard
"""
)
models_to_rename = [ models_to_rename = [
# Contract Line Wizard # Contract Line Wizard
('account.analytic.invoice.line.wizard', 'contract.line.wizard'), ('account.analytic.invoice.line.wizard', 'contract.line.wizard'),
# Abstract Contract # Abstract Contract
('account.abstract.analytic.contract', 'contract.abstract.contract'), ('account.abstract.analytic.contract', 'contract.abstract.contract'),
# Abstract Contract Line # Abstract Contract Line
('account.abstract.analytic.contract.line',
'contract.abstract.contract.line'),
(
'account.abstract.analytic.contract.line',
'contract.abstract.contract.line',
),
# Contract Line # Contract Line
('account.analytic.invoice.line', 'contract.line'), ('account.analytic.invoice.line', 'contract.line'),
# Contract Template # Contract Template
@ -40,20 +37,34 @@ def migrate(env, version):
('account_analytic_contract_line', 'contract_template_line'), ('account_analytic_contract_line', 'contract_template_line'),
] ]
xmlids_to_rename = [ 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_models(cr, models_to_rename)
openupgrade.rename_tables(cr, tables_to_rename) openupgrade.rename_tables(cr, tables_to_rename)
@ -65,18 +76,38 @@ def migrate(env, version):
""" """
ALTER TABLE account_invoice_line ALTER TABLE account_invoice_line
ADD COLUMN contract_line_id_tmp INTEGER ADD COLUMN contract_line_id_tmp INTEGER
"""
""",
) )
if openupgrade.column_exists(
cr, 'account_invoice_line', 'contract_line_id'
):
openupgrade.logged_query( openupgrade.logged_query(
cr, cr,
""" """
UPDATE account_invoice_line UPDATE account_invoice_line
SET contract_line_id_tmp = contract_line_id SET contract_line_id_tmp = contract_line_id
"""
""",
) )
openupgrade.logged_query( openupgrade.logged_query(
cr, cr,
""" """
UPDATE account_invoice_line SET contract_line_id = NULL 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
""",
) )

4
contract/models/account_invoice.py

@ -8,6 +8,4 @@ class AccountInvoice(models.Model):
_inherit = 'account.invoice' _inherit = 'account.invoice'
# We keep this field for migration purpose # We keep this field for migration purpose
old_contract_id = fields.Many2one(
'contract.contract', oldname="contract_id"
)
old_contract_id = fields.Many2one('contract.contract')

11
contract/models/contract.py

@ -13,7 +13,12 @@ from odoo.tools.translate import _
class ContractContract(models.Model): class ContractContract(models.Model):
_name = 'contract.contract' _name = 'contract.contract'
_inherit = ['mail.thread', 'contract.abstract.contract']
_description = "Contract"
_inherit = [
'mail.thread',
'mail.activity.mixin',
'contract.abstract.contract',
]
active = fields.Boolean( active = fields.Boolean(
default=True, default=True,
@ -26,10 +31,6 @@ class ContractContract(models.Model):
comodel_name='account.analytic.account', comodel_name='account.analytic.account',
ondelete='restrict', ondelete='restrict',
) )
analytic_account_id = fields.Many2one(
string="Analytic account",
comodel_name='account.analytic.account',
)
currency_id = fields.Many2one( currency_id = fields.Many2one(
related="company_id.currency_id", related="company_id.currency_id",
string="Currency", string="Currency",

10
contract/models/contract_line.py

@ -13,6 +13,7 @@ from .contract_line_constraints import get_allowed
class ContractLine(models.Model): class ContractLine(models.Model):
_name = 'contract.line' _name = 'contract.line'
_description = "Contract Line"
_inherit = 'contract.abstract.contract.line' _inherit = 'contract.abstract.contract.line'
sequence = fields.Integer( sequence = fields.Integer(
@ -26,6 +27,10 @@ class ContractLine(models.Model):
ondelete='cascade', ondelete='cascade',
oldname='analytic_account_id', oldname='analytic_account_id',
) )
analytic_account_id = fields.Many2one(
string="Analytic account",
comodel_name='account.analytic.account',
)
date_start = fields.Date( date_start = fields.Date(
string='Date Start', string='Date Start',
required=True, required=True,
@ -518,7 +523,7 @@ class ContractLine(models.Model):
invoice_line_vals.update( invoice_line_vals.update(
{ {
'name': name, '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, 'price_unit': self.price_unit,
} }
) )
@ -597,6 +602,9 @@ class ContractLine(models.Model):
def _init_last_date_invoiced(self): def _init_last_date_invoiced(self):
"""Used to init last_date_invoiced for migration purpose""" """Used to init last_date_invoiced for migration purpose"""
for rec in self: for rec in self:
last_date_invoiced = rec.recurring_next_date - relativedelta(
days=1
)
if rec.recurring_rule_type == 'monthlylastday': if rec.recurring_rule_type == 'monthlylastday':
last_date_invoiced = ( last_date_invoiced = (
rec.recurring_next_date rec.recurring_next_date

3
contract/readme/CONTRIBUTORS.rst

@ -6,4 +6,5 @@
* Miquel Raïch <miquel.raich@eficent.com> * Miquel Raïch <miquel.raich@eficent.com>
* Souheil Bejaoui <souheil.bejaoui@acsone.eu> * Souheil Bejaoui <souheil.bejaoui@acsone.eu>
* Thomas Binsfeld <thomas.binsfeld@acsone.eu> * Thomas Binsfeld <thomas.binsfeld@acsone.eu>
* Rafael Blasco <rafael.blasco@tecnativa.com>
* Guillaume Vandamme <guillaume.vandamme@acsone.eu>

0
contract/views/account_invoice_view.xml

4
contract/views/contract.xml

@ -105,8 +105,8 @@
</notebook> </notebook>
</sheet> </sheet>
<div class="oe_chatter"> <div class="oe_chatter">
<field name="message_follower_ids"
widget="mail_followers"/>
<field name="message_follower_ids" widget="mail_followers"/>
<field name="activity_ids" widget="mail_activity"/>
<field name="message_ids" widget="mail_thread"/> <field name="message_ids" widget="mail_thread"/>
</div> </div>
</form> </form>

6
contract/views/contract_line.xml

@ -35,6 +35,11 @@
<field name="successor_contract_line_id"/> <field name="successor_contract_line_id"/>
</group> </group>
</group> </group>
<group name="recurrence_info" position="after">
<group name="analytic" groups="analytic.group_analytic_accounting">
<field name="analytic_account_id"/>
</group>
</group>
</field> </field>
</record> </record>
@ -79,6 +84,7 @@
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle"/>
<field name="product_id"/> <field name="product_id"/>
<field name="name"/> <field name="name"/>
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
<field name="quantity"/> <field name="quantity"/>
<field name="uom_id"/> <field name="uom_id"/>
<field name="automatic_price"/> <field name="automatic_price"/>

Loading…
Cancel
Save