1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 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
25 class analytic_user_funct_grid(osv.osv):
27 _name="analytic_user_funct_grid"
28 _description= "Relation table between users and products on a analytic account"
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,),
35 analytic_user_funct_grid()
38 class account_analytic_account(osv.osv):
40 _inherit = "account.analytic.account"
42 'user_product_ids' : fields.one2many('analytic_user_funct_grid', 'account_id', 'Users/Products Rel.'),
45 account_analytic_account()
48 class hr_analytic_timesheet(osv.osv):
50 _inherit = "hr.analytic.timesheet"
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):
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)
63 return self._get_related_user_account_recursiv(cr,uid,user_id,account.parent_id.id)
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,)}}
72 #avoid a useless call to super
76 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id)
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) ])
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)
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
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
95 a = r.product_id.categ_id.property_account_expense_categ.id
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
107 def on_change_user_id(self, cr, uid, ids,user_id, account_id, unit_amount=0):
110 #avoid a useless call to super
113 #get the old values from super
114 res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids,user_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)
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
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
128 a = r.product_id.categ_id.property_account_expense_categ.id
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
140 hr_analytic_timesheet()
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: