Administrator
5 years ago
21 changed files with 453 additions and 588 deletions
-
4contract/__manifest__.py
-
94contract/migrations/12.0.2.0.0/pre-migration.py
-
63contract/migrations/12.0.4.0.0/post-migration.py
-
147contract/migrations/12.0.4.0.0/pre-migration.py
-
4contract/models/__init__.py
-
21contract/models/abstract_contract.py
-
106contract/models/abstract_contract_line.py
-
12contract/models/account_invoice.py
-
12contract/models/account_move.py
-
4contract/models/account_move_line.py
-
152contract/models/contract.py
-
334contract/models/contract_line.py
-
1contract/models/contract_template_line.py
-
4contract/tests/test_contract.py
-
6contract/views/abstract_contract_line.xml
-
28contract/views/account_move.xml
-
28contract/views/contract.xml
-
10contract/views/contract_line.xml
-
1contract/views/contract_template.xml
-
6contract/views/res_partner_view.xml
-
4contract/wizards/contract_line_wizard.py
@ -1,94 +0,0 @@ |
|||
# Copyright 2018 ACSONE SA/NV |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
|
|||
import logging |
|||
|
|||
from openupgradelib import openupgrade |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
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") |
|||
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) |
@ -1,63 +0,0 @@ |
|||
# Copyright 2019 ACSONE SA/NV |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
|
|||
import logging |
|||
|
|||
from openupgradelib import openupgrade |
|||
from odoo.tools import parse_version |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
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() |
|||
|
|||
|
|||
def assign_salesman(env): |
|||
"""As v11 takes salesman from linked partner and now the salesman is a |
|||
field in the contract that is initialized to current user, we need |
|||
to assign to the recently converted contracts following old logic, or they |
|||
will have admin as responsible. |
|||
""" |
|||
openupgrade.logged_query( |
|||
env.cr, """ |
|||
UPDATE contract_contract cc |
|||
SET user_id = rp.user_id |
|||
FROM res_partner rp |
|||
WHERE rp.id = cc.partner_id""", |
|||
) |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
_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) |
|||
assign_salesman(env) |
@ -1,147 +0,0 @@ |
|||
# 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), |
|||
), |
|||
) |
|||
# Deactivate disabled contracts |
|||
openupgrade.logged_query( |
|||
cr, """UPDATE contract_contract cc |
|||
SET active = False |
|||
FROM account_analytic_account aaa |
|||
WHERE aaa.id = cc.id |
|||
AND NOT aaa.recurring_invoices""", |
|||
) |
|||
# Handle id sequence |
|||
cr.execute("CREATE SEQUENCE IF NOT EXISTS contract_contract_id_seq") |
|||
cr.execute("SELECT setval('contract_contract_id_seq', " |
|||
"(SELECT MAX(id) FROM contract_contract))") |
|||
cr.execute("ALTER TABLE contract_contract ALTER id " |
|||
"SET DEFAULT NEXTVAL('contract_contract_id_seq')") |
|||
# Move common stuff from one table to the other |
|||
mapping = [ |
|||
('ir_attachment', 'res_model', 'res_id'), |
|||
('mail_message', 'model', 'res_id'), |
|||
('mail_activity', 'res_model', 'res_id'), |
|||
('mail_followers', 'res_model', 'res_id'), |
|||
] |
|||
for table, model_column, id_column in mapping: |
|||
openupgrade.logged_query( |
|||
cr, sql.SQL(""" |
|||
UPDATE {table} SET {model_column}='contract.contract' |
|||
WHERE {model_column}='account.analytic.account' |
|||
AND {id_column} IN (SELECT DISTINCT {col} FROM contract_line) |
|||
""").format( |
|||
table=sql.Identifier(table), |
|||
model_column=sql.Identifier(model_column), |
|||
id_column=sql.Identifier(id_column), |
|||
col=sql.Identifier(contract_field_name), |
|||
), |
|||
) |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
cr = env.cr |
|||
openupgrade.rename_models(cr, models_to_rename) |
|||
openupgrade.rename_tables(cr, tables_to_rename) |
|||
openupgrade.rename_xmlids(cr, xmlids_to_rename) |
|||
openupgrade.copy_columns(cr, columns_to_copy) |
|||
create_contract_records(cr) |
@ -1,12 +0,0 @@ |
|||
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com> |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
|
|||
from odoo import fields, models |
|||
|
|||
|
|||
class AccountInvoice(models.Model): |
|||
_inherit = 'account.invoice' |
|||
|
|||
# We keep this field for migration purpose |
|||
old_contract_id = fields.Many2one( |
|||
'contract.contract', oldname='contract_id') |
@ -0,0 +1,12 @@ |
|||
# Copyright 2018 ACSONE SA/NV. |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
|
|||
from odoo import fields, models |
|||
|
|||
|
|||
class AccountMove(models.Model): |
|||
_inherit = 'account.move' |
|||
|
|||
contract_id = fields.Many2one('contract.contract', string='Generated by contract', index=True, readonly=True) |
|||
|
|||
|
@ -0,0 +1,28 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<!-- Copyright 2017 Carlos Dauden <carlos.dauden@tecnativa.com> |
|||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> |
|||
<odoo> |
|||
|
|||
<record id="contract_in_view_move_form" model="ir.ui.view"> |
|||
<field name="inherit_id" ref="account.view_move_form" /> |
|||
<field name="model">account.move</field> |
|||
<field name="groups_id" eval="[(4, ref('account.group_account_invoice'))]"/> |
|||
<field type="xml" name="arch"> |
|||
<field name="ref" position="after"> |
|||
<field name="contract_id" /> |
|||
</field> |
|||
</field> |
|||
</record> |
|||
|
|||
<record id="contract_view_account_invoice_filter" model="ir.ui.view"> |
|||
<field name="inherit_id" ref="account.view_account_invoice_filter" /> |
|||
<field name="model">account.move</field> |
|||
<field type="xml" name="arch"> |
|||
<filter name="duedate" position="after"> |
|||
<filter string="Contract" name="contract" context="{'group_by': 'contract_id'}"/> |
|||
</filter> |
|||
|
|||
</field> |
|||
</record> |
|||
|
|||
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue