[IMP]improve code and add tooltip on task_management
[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_toinvoice(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] = max(account.est_expenses - account.expense_invoiced, account.expense_to_invoice)
62             else:
63                 res[account.id]=0.0
64         return res
65
66     def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
67         res = {}
68         res_final = {}
69         child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
70         for i in child_ids:
71             res[i] =  0.0
72         if not child_ids:
73             return res
74
75         if child_ids:
76             cr.execute("""SELECT account_analytic_account.id, \
77                                 COALESCE(SUM (product_template.list_price * \
78                                     account_analytic_line.unit_amount * \
79                                     ((100-hr_timesheet_invoice_factor.factor)/100)), 0.0) \
80                                     AS ca_to_invoice \
81                             FROM product_template \
82                             JOIN product_product \
83                                 ON product_template.id = product_product.product_tmpl_id \
84                             JOIN account_analytic_line \
85                                 ON account_analytic_line.product_id = product_product.id \
86                             JOIN account_analytic_journal \
87                                 ON account_analytic_line.journal_id = account_analytic_journal.id \
88                             JOIN account_analytic_account \
89                                 ON account_analytic_account.id = account_analytic_line.account_id \
90                             JOIN hr_timesheet_invoice_factor \
91                                 ON hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice \
92                             WHERE account_analytic_account.id IN %s \
93                                 AND account_analytic_line.invoice_id IS NULL \
94                                 AND account_analytic_line.to_invoice IS NOT NULL \
95                                 AND account_analytic_journal.type = 'purchase' \
96                             GROUP BY account_analytic_account.id;""",(child_ids,))
97             for account_id, sum in cr.fetchall():
98                 res[account_id] = sum
99         res_final = res
100         return res_final
101
102     def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
103         lines_obj = self.pool.get('account.analytic.line')
104         res = {}
105         for account in self.browse(cr, uid, ids, context=context):
106             res[account.id] = 0.0
107             line_ids = lines_obj.search(cr, uid, [('account_id','=', account.id), ('invoice_id','!=',False), ('to_invoice','!=', False), ('journal_id.type', '=', 'purchase')], context=context)
108             for line in lines_obj.browse(cr, uid, line_ids, context=context):
109                 res[account.id] += line.invoice_id.amount_untaxed
110         return res
111
112
113     _columns = {
114         'charge_expenses' : fields.boolean('Charge Expenses'),
115         'expense_invoiced' : fields.function(_expense_invoiced_calc, type="float"),
116         'expense_to_invoice' : fields.function(_expense_to_invoice_calc, type='float'),
117         'remaining_expense' : fields.function(_remaining_expnse_calc, type="float"), 
118         'est_expenses': fields.float('Estimation of Expenses to Invoice'),
119     }
120
121     def on_change_template(self, cr, uid, id, template_id, context=None):
122         res = super(account_analytic_account, self).on_change_template(cr, uid, id, template_id, context=context)
123         if template_id and 'value' in res:
124             template = self.browse(cr, uid, template_id, context=context)
125             res['value']['charge_expenses'] = template.charge_expenses
126             res['value']['est_expenses'] = template.est_expenses
127         return res
128
129     def open_hr_expense(self, cr, uid, ids, context=None):
130         line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account', 'in', ids)])
131         domain = [('line_ids', 'in', line_ids)]
132         names = [record.name for record in self.browse(cr, uid, ids, context=context)]
133         name = _('Expenses of %s') % ','.join(names)
134         return {
135             'type': 'ir.actions.act_window',
136             'name': name,
137             'view_type': 'form',
138             'view_mode': 'tree,form',
139             'domain' : domain,
140             'res_model': 'hr.expense.expense',
141             'nodestroy': True,
142         }
143
144     def hr_to_invoice_expense(self, cr, uid, ids, context=None):
145         domain = [('invoice_id','=',False),('to_invoice','!=',False), ('journal_id.type', '=', 'purchase'), ('account_id', 'in', ids)]
146         names = [record.name for record in self.browse(cr, uid, ids, context=context)]
147         name = _('Expenses to Invoice of %s') % ','.join(names)
148         return {
149             'type': 'ir.actions.act_window',
150             'name': name,
151             'view_type': 'form',
152             'view_mode': 'tree,form',
153             'domain' : domain,
154             'res_model': 'account.analytic.line',
155             'nodestroy': True,
156         }
157
158 account_analytic_account()
159
160 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: