[IMP] stock: Improved code quality for precision and improved name for return moves...
[odoo/odoo.git] / addons / stock / wizard / stock_return_picking.py
index 47832c1..cdf8bfe 100644 (file)
@@ -24,13 +24,14 @@ import time
 
 from osv import osv,fields
 from tools.translate import _
+import decimal_precision as dp
 
 class stock_return_picking_memory(osv.osv_memory):
     _name = "stock.return.picking.memory"
     _rec_name = 'product_id'
     _columns = {
         'product_id' : fields.many2one('product.product', string="Product", required=True),
-        'quantity' : fields.float("Quantity", required=True),
+        'quantity' : fields.float("Quantity", digits_compute=dp.get_precision('Product UoM'), required=True),
         'wizard_id' : fields.many2one('stock.return.picking', string="Wizard"),
         'move_id' : fields.many2one('stock.move', "Move"),
     }
@@ -99,7 +100,7 @@ class stock_return_picking(osv.osv_memory):
             valid_lines = 0
             return_history = self.get_return_history(cr, uid, record_id, context)
             for m  in pick.move_lines:
-                if m.product_qty * m.product_uom.factor > return_history[m.id]:
+                if (return_history.get(m.id) is not None) and (m.product_qty * m.product_uom.factor > return_history[m.id]):
                         valid_lines += 1
             if not valid_lines:
                 raise osv.except_osv(_('Warning !'), _("There are no products to return (only lines in Done state and not fully returned yet can be returned)!"))
@@ -122,7 +123,12 @@ class stock_return_picking(osv.osv_memory):
             if m.state == 'done':
                 return_history[m.id] = 0
                 for rec in m.move_history_ids2:
-                    return_history[m.id] += (rec.product_qty * rec.product_uom.factor)
+                    # only take into account 'product return' stock move
+                    # (i.e move with exact opposite of ours:
+                    #     (location, dest location) = (dest location, location))
+                    if rec.location_dest_id.id == m.location_id.id \
+                        and rec.location_id.id == m.location_dest_id.id:
+                        return_history[m.id] += (rec.product_qty * rec.product_uom.factor)
         return return_history
 
     def create_returns(self, cr, uid, ids, context=None):
@@ -157,18 +163,25 @@ class stock_return_picking(osv.osv_memory):
             new_type = 'out'
         else:
             new_type = 'internal'
-        new_picking = pick_obj.copy(cr, uid, pick.id, {'name':'%s-return' % pick.name,
+        seq_obj_name = 'stock.picking.' + new_type
+        new_pick_name = self.pool.get('ir.sequence').get(cr, uid, seq_obj_name)
+        new_picking = pick_obj.copy(cr, uid, pick.id, {'name':'%s-%s-return' % (new_pick_name, pick.name),
                 'move_lines':[], 'state':'draft', 'type':new_type,
                 'date':date_cur, 'invoice_state':data['invoice_state'],})
         
         val_id = data['product_return_moves']
         for v in val_id:
-            data_get = data_obj.read(cr, uid, v)
-            mov_id = data_get['move_id']
-            new_qty = data_get['quantity']
+            data_get = data_obj.browse(cr, uid, v, context=context)
+            mov_id = data_get.move_id.id
+            new_qty = data_get.quantity
             move = move_obj.browse(cr, uid, mov_id, context=context)
-            new_location=move.location_dest_id.id
+            new_location = move.location_dest_id.id
             returned_qty = move.product_qty
+            if new_qty > returned_qty:
+                precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Product UoM')
+                raise osv.except_osv(_('Returning Error'),
+                 _('Returning quantity %.*f for %s is larger than the available quantity %.*f!')\
+                 % (precision, new_qty, move.product_id.name, precision, returned_qty))
             for rec in move.move_history_ids2:
                 returned_qty -= rec.product_qty