diff --git a/addons/mrp/procurement.py b/addons/mrp/procurement.py index df80ac2ea9f..d342198e461 100644 --- a/addons/mrp/procurement.py +++ b/addons/mrp/procurement.py @@ -40,6 +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 _run(self, cr, uid, procurement, context=None): if procurement.rule_id and procurement.rule_id.action == 'manufacture': #make a manufacturing order for the procurement diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index e378ec83e20..4f610575740 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -1082,6 +1082,11 @@ class procurement_order(osv.osv): 'purchase_line_id': fields.many2one('purchase.order.line', 'Purchase Order'), } + def propagate_cancel(self, cr, uid, procurement, context=None): + if procurement.rule_id.action == 'buy' and procurement.purchase_line_id: + self.pool.get('purchase.order.line').write(cr, uid, [procurement.purchase_line_id.id], {'state': 'cancel'}, context=context) + return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context) + def _run(self, cr, uid, procurement, context=None): if procurement.rule_id: print procurement.rule_id.action diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index c1866867894..eb73980f47e 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -66,23 +66,22 @@ class procurement_rule(osv.osv): help="Source location is action=move"), 'route_id': fields.many2one('stock.location.route', 'Route', help="If route_id is False, the rule is global"), - 'procure_method': fields.selection([('make_to_stock','Make to Stock'),('make_to_order','Make to Order')], 'Procure Method', required=True, help="'Make to Stock': When needed, take from the stock or wait until re-supplying. 'Make to Order': When needed, purchase or produce for the procurement request."), + 'procure_method': fields.selection([('make_to_stock', 'Make to Stock'), ('make_to_order', 'Make to Order')], 'Procure Method', required=True, help="'Make to Stock': When needed, take from the stock or wait until re-supplying. 'Make to Order': When needed, purchase or produce for the procurement request."), 'route_sequence': fields.related('route_id', 'sequence', string='Route Sequence', store={ 'stock.location.route': (_get_rules, ['sequence'], 10), 'procurement.rule': (lambda self, cr, uid, ids, c={}: ids, ['route_id'], 10), }), 'sequence': fields.integer('Sequence'), - 'picking_type_id': fields.many2one('stock.picking.type', 'Picking Type', + 'picking_type_id': fields.many2one('stock.picking.type', 'Picking Type', help="Picking Type determines the way the picking should be shown in the view, reports, ..."), 'active': fields.related('route_id', 'active', type='boolean', string='Active', store={ 'stock.location.route': (_get_route, ['active'], 20), - 'procurement.rule': (lambda self, cr, uid, ids, c={}: ids, ['route_id'], 20),}, - help="If the active field is set to False, it will allow you to hide the rule without removing it." ), + 'procurement.rule': (lambda self, cr, uid, ids, c={}: ids, ['route_id'], 20)}, + help="If the active field is set to False, it will allow you to hide the rule without removing it."), 'delay': fields.integer('Number of Days'), 'partner_address_id': fields.many2one('res.partner', 'Partner Address'), 'propagate': fields.boolean('Propagate cancel and split', help='If checked, when the previous move of the move (which was generated by a next procurement) is cancelled or split, the move generated by this move will too'), - } _defaults = { @@ -102,7 +101,22 @@ class procurement_order(osv.osv): 'route_ids': fields.many2many('stock.location.route', 'stock_location_route_procurement', 'procurement_id', 'route_id', 'Followed Route', help="Preferred route to be followed by the procurement order"), } - + + 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 cancel(self, cr, uid, ids, context=None): + if context is None: + context = {} + ctx = context.copy() + #set the context for the propagation of the procurement cancelation + ctx['cancel_procurement'] = True + for procurement in self.browse(cr, uid, ids, context=ctx): + if procurement.rule_id and procurement.rule_id.propagate: + self.propagate_cancel(cr, uid, procurement, context=ctx) + return super(procurement_order, self).cancel(cr, uid, ids, context=ctx) + def _find_parent_locations(self, cr, uid, procurement, context=None): location = procurement.location_id res = [location.id] diff --git a/addons/stock/stock.py b/addons/stock/stock.py index d47b2fc1d63..fb5c92d4c71 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1700,6 +1700,7 @@ class stock_move(osv.osv): """ Cancels the moves and if all moves are cancelled it cancels the picking. @return: True """ + procurement_obj = self.pool.get('procurement.order') context = context or {} for move in self.browse(cr, uid, ids, context=context): if move.state == 'done': @@ -1707,7 +1708,12 @@ class stock_move(osv.osv): _('You cannot cancel a stock move that has been set to \'Done\'.')) if move.reserved_quant_ids: self.pool.get("stock.quant").quants_unreserve(cr, uid, move, context=context) - if move.move_dest_id: + if context.get('cancel_procurement'): + if move.propagate: + procurement_ids = procurement_obj.search(cr, uid, [('move_dest_id', '=', move.id)], context=context) + procurement_obj.cancel(cr, uid, procurement_ids, context=context) + elif move.move_dest_id: + #cancel chained moves if move.propagate: self.action_cancel(cr, uid, [move.move_dest_id.id], context=context) elif move.move_dest_id.state == 'waiting':