<commit_before>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################

import datetime
from odoo import api, fields, models
from odoo.exceptions import UserError


class ProductReplenish(models.TransientModel):
    """
    This is a backport from odoo v12, replaces the "request procurement"
     wizard from v10, lost during the stock refactoing.
    TODO remove in v12
    """
    _name = 'product.replenish'
    _description = 'Product Replenish'

    product_id = fields.Many2one(
        'product.product', string='Product', required=True)
    product_tmpl_id = fields.Many2one(
        'product.template', String='Product Template', required=True)
    product_has_variants = fields.Boolean(
        'Has variants', default=False, required=True)
    product_uom_category_id = fields.Many2one(
        'product.uom.categ', related='product_id.uom_id.category_id',
        readonly=True, required=True)
    product_uom_id = fields.Many2one(
        'product.uom', string='Unity of measure', required=True)
    quantity = fields.Float('Quantity', default=1, required=True)
    date_planned = fields.Datetime(
        'Scheduled Date',
        help="Date at which the replenishment should take place.")
    warehouse_id = fields.Many2one(
        'stock.warehouse', string='Warehouse', required=True)
    route_ids = fields.Many2many(
        'stock.location.route', string='Preferred Routes',
        help="Apply specific route(s) for the replenishment"
        " instead of product's default routes.")

    @api.model
    def default_get(self, fields):
        res = super(ProductReplenish, self).default_get(fields)
        company_user = self.env.user.company_id
        warehouse = self.env['stock.warehouse'].search(
            [('company_id', '=', company_user.id)], limit=1)
        product_tmpl_id = False
        if 'product_id' in fields:
            if self.env.context.get('default_product_id'):
                product_id = self.env['product.product'].browse(
                    self.env.context['default_product_id'])
                product_tmpl_id = product_id.product_tmpl_id
                res['product_tmpl_id'] = product_id.product_tmpl_id.id
                res['product_id'] = product_id.id
            elif self.env.context.get('default_product_tmpl_id'):
                product_tmpl_id = self.env['product.template'].browse(
                    self.env.context['default_product_tmpl_id'])
                res['product_tmpl_id'] = product_tmpl_id.id
                res['product_id'] = product_tmpl_id.product_variant_id.id
                if len(product_tmpl_id.product_variant_ids) > 1:
                    res['product_has_variants'] = True
        if 'product_uom_id' in fields:
            res['product_uom_id'] = product_tmpl_id.uom_id.id
        if 'warehouse_id' in fields:
            res['warehouse_id'] = warehouse.id
        if 'date_planned' in fields:
            res['date_planned'] = datetime.datetime.now()
        return res

    def launch_replenishment(self):
        uom_reference = self.product_id.uom_id
        self.quantity = self.product_uom_id._compute_quantity(
            self.quantity, uom_reference)
        try:
            self.env['procurement.group'].run(
                self.product_id,
                self.quantity,
                uom_reference,
                self.warehouse_id.lot_stock_id,  # Location
                "Manual Replenishment",  # Name
                "Manual Replenishment",  # Origin
                self._prepare_run_values()  # Values
            )
        except UserError as error:
            raise UserError(error)

    def _prepare_run_values(self):
        replenishment = self.env['procurement.group'].create({
            'partner_id': self.product_id.responsible_id.partner_id.id,
        })

        values = {<commit_msg>[FIX] stock_ux: fix in the context when run a procurement with the replenish method<commit_after>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################

import datetime
from odoo import api, fields, models
from odoo.exceptions import UserError


class ProductReplenish(models.TransientModel):
    """
    This is a backport from odoo v12, replaces the "request procurement"
     wizard from v10, lost during the stock refactoing.
    TODO remove in v12
    """
    _name = 'product.replenish'
    _description = 'Product Replenish'

    product_id = fields.Many2one(
        'product.product', string='Product', required=True)
    product_tmpl_id = fields.Many2one(
        'product.template', String='Product Template', required=True)
    product_has_variants = fields.Boolean(
        'Has variants', default=False, required=True)
    product_uom_category_id = fields.Many2one(
        'product.uom.categ', related='product_id.uom_id.category_id',
        readonly=True, required=True)
    product_uom_id = fields.Many2one(
        'product.uom', string='Unity of measure', required=True)
    quantity = fields.Float('Quantity', default=1, required=True)
    date_planned = fields.Datetime(
        'Scheduled Date',
        help="Date at which the replenishment should take place.")
    warehouse_id = fields.Many2one(
        'stock.warehouse', string='Warehouse', required=True)
    route_ids = fields.Many2many(
        'stock.location.route', string='Preferred Routes',
        help="Apply specific route(s) for the replenishment"
        " instead of product's default routes.")

    @api.model
    def default_get(self, fields):
        res = super(ProductReplenish, self).default_get(fields)
        company_user = self.env.user.company_id
        warehouse = self.env['stock.warehouse'].search(
            [('company_id', '=', company_user.id)], limit=1)
        product_tmpl_id = False
        if 'product_id' in fields:
            if self.env.context.get('default_product_id'):
                product_id = self.env['product.product'].browse(
                    self.env.context['default_product_id'])
                product_tmpl_id = product_id.product_tmpl_id
                res['product_tmpl_id'] = product_id.product_tmpl_id.id
                res['product_id'] = product_id.id
            elif self.env.context.get('default_product_tmpl_id'):
                product_tmpl_id = self.env['product.template'].browse(
                    self.env.context['default_product_tmpl_id'])
                res['product_tmpl_id'] = product_tmpl_id.id
                res['product_id'] = product_tmpl_id.product_variant_id.id
                if len(product_tmpl_id.product_variant_ids) > 1:
                    res['product_has_variants'] = True
        if 'product_uom_id' in fields:
            res['product_uom_id'] = product_tmpl_id.uom_id.id
        if 'warehouse_id' in fields:
            res['warehouse_id'] = warehouse.id
        if 'date_planned' in fields:
            res['date_planned'] = datetime.datetime.now()
        return res

    def launch_replenishment(self):
        uom_reference = self.product_id.uom_id
        self.quantity = self.product_uom_id._compute_quantity(
            self.quantity, uom_reference)
        try:
            context = {k: v for k, v in self.env.context.items()
                       if not k.startswith('default_')}
            self.env['procurement.group'].with_context(context).run(
                self.product_id,
                self.quantity,
                uom_reference,
                self.warehouse_id.lot_stock_id,  # Location
                "Manual Replenishment",  # Name
                "Manual Replenishment",  # Origin
                self._prepare_run_values()  # Values
            )
        except UserError as error:
            raise UserError(error)

    def _prepare_run_values(self):
        replenishment = self.env['procurement.group'].create({
            'partner_id': self.product_id.responsible_id.partner_id.id,
        })

        values = {
{'fraction_non_alphanumeric': 0.13495816660603857, 'fraction_numerical': 0.0036376864314296106, 'mean_word_length': 2.63668430335097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4876433', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2343, 'n_words': 470}