Launchpad automatic translations update.
[odoo/odoo.git] / addons / analytic_journal_billing_rate / analytic_journal_billing_rate.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 osv import orm
24
25 class analytic_journal_rate_grid(osv.osv):
26
27     _name="analytic_journal_rate_grid"
28     _description= "Relation table between journals and billing rates"
29     _columns={
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",),
33         }
34
35 analytic_journal_rate_grid()
36
37 class account_analytic_account(osv.osv):
38
39     _inherit = "account.analytic.account"
40     _columns = {
41         'journal_rate_ids': fields.one2many('analytic_journal_rate_grid', 'account_id', 'Invoicing Rate per Journal'),
42     }
43
44 account_analytic_account()
45
46 class hr_analytic_timesheet(osv.osv):
47
48     _inherit = "hr.analytic.timesheet"
49
50
51     def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, journal_id=0):
52         res = {}
53         if not (account_id):
54             #avoid a useless call to super
55             return res 
56
57         if not (journal_id):
58             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id, user_id, unit_amount)
59
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) ])
62
63         if not temp:
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)
66         else:
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']
71             if r.rate_id.id:
72                 res['value']['to_invoice'] = r.rate_id.id
73     
74         return res
75
76
77     def on_change_journal_id(self, cr, uid, ids, journal_id, account_id):
78         res = {}
79         if not (journal_id and account_id):
80             return res 
81
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) ])
84         if temp:
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',{})
88             if r.rate_id.id:
89                 res['value']['to_invoice'] = r.rate_id.id
90                 return res
91         to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [account_id], ['to_invoice'])[0]['to_invoice']
92         if to_invoice:
93             res.setdefault('value',{})
94             res['value']['to_invoice'] = to_invoice[0]
95
96         return res
97
98 hr_analytic_timesheet()
99
100
101 class account_invoice(osv.osv):
102     _inherit = "account.invoice"
103
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]
107         for il in iml:
108             if il['account_analytic_id'] and il.get('analytic_lines', False):
109
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) ])
115
116                     if temp:
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
119         return iml
120
121 account_invoice()
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
123