[MERGE] stock : Merged Valencia's branch for prodlot warning. (Case:6001)
[odoo/odoo.git] / addons / stock / wizard / stock_move.py
index 0ce4855..cebb62d 100644 (file)
@@ -20,7 +20,8 @@
 ##############################################################################
 
 from osv import fields, osv
-from tools.translate import _
+
+import decimal_precision as dp
 
 class stock_move_track(osv.osv_memory):
     _name = "stock.move.track"
@@ -35,7 +36,7 @@ class stock_move_track(osv.osv_memory):
         'quantity': lambda *x: 1
     }
 
-    def track_lines(self, cr, uid, ids, context={}):
+    def track_lines(self, cr, uid, ids, context=None):
         """ To track stock moves lines
         @param self: The object pointer.
         @param cr: A database cursor
@@ -47,7 +48,7 @@ class stock_move_track(osv.osv_memory):
         datas = self.read(cr, uid, ids)[0]
         move_obj = self.pool.get('stock.move')
         move_obj._track_lines(cr, uid, context['active_id'], datas, context=context)
-        return {}
+        return {'type': 'ir.actions.act_window_close'}
 
 stock_move_track()
 
@@ -71,6 +72,8 @@ class stock_move_consume(osv.osv_memory):
         @param context: A standard dictionary
         @return: default values of fields
         """
+        if context is None:
+            context = {}
         res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
         move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
         if 'product_id' in fields:
@@ -84,7 +87,7 @@ class stock_move_consume(osv.osv_memory):
 
         return res
 
-    def do_move_consume(self, cr, uid, ids, context={}):
+    def do_move_consume(self, cr, uid, ids, context=None):
         """ To move consumed products
         @param self: The object pointer.
         @param cr: A database cursor
@@ -93,13 +96,15 @@ class stock_move_consume(osv.osv_memory):
         @param context: A standard dictionary
         @return:
         """
+        if context is None:
+            context = {}
         move_obj = self.pool.get('stock.move')
         move_ids = context['active_ids']
         for data in self.read(cr, uid, ids):
             move_obj.action_consume(cr, uid, move_ids,
                              data['product_qty'], data['location_id'],
                              context=context)
-        return {}
+        return {'type': 'ir.actions.act_window_close'}
 
 stock_move_consume()
 
@@ -122,6 +127,8 @@ class stock_move_scrap(osv.osv_memory):
         @param context: A standard dictionary
         @return: default values of fields
         """
+        if context is None:
+            context = {}
         res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
         move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
         location_obj = self.pool.get('stock.location')
@@ -141,8 +148,8 @@ class stock_move_scrap(osv.osv_memory):
 
         return res
 
-    def move_scrap(self, cr, uid, ids, context={}):
-        """ To move scraped products
+    def move_scrap(self, cr, uid, ids, context=None):
+        """ To move scrapped products
         @param self: The object pointer.
         @param cr: A database cursor
         @param uid: ID of the user currently logged in
@@ -150,13 +157,15 @@ class stock_move_scrap(osv.osv_memory):
         @param context: A standard dictionary
         @return:
         """
+        if context is None:
+            context = {}
         move_obj = self.pool.get('stock.move')
         move_ids = context['active_ids']
         for data in self.read(cr, uid, ids):
             move_obj.action_scrap(cr, uid, move_ids,
                              data['product_qty'], data['location_id'],
                              context=context)
-        return {}
+        return {'type': 'ir.actions.act_window_close'}
 
 stock_move_scrap()
 
@@ -174,21 +183,31 @@ class split_in_production_lot(osv.osv_memory):
         @param context: A standard dictionary
         @return: Default values of fields
         """
-
+        if context is None:
+            context = {}
         res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
-        move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
-        if 'product_id' in fields:
-            res.update({'product_id': move.product_id.id})
-        if 'qty' in fields:
-            res.update({'qty': move.product_qty})
+        if context.get('active_id'):
+            move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
+            if 'product_id' in fields:
+                res.update({'product_id': move.product_id.id})
+            if 'product_uom' in fields:
+                res.update({'product_uom': move.product_uom.id})
+            if 'qty' in fields:
+                res.update({'qty': move.product_qty})
+            if 'use_exist' in fields:
+                res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
+            if 'location_id' in fields:
+                res.update({'location_id': move.location_id.id})
         return res
 
     _columns = {
-        'qty': fields.integer('Quantity'),
+        'qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
         'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
-        'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Lots Number'),
-        'line_exist_ids': fields.one2many('stock.move.split.lines.exist', 'lot_id', 'Lots Existing Numbers'),
-        'use_exist' : fields.boolean('Use Exist'),
+        'product_uom': fields.many2one('product.uom', 'UoM'),
+        'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Production Lots'),
+        'line_exist_ids': fields.one2many('stock.move.split.lines.exist', 'lot_id', 'Production Lots'),
+        'use_exist' : fields.boolean('Existing Lots', help="Check this option to select existing lots in the list below, otherwise you should enter new ones line by line."),
+        'location_id': fields.many2one('stock.location', 'Source Location')
      }
 
     def split_lot(self, cr, uid, ids, context=None):
@@ -200,8 +219,10 @@ class split_in_production_lot(osv.osv_memory):
         @param context: A standard dictionary
         @return:
         """
