1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
22 from osv import fields,osv
24 from tools.translate import _
26 class analytic_user_funct_grid(osv.osv):
28 _name="analytic_user_funct_grid"
29 _description= "Relation table between users and products on a analytic account"
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,),
36 analytic_user_funct_grid()
39 class account_analytic_account(osv.osv):
41 _inherit = "account.analytic.account"
43 'user_product_ids' : fields.one2many('analytic_user_funct_grid', 'account_id', 'Users/Products Rel.'),
46 account_analytic_account()
49 class hr_analytic_timesheet(osv.osv):
51 _inherit = "hr.analytic.timesheet"
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):
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)
64 return self._get_related_user_account_recursiv(cr,uid,user_id,account.parent_id.id)
69 def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0):
70 #{'value': {'to_invoice': False, 'amount': (-162.0,), 'product_id': 7, 'general_account_id': (5,)}}
73 #avoid a useless call to super
77 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)
79 #get the browse record related to user_id and account_id
80 temp = self._get_related_user_account_recursiv(cr,uid,user_id,account_id)
81 # temp = self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
83 #if there isn't any record for this user_id and account_id
84 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)
86 #get the old values from super and add the value from the new relation analytic_user_funct_grid
87 r = self.pool.get('analytic_user_funct_grid').browse(cr, uid, temp)[0]
88 res.setdefault('value',{})
89 res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)['value']
90 res['value']['product_id'] = r.product_id.id
91 res['value']['product_uom_id'] = r.product_id.product_tmpl_id.uom_id.id
93 #the change of product has to impact the amount, uom and general_account_id
94 a = r.product_id.product_tmpl_id.property_account_expense.id
96 a = r.product_id.categ_id.property_account_expense_categ.id
98 raise osv.except_osv(_('Error !'),
99 _('There is no expense account define ' \
100 'for this product: "%s" (id:%d)') % \
101 (r.product_id.name, r.product_id.id,))
102 amount = unit_amount * self.pool.get('product.uom')._compute_price(cr, uid,
103 r.product_id.uom_id.id, r.product_id.standard_price, False)
104 res ['value']['amount']= - round(amount, 2)
105 res ['value']['general_account_id']= a
108 def on_change_user_id(self, cr, uid, ids,user_id, account_id, unit_amount=0):
111 #avoid a useless call to super
114 #get the old values from super
115 res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids,user_id)
118 #get the browse record related to user_id and account_id
119 # temp = self.pool.get('analytic_user_funct_grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
120 temp = self._get_related_user_account_recursiv(cr,uid,user_id,account_id)
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
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
129 a = r.product_id.categ_id.property_account_expense_categ.id
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 amount = unit_amount * self.pool.get('product.uom')._compute_price(cr, uid,
136 r.product_id.uom_id.id, r.product_id.standard_price, False)
137 res ['value']['amount']= - round(amount, 2)
138 res ['value']['general_account_id']= a
141 hr_analytic_timesheet()