~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to mrp_prodlot_autosplit/prodlot_wizard.py

  • Committer: Albert Cervera i Areny
  • Date: 2011-06-14 09:51:35 UTC
  • mfrom: (5345.1.165 openobject-addons)
  • Revision ID: albert@nan-tic.com-20110614095135-1x3p6tmil5lxkl9b
Merge and add nan_remove_default_filters

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
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
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv, fields
 
24
from tools.translate import _
 
25
 
 
26
def is_integer(value):
 
27
    try:
 
28
        int(value)
 
29
        return True
 
30
    except:
 
31
        return False
 
32
 
 
33
class stock_picking_prodlot_selection_wizard(osv.osv_memory):
 
34
    _name = 'stock.picking.prodlot.selection'
 
35
 
 
36
    _columns = {
 
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),
 
40
    }
 
41
 
 
42
    def action_cancel(self, cr, uid, ids, context=None):
 
43
        return {}
 
44
 
 
45
    def action_accept(self, cr, uid, ids, context=None):
 
46
        if context is None:
 
47
            context = {}
 
48
        if not ids:
 
49
            return {}
 
50
        if not 'active_id' in context:
 
51
            return {}
 
52
 
 
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.'))
 
58
 
 
59
 
 
60
        first_number = ''
 
61
        last_number = ''
 
62
        position = -1
 
63
        for x in xrange(len(first)):
 
64
            if not position and first[x] == last[x]:
 
65
                continue
 
66
            if not position:
 
67
                position = 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]
 
72
 
 
73
        if position >= 0:
 
74
            prefix = first[:position-1]
 
75
        else:
 
76
            prefix = ''
 
77
        
 
78
        number_fill = len(first_number)
 
79
        first_number = int(first_number)
 
80
        last_number = int(last_number)
 
81
 
 
82
        if last_number < first_number:
 
83
            raise osv.except_osv(_('Invalid lot numbers'), _('First lot number is greater than the last one.'))
 
84
 
 
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:
 
89
                continue
 
90
 
 
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)
 
93
            if not lot_ids:
 
94
                raise osv.except_osv(_('Invalid lot numbers'), _('Production lot %s not found.') % current_lot)
 
95
 
 
96
            ctx = context.copy()
 
97
            ctx['location_id'] = move.location_id.id
 
98
            prodlot = self.pool.get('stock.production.lot').browse(cr, uid, lot_ids[0], ctx)
 
99
            
 
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))
 
102
 
 
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)
 
105
            
 
106
            self.pool.get('stock.move').write(cr, uid, [move.id], {
 
107
                'prodlot_id': lot_ids[0],
 
108
            }, context)
 
109
 
 
110
            current_number += 1
 
111
            if current_number > last_number:
 
112
                break
 
113
 
 
114
        return {}
 
115
 
 
116
        
 
117
 
 
118
stock_picking_prodlot_selection_wizard()
 
119