diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index cdcb86427ae..3a95e3278ae 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -102,6 +102,7 @@ class procurement_order(osv.osv): 'move_dest_id': fields.many2one('stock.move', 'Destination Move', help="Move which caused (created) the procurement"), 'route_ids': fields.many2many('stock.location.route', 'stock_location_route_procurement', 'procurement_id', 'route_id', 'Preferred Routes', help="Preferred route to be followed by the procurement order. Usually copied from the generating document (SO) but could be set up manually."), 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', help="Warehouse to consider for the route selection"), + 'orderpoint_id': fields.many2one('stock.warehouse.orderpoint', 'Minimum Stock Rule'), } def propagate_cancel(self, cr, uid, procurement, context=None): @@ -329,15 +330,18 @@ class procurement_order(osv.osv): return date_planned.strftime(DEFAULT_SERVER_DATE_FORMAT) def _prepare_orderpoint_procurement(self, cr, uid, orderpoint, product_qty, context=None): - return {'name': orderpoint.name, - 'date_planned': self._get_orderpoint_date_planned(cr, uid, orderpoint, datetime.today(), context=context), - 'product_id': orderpoint.product_id.id, - 'product_qty': product_qty, - 'company_id': orderpoint.company_id.id, - 'product_uom': orderpoint.product_uom.id, - 'location_id': orderpoint.location_id.id, - 'origin': orderpoint.name, - 'warehouse_id': orderpoint.warehouse_id.id} + return { + 'name': orderpoint.name, + 'date_planned': self._get_orderpoint_date_planned(cr, uid, orderpoint, datetime.today(), context=context), + 'product_id': orderpoint.product_id.id, + 'product_qty': product_qty, + 'company_id': orderpoint.company_id.id, + 'product_uom': orderpoint.product_uom.id, + 'location_id': orderpoint.location_id.id, + 'origin': orderpoint.name, + 'warehouse_id': orderpoint.warehouse_id.id, + 'orderpoint_id': orderpoint.id, + } def _product_virtual_get(self, cr, uid, order_point): product_obj = self.pool.get('product.product') @@ -378,28 +382,14 @@ class procurement_order(osv.osv): if qty <= 0: continue if op.product_id.type not in ('consu'): - procurement_draft_ids = orderpoint_obj.get_draft_procurements(cr, uid, op. id, context=context) - if procurement_draft_ids: - # Check draft procurement related to this order point - procure_datas = procurement_obj.read( - cr, uid, procurement_draft_ids, ['id', 'product_qty'], context=context) - to_generate = qty - for proc_data in procure_datas: - if to_generate >= proc_data['product_qty']: - self.signal_button_confirm(cr, uid, [proc_data['id']]) - procurement_obj.write(cr, uid, [proc_data['id']], {'origin': op.name}, context=context) - to_generate -= proc_data['product_qty'] - if not to_generate: - break - qty = to_generate + qty -= orderpoint_obj.subtract_procurements(cr, uid, op, context=context) - if qty: + if qty > 0: proc_id = procurement_obj.create(cr, uid, self._prepare_orderpoint_procurement(cr, uid, op, qty, context=context), context=context) self.check(cr, uid, [proc_id]) self.run(cr, uid, [proc_id]) - orderpoint_obj.write(cr, uid, [op.id], {'procurement_id': proc_id}, context=context) offset += len(ids) if use_new_cursor: cr.commit() diff --git a/addons/stock/stock.py b/addons/stock/stock.py index b492496243e..9798c4e24fd 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -3861,15 +3861,17 @@ class stock_warehouse_orderpoint(osv.osv): _name = "stock.warehouse.orderpoint" _description = "Minimum Inventory Rule" - def get_draft_procurements(self, cr, uid, ids, context=None): - if context is None: - context = {} - if not isinstance(ids, list): - ids = [ids] - procurement_obj = self.pool.get('procurement.order') - for orderpoint in self.browse(cr, uid, ids, context=context): - procurement_ids = procurement_obj.search(cr, uid, [('state', 'not in', ('cancel', 'done')), ('product_id', '=', orderpoint.product_id.id), ('location_id', '=', orderpoint.location_id.id)], context=context) - return list(set(procurement_ids)) + def subtract_procurements(self, cr, uid, orderpoint, context=None): + '''This function returns quantity of product that needs to be deducted from the orderpoint computed quantity because there's already a procurement created with aim to fulfill it. + ''' + qty = 0 + for procurement in orderpoint.procurement_ids: + if procurement in ('cancel', 'done'): + continue + for move in procurement.move_ids: + if move.state not in ('draft', 'cancel'): + qty += move.product_qty + return qty def _check_product_uom(self, cr, uid, ids, context=None): ''' @@ -3884,16 +3886,16 @@ class stock_warehouse_orderpoint(osv.osv): return True - def action_view_proc_to_process(self, cr, uid, ids, context=None): + def action_view_proc_to_process(self, cr, uid, ids, context=None): act_obj = self.pool.get('ir.actions.act_window') mod_obj = self.pool.get('ir.model.data') - draft_ids = self.get_draft_procurements(cr, uid, ids, context=context) + proc_ids = self.pool.get('procurement.order').search(cr, uid, [('orderpoint_id', 'in', ids), ('state', 'not in', ('done', 'cancel'))], context=context) result = mod_obj.get_object_reference(cr, uid, 'procurement', 'do_view_procurements') if not result: return False - + result = act_obj.read(cr, uid, [result[1]], context=context)[0] - result['domain'] = "[('id', 'in', [" + ','.join(map(str, draft_ids)) + "])]" + result['domain'] = "[('id', 'in', [" + ','.join(map(str, proc_ids)) + "])]" return result _columns = { @@ -3912,8 +3914,8 @@ class stock_warehouse_orderpoint(osv.osv): "a procurement to bring the forecasted quantity to the Quantity specified as Max Quantity."), 'qty_multiple': fields.integer('Qty Multiple', required=True, help="The procurement quantity will be rounded up to this multiple."), - 'procurement_id': fields.many2one('procurement.order', 'Latest procurement', ondelete="set null"), - 'company_id': fields.many2one('res.company', 'Company', required=True) + 'procurement_ids': fields.one2many('procurement.order', 'orderpoint_id', 'Created Procurements'), + 'company_id': fields.many2one('res.company', 'Company', required=True) } _defaults = { 'active': lambda *a: 1,