Launchpad automatic translations update.
[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 tools.translate import _
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, account_id, user_id=False, unit_amount=0):
69         res = {}
70         if not (account_id):
71             #avoid a useless call to super
72             return res
73
74         if not (user_id):
75             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
76
77         #get the browse record related to user_id and account_id
78         temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
79         if not temp:
80             #if there isn't any record for this user_id and account_id
81             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
82         else:
83             #get the old values from super and add the value from the new relation analytic_user_funct_grid
84             r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0]
85             res.setdefault('value',{})
86             res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)['value']
87             res['value']['product_id'] = r.product_id.id
88             res['value']['product_uom_id'] = r.product_id.product_tmpl_id.uom_id.id
89
90             #the change of product has to impact the amount, uom and general_account_id
91             a = r.product_id.product_tmpl_id.property_account_expense.id
92             if not a:
93                 a = r.product_id.categ_id.property_account_expense_categ.id
94             if not a:
95                 raise osv.except_osv(_('Error !'),
96                         _('There is no expense account define ' \
97                                 'for this product: "%s" (id:%d)') % \
98                                 (r.product_id.name, r.product_id.id,))
99             # Compute based on pricetype
100             if unit_amount:
101                 amount_unit = self.on_change_unit_amount(cr, uid, ids,
102                             r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
103                 amount = unit_amount *  amount_unit
104                 res ['value']['amount']= - round(amount, 2)
105             res ['value']['general_account_id']= a
106         return res
107
108     def on_change_user_id(self, cr, uid, ids, user_id, account_id, unit_amount=0):
109         res = {}
110         if not (user_id):
111             #avoid a useless call to super
112             return res
113
114         #get the old values from super
115         res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids, user_id)
116
117         if account_id:
118             #get the browse record related to user_id and 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                 # Compute based on pricetype
135                 if unit_amount:
136                     amount_unit = self.on_change_unit_amount(cr, uid, ids,
137                         r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
138
139                     amount = unit_amount * amount_unit
140                     res ['value']['amount']= - round(amount, 2)
141                 res ['value']['general_account_id']= a
142         return res
143
144 hr_analytic_timesheet()
145
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: