mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[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.
This commit is contained in:
@@ -8,164 +8,6 @@ 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',
|
|
||||||
]
|
|
||||||
|
|
||||||
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 ("
|
|
||||||
+ ', '.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,
|
|
||||||
"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(
|
|
||||||
cr,
|
|
||||||
"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(
|
|
||||||
cr,
|
|
||||||
"""
|
|
||||||
UPDATE account_invoice_line
|
|
||||||
SET contract_line_id = contract_line_id_tmp
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _update_no_update_ir_cron(env):
|
def _update_no_update_ir_cron(env):
|
||||||
# Update ir.cron
|
# Update ir.cron
|
||||||
@@ -196,12 +38,7 @@ def _init_invoicing_partner_id_on_contracts(env):
|
|||||||
|
|
||||||
@openupgrade.migrate()
|
@openupgrade.migrate()
|
||||||
def migrate(env, version):
|
def migrate(env, version):
|
||||||
cr = env.cr
|
|
||||||
|
|
||||||
_copy_contract_table(cr)
|
|
||||||
_copy_contract_line_table(cr)
|
|
||||||
_update_no_update_ir_cron(env)
|
_update_no_update_ir_cron(env)
|
||||||
|
|
||||||
if parse_version(version) < parse_version('12.0.2.0.0'):
|
if parse_version(version) < parse_version('12.0.2.0.0'):
|
||||||
# We check the version here as this post-migration script was in
|
# 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
|
# 12.0.2.0.0 and already done for those who used the module when
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
# Copyright 2019 ACSONE SA/NV
|
# Copyright 2019 ACSONE SA/NV
|
||||||
|
# Copyright 2019 Tecnativa 2019 - Pedro M. Baeza
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from openupgradelib import openupgrade
|
from openupgradelib import openupgrade
|
||||||
|
from psycopg2 import sql
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
models_to_rename = [
|
||||||
@openupgrade.migrate()
|
|
||||||
def migrate(env, version):
|
|
||||||
_logger.info(">> Pre-Migration 12.0.4.0.0")
|
|
||||||
cr = env.cr
|
|
||||||
|
|
||||||
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
|
||||||
@@ -29,14 +25,21 @@ def migrate(env, version):
|
|||||||
('account.analytic.contract', 'contract.template'),
|
('account.analytic.contract', 'contract.template'),
|
||||||
# Contract Template Line
|
# Contract Template Line
|
||||||
('account.analytic.contract.line', 'contract.template.line'),
|
('account.analytic.contract.line', 'contract.template.line'),
|
||||||
]
|
]
|
||||||
tables_to_rename = [
|
tables_to_rename = [
|
||||||
|
# Contract Line
|
||||||
|
('account_analytic_invoice_line', 'contract_line'),
|
||||||
# Contract Template
|
# Contract Template
|
||||||
('account_analytic_contract', 'contract_template'),
|
('account_analytic_contract', 'contract_template'),
|
||||||
# Contract Template Line
|
# Contract Template Line
|
||||||
('account_analytic_contract_line', 'contract_template_line'),
|
('account_analytic_contract_line', 'contract_template_line'),
|
||||||
]
|
]
|
||||||
xmlids_to_rename = [
|
columns_to_copy = {
|
||||||
|
'contract_line': [
|
||||||
|
('analytic_account_id', 'contract_id', None),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
xmlids_to_rename = [
|
||||||
(
|
(
|
||||||
'contract.account_analytic_cron_for_invoice',
|
'contract.account_analytic_cron_for_invoice',
|
||||||
'contract.contract_cron_for_invoice',
|
'contract.contract_cron_for_invoice',
|
||||||
@@ -65,49 +68,46 @@ def migrate(env, version):
|
|||||||
'contract.account_analytic_contract_line_user',
|
'contract.account_analytic_contract_line_user',
|
||||||
'contract.contract_template_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):
|
||||||
|
cr = env.cr
|
||||||
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)
|
||||||
openupgrade.rename_xmlids(cr, xmlids_to_rename)
|
openupgrade.rename_xmlids(cr, xmlids_to_rename)
|
||||||
# A temporary column is needed to avoid breaking the foreign key constraint
|
openupgrade.copy_columns(cr, columns_to_copy)
|
||||||
# The temporary column is dropped in the post-migration script
|
create_contract_records(cr)
|
||||||
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
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ class ContractAbstractContractLine(models.AbstractModel):
|
|||||||
comodel_name='contract.abstract.contract',
|
comodel_name='contract.abstract.contract',
|
||||||
required=True,
|
required=True,
|
||||||
ondelete='cascade',
|
ondelete='cascade',
|
||||||
oldname='analytic_account_id',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@api.depends(
|
@api.depends(
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from odoo.tools.translate import _
|
|||||||
class ContractContract(models.Model):
|
class ContractContract(models.Model):
|
||||||
_name = 'contract.contract'
|
_name = 'contract.contract'
|
||||||
_description = "Contract"
|
_description = "Contract"
|
||||||
|
_order = 'code, name asc'
|
||||||
_inherit = [
|
_inherit = [
|
||||||
'mail.thread',
|
'mail.thread',
|
||||||
'mail.activity.mixin',
|
'mail.activity.mixin',
|
||||||
@@ -44,7 +45,6 @@ class ContractContract(models.Model):
|
|||||||
comodel_name='contract.line',
|
comodel_name='contract.line',
|
||||||
inverse_name='contract_id',
|
inverse_name='contract_id',
|
||||||
copy=True,
|
copy=True,
|
||||||
oldnae='contract_line_ids',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
user_id = fields.Many2one(
|
user_id = fields.Many2one(
|
||||||
@@ -273,7 +273,7 @@ class ContractContract(models.Model):
|
|||||||
if self.contract_type == 'purchase':
|
if self.contract_type == 'purchase':
|
||||||
invoice_type = 'in_invoice'
|
invoice_type = 'in_invoice'
|
||||||
return {
|
return {
|
||||||
'reference': self.code,
|
'name': self.code,
|
||||||
'type': invoice_type,
|
'type': invoice_type,
|
||||||
'partner_id': self.invoice_partner_id.id,
|
'partner_id': self.invoice_partner_id.id,
|
||||||
'currency_id': currency.id,
|
'currency_id': currency.id,
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ class ContractLine(models.Model):
|
|||||||
required=True,
|
required=True,
|
||||||
index=True,
|
index=True,
|
||||||
ondelete='cascade',
|
ondelete='cascade',
|
||||||
oldname='analytic_account_id',
|
|
||||||
)
|
)
|
||||||
analytic_account_id = fields.Many2one(
|
analytic_account_id = fields.Many2one(
|
||||||
string="Analytic account",
|
string="Analytic account",
|
||||||
|
|||||||
@@ -19,5 +19,4 @@ class ContractTemplate(models.Model):
|
|||||||
inverse_name='contract_id',
|
inverse_name='contract_id',
|
||||||
copy=True,
|
copy=True,
|
||||||
string='Contract template lines',
|
string='Contract template lines',
|
||||||
oldname='contract_line_ids',
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ class ResPartner(models.Model):
|
|||||||
string='Purchase Contracts',
|
string='Purchase Contracts',
|
||||||
compute='_compute_contract_count',
|
compute='_compute_contract_count',
|
||||||
)
|
)
|
||||||
|
contract_ids = fields.One2many(
|
||||||
|
comodel_name='contract.contract',
|
||||||
|
inverse='partner_id',
|
||||||
|
string="Contracts",
|
||||||
|
)
|
||||||
|
|
||||||
def _compute_contract_count(self):
|
def _compute_contract_count(self):
|
||||||
contract_model = self.env['contract.contract']
|
contract_model = self.env['contract.contract']
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
||||||
<odoo>
|
<odoo>
|
||||||
|
|
||||||
<record id="view_partner_form" model="ir.ui.view">
|
<record id="view_partner_form" model="ir.ui.view">
|
||||||
<field name="inherit_id" ref="base.view_partner_form" />
|
<field name="inherit_id" ref="base.view_partner_form" />
|
||||||
<field name="model">res.partner</field>
|
<field name="model">res.partner</field>
|
||||||
<field type="xml" name="arch">
|
<field type="xml" name="arch">
|
||||||
@@ -22,6 +22,21 @@
|
|||||||
</button>
|
</button>
|
||||||
</xpath>
|
</xpath>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
<record id="view_res_partner_filter" model="ir.ui.view">
|
||||||
|
<field name="inherit_id" ref="base.view_res_partner_filter" />
|
||||||
|
<field name="model">res.partner</field>
|
||||||
|
<field type="xml" name="arch">
|
||||||
|
<filter name="inactive" position="after">
|
||||||
|
<separator/>
|
||||||
|
<filter
|
||||||
|
name="filter_running_contract"
|
||||||
|
string="With running contracts"
|
||||||
|
domain="['|', ('contract_ids.date_end', '>=', context_today().strftime('%Y-%m-%d')), ('contract_ids.date_end', '=', False)]"
|
||||||
|
/>
|
||||||
|
</filter>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|||||||
Reference in New Issue
Block a user