<commit_before>from osv import fields, osv
import tools
import ir
import pooler


class stock_move(osv.osv):
    _inherit = "stock.move"
    
    def copy(self, cr, uid, id, default=None, context={}):
        if not default:
            default = {}
        default['new_prodlot_code'] = False
        return super(stock_move, self).copy(cr, uid, id, default, context=context)
    
     
    def _get_prodlot_code(self, cr, uid, ids, field_name, arg, context={}):
        res = {}
        for move in self.browse(cr, uid, ids):
            res[move.id] = move.prodlot_id and move.prodlot_id.name or False
        return res
    
    def _set_prodlot_code(self, cr, uid, ids, name, value, arg, context):
        if not value: return False
        
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        for move in self.browse(cr, uid, ids):
            product_id = move.product_id.id
            existing_prodlot_id = self.pool.get('stock.production.lot').search(cr, uid, [('product_id','=', product_id), ('name', '=', value)])
            if not existing_prodlot_id: #avoid creating a prodlot twice
                prodlot_id = self.pool.get('stock.production.lot').create(cr, uid, {
                    'name': value,
                    'product_id': product_id,
                })
                self.write(cr, uid, ids, {'prodlot_id': prodlot_id})
            else:
                self.pool.get('stock.production.lot').write(cr, uid, existing_prodlot_id, {'name': value})
            

    _columns = {        
        'new_prodlot_code': fields.function(_get_prodlot_code, fnct_inv=_set_prodlot_code,
                 method=True, type='char', size=64, string='Production Tracking Code To Create', select=1),
    }
    

#    def _check_unique_product_lot(self, cr, uid, ids):
#        print "########"
#        for move in self.browse(cr, uid, ids):
#            if move.state == 'done' and move.product_id.unique_production_number and move.product_qty > 1 and (\
#                (move.product_id.track_production and move.location_id.usage == 'production') or \
#                (move.product_id.track_production and move.location_dest_id.usage == 'production') or \
#                (move.product_id.track_incoming and move.location_id.usage == 'supplier') or \
#                (move.product_id.track_outgoing and move.location_dest_id.usage == 'customer')):
#                return False
#        return True
#        
#    _constraints = [
#        (_check_unique_product_lot,<commit_msg>- mrp_prodlot_autosplit: bug fix: totally accurate selection of existing move.prodlot<commit_after>from osv import fields, osv
import tools
import ir
import pooler


class stock_move(osv.osv):
    _inherit = "stock.move"
    
    def copy(self, cr, uid, id, default=None, context={}):
        if not default:
            default = {}
        default['new_prodlot_code'] = False
        return super(stock_move, self).copy(cr, uid, id, default, context=context)
    
     
    def _get_prodlot_code(self, cr, uid, ids, field_name, arg, context={}):
        res = {}
        for move in self.browse(cr, uid, ids):
            res[move.id] = move.prodlot_id and move.prodlot_id.name or False
        return res
    
    def _set_prodlot_code(self, cr, uid, ids, name, value, arg, context):
        if not value: return False
        
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        for move in self.browse(cr, uid, ids):
            product_id = move.product_id.id
            existing_prodlot = move.prodlot_id
            if existing_prodlot: #avoid creating a prodlot twice
                self.pool.get('stock.production.lot').write(cr, uid, existing_prodlot.id, {'name': value})
            else:
                prodlot_id = self.pool.get('stock.production.lot').create(cr, uid, {
                    'name': value,
                    'product_id': product_id,
                })
                self.write(cr, uid, ids, {'prodlot_id': prodlot_id})
            

    _columns = {        
        'new_prodlot_code': fields.function(_get_prodlot_code, fnct_inv=_set_prodlot_code,
                 method=True, type='char', size=64, string='Production Tracking Code To Create', select=1),
    }
    

#    def _check_unique_product_lot(self, cr, uid, ids):
#        print "########"
#        for move in self.browse(cr, uid, ids):
#            if move.state == 'done' and move.product_id.unique_production_number and move.product_qty > 1 and (\
#                (move.product_id.track_production and move.location_id.usage == 'production') or \
#                (move.product_id.track_production and move.location_dest_id.usage == 'production') or \
#                (move.product_id.track_incoming and move.location_id.usage == 'supplier') or \
#                (move.product_id.track_outgoing and move.location_dest_id.usage == 'customer')):
#                return False
#        return True
#        
#    _constraints = [
#        (_check_unique_product_lot,
{'fraction_non_alphanumeric': 0.11949186184994046, 'fraction_numerical': 0.0015879317189360857, 'mean_word_length': 2.2136479591836733, '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': '1530810', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1519, 'n_words': 358}