[FIX] mail: Use a default value if the signature and the subject are empty
[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
24 class analytic_journal_rate_grid(osv.osv):
25
26     _name="analytic_journal_rate_grid"
27     _description= "Relation table between journals and billing rates"
28     _columns={
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",),
32         }
33
34 analytic_journal_rate_grid()
35
36 class account_analytic_account(osv.osv):
37
38     _inherit = "account.analytic.account"
39     _columns = {
40         'journal_rate_ids': fields.one2many('analytic_journal_rate_grid', 'account_id', 'Invoicing Rate per Journal'),
41     }
42
43 account_analytic_account()
44
45 class hr_analytic_timesheet(osv.osv):
46
47     _inherit = "hr.analytic.timesheet"
48
49
50     def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, journal_id=0):
51         res = {}
52         if not (account_id):
53             #avoid a useless call to super
54             return res 
55
56         if not (journal_id):
57             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids,account_id, user_id, unit_amount)
58
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) ])
61
62         if not temp:
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)
65         else:
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']
70             if r.rate_id.id:
71                 res['value']['to_invoice'] = r.rate_id.id
72     
73         return res
74
75
76     def on_change_journal_id(self, cr, uid, ids, journal_id, account_id):
77         res = {}
78         if not (journal_id and account_id):
79             return res 
80
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) ])
83         if temp:
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',{})
87             if r.rate_id.id:
88                 res['value']['to_invoice'] = r.rate_id.id
89                 return res
90         to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [account_id], ['to_invoice'])[0]['to_invoice']
91         if to_invoice:
92             res.setdefault('value',{})
93             res['value']['to_invoice'] = to_invoice[0]
94
95         return res
96
97 hr_analytic_timesheet()
98
99
100 class account_invoice(osv.osv):
101     _inherit = "account.invoice"
102
103     def _get_analytic_lines(self, cr, uid, id, context=None):
104         iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id, context=context)
105         for il in iml:
106             if il['account_analytic_id'] and il.get('analytic_lines', False):
107
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)], context=context)
113
114                     if temp:
115                         r = self.pool.get('analytic_journal_rate_grid').browse(cr, uid, temp, context=context)[0]
116                         il['analytic_lines'][0][2]['to_invoice'] = r.rate_id.id
117         return iml
118
119 account_invoice()
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
121