[FIX] Sale_*: Misc improvements (tooltips,strings...etc)
[odoo/odoo.git] / addons / sale_mrp / 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 Affero 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 Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero 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     _inherit = 'mrp.production'
27
28     def _ref_calc(self, cr, uid, ids, field_names=None, arg=False, context=None):
29         """ Finds reference of sales order for production order.
30         @param field_names: Names of fields.
31         @param arg: User defined arguments
32         @return: Dictionary of values.
33         """
34         res = {}
35         if context is None:
36             context = {}
37         if not field_names:
38             field_names = []
39         for id in ids:
40             res[id] = {}.fromkeys(field_names, False)
41         for f in field_names:
42             field_name = False
43             if f == 'sale_name':
44                 field_name = 'name'
45             if f == 'sale_ref':
46                 field_name = 'client_order_ref'
47             for key, value in self._get_sale_ref(cr, uid, ids, field_name).items():
48                 res[key][f] = value
49         return res
50
51     def _get_sale_ref(self, cr, uid, ids, field_name=False):
52         move_obj = self.pool.get('stock.move')
53
54         def get_parent_move(move_id):
55             move = move_obj.browse(cr, uid, move_id)
56             if move.move_dest_id:
57                 return get_parent_move(move.move_dest_id.id)
58             return move_id
59
60         res = {}
61         productions = self.browse(cr, uid, ids)
62         for production in productions:
63             res[production.id] = False
64             if production.move_prod_id:
65                 parent_move_line = get_parent_move(production.move_prod_id.id)
66                 if parent_move_line:
67                     move = move_obj.browse(cr, uid, parent_move_line)
68                     if field_name == 'name':
69                         res[production.id] = move.sale_line_id and move.sale_line_id.order_id.name or False
70                     if field_name == 'client_order_ref':
71                         res[production.id] = move.sale_line_id and move.sale_line_id.order_id.client_order_ref or False
72         return res
73
74     _columns = {
75         'sale_name': fields.function(_ref_calc, method=True, multi='sale_name', type='char', string='Sales Name', help='Indicate the name of sales order.'),
76         'sale_ref': fields.function(_ref_calc, method=True, multi='sale_name', type='char', string='Sales Reference', help='Indicate the Customer Reference from sales order.'),
77     }
78
79 mrp_production()
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: