[IMP] stock: Improved code quality for precision and improved name for return moves...
[odoo/odoo.git] / addons / stock / wizard / stock_return_picking.py
index c7d4b1e..cdf8bfe 100644 (file)
@@ -100,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 return_history.get(m.id) and 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)!"))
@@ -123,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):
@@ -158,7 +163,9 @@ 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'],})
         
@@ -170,6 +177,11 @@ class stock_return_picking(osv.osv_memory):
             move = move_obj.browse(cr, uid, mov_id, context=context)
             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