[FIX] purchase_requisition: duplicated stock moves
authorDenis Ledoux <dle@odoo.com>
Wed, 25 Jun 2014 10:00:14 +0000 (12:00 +0200)
committerDenis Ledoux <dle@odoo.com>
Wed, 25 Jun 2014 10:00:14 +0000 (12:00 +0200)
When a purchase requisition is created from a procurement order, a first stock move is created, not associated to any purchase orders
Then, on purchase order creation and confirmation, in the purchase requisition, new stock moves are created, associated to the purchase order.
The existing stock move issued from the procurement order which created the purchase requisition remained untouched, leading to wrong inventory values
To fix this, the destination location of the stock move of the procurement order is written on the source location
A proper fix should be to use a dedicated workflow for puchase requisition, but this can't be done in 7.0, it has to be done in master/trunk

addons/purchase_requisition/purchase_requisition.py

index 3b8ea1c..031c902 100644 (file)
@@ -73,7 +73,7 @@ class purchase_requisition(osv.osv):
                 if str(purchase_id.state) in('draft'):
                     purchase_order_obj.action_cancel(cr,uid,[purchase_id.id])
         procurement_ids = self.pool['procurement.order'].search(cr, uid, [('requisition_id', 'in', ids)], context=context)
-        self.pool['procurement.order'].write(cr, uid, procurement_ids, {'state': 'cancel'}, context=context)
+        self.pool['procurement.order'].action_done(cr, uid, procurement_ids)
         return self.write(cr, uid, ids, {'state': 'cancel'})
 
     def tender_in_progress(self, cr, uid, ids, context=None):
@@ -84,7 +84,7 @@ class purchase_requisition(osv.osv):
 
     def tender_done(self, cr, uid, ids, context=None):
         procurement_ids = self.pool['procurement.order'].search(cr, uid, [('requisition_id', 'in', ids)], context=context)
-        self.pool['procurement.order'].write(cr, uid, procurement_ids, {'state': 'done'}, context=context)
+        self.pool['procurement.order'].action_done(cr, uid, procurement_ids)
         return self.write(cr, uid, ids, {'state':'done', 'date_end':time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
 
     def _planned_date(self, requisition, delay=0.0):
@@ -218,6 +218,10 @@ class purchase_order(osv.osv):
                         wf_service = netsvc.LocalService("workflow")
                         wf_service.trg_validate(uid, 'purchase.order', order.id, 'purchase_cancel', cr)
                     po.requisition_id.tender_done(context=context)
+            if po.requisition_id and all(purchase_id.state in ['draft', 'cancel'] for purchase_id in po.requisition_id.purchase_ids if purchase_id.id != po.id):
+                procurement_ids = self.pool['procurement.order'].search(cr, uid, [('requisition_id', '=', po.requisition_id.id)], context=context)
+                for procurement in proc_obj.browse(cr, uid, procurement_ids, context=context):
+                    procurement.move_id.write({'location_id': procurement.move_id.location_dest_id.id})
         return res
 
 purchase_order()