1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
24 class analytic_journal_rate_grid(osv.osv):
26 _name="analytic_journal_rate_grid"
27 _description= "Relation table between journals and billing rates"
29 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True,),
30 'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,),
31 'rate_id': fields.many2one("hr_timesheet_invoice.factor", "Invoicing Rate",),
34 analytic_journal_rate_grid()
36 class account_analytic_account(osv.osv):
38 _inherit = "account.analytic.account"
40 'journal_rate_ids': fields.one2many('analytic_journal_rate_grid', 'account_id', 'Invoicing Rate per Journal'),
43 account_analytic_account()
45 class hr_analytic_timesheet(osv.osv):
47 _inherit = "hr.analytic.timesheet"
50 def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, journal_id=0):
53 #avoid a useless call to super
57 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id, user_id, unit_amount)
59 #get the browse record related to journal_id and account_id
60 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
63 #if there isn't any record for this journal_id and account_id
64 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id,user_id, unit_amount)
66 #get the old values from super and add the value from the new relation analytic_journal_rate_grid
67 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
68 res.setdefault('value',{})
69 res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id, user_id, unit_amount)['value']
71 res['value']['to_invoice'] = r.rate_id.id
76 def on_change_journal_id(self, cr, uid, ids, journal_id, account_id):
78 if not (journal_id and account_id):
81 #get the browse record related to journal_id and account_id
82 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
84 #add the value from the new relation analytic_user_funct_grid
85 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
86 res.setdefault('value',{})
88 res['value']['to_invoice'] = r.rate_id.id
90 to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [account_id], ['to_invoice'])[0]['to_invoice']
92 res.setdefault('value',{})
93 res['value']['to_invoice'] = to_invoice[0]
97 hr_analytic_timesheet()
100 class account_invoice(osv.osv):
101 _inherit = "account.invoice"
103 def _get_analytic_lines(self, cr, uid, id):
104 iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id)
106 if il['account_analytic_id'] and il.get('analytic_lines', False):
108 #get the browse record related to journal_id and account_id
109 journal_id = il['analytic_lines'][0][2]['journal_id']
110 account_id = il['analytic_lines'][0][2]['account_id']
111 if journal_id and account_id:
112 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
115 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
116 il['analytic_lines'][0][2]['to_invoice'] = r.rate_id.id
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: