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
25 class analytic_journal_rate_grid(osv.osv):
27 _name="analytic_journal_rate_grid"
28 _description= "Relation table between journals and billing rates"
30 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True,),
31 'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,),
32 'rate_id': fields.many2one("hr_timesheet_invoice.factor", "Invoicing Rate",),
35 analytic_journal_rate_grid()
37 class account_analytic_account(osv.osv):
39 _inherit = "account.analytic.account"
41 'journal_rate_ids': fields.one2many('analytic_journal_rate_grid', 'account_id', 'Invoicing Rate per Journal'),
44 account_analytic_account()
46 class hr_analytic_timesheet(osv.osv):
48 _inherit = "hr.analytic.timesheet"
51 def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, journal_id=0):
54 #avoid a useless call to super
58 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id, user_id, unit_amount)
60 #get the browse record related to journal_id and account_id
61 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
64 #if there isn't any record for this journal_id and account_id
65 return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id,user_id, unit_amount)
67 #get the old values from super and add the value from the new relation analytic_journal_rate_grid
68 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
69 res.setdefault('value',{})
70 res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id, user_id, unit_amount)['value']
72 res['value']['to_invoice'] = r.rate_id.id
77 def on_change_journal_id(self, cr, uid, ids, journal_id, account_id):
79 if not (journal_id and account_id):
82 #get the browse record related to journal_id and account_id
83 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
85 #add the value from the new relation analytic_user_funct_grid
86 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
87 res.setdefault('value',{})
89 res['value']['to_invoice'] = r.rate_id.id
91 to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [account_id], ['to_invoice'])[0]['to_invoice']
93 res.setdefault('value',{})
94 res['value']['to_invoice'] = to_invoice[0]
98 hr_analytic_timesheet()
101 class account_invoice(osv.osv):
102 _inherit = "account.invoice"
104 def _get_analytic_lines(self, cr, uid, id):
105 iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id)
106 inv = self.browse(cr, uid, [id])[0]
108 if il['account_analytic_id'] and il.get('analytic_lines', False):
110 #get the browse record related to journal_id and account_id
111 journal_id = il['analytic_lines'][0][2]['journal_id']
112 account_id = il['analytic_lines'][0][2]['account_id']
113 if journal_id and account_id:
114 temp = self.pool.get('analytic_journal_rate_grid').search(cr, uid, [('journal_id', '=', journal_id),('account_id', '=', account_id) ])
117 r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp)[0]
118 il['analytic_lines'][0][2]['to_invoice'] = r.rate_id.id
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: