[IMP]improve view in contract
[odoo/odoo.git] / addons / analytic_contract_hr_expense / analytic_contract_hr_expense.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 osv, fields
23 from osv.orm import intersect, except_orm
24 import tools.sql
25 from tools.translate import _
26 from decimal_precision import decimal_precision as dp
27
28
29 class account_analytic_account(osv.osv):
30     _name = "account.analytic.account"
31     _inherit = "account.analytic.account"
32
33     def _get_total_estimation(self, account):
34         tot_est = super(account_analytic_account, self)._get_total_estimation(account)
35         if account.charge_expenses:
36             tot_est += account.est_expenses
37         return tot_est
38
39     def _get_total_invoiced(self, account):
40         total_invoiced = super(account_analytic_account, self)._get_total_invoiced(account)
41         if account.charge_expenses:
42             total_invoiced += account.expense_invoiced
43         return total_invoiced
44
45     def _get_total_remaining(self, account):
46         total_remaining = super(account_analytic_account, self)._get_total_remaining(account)
47         if account.charge_expenses:
48             total_remaining += account.remaining_expense
49         return total_remaining
50
51     def _get_total_remaining(self, account):
52         total_toinvoice = super(account_analytic_account, self)._get_total_toinvoice(account)
53         if account.charge_expenses:
54             total_toinvoice += account.expense_to_invoice
55         return total_toinvoice
56
57     def _remaining_expnse_calc(self, cr, uid, ids, name, arg, context=None):
58         res = {}
59         for account in self.browse(cr, uid, ids, context=context):
60             if account.est_expenses != 0:
61                 res[account.id] = account.est_expenses - account.expense_invoiced
62             else:
63                 res[account.id]=0.0
64         for id in ids:
65             res[id] = round(res.get(id, 0.0),2)
66         return res
67
68     def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
69         res = {}
70         res_final = {}
71         child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
72         for i in child_ids:
73             res[i] =  0.0
74         if not child_ids:
75             return res
76
77         if child_ids:
78             cr.execute("SELECT hel.analytic_account, SUM(hel.unit_amount*hel.unit_quantity) \
79                     FROM hr_expense_line AS hel\
80                     LEFT JOIN hr_expense_expense AS he \
81                         ON he.id = hel.expense_id\
82                     WHERE he.state = 'invoiced' \
83                         AND hel.analytic_account IN %s \
84                     GROUP BY hel.analytic_account",(child_ids,))
85             for account_id, sum in cr.fetchall():
86                 res[account_id] = sum
87         res_final = res
88         return res_final
89
90     def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
91         res = {}
92         res_final = {}
93         child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
94         for i in child_ids:
95             res[i] =  0.0
96         if not child_ids:
97             return res
98
99         if child_ids:
100             cr.execute("SELECT hel.analytic_account,SUM(hel.unit_amount*hel.unit_quantity)\
101                     FROM hr_expense_line AS hel\
102                     LEFT JOIN hr_expense_expense AS he \
103                         ON he.id = hel.expense_id\
104                     WHERE he.state = 'paid' \
105                          AND hel.analytic_account IN %s \
106                     GROUP BY hel.analytic_account",(child_ids,))
107             for account_id, sum in cr.fetchall():
108                 res[account_id] = sum
109         res_final = res
110         return res_final
111
112     _columns = {
113         'charge_expenses' : fields.boolean('Charge Expenses'),
114         'expense_invoiced' : fields.function(_expense_invoiced_calc, type="float"),
115         'expense_to_invoice' : fields.function(_expense_to_invoice_calc, type='float'),
116         'remaining_expense' : fields.function(_remaining_expnse_calc, type="float"), 
117         'est_expenses': fields.float('Estimation of Expenses to Invoice'),
118     }
119
120     def on_change_template(self, cr, uid, id, template_id, context=None):
121         res = super(account_analytic_account, self).on_change_template(cr, uid, id, template_id, context=context)
122         if template_id and 'value' in res:
123             template = self.browse(cr, uid, template_id, context=context)
124             res['value']['charge_expenses'] = template.charge_expenses
125             res['value']['expense_max'] = template.expense_max
126         return res
127
128     def open_hr_expense(self, cr, uid, ids, context=None):
129         account = self.browse(cr, uid, ids[0], context)
130         data_obj = self.pool.get('ir.model.data')
131         try:
132             journal_id = data_obj.get_object(cr, uid, 'hr_timesheet', 'analytic_journal').id
133         except ValueError:
134             journal_id = False
135         line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account','=',account.id)])
136         id2 = data_obj._get_id(cr, uid, 'hr_expense', 'view_expenses_form')
137         id3 = data_obj._get_id(cr, uid, 'hr_expense', 'view_expenses_tree')
138         if id2:
139             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
140         if id3:
141             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
142         domain = [('line_ids','in',line_ids)]
143         return {
144             'type': 'ir.actions.act_window',
145             'name': _('Expenses'),
146             'view_type': 'form',
147             'view_mode': 'tree,form',
148             'views': [(id3,'tree'),(id2,'form')],
149             'domain' : domain,
150             'res_model': 'hr.expense.expense',
151             'nodestroy': True,
152         }
153
154     def hr_to_invoiced_expense(self, cr, uid, ids, context=None):
155          res = self.open_hr_expense(cr,uid,ids,context)
156          account = self.browse(cr, uid, ids[0], context)
157          line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account','=',account.id)])
158          res['domain'] = [('line_ids','in',line_ids),('state','=','invoiced')]
159          return res
160
161 account_analytic_account()
162
163 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: