<commit_before># Copyright 2019 <NAME>/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()


@openupgrade.migrate()
def migrate(env, version):
    _update_no_update_ir_cron(env)
    if parse_version(version) < parse_version('172.16.31.10.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)<commit_msg>[MIG] contract: Assign old partner's salesman to migrated contracts<commit_after># Copyright 2019 <NAME>/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)
{'fraction_non_alphanumeric': 0.09581646423751687, 'fraction_numerical': 0.011336032388663968, 'mean_word_length': 3.8827404479578393, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3218862', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1203, 'n_words': 254}