<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>, <NAME>
#    Copyright (C) 2014 Camptocamp SA
#              (C) 2014 Therp BV
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from openerp.openupgrade import openupgrade, openupgrade_80
from openerp import pooler, SUPERUSER_ID as uid

logger = logging.getLogger('OpenUpgrade.purchase')


def create_workitem_picking(cr, uid, pool):
    """
    Recover workitems missing in migration.
    :param cr: Database cursor
    :param uid: User Id
    :param pool: Database pool
    """

    pool = pooler.get_pool(cr.dbname)
    pol_obj = pool['purchase.order.line']

    # Activity
    cr.execute(
        """SELECT id FROM wkf_activity WHERE name = %s AND action = %s""",
        ('picking', 'action_picking_create()'))
    res = cr.fetchone()
    wkf_activity = res and res[0]

    # Get the not shipped moves linked to any Purchase Order
    cr.execute(
        """SELECT purchase_line_id
        FROM stock_move
        WHERE purchase_line_id is not null
        AND state != %s""",
        ('done',))
    for res in cr.fetchall():
        pol = pol_obj.browse(cr, uid, res[0])

        # Instance
        cr.execute(
            """SELECT id
            FROM wkf_instance
            WHERE res_id = %s AND res_type = %s""",
            (pol.order_id.id, 'purchase.order'))
        res3 = cr.fetchone()
        wkf_instance = res3 and res3[0]

        # Create workItem
        cr.execute(
            """SELECT id
            FROM wkf_workitem
            WHERE act_id = %s AND inst_id = %s""" % (
                wkf_activity, wkf_instance))
        res2 = cr.fetchone()
        wkf_workitem = res2 and res2[0]
        if not wkf_workitem:
            cr.execute(
                """INSERT INTO wkf_workitem (act_id, inst_id, state)
                VALUES (%s, %s, %s)""",
                (wkf_activity, wkf_instance, 'complete',))


def migrate_product_supply_method(cr):
    """
    Procurements of products: change the supply_method for the matching route
    Buy -> Buy Rule
    :param cr: Database cursor
    """
    pool = pooler.get_pool(cr.dbname)
    template_obj = pool['product.template']

    mto_route_id = pool['ir.model.data'].get_object_reference(
        cr, uid, 'purchase', 'route_warehouse0_buy')[1]

    procure_method_legacy = openupgrade.get_legacy_name('supply_method')
    if mto_route_id:
        product_ids = []
        cr.execute("""SELECT id FROM product_template WHERE %s = %%s""" % (
            procure_method_legacy), ('buy',))
        product_ids = [res[0] for res in cr.fetchall()]

        template_obj.write(
            cr, uid, product_ids, {'route_ids': [(4, mto_route_id)]})


