From a49dca23de9f86a6fdd23b1089659b3d61d2d777 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 30 Apr 2014 09:40:22 +0200 Subject: [PATCH 1/4] [IMP] Put partner_id from rule on move when necessary bzr revid: jco@openerp.com-20140430074022-cekktpwoucv1rhcd --- addons/stock/procurement.py | 2 +- addons/stock/stock.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index bdae93a4054..99ba231daeb 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -174,7 +174,7 @@ class procurement_order(osv.osv): 'product_uom_qty': qty_left, 'product_uos_qty': (procurement.product_uos and qty_uos_left) or qty_left, '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, + 'partner_id': procurement.rule_id.partner_address_id.id or (procurement.group_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, diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 7d3dba2f47b..32f28bb7722 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1985,7 +1985,7 @@ class stock_move(osv.osv): 'origin': move.origin, 'company_id': move.company_id and move.company_id.id or False, 'move_type': move.group_id and move.group_id.move_type or 'one', - 'partner_id': move.group_id and move.group_id.partner_id and move.group_id.partner_id.id or False, + 'partner_id': move.partner_id.id or False, 'picking_type_id': move.picking_type_id and move.picking_type_id.id or False, } pick = pick_obj.create(cr, uid, values, context=context) From c25cd043f3435f98b8c761b429328a4c0494c0ee Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 30 Apr 2014 10:00:37 +0200 Subject: [PATCH 2/4] [IMP] If no company_id given in procurement rules, you should use locations or even the company of the procurement bzr revid: jco@openerp.com-20140430080037-nqyfrsrtxul839sp --- addons/stock/procurement.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index 99ba231daeb..98eef0f55c0 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -167,7 +167,7 @@ class procurement_order(osv.osv): qty_uos_left = max(procurement.product_uos_qty - already_done_qty_uos, 0) vals = { 'name': procurement.name, - 'company_id': procurement.company_id.id, + 'company_id': procurement.rule_id.company_id.id or procurement.rule_id.location_src_id.company_id.id or procurement.rule_id.location_id.company_id.id or procurement.company_id.id, 'product_id': procurement.product_id.id, 'product_qty': qty_left, 'product_uom': procurement.product_uom.id, From 0ecc81b22c56614117e8fa27ee1e685cbb03389d Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 30 Apr 2014 13:54:55 +0200 Subject: [PATCH 3/4] [IMP] Create MTO pull rule for resupply with delivery one step bzr revid: jco@openerp.com-20140430115455-1fhssek8z7s28nck --- addons/stock/procurement.py | 2 +- addons/stock/stock.py | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/addons/stock/procurement.py b/addons/stock/procurement.py index 98eef0f55c0..d2a77e78eaf 100644 --- a/addons/stock/procurement.py +++ b/addons/stock/procurement.py @@ -185,7 +185,7 @@ class procurement_order(osv.osv): 'picking_type_id': procurement.rule_id.picking_type_id.id, 'group_id': group_id, 'route_ids': [(4, x.id) for x in procurement.route_ids], - 'warehouse_id': procurement.rule_id.propagate_warehouse_id and procurement.rule_id.propagate_warehouse_id.id or procurement.rule_id.warehouse_id.id, + 'warehouse_id': procurement.rule_id.propagate_warehouse_id.id or procurement.rule_id.warehouse_id.id, 'date': newdate, 'date_expected': newdate, 'propagate': procurement.rule_id.propagate, diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 32f28bb7722..29248051202 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -2822,6 +2822,9 @@ class stock_warehouse(osv.osv): output_loc = wh.wh_output_stock_loc_id if wh.delivery_steps == 'ship_only': output_loc = wh.lot_stock_id + # Create extra MTO rule + mto_pull_vals = self._get_mto_pull_rule(cr, uid, wh, [(output_loc, transit_location, wh.out_type_id.id)], context=context) + pull_obj.create(cr, uid, mto_pull_vals, context=context) inter_wh_route_vals = self._get_inter_wh_route(cr, uid, warehouse, wh, context=context) inter_wh_route_id = route_obj.create(cr, uid, vals=inter_wh_route_vals, context=context) values = [(output_loc, transit_location, wh.out_type_id.id, wh), (transit_location, input_loc, warehouse.in_type_id.id, warehouse)] @@ -2905,7 +2908,7 @@ class stock_warehouse(osv.osv): 'route_id': new_route_id, 'action': 'move', 'picking_type_id': pick_type_id, - 'procure_method': 'make_to_order', + 'procure_method': warehouse.lot_stock_id.id != from_loc.id and 'make_to_order' or 'make_to_stock', # Ship only is MTS 'warehouse_id': supplied_warehouse.id, 'propagate_warehouse_id': warehouse.id, }) @@ -2940,7 +2943,7 @@ class stock_warehouse(osv.osv): first_rule = False return push_rules_list, pull_rules_list - def _get_mto_pull_rule(self, cr, uid, warehouse, values, context=None): + def _get_mto_route(self, cr, uid, context=None): route_obj = self.pool.get('stock.location.route') data_obj = self.pool.get('ir.model.data') try: @@ -2950,7 +2953,21 @@ class stock_warehouse(osv.osv): mto_route_id = mto_route_id and mto_route_id[0] or False if not mto_route_id: raise osv.except_osv(_('Error!'), _('Can\'t find any generic Make To Order route.')) + return mto_route_id + + def _check_remove_mto_resupply_rules(self, cr, uid, warehouse, context=None): + """ + Checks that the moves from the different + """ + pull_obj = self.pool.get('procurement.rule') + mto_route_id = self._get_mto_route(cr, uid, context=context) + rules = pull_obj.search(cr, uid, ['&', ('location_src_id', '=', warehouse.lot_stock_id.id), ('location_id.usage', '=', 'transit')], context=context) + pull_obj.unlink(cr, uid, rules, context=context) + + + def _get_mto_pull_rule(self, cr, uid, warehouse, values, context=None): + mto_route_id = self._get_mto_route(cr, uid, context=context) from_loc, dest_loc, pick_type_id = values[0] return { 'name': self._format_rulename(cr, uid, warehouse, from_loc, dest_loc, context=context) + _(' MTO'), @@ -3006,6 +3023,7 @@ class stock_warehouse(osv.osv): #create MTO pull rule and link it to the generic MTO route mto_pull_vals = self._get_mto_pull_rule(cr, uid, warehouse, values, context=context) mto_pull_id = pull_obj.create(cr, uid, mto_pull_vals, context=context) + #create a route for cross dock operations, that can be set on products and product categories route_name, values = routes_dict['crossdock'] @@ -3311,6 +3329,7 @@ class stock_warehouse(osv.osv): to_remove_route_ids = route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id), ('supplier_wh_id', 'in', list(to_remove_wh_ids))], context=context) if to_remove_route_ids: route_obj.unlink(cr, uid, to_remove_route_ids, context=context) + else: #not implemented pass From 67afb31970c64b42051d561ce323f57f328f953e Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 30 Apr 2014 18:45:26 +0200 Subject: [PATCH 4/4] [IMP] When changing number of steps, it will also change the resupply rules of the warehouses that are resupplied from this warehouse and the associated mto rules bzr revid: jco@openerp.com-20140430164526-jiq8zh1lt3x9c3mc --- addons/stock/stock.py | 61 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 29248051202..d020fb8d6a1 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -3286,6 +3286,63 @@ class stock_warehouse(osv.osv): #change the mto pull rule name pull_obj.write(cr, uid, warehouse.mto_pull_id.id, {'name': warehouse.mto_pull_id.name.replace(warehouse.name, name, 1)}, context=context) + def _check_delivery_resupply(self, cr, uid, warehouse, new_location, change_to_multiple, context=None): + """ + Will check if the resupply routes from this warehouse follow the changes of number of delivery steps + """ + #Check routes that are being delivered by this warehouse and change the rule going to transit location + route_obj = self.pool.get("stock.location.route") + pull_obj = self.pool.get("procurement.rule") + routes = route_obj.search(cr, uid, [('supplier_wh_id','=', warehouse.id)], context=context) + pulls= pull_obj.search(cr, uid, ['&', ('route_id', 'in', routes), ('location_id.usage', '=', 'transit')], context=context) + if pulls: + pull_obj.write(cr, uid, pulls, {'location_src_id': new_location, 'procure_method': change_to_multiple and "make_to_order" or "make_to_stock"}, context=context) + # Create or clean MTO rules + mto_route_id = self._get_mto_route(cr, uid, context=context) + if not change_to_multiple: + # If single delivery we should create the necessary MTO rules for the resupply + # pulls = pull_obj.search(cr, uid, ['&', ('route_id', '=', mto_route_id), ('location_id.usage', '=', 'transit'), ('location_src_id', '=', warehouse.lot_stock_id.id)], context=context) + pull_recs = pull_obj.browse(cr, uid, pulls, context=context) + transfer_locs = list(set([x.location_id for x in pull_recs])) + vals = [(warehouse.lot_stock_id , x, warehouse.out_type_id.id) for x in transfer_locs] + mto_pull_vals = self._get_mto_pull_rule(cr, uid, warehouse, vals, context=context) + pull_obj.create(cr, uid, mto_pull_vals, context=context) + else: + # We need to delete all the MTO pull rules, otherwise they risk to be used in the system + pulls = pull_obj.search(cr, uid, ['&', ('route_id', '=', mto_route_id), ('location_id.usage', '=', 'transit'), ('location_src_id', '=', warehouse.lot_stock_id.id)], context=context) + if pulls: + pull_obj.unlink(cr, uid, pulls, context=context) + + def _check_reception_resupply(self, cr, uid, warehouse, new_location, context=None): + """ + Will check if the resupply routes to this warehouse follow the changes of number of reception steps + """ + #Check routes that are being delivered by this warehouse and change the rule coming from transit location + route_obj = self.pool.get("stock.location.route") + pull_obj = self.pool.get("procurement.rule") + routes = route_obj.search(cr, uid, [('supplied_wh_id','=', warehouse.id)], context=context) + pulls= pull_obj.search(cr, uid, ['&', ('route_id', 'in', routes), ('location_src_id.usage', '=', 'transit')]) + if pulls: + pull_obj.write(cr, uid, pulls, {'location_id': new_location}, context=context) + + def _check_resupply(self, cr, uid, warehouse, reception_new, delivery_new, context=None): + if reception_new: + old_val = warehouse.reception_steps + new_val = reception_new + change_to_one = (old_val != 'one_step' and new_val == 'one_step') + change_to_multiple = (old_val == 'one_step' and new_val != 'one_step') + if change_to_one or change_to_multiple: + new_location = change_to_one and warehouse.lot_stock_id.id or warehouse.wh_input_stock_loc_id.id + self._check_reception_resupply(cr, uid, warehouse, new_location, context=context) + if delivery_new: + old_val = warehouse.delivery_steps + new_val = delivery_new + change_to_one = (old_val != 'ship_only' and new_val == 'ship_only') + change_to_multiple = (old_val == 'ship_only' and new_val != 'ship_only') + if change_to_one or change_to_multiple: + new_location = change_to_one and warehouse.lot_stock_id.id or warehouse.wh_output_stock_loc_id.id + self._check_delivery_resupply(cr, uid, warehouse, new_location, change_to_multiple, context=context) + def write(self, cr, uid, ids, vals, context=None): if context is None: context = {} @@ -3293,7 +3350,6 @@ class stock_warehouse(osv.osv): ids = [ids] seq_obj = self.pool.get('ir.sequence') route_obj = self.pool.get('stock.location.route') - context_with_inactive = context.copy() context_with_inactive['active_test'] = False for warehouse in self.browse(cr, uid, ids, context=context_with_inactive): @@ -3303,6 +3359,8 @@ class stock_warehouse(osv.osv): self.switch_location(cr, uid, warehouse.id, warehouse, vals.get('reception_steps', False), vals.get('delivery_steps', False), context=context) # switch between route self.change_route(cr, uid, ids, warehouse, vals.get('reception_steps', False), vals.get('delivery_steps', False), context=context_with_inactive) + # Check if we need to change something to resupply warehouses and associated MTO rules + self._check_resupply(cr, uid, warehouse, vals.get('reception_steps'), vals.get('delivery_steps'), context=context) warehouse.refresh() if vals.get('code') or vals.get('name'): name = warehouse.name @@ -3329,7 +3387,6 @@ class stock_warehouse(osv.osv): to_remove_route_ids = route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id), ('supplier_wh_id', 'in', list(to_remove_wh_ids))], context=context) if to_remove_route_ids: route_obj.unlink(cr, uid, to_remove_route_ids, context=context) - else: #not implemented pass