From e0a5a141ded238d514bb40ffbe5cdf00ffd33a04 Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Mon, 1 Jul 2013 08:30:34 +0200 Subject: [PATCH] [IMP] routing and procurements bzr revid: fp@tinyerp.com-20130701063034-0sc896udy0qz32n1 --- addons/procurement/procurement.py | 11 ++++- addons/stock/procurement.py | 52 ++++++++++++++--------- addons/stock/stock.py | 2 +- addons/stock_location/stock_location.py | 56 ++++++++++++------------- 4 files changed, 70 insertions(+), 51 deletions(-) diff --git a/addons/procurement/procurement.py b/addons/procurement/procurement.py index 8fdaa349ca4..8bf77f04ff8 100644 --- a/addons/procurement/procurement.py +++ b/addons/procurement/procurement.py @@ -146,7 +146,13 @@ class procurement_order(osv.osv): def run(self, cr, uid, ids, context=None): for procurement in self.browse(cr, uid, ids, context=context or {}): if procurement.procure_method=='make_to_order': - result = self._run(cr, uid, procurement.id, context=context or {}) + rule = self._assign(cr, uid, procurement, context=context) + if rule: + self.write(cr, uid, [procurement.id], {'rule_id', rule.id}, context=context) + procurement.refresh() + self._run(cr, uid, procurement, context=context or {}) + else: + self.message_post(cr, uid, [procurement.id], body=_('No rule matching this procurement'), context=context) else: result = True if result: @@ -167,6 +173,9 @@ class procurement_order(osv.osv): # # Method to overwrite in different procurement modules # + def _assign(self, cr, uid, procurement, context=None): + return False + def _run(self, cr, uid, procurement, context=None): return True diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index c1268902d96..ee7f540b0b6 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -47,38 +47,50 @@ class procurement_rule(osv.osv): class procurement_order(osv.osv): _inherit = "procurement.order" _columns = { + 'location_id': fields.many2one('stock.location', 'Destination Location') 'move_id': fields.many2one('stock.move', 'Move') 'move_dest_id': fields.many2one('stock.move', 'Destination Move') } + def _assign(self, cr, uid, procurement, context=None): + rule_obj = self.pool.get('procurement.rule') + res = rule_obj.search(cr, uid, [('location_id','=',procurement.location_id.id)], context=context) + if not res: + return super(procurement_order, self)._assign(cr, uid, procurement, context=context) + return rule_obj.browse(cr, uid, res[0], context=context) + + def _run_move_create(self, cr, uid, procurement, context=None) + return { + 'name': procurement.name, + 'company_id': procurement.company_id.id, + 'product_id': procurement.product_id.id, + 'date': procurement.date_planned, + 'product_qty': procurement.product_qty, + 'product_uom': procurement.product_uom.id, + 'product_uos_qty': (procurement.product_uos and procurement.product_uos_qty)\ + or procurement.product_qty, + 'product_uos': (procurement.product_uos and procurement.product_uos.id)\ + or procurement.product_uom.id, + 'partner_id': procurement.group_id and procurement.group_id.partner_id and \ + procurement.group_id.partner_id.id or False, + 'location_id': procurement.rule_id.location_src_id.id, + 'location_dest_id': procurement.rule_id.location_id.id, + 'move_dest_id': procurement.move_dest_id and procurement.move_dest_id.id or False, + 'cancel_cascade': procurement.rule_id and procurement.rule_id.cancel_cascade or False, + 'group_id': procurement.group_id and procurement.group_id.id or False, + } def _run(self, cr, uid, procurement, context=None): + context=context or {} if procurement.rule_id and procurement.rule_id.action == 'move': if not procurement.rule_id.location_src_id: self.message_post(cr, uid, [procurement.id], body=_('No source location defined!'), context=context) return False move_obj = self.pool.get('stock.move') - move_id = move_obj.create(cr, uid, { - 'name': procurement.name, - 'company_id': procurement.company_id.id, - 'product_id': procurement.product_id.id, - 'date': procurement.date_planned, - 'product_qty': procurement.product_qty, - 'product_uom': procurement.product_uom.id, - 'product_uos_qty': (procurement.product_uos and procurement.product_uos_qty)\ - or procurement.product_qty, - 'product_uos': (procurement.product_uos and procurement.product_uos.id)\ - or procurement.product_uom.id, - 'partner_id': procurement.group_id and procurement.group_id.partner_id and \ - procurement.group_id.partner_id.id or False, - 'location_id': procurement.rule_id.location_src_id.id, - 'location_dest_id': procurement.rule_id.location_id.id, - 'move_dest_id': procurement.move_dest_id and procurement.move_dest_id.id or False, - 'cancel_cascade': procurement.rule_id and procurement.rule_id.cancel_cascade or False, - 'group_id': procurement.group_id and procurement.group_id.id or False, - }) + move_dict = self._run_move_create(cr, uid, procurement, context=context) + move_id = move_obj.create(cr, uid, move_dict, context=context) move_obj.button_confirm(cr,uid, [move_id], context=context) self.write(cr, uid, [procurement.id], {'move_id': move_id}, context=context) - return True + return move_id return super(procurement_order, self)._run(cr, uid, procurement, context) def _check(self, cr, uid, procurement, context=None): diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 3f79d327282..c5d676dee5e 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1970,7 +1970,7 @@ class stock_move(osv.osv): states[state].append(move.id) if not move.picking_id: - # TODO: Put the move in the right picking according to groups + # TODO: Put the move in the right picking according to group_id pass for state, write_ids in states.items(): diff --git a/addons/stock_location/stock_location.py b/addons/stock_location/stock_location.py index dba59250ca6..57e86704363 100644 --- a/addons/stock_location/stock_location.py +++ b/addons/stock_location/stock_location.py @@ -21,6 +21,7 @@ from openerp.osv import fields,osv + class stock_location_route(osv.osv): _name = 'stock.location.route' _description = "Inventory Routes" @@ -28,9 +29,8 @@ class stock_location_route(osv.osv): _columns = { 'name': fields.char('Route Name', required=True), 'sequence': fields.integer('Sequence'), - 'pull_ids': fields.one2many('stock.location.pull', 'route_id', 'Pull Rules'), + 'pull_ids': fields.one2many('procurement.rule', 'route_id', 'Pull Rules'), 'push_ids': fields.one2many('stock.location.path', 'route_id', 'Push Rules'), - 'journal_id': fields.many2one('stock.journal','Journal'), } _defaults = { @@ -102,12 +102,13 @@ class stock_location_path(osv.osv): return move_id -class product_pulled_flow(osv.osv): - _inherit = 'product.pulled.flow' +class procurement.rule(osv.osv): + _inherit = 'procurement.rule' _columns = { - 'route_id': fields.many2one('stock.location.route', 'Route'), + 'route_id': fields.many2one('stock.location.route', 'Route', + help="If route_id is False, the route is global"), 'delay': fields.integer('Number of Hours'), - 'location_id': fields.many2one('stock.location','Destination Location', required=True, help="Is the destination location that needs supplying"), + 'route_id': fields.many2one('stock.location.route', 'Route'), '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."), 'type_proc': fields.selection([('produce','Produce'),('buy','Buy'),('move','Move')], 'Type of Procurement', required=True), 'partner_address_id': fields.many2one('res.partner', 'Partner Address'), @@ -122,32 +123,29 @@ class product_pulled_flow(osv.osv): 'type_proc': 'move', 'invoice_state': 'none', } - # FP Note: implement this - def _apply(self, cr, uid, rule, move, context=None): + + +class procurement_order(osv.osv): + def _run_move_create(self, cr, uid, procurement, move, context=None): + d = super(procurement_order, self)._run_move_create(cr, uid, procurement, move, context=context) newdate = (datetime.strptime(move.date, '%Y-%m-%d %H:%M:%S') - relativedelta(days=rule.delay or 0)).strftime('%Y-%m-%d %H:%M:%S') - - proc_obj = self.pool.get('procurement.order') - proc_id = proc_obj.create(cr, uid, { - 'name': move.name, - 'origin': move.origin, - - 'company_id': move.company_id and move.company_id.id or False, + d.update({ 'date_planned': newdate, - - 'product_id': move.product_id.id, - 'product_qty': move.product_qty, - 'product_uom': move.product_uom.id, - 'product_uos_qty': (move.product_uos and move.product_uos_qty)\ - or move.product_qty, - 'product_uos': (move.product_uos and move.product_uos.id)\ - or move.product_uom.id, - 'location_id': move.location_id.id, 'procure_method': rule.procure_method, }) - proc_obj.signal_button_confirm(cr, uid, [proc_id]) - msg = _('Pulled from another location.') - self.message_post(cr, uid, [proc.id], body=msg, context=context) - return True + return d + + # TODO: implement using routes on products + def _assign(self, cr, uid, procurement, context=None): + if procurement.location_id: + rule_obj = self.pool.get('procurement.rule') + route_ids =[False] + [x.id for x in procurement.product_id.route_ids] + res = rule_obj.search(cr, uid, [('location_id','=',procurement.location_id.id),('route_id', 'in', route_ids)], context=context) + if not res: + return False + return res[1] + return super(procurement_order, self)._assign(cr, uid, procurement, context=context) + class product_putaway_strategy(osv.osv): _name = 'product.putaway' @@ -204,7 +202,7 @@ class stock_move(osv.osv): found = False for rule in route.pull_ids: if rule.location_id.id == move.location_id.id: - self.pool.get('stock.location.pull')._apply(cr, uid, rule, move, context=context) + self.pool.get('procurement.rule')._apply(cr, uid, rule, move, context=context) found = True break if found: break