def migrate_procurement_order(cr):
    """
    On procurements, purchase_id is replaced by purchase_line_id. We should be
    able to match most purchase lines because they got the procurement's
    move_id as their move_dest_id. Fallback on product_id, for presumably the
    manually created procurements without a related move from a sale or
    production order.

    In Odoo 8.0, stock moves generated for the procurement (moves from the
    supplier or production location to stock) are also recorded on the
    procurement. For purchase procurements, gather them here.
    """
    openupgrade.logged_query(
        cr,
        """
        UPDATE procurement_order proc
        SET purchase_line_id = pol.id
        FROM purchase_order_line pol
        WHERE proc.{purchase_id} = pol.order_id
             AND pol.{move_dest_id} IS NOT NULL
             AND pol.{move_dest_id} = proc.{move_id}
        """.format(
            purchase_id=openupgrade.get_legacy_name('purchase_id'),
            move_dest_id=openupgrade.get_legacy_name('move_dest_id'),
            move_id=openupgrade.get_legacy_name('move_id')))

    openupgrade.logged_query(
        cr,
        """
        UPDATE procurement_order proc
        SET purchase_line_id = pol.id
        FROM purchase_order_line pol
        WHERE proc.{purchase_id} = pol.order_id
             AND proc.purchase_line_id IS NULL
             AND pol.product_id = proc.product_id
             AND pol.id NOT IN (
                 SELECT purchase_line_id
                 FROM procurement_order
                 WHERE purchase_line_id IS NOT NULL)
        """.format(
            purchase_id=openupgrade.get_legacy_name('purchase_id')))

    # Warn about dangling procurements
    cr.execute(
        """<commit_msg>[RFR] Keeping in sync with changes to stock migration in other PR<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>, <NAME>
#    Copyright (C) 2014 Camptocamp SA
#              (C) 2014 Therp BV
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from openerp.openupgrade import openupgrade, openupgrade_80
from openerp import pooler, SUPERUSER_ID as uid

logger = logging.getLogger('OpenUpgrade.purchase')


def create_workitem_picking(cr, uid, pool):
    """
    Recover workitems missing in migration.
    :param cr: Database cursor
    :param uid: User Id
    :param pool: Database pool
    """

    pool = pooler.get_pool(cr.dbname)
    pol_obj = pool['purchase.order.line']

    # Activity
    cr.execute(
        """SELECT id FROM wkf_activity WHERE name = %s AND action = %s""",
        ('picking', 'action_picking_create()'))
    res = cr.fetchone()
    wkf_activity = res and res[0]

    # Get the not shipped moves linked to any Purchase Order
    cr.execute(
        """SELECT purchase_line_id
        FROM stock_move
        WHERE purchase_line_id is not null
        AND state != %s""",
        ('done',))
    for res in cr.fetchall():
        pol = pol_obj.browse(cr, uid, res[0])

        # Instance
        cr.execute(
            """SELECT id
            FROM wkf_instance
            WHERE res_id = %s AND res_type = %s""",
            (pol.order_id.id, 'purchase.order'))
        res3 = cr.fetchone()
        wkf_instance = res3 and res3[0]

        # Create workItem
        cr.execute(
            """SELECT id
            FROM wkf_workitem
            WHERE act_id = %s AND inst_id = %s""" % (
                wkf_activity, wkf_instance))
        res2 = cr.fetchone()
        wkf_workitem = res2 and res2[0]
        if not wkf_workitem:
            cr.execute(
                """INSERT INTO wkf_workitem (act_id, inst_id, state)
                VALUES (%s, %s, %s)""",
                (wkf_activity, wkf_instance, 'complete',))


def migrate_product_supply_method(cr):
    """
    Procurements of products: change the supply_method for the matching route
    Buy -> Buy Rule
    :param cr: Database cursor
    """
    pool = pooler.get_pool(cr.dbname)
    template_obj = pool['product.template']

    mto_route_id = pool['ir.model.data'].get_object_reference(
        cr, uid, 'purchase', 'route_warehouse0_buy')[1]

    procure_method_legacy = openupgrade.get_legacy_name('supply_method')
    if mto_route_id:
        product_ids = []
        cr.execute("""SELECT id FROM product_template WHERE %s = %%s""" % (
            procure_method_legacy), ('buy',))
        product_ids = [res[0] for res in cr.fetchall()]

        template_obj.write(
            cr, uid, product_ids, {'route_ids': [(4, mto_route_id)]})


def migrate_procurement_order(cr):
    """
    On procurements, purchase_id is replaced by purchase_line_id. We should be
    able to match most purchase lines because they got the procurement's
    move_id as their move_dest_id. Fallback on product_id, for presumably the
    manually created procurements without a related move from a sale or
    production order.

    In Odoo 8.0, stock moves generated for the procurement (moves from the
    supplier or production location to stock) are also recorded on the
    procurement. For purchase procurements, gather them here.

    Note that procurement_order.move_dest_id was proc.move_id in 7.0, renamed
    to legacy in the 8.0 procurement migration script and then renamed again
    to move_dest_id in the stock migration script.
    """
    openupgrade.logged_query(
        cr,
        """
        UPDATE procurement_order proc
        SET purchase_line_id = pol.id
        FROM purchase_order_line pol
        WHERE proc.{purchase_id} = pol.order_id
             AND pol.{move_dest_id} IS NOT NULL
             AND pol.{move_dest_id} = proc.move_dest_id
        """.format(
            purchase_id=openupgrade.get_legacy_name('purchase_id'),
            move_dest_id=openupgrade.get_legacy_name('move_dest_id')))

    openupgrade.logged_query(
        cr,
        """
        UPDATE procurement_order proc
        SET purchase_line_id = pol.id
        FROM purchase_order_line pol
        WHERE proc.{purchase_id} = pol.order_id
             AND proc.purchase_line_id IS NULL
             AND pol.product_id = proc.product_id
             AND pol.id NOT IN (
                 SELECT purchase_line_id
                 FROM procurement_order
                 WHERE purchase_line_id IS NOT NULL)
        """.format(
            purchase_id=openupgrade.get_legacy_name('purchase_id')))

    # Warn about dangling procurements
    cr.execute(
        """
{'fraction_non_alphanumeric': 0.11478925773964939, 'fraction_numerical': 0.005594927265945543, 'mean_word_length': 2.5595751742449386, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '7145261', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3060, 'n_words': 971}