fixed bug #310530
[odoo/odoo.git] / addons / analytic_user_function / analytic_user_function.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 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 from osv import fields,osv
23 from osv import orm
24
25 class analytic_user_funct_grid(osv.osv):
26
27     _name="analytic_user_funct_grid"
28     _description= "Relation table between users and products on a analytic account"
29     _columns={
30         'user_id': fields.many2one("res.users","User",required=True,),
31         'product_id': fields.many2one("product.product","Product",required=True,),
32         'account_id': fields.many2one("account.analytic.account", "Analytic Account",required=True,),
33         }
34
35 analytic_user_funct_grid()
36
37
38 class account_analytic_account(osv.osv):
39
40     _inherit = "account.analytic.account"
41     _columns = {
42         'user_product_ids' : fields.one2many('analytic_user_funct_grid', 'account_id', 'Users/Products Rel.'),
43     }
44
45 account_analytic_account()
46
47
48 class hr_analytic_timesheet(osv.osv):
49
50     _inherit = "hr.analytic.timesheet"
51
52
53     # Look in account, if no value for the user => look in parent until there is no more parent to look
54     # Take the first found... if nothing found => return False
55     def _get_related_user_account_recursiv(self,cr,uid,user_id,account_id):
56         
57         temp=self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
58         account=self.pool.get('account.analytic.account').browse(cr,uid,account_id)
59         if temp:
60             return temp
61         else:
62             if account.parent_id:
63                 return self._get_related_user_account_recursiv(cr,uid,user_id,account.parent_id.id)
64             else:
65                 return False
66                 
67                 
68     def on_change_account_id(self, cr, uid, ids,user_id, account_id, unit_amount=0):
69         #{'value': {'to_invoice': False, 'amount': (-162.0,), 'product_id': 7, 'general_account_id': (5,)}}
70         res = {}
71         if not (account_id):
72             #avoid a useless call to super
73             return res 
74
75         if not (user_id):
76             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)
77
78         #get the browse record related to user_id and account_id
79         temp = self._get_related_user_account_recursiv(cr,uid,user_id,account_id)
80         # temp = self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
81         if not temp:
82             #if there isn't any record for this user_id and account_id
83             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)
84         else:
85             #get the old values from super and add the value from the new relation analytic_user_funct_grid
86             r = self.pool.get('analytic_user_funct_grid').browse(cr, uid, temp)[0]
87             res.setdefault('value',{})
88             res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)['value']
89             res['value']['product_id'] = r.product_id.id
90             res['value']['product_uom_id'] = r.product_id.product_tmpl_id.uom_id.id
91
92             #the change of product has to impact the amount, uom and general_account_id
93             a = r.product_id.product_tmpl_id.property_account_expense.id
94             if not a:
95                 a = r.product_id.categ_id.property_account_expense_categ.id
96             if not a:
97                 raise osv.except_osv('Error !',
98                         'There is no expense account define ' \
99                                 'for this product: "%s" (id:%d)' % \
100                                 (r.product_id.name, r.product_id.id,))
101             amount = unit_amount *  self.pool.get('product.uom')._compute_price(cr, uid,
102                     r.product_id.uom_id.id, r.product_id.standard_price, False)
103             res ['value']['amount']= - round(amount, 2)
104             res ['value']['general_account_id']= a
105         return res
106
107     def on_change_user_id(self, cr, uid, ids,user_id, account_id, unit_amount=0):
108         res = {}
109         if not (user_id):
110             #avoid a useless call to super
111             return res 
112
113         #get the old values from super
114         res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids,user_id)
115
116         if account_id:
117             #get the browse record related to user_id and account_id
118             # temp = self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
119             temp = self._get_related_user_account_recursiv(cr,uid,user_id,account_id)
120             if temp:
121                 #add the value from the new relation analytic_user_funct_grid
122                 r = self.pool.get('analytic_user_funct_grid').browse(cr, uid, temp)[0]
123                 res['value']['product_id'] = r.product_id.id    
124
125                 #the change of product has to impact the amount, uom and general_account_id
126                 a = r.product_id.product_tmpl_id.property_account_expense.id
127                 if not a:
128                     a = r.product_id.categ_id.property_account_expense_categ.id
129                 if not a:
130                     raise osv.except_osv('Error !',
131                             'There is no expense account define ' \
132                                     'for this product: "%s" (id:%d)' % \
133                                     (r.product_id.name, r.product_id.id,))
134                 amount = unit_amount * r.product_id.uom_id._compute_price(cr, uid,
135                         r.product_id.uom_id.id, r.product_id.standard_price, False)
136                 res ['value']['amount']= - round(amount, 2)
137                 res ['value']['general_account_id']= a
138         return res
139
140 hr_analytic_timesheet()
141
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
143