[IMP] analytic_user_function: Improved on_change_user_id method
[odoo/odoo.git] / addons / analytic_user_function / analytic_user_function.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields,osv
23 from osv import orm
24 from tools.translate import _
25
26 class analytic_user_funct_grid(osv.osv):
27
28     _name="analytic_user_funct_grid"
29     _description= "Relation table between users and products on a analytic account"
30     _columns={
31         'user_id': fields.many2one("res.users", "User", required=True,),
32         'product_id': fields.many2one("product.product", "Product", required=True,),
33         'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,),
34         }
35
36 analytic_user_funct_grid()
37
38
39 class account_analytic_account(osv.osv):
40
41     _inherit = "account.analytic.account"
42     _columns = {
43         'user_product_ids': fields.one2many('analytic_user_funct_grid', 'account_id', 'Users/Products Rel.'),
44     }
45
46 account_analytic_account()
47
48
49 class hr_analytic_timesheet(osv.osv):
50
51     _inherit = "hr.analytic.timesheet"
52
53
54     # Look in account, if no value for the user => look in parent until there is no more parent to look
55     # Take the first found... if nothing found => return False
56     def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id):
57
58         temp=self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
59         account=self.pool.get('account.analytic.account').browse(cr, uid, account_id)
60         if temp:
61             return temp
62         else:
63             if account.parent_id:
64                 return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id)
65             else:
66                 return False
67
68
69     def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0.0):
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         if not temp:
81             #if there isn't any record for this user_id and account_id
82             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
83         else:
84             #get the old values from super and add the value from the new relation analytic_user_funct_grid
85             r = self.pool.get('analytic_user_funct_grid').browse(cr, uid, temp)[0]
86             res.setdefault('value',{})
87             res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)['value']
88             res['value']['product_id'] = r.product_id.id
89             res['value']['product_uom_id'] = r.product_id.product_tmpl_id.uom_id.id
90
91             #the change of product has to impact the amount, uom and general_account_id
92             a = r.product_id.product_tmpl_id.property_account_expense.id
93             if not a:
94                 a = r.product_id.categ_id.property_account_expense_categ.id
95             if not a:
96                 raise osv.except_osv(_('Error !'),
97                         _('There is no expense account define ' \
98                                 'for this product: "%s" (id:%d)') % \
99                                 (r.product_id.name, r.product_id.id,))
100             # Compute based on pricetype
101             if unit_amount:
102                 amount_unit = self.on_change_unit_amount(cr, uid, ids,
103                             r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
104                 amount = unit_amount *  amount_unit
105                 res ['value']['amount']= - round(amount, 2)
106             res ['value']['general_account_id']= a
107         return res
108
109     def on_change_user_id(self, cr, uid, ids, user_id, account_id, unit_amount=0.0):
110         res = {}
111         if not (user_id):
112             #avoid a useless call to super
113             return res
114
115         #get the old values from super
116         res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids, user_id)
117
118         if account_id:
119             #get the browse record related to user_id and account_id
120             temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
121             if temp:
122                 #add the value from the new relation analytic_user_funct_grid
123                 r = self.pool.get('analytic_user_funct_grid').browse(cr, uid, temp)[0]
124                 res['value']['product_id'] = r.product_id.id
125
126                 #the change of product has to impact the amount, uom and general_account_id
127                 a = r.product_id.product_tmpl_id.property_account_expense.id
128                 if not a:
129                     a = r.product_id.categ_id.property_account_expense_categ.id
130                 if not a:
131                     raise osv.except_osv(_('Error !'),
132                             _('There is no expense account define ' \
133                                     'for this product: "%s" (id:%d)') % \
134                                     (r.product_id.name, r.product_id.id,))
135                 # Compute based on pricetype
136                 if unit_amount:
137                     amount_unit = self.on_change_unit_amount(cr, uid, ids,
138                         r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
139
140                     amount = unit_amount * amount_unit
141                     res ['value']['amount']= - round(amount, 2)
142                 res ['value']['general_account_id']= a
143         return res
144
145 hr_analytic_timesheet()
146