1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# MRP prodlot auto-split module for OpenERP
5
# Copyright (C) 2010 NaN Projectes de Programari Lliure, S.L.
6
# http://www.NaN-tic.com
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as
10
# published by the Free Software Foundation, either version 3 of the
11
# License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Affero General Public License for more details.
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
23
from osv import osv, fields
24
from tools.translate import _
26
def is_integer(value):
33
class stock_picking_prodlot_selection_wizard(osv.osv_memory):
34
_name = 'stock.picking.prodlot.selection'
37
'product_id': fields.many2one('product.product', 'Product', required=True),
38
'first_lot': fields.char('First Lot Number', size=256),
39
'last_lot': fields.char('Last Lot Number', size=256),
42
def action_cancel(self, cr, uid, ids, context=None):
45
def action_accept(self, cr, uid, ids, context=None):
50
if not 'active_id' in context:
53
record = self.browse(cr, uid, ids[0], context)
54
first = record.first_lot
55
last = record.last_lot
56
if len(first) != len(last):
57
raise osv.except_osv(_('Invalid lot numbers'), _('First and last lot numbers must have the same length.'))
63
for x in xrange(len(first)):
64
if not position and first[x] == last[x]:
68
if not is_integer(first[x]) or not is_integer(last[x]):
69
raise osv.except_osv(_('Invalid lot numbers'), _('First and last lot numbers differ in non-numeric values.'))
70
first_number += first[x]
71
last_number += last[x]
74
prefix = first[:position-1]
78
number_fill = len(first_number)
79
first_number = int(first_number)
80
last_number = int(last_number)
82
if last_number < first_number:
83
raise osv.except_osv(_('Invalid lot numbers'), _('First lot number is greater than the last one.'))
85
picking_id = context['active_id']
86
current_number = first_number
87
for move in self.pool.get('stock.picking').browse(cr, uid, picking_id, context).move_lines:
88
if move.prodlot_id or move.product_id != record.product_id:
91
current_lot = '%%s%%0%dd' % number_fill % (prefix, current_number)
92
lot_ids = self.pool.get('stock.production.lot').search(cr, uid, [('name','=',current_lot)], limit=1, context=context)
94
raise osv.except_osv(_('Invalid lot numbers'), _('Production lot %s not found.') % current_lot)
97
ctx['location_id'] = move.location_id.id
98
prodlot = self.pool.get('stock.production.lot').browse(cr, uid, lot_ids[0], ctx)
100
if prodlot.product_id != record.product_id:
101
raise osv.except_osv(_('Invalid lot numbers'), _('Production lot %s exists but not for product %s.') % (current_lot, record.product_id.name))
103
if prodlot.stock_available < move.product_qty:
104
raise osv.except_osv(_('Invalid lot numbers'), _('Not enough stock available of production lot %s.') % current_lot)
106
self.pool.get('stock.move').write(cr, uid, [move.id], {
107
'prodlot_id': lot_ids[0],
111
if current_number > last_number:
118
stock_picking_prodlot_selection_wizard()