# -*- coding: utf-8 -*-
# © 2015 Serv. Tec. Avanzados - Pedro M. Baeza (http://www.serviciosbaeza.com)
# © 2015 AvanzOsc (http://www.avanzosc.es)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import models, fields, api, exceptions, _


class StockProductionLot(models.Model):
    _name = 'stock.production.lot'
    _inherit = ['stock.production.lot', 'mail.thread']

    _mail_post_access = 'read'
    _track = {
        'locked': {
            'stock_lock_lot.mt_lock_lot': lambda self, cr, uid, obj,
            ctx=None: obj.locked,
            'stock_lock_lot.mt_unlock_lot': lambda self, cr, uid, obj,
            ctx=None: not obj.locked,
        },
    }

    def _get_product_locked(self, product):
        """Should create locked? (including categories and parents)

        @param product: browse-record for product.product
        @return True when the category of the product or one of the parents
                demand new lots to be locked"""
        _locked = product.categ_id.lot_default_locked
        categ = product.categ_id.parent_id
        while categ and not _locked:
            _locked = categ.lot_default_locked
            categ = categ.parent_id
        return _locked

    @api.one
    def _get_locked_value(self):
        return self._get_product_locked(self.product_id)

    locked = fields.Boolean(string='Blocked', default='_get_locked_value',
                            readonly=True)

    @api.one
    @api.onchange('product_id')
    def onchange_product_id(self):
        '''Instruct the client to lock/unlock a lot on product change'''
        self.locked = self._get_product_locked(self.product_id)

    @api.multi
    def button_lock(self):
        '''Lock the lot if the reservations allow it'''
        stock_quant_obj = self.env['stock.quant']
        for lot in self:
            cond = [('lot_id', '=', lot.id),
                    ('reservation_id', '!=', False)]
            for quant in stock_quant_obj.search(cond):
                if quant.reservation_id.state not in ('cancel', 'done'):
                    raise exceptions.Warning(
                        _('Error! Serial Number/Lot "%s" currently has '
                          'reservations.')
                        % (lot.name))
        return self.write({'locked': True})

    @api.multi
    def button_unlock(self):
        return self.write({'locked': False})

    # Kept in old API to maintain compatibility
    def create(self, cr, uid, vals, context=None):
        '''Force the locking/unlocking, ignoring the value of 'locked'.'''
        product = self.pool['product.product'].browse(
            cr, uid, vals.get('product_id'))
        vals['locked'] = self._get_product_locked(product)
        return super(StockProductionLot, self).create(
            cr, uid, vals, context=context)

    @api.multi
    def write(self, values):
        '''Lock the lot if changing the product and locking is required'''
        if 'product_id' in values:
            product = self.env['product.product'].browse(
                values.get('product_id'))
            values['locked'] = self._get_product_locked(product)
        return super(StockProductionLot, self).write(values)

@@ -2590,157 +2590,345 @@
     
-product = self.pool%5B'product.product'%5D.browse(%0A            cr, uid, vals.get('product_id'))%0A        vals%5B'locked'%5D = self._get_product_locked(product
+# %C2%A0Web quick-create doesn't provide product_id in vals, but in context%0A        product_id = vals.get('product_id', context.get('product_id', False))%0A        if product_id:%0A            vals%5B'locked'%5D = self._get_product_locked(%0A                self.pool%5B'product.product'%5D.browse(%0A                    cr, uid, product_id, context=context)
 )%0A  

enforce lot locking when using quick-create in web client