[REF] purchase: search view of purchase order and form view of merge order wizard
[odoo/odoo.git] / addons / sale / mrp.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution    
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24
25 class mrp_production(osv.osv):
26     
27     _inherit = 'mrp.production'
28     
29     def _ref_calc(self, cr, uid, ids, field_names=None, arg=False, context={}):
30         if not field_names:
31             field_names=[]
32         res = {}
33         for id in ids:
34             res[id] = {}.fromkeys(field_names, False)
35         for f in field_names:  
36             field_name = False          
37             if f=='sale_name':
38                 field_name = 'name'
39             if f=='sale_ref':
40                 field_name = 'client_order_ref'                           
41             for key, value in self._get_sale_ref(cr, uid, ids, field_name).items():
42                 res[key][f] = value
43         return res
44         
45     def _get_sale_ref(self, cr, uid, ids, field_name=False):
46         move_obj=self.pool.get('stock.move')
47         
48         def get_parent_move(move_id):
49             move = move_obj.browse(cr, uid, move_id)
50             if move.move_dest_id:
51                 return get_parent_move(move.move_dest_id.id)
52             return move_id
53             
54         productions = self.read(cr, uid, ids, ['id','move_prod_id'])       
55         res={}
56         for production in productions:
57             res[production['id']] = False
58             if production.get('move_prod_id',False):
59                 parent_move_line = get_parent_move(production['move_prod_id'][0])
60                 if parent_move_line:
61                     move = move_obj.browse(cr,uid,parent_move_line)                    
62                     if field_name == 'name':
63                         res[production['id']] = move.sale_line_id and move.sale_line_id.order_id.name or False
64                     if field_name=='client_order_ref':
65                         res[production['id']] = move.sale_line_id and move.sale_line_id.order_id.client_order_ref or False
66         return res
67     
68 mrp_production()