From c973db83e4ccd0817ae0929a517d189d8b7fb889 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Fri, 5 Dec 2014 17:17:46 +0100 Subject: [PATCH] [IMP] Propagate cancel should be done for multiple procurements at once --- addons/mrp/procurement.py | 9 +++++---- addons/purchase/purchase.py | 27 +++++++++++++++++++-------- addons/stock/procurement.py | 16 ++++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/addons/mrp/procurement.py b/addons/mrp/procurement.py index 58727d9c160..072f4398274 100644 --- a/addons/mrp/procurement.py +++ b/addons/mrp/procurement.py @@ -40,10 +40,11 @@ class procurement_order(osv.osv): 'production_id': fields.many2one('mrp.production', 'Manufacturing Order'), } - def propagate_cancel(self, cr, uid, procurement, context=None): - if procurement.rule_id.action == 'manufacture' and procurement.production_id: - self.pool.get('mrp.production').action_cancel(cr, uid, [procurement.production_id.id], context=context) - return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context) + def propagate_cancels(self, cr, uid, ids, context=None): + for procurement in self.browse(cr, uid, ids, context=context): + if procurement.rule_id.action == 'manufacture' and procurement.production_id: + self.pool.get('mrp.production').action_cancel(cr, uid, [procurement.production_id.id], context=context) + return super(procurement_order, self).propagate_cancels(cr, uid, ids, context=context) def _run(self, cr, uid, procurement, context=None): if procurement.rule_id and procurement.rule_id.action == 'manufacture': diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 905296bce52..7af64ef8c31 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -1177,14 +1177,25 @@ class procurement_order(osv.osv): 'purchase_id': fields.related('purchase_line_id', 'order_id', type='many2one', relation='purchase.order', string='Purchase Order'), } - def propagate_cancel(self, cr, uid, procurement, context=None): - if procurement.rule_id.action == 'buy' and procurement.purchase_line_id: - purchase_line_obj = self.pool.get('purchase.order.line') - if procurement.purchase_line_id.product_qty > procurement.product_qty and procurement.purchase_line_id.order_id.state == 'draft': - purchase_line_obj.write(cr, uid, [procurement.purchase_line_id.id], {'product_qty': procurement.purchase_line_id.product_qty - procurement.product_qty}, context=context) - else: - purchase_line_obj.action_cancel(cr, uid, [procurement.purchase_line_id.id], context=context) - return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context) + def propagate_cancels(self, cr, uid, ids, context=None): + purchase_line_obj = self.pool.get('purchase.order.line') + lines_to_cancel = [] + uom_obj = self.pool.get("product.uom") + for procurement in self.browse(cr, uid, ids, context=context): + if procurement.rule_id.action == 'buy' and procurement.purchase_line_id: + uom = procurement.purchase_line_id.product_uom + product_qty = uom_obj._compute_qty_obj(cr, uid, procurement.product_uom, procurement.product_qty, uom, context=context) + if procurement.purchase_line_id.state not in ('draft', 'cancel'): + raise osv.except_osv(_('Error!'), + _('Can not cancel this procurement like this as the related purchase order has been confirmed already. Please cancel the purchase order first. ')) + if float_compare(procurement.purchase_line_id.product_qty, product_qty, 0, precision_rounding=uom.rounding) > 0: + purchase_line_obj.write(cr, uid, [procurement.purchase_line_id.id], {'product_qty': procurement.purchase_line_id.product_qty - product_qty}, context=context) + else: + if procurement.purchase_line_id.id not in lines_to_cancel: + lines_to_cancel += [procurement.purchase_line_id.id] + if lines_to_cancel: + purchase_line_obj.action_cancel(cr, uid, lines_to_cancel, context=context) + return super(procurement_order, self).propagate_cancels(cr, uid, ids, context=context) def _run(self, cr, uid, procurement, context=None): if procurement.rule_id and procurement.rule_id.action == 'buy': diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index c105ed6c017..4611365f38d 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -88,19 +88,23 @@ class procurement_order(osv.osv): 'orderpoint_id': fields.many2one('stock.warehouse.orderpoint', 'Minimum Stock Rule'), } - def propagate_cancel(self, cr, uid, procurement, context=None): - if procurement.rule_id.action == 'move' and procurement.move_ids: - self.pool.get('stock.move').action_cancel(cr, uid, [m.id for m in procurement.move_ids], context=context) + def propagate_cancels(self, cr, uid, ids, context=None): + move_cancel = [] + for procurement in self.browse(cr, uid, ids, context=context): + if procurement.rule_id.action == 'move' and procurement.move_ids: + move_cancel += [m.id for m in procurement.move_ids] + if move_cancel: + self.pool.get('stock.move').action_cancel(cr, uid, move_cancel, context=context) + return super(procurement_order, self).propagate_cancels(cr, uid, ids, context=context) def cancel(self, cr, uid, ids, context=None): if context is None: context = {} to_cancel_ids = self.get_cancel_ids(cr, uid, ids, context=context) ctx = context.copy() - #set the context for the propagation of the procurement cancelation + #set the context for the propagation of the procurement cancellation ctx['cancel_procurement'] = True - for procurement in self.browse(cr, uid, to_cancel_ids, context=ctx): - self.propagate_cancel(cr, uid, procurement, context=ctx) + self.propagate_cancels(cr, uid, to_cancel_ids, context=ctx) return super(procurement_order, self).cancel(cr, uid, to_cancel_ids, context=ctx) def _find_parent_locations(self, cr, uid, procurement, context=None):