+        if context is None:
+            context = {}
         self.split(cr, uid, ids, context.get('active_ids'), context=context)
-        return {}
+        return {'type': 'ir.actions.act_window_close'}
 
     def split(self, cr, uid, ids, move_ids, context=None):
         """ To split stock moves into production lot
@@ -213,12 +234,15 @@ class split_in_production_lot(osv.osv_memory):
         @param context: A standard dictionary
         @return:
         """
+        if context is None:
+            context = {}
+        inventory_id = context.get('inventory_id', False)
         prodlot_obj = self.pool.get('stock.production.lot')
-        ir_sequence_obj = self.pool.get('ir.sequence')
+        inventory_obj = self.pool.get('stock.inventory')
         move_obj = self.pool.get('stock.move')
         new_move = []
-        for data in self.browse(cr, uid, ids):
-            for move in move_obj.browse(cr, uid, move_ids):
+        for data in self.browse(cr, uid, ids, context=context):
+            for move in move_obj.browse(cr, uid, move_ids, context=context):
                 move_qty = move.product_qty
                 quantity_rest = move.product_qty
                 uos_qty_rest = move.product_uos_qty
@@ -243,27 +267,23 @@ class split_in_production_lot(osv.osv_memory):
                         'state': move.state
                     }
                     if quantity_rest > 0:
-                        current_move = move_obj.copy(cr, uid, move.id, default_val)
-                        move_obj.action_confirm(cr, uid, [current_move])
+                        current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
+                        if inventory_id and current_move:
+                            inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
                         new_move.append(current_move)
+
                     if quantity_rest == 0:
                         current_move = move.id
                     prodlot_id = False
-                    if data.use_exist and line.prodlot_id:
-                        prodlot_id = prodlot_obj.search(cr, uid, [('id','=',line.prodlot_id.id)])
-                        if prodlot_id:
-                            prodlot_id = prodlot_id[0]
+                    if data.use_exist:
+                        prodlot_id = line.prodlot_id.id
                     if not prodlot_id:
-                      #  sequence = ir_sequence_obj.get(cr, uid, 'stock.lot.serial')
-                        prodlot_id = prodlot_obj.create(cr, uid, {'name': line.name},
-                        # 'prefix' : line.name},
-                                                 {'product_id': move.product_id.id})
+                        prodlot_id = prodlot_obj.create(cr, uid, {
+                            'name': line.name,
+                            'product_id': move.product_id.id},
+                        context=context)
+
                     move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
-                    prodlot = prodlot_obj.browse(cr, uid, prodlot_id)
-                 #   ref = '%d' % (move.id)
-                 #   if prodlot.ref:
-                 #       ref = '%s, %s' % (prodlot.ref, ref)
-                 #   prodlot_obj.write(cr, uid, [prodlot_id], {'ref': ref})
 
                     update_val = {}
                     if quantity_rest > 0:
@@ -271,7 +291,9 @@ class split_in_production_lot(osv.osv_memory):
                         update_val['product_uos_qty'] = uos_qty_rest
                         update_val['state'] = move.state
                         move_obj.write(cr, uid, [move.id], update_val)
+
         return new_move
+
 split_in_production_lot()
 
 class stock_move_split_lines_exist(osv.osv_memory):
@@ -279,14 +301,19 @@ class stock_move_split_lines_exist(osv.osv_memory):
     _description = "Exist Split lines"
     _columns = {
         'name': fields.char('Tracking serial', size=64),
-        'quantity': fields.integer('Quantity'),
+        'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
         'lot_id': fields.many2one('stock.move.split', 'Lot'),
         'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
     }
     _defaults = {
-        'quantity': lambda *x: 1,
+        'quantity': lambda *x: 1.00,
     }
 
+    def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False,
+                        loc_id=False, product_id=False, uom_id=False):
+        return self.pool.get('stock.move').onchange_lot_id(cr, uid, [], prodlot_id, product_qty,
+                        loc_id, product_id, uom_id)
+
 stock_move_split_lines_exist()
 
 class stock_move_split_lines(osv.osv_memory):
@@ -294,13 +321,13 @@ class stock_move_split_lines(osv.osv_memory):
     _description = "Split lines"
     _columns = {
         'name': fields.char('Tracking serial', size=64),
-        'quantity': fields.integer('Quantity'),
-        'use_exist' : fields.boolean('Use Exist'),
+        'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
+        'use_exist' : fields.boolean('Existing Lot'),
         'lot_id': fields.many2one('stock.move.split', 'Lot'),
         'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
     }
     _defaults = {
-        'quantity': lambda *x: 1,
+        'quantity': lambda *x: 1.00,
         'action' : lambda *x: 'split',
     }
 stock_move_split_lines()