<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>
#    Copyright 2013 Camptocamp SA
#
#    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/>.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _


class stock_reservation(orm.Model):
    """ Allow to reserve products.

    The fields mandatory for the creation of a reservation are:

    * product_id
    * product_qty
    * product_uom
    * name

    The following fields are required but have default values that you may
    want to override:

    * company_id
    * location_id
    * dest_location_id

    Optionally, you may be interested to define:

    * date_validity  (once passed, the reservation will be released)
    * note
    """
    _name = 'stock.reservation'
    _description = 'Stock Reservation'
    _inherits = {'stock.move': 'move_id'}

    _columns = {
        'move_id': fields.many2one('stock.move',
                                   'Reservation Move',
                                   required=True,
                                   readonly=True,
                                   ondelete='cascade',
                                   select=1),
        'date_validity': fields.date('Validity Date'),
    }

    def _get_location_from_ref(self, cr, uid, ref, context=None):
        """ Get a location from a xmlid if allowed
        :param ref: tuple (module, xmlid)
        """
        location_obj = self.pool.get('stock.location')
        data_obj = self.pool.get('ir.model.data')
        get_ref = data_obj.get_object_reference
        try:
            __, location_id = get_ref(cr, uid, *ref)
            location_obj.check_access_rule(cr, uid, [location_id],
                                           'read', context=context)
        except (orm.except_orm, ValueError):
            location_id = False
        return location_id

    def _default_location_id(self, cr, uid, context=None):
        ref = ('stock', 'stock_location_stock')
        return self._get_location_from_ref(cr, uid, ref, context=context)

    def _default_dest_location_id(self, cr, uid, context=None):
        ref = ('stock_reserve', 'stock_location_reservation')
        return self._get_location_from_ref(cr, uid, ref, context=context)

    _defaults = {
        'type': 'internal',
        'location_id': _default_location_id,
        'location_dest_id': _default_dest_location_id,
        'product_qty': 1.0,
    }

    def reserve(self, cr, uid, ids, context=None):
        """ Confirm a reservation

        The reservation is done using the default UOM of the product.
        A date until which the product is reserved can be specified.
        """
        move_obj = self.pool.get('stock.move')
        reservations = self.browse(cr, uid, ids, context=context)
        move_ids = [reserv.move_id.id for reserv in reservations]
        move_obj.write(cr, uid, move_ids,
                       {'date_expected': fields.datetime.now()},
                       context=context)
        move_obj.action_confirm(cr, uid, move_ids, context=context)
        move_obj.force_assign(cr, uid, move_ids, context=context)
        move_obj.action_done(cr, uid, move_ids, context=context)
        return True

    def release(self, cr, uid, ids, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        reservations = self.read(cr, uid, ids, ['move_id'],
                                 context=context, load='_classic_write')
        move_obj = self.pool.get('stock.move')
        move_ids = [reserv['move_id'] for reserv in reservations]
        move_obj.action_cancel(cr, uid, move_ids, context=context)
        return True

    def release_validity_exceeded(self, cr, uid, ids=None, context=None):
        """ Release all the reservation having an exceeded validity date """
        domain = [('date_validity', '<', fields.date.today()),
                  ('state', '=', 'done')]
        if ids:
            domain.append(('id', 'in', ids))
        reserv_ids = self.search(cr, uid, domain, context=context)
        self.release(cr, uid, reserv_ids, context=context)<commit_msg>[FIX] the move should be assigned and not done: items are not physically moved and would appear in an inventory<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Author: <NAME>
#    Copyright 2013 Camptocamp SA
#
#    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/>.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _


class stock_reservation(orm.Model):
    """ Allow to reserve products.

    The fields mandatory for the creation of a reservation are:

    * product_id
    * product_qty
    * product_uom
    * name

    The following fields are required but have default values that you may
    want to override:

    * company_id
    * location_id
    * dest_location_id

    Optionally, you may be interested to define:

    * date_validity  (once passed, the reservation will be released)
    * note
    """
    _name = 'stock.reservation'
    _description = 'Stock Reservation'
    _inherits = {'stock.move': 'move_id'}

    _columns = {
        'move_id': fields.many2one('stock.move',
                                   'Reservation Move',
                                   required=True,
                                   readonly=True,
                                   ondelete='cascade',
                                   select=1),
        'date_validity': fields.date('Validity Date'),
    }

    def _get_location_from_ref(self, cr, uid, ref, context=None):
        """ Get a location from a xmlid if allowed
        :param ref: tuple (module, xmlid)
        """
        location_obj = self.pool.get('stock.location')
        data_obj = self.pool.get('ir.model.data')
        get_ref = data_obj.get_object_reference
        try:
            __, location_id = get_ref(cr, uid, *ref)
            location_obj.check_access_rule(cr, uid, [location_id],
                                           'read', context=context)
        except (orm.except_orm, ValueError):
            location_id = False
        return location_id

    def _default_location_id(self, cr, uid, context=None):
        ref = ('stock', 'stock_location_stock')
        return self._get_location_from_ref(cr, uid, ref, context=context)

    def _default_dest_location_id(self, cr, uid, context=None):
        ref = ('stock_reserve', 'stock_location_reservation')
        return self._get_location_from_ref(cr, uid, ref, context=context)

    _defaults = {
        'type': 'internal',
        'location_id': _default_location_id,
        'location_dest_id': _default_dest_location_id,
        'product_qty': 1.0,
    }

    def reserve(self, cr, uid, ids, context=None):
        """ Confirm a reservation

        The reservation is done using the default UOM of the product.
        A date until which the product is reserved can be specified.
        """
        move_obj = self.pool.get('stock.move')
        reservations = self.browse(cr, uid, ids, context=context)
        move_ids = [reserv.move_id.id for reserv in reservations]
        move_obj.write(cr, uid, move_ids,
                       {'date_expected': fields.datetime.now()},
                       context=context)
        move_obj.action_confirm(cr, uid, move_ids, context=context)
        move_obj.force_assign(cr, uid, move_ids, context=context)
        return True

    def release(self, cr, uid, ids, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        reservations = self.read(cr, uid, ids, ['move_id'],
                                 context=context, load='_classic_write')
        move_obj = self.pool.get('stock.move')
        move_ids = [reserv['move_id'] for reserv in reservations]
        move_obj.action_cancel(cr, uid, move_ids, context=context)
        return True

    def release_validity_exceeded(self, cr, uid, ids=None, context=None):
        """ Release all the reservation having an exceeded validity date """
        domain = [('date_validity', '<', fields.date.today()),
                  ('state', '=', 'done')]
        if ids:
            domain.append(('id', 'in', ids))
        reserv_ids = self.search(cr, uid, domain, context=context)
        self.release(cr, uid, reserv_ids, context=context)
{'fraction_non_alphanumeric': 0.12684515931996335, 'fraction_numerical': 0.0020360378703043877, 'mean_word_length': 2.472605160834217, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4024704', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2652, 'n_words': 816}