[IMP] group push rules by purchase, purchase order line has procurements, one route...
authorJosse Colpaert <jco@openerp.com>
Fri, 13 Sep 2013 09:42:16 +0000 (11:42 +0200)
committerJosse Colpaert <jco@openerp.com>
Fri, 13 Sep 2013 09:42:16 +0000 (11:42 +0200)
bzr revid: jco@openerp.com-20130913094216-lyfj84amuxlcnpq9

addons/purchase/purchase.py
addons/purchase/purchase_view.xml
addons/stock/product.py
addons/stock_location/stock_location.py
addons/stock_location/stock_location_view.xml

index e336e14..4c0d0f1 100644 (file)
@@ -682,7 +682,7 @@ class purchase_order(osv.osv):
             'picking_type_id': type_id, 
         }
 
-    def _prepare_order_line_move(self, cr, uid, order, order_line, picking_id, context=None):
+    def _prepare_order_line_move(self, cr, uid, order, order_line, picking_id, group_id, context=None):
         ''' prepare the stock move data from the PO line '''
         type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'picking_type_in')[1]
         price_unit = order_line.price_unit
@@ -710,6 +710,7 @@ class purchase_order(osv.osv):
             'company_id': order.company_id.id,
             'price_unit': price_unit,
             'picking_type_id': type_id, 
+            'group_id': group_id, 
         }
 
     def _create_pickings(self, cr, uid, order, order_lines, picking_id=False, context=None):
@@ -736,11 +737,19 @@ class purchase_order(osv.osv):
             picking_id = stock_picking.create(cr, uid, self._prepare_order_picking(cr, uid, order, context=context))
         todo_moves = []
         stock_move = self.pool.get('stock.move')
+        
+        new_group = False
+        if any([(x.group_id == False) for x in order_lines]):
+            new_group = self.pool.get("procurement.group").create(cr, uid, {'name':order.name, 'partner_id': order.partner_id.id}, context=context)
+        
+        
         for order_line in order_lines:
             if not order_line.product_id:
                 continue
+            
             if order_line.product_id.type in ('product', 'consu'):
-                move = stock_move.create(cr, uid, self._prepare_order_line_move(cr, uid, order, order_line, picking_id, context=context))
+                group_id = order_line.group_id and order_line.group_id.id or new_group
+                move = stock_move.create(cr, uid, self._prepare_order_line_move(cr, uid, order, order_line, picking_id, group_id, context=context))
                 if order_line.move_dest_id:
                     order_line.move_dest_id.write({'location_id': order.location_id.id})
                 todo_moves.append(move)
@@ -957,6 +966,8 @@ class purchase_order_line(osv.osv):
         'invoiced': fields.boolean('Invoiced', readonly=True),
         'partner_id': fields.related('order_id','partner_id',string='Partner',readonly=True,type="many2one", relation="res.partner", store=True),
         'date_order': fields.related('order_id','date_order',string='Order Date',readonly=True,type="date"),
+        'procurement_ids': fields.one2many('procurement.order', 'purchase_line_id', string='Associated procurements'),
+        'group_id': fields.related('procurement_ids', 'group_id', type='many2one', relation='procurement.group', string='Procurement Group'),
     }
     _defaults = {
         'product_qty': lambda *a: 1.0,
@@ -1115,7 +1126,7 @@ class procurement_rule(osv.osv):
 class procurement_order(osv.osv):
     _inherit = 'procurement.order'
     _columns = {
-        'purchase_id': fields.many2one('purchase.order', 'Purchase Order'),
+        'purchase_line_id': fields.many2one('purchase.order.line', 'Purchase Order'),
     }
 
     def _find_suitable_rule(self, cr, uid, procurement, context=None):
@@ -1133,7 +1144,7 @@ class procurement_order(osv.osv):
         return super(procurement_order, self)._run(cr, uid, procurement, context=context)
 
     def _check(self, cr, uid, procurement, context=None):
-        if procurement.purchase_id and procurement.purchase_id.shipped:  # TOCHECK: does it work for several deliveries?
+        if procurement.purchase_line_id and procurement.purchase_line_id.order_id.shipped:  # TOCHECK: does it work for several deliveries?
             return True
         return super(procurement_order, self)._check(cr, uid, procurement, context=context)
 
@@ -1290,7 +1301,8 @@ class procurement_order(osv.osv):
                     'payment_term_id': partner.property_supplier_payment_term.id or False,
                 }
                 res[procurement.id] = self.create_procurement_purchase_order(cr, uid, procurement, po_vals, line_vals, context=new_context)
-                self.write(cr, uid, [procurement.id], {'purchase_id': res[procurement.id]})
+                procurement_line = self.pool.get('purchase.order').browse(cr, uid, res[procurement.id]).order_line[0].id
+                self.write(cr, uid, [procurement.id], {'purchase_line_id': procurement_line}, context=context)
                 pass_ids += [procurement.id]
         if pass_ids:
             self.message_post(cr, uid, pass_ids, body=_("Draft Purchase Order created"), context=context)
index 5e52053..b23a71b 100644 (file)
             <field name="inherit_id" ref="procurement.procurement_form_view"/>
             <field name="arch" type="xml">
                 <xpath expr="//field[@name='origin']" position="after">
-                    <field name="purchase_id"/>
+                    <field name="purchase_line_id"/>
                 </xpath>
             </field>
         </record>
index fdfbf0e..9e436dd 100644 (file)
@@ -216,7 +216,7 @@ class product_product(osv.osv):
         'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one'),
         'warehouse_id': fields.dummy(string='Warehouse', relation='stock.warehouse', type='many2one'),
         'orderpoint_ids': fields.one2many('stock.warehouse.orderpoint', 'product_id', 'Minimum Stock Rules'),
-        'route_ids': fields.many2many('stock.location.route', 'stock_location_route_product', 'product_id', 'route_id', 'Routes', 
+        'route_ids': fields.many2many('stock.location.route', 'stock_route_product', 'product_id', 'route_id', 'Routes', 
                                     help="Depending on the modules installed, this will allow you to define the route of the product: whether it will be bought, manufactured, MTO/MTS,..."),
     }
 
index 9468152..cf6cd4b 100644 (file)
@@ -153,7 +153,7 @@ class procurement_order(osv.osv):
         '''we try to first find a rule among the ones defined on the procurement order group and if none is found, we try on the routes defined for the product, and finally we fallback on the default behavior'''
         categ_obj = self.pool.get("product.category")
         categ_id = procurement.product_id.categ_id.id
-        route_ids = [x.id for x in procurement.route_ids] + [x.id for x in procurement.product_id.route_ids] + categ_obj.calculate_total_routes(cr, uid, [categ_id], False, False, context=context)[categ_id]
+        route_ids = [x.id for x in procurement.product_id.route_ids + procurement.product_id.categ_id.total_route_ids]
         res = self.pool.get('procurement.rule').search(cr, uid, domain + [('route_id', 'in', route_ids)], order = 'route_sequence, sequence', context=context)
         if not res:
             res = self.pool.get('procurement.rule').search(cr, uid, domain + [('route_id', '=', False)], order='sequence', context=context)
@@ -186,7 +186,7 @@ class product_removal_strategy(osv.osv):
 class product_product(osv.osv):
     _inherit = 'product.product'
     _columns = {
-        'route_ids': fields.many2many('stock.location.route', 'stock_location_route_product', 'product_id', 'route_id', 'Routes', domain="[('product_selectable', '=', True)]"), #Adds domain
+        'route_ids': fields.many2many('stock.location.route', 'stock_route_product', 'product_id', 'route_id', 'Routes', domain="[('product_selectable', '=', True)]"), #Adds domain
     }
 
 class product_category(osv.osv):
@@ -249,7 +249,7 @@ class stock_move(osv.osv):
         for move in moves:
             if not move.move_dest_id:
                 categ_id = move.product_id.categ_id.id
-                routes = [x.id for x in move.product_id.route_ids] + categ_obj.calculate_total_routes(cr, uid, [categ_id], False, False, context=context)[categ_id]
+                routes = [x.id for x in move.product_id.route_ids + move.product_id.categ_id.total_route_ids]
                 rules = push_obj.search(cr, uid, [('route_id', 'in', routes), ('location_from_id', '=', move.location_dest_id.id)], context=context)
                 if rules: 
                     rule = push_obj.browse(cr, uid, rules[0], context=context)
index 2607883..a8cd0ce 100644 (file)
                        </field>
                </record>
 
-               <record id="product_normal_form_inherit_location" model="ir.ui.view">
-                       <field name="name">product.product.form</field>
-                       <field name="model">product.product</field>
-                       <field name="inherit_id" ref="product.product_normal_form_view" />
-                       <field name="arch" type="xml">
-                               <group name="inventory" position="after">
-                                       <group string="Routes">
-                                               <field name="route_ids" colspan="4" nolabel="1" widget="many2many_tags"/>
-                                       </group>
-                               </group>
-                       </field>
-               </record>
-
-
 
                <record id="product_category_form_view_inherit" model="ir.ui.view">
                        <field name="name">product.category.form</field>