[FIX] tools.convert: use tools.ustr() instead of str() on exceptions.
[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 from openerp.osv import fields, osv
22 from openerp.osv.orm import intersect
23 from openerp.tools.translate import _
24
25 class account_analytic_account(osv.osv):
26     _name = "account.analytic.account"
27     _inherit = "account.analytic.account"
28
29     def _get_total_estimation(self, account):
30         tot_est = super(account_analytic_account, self)._get_total_estimation(account)
31         if account.charge_expenses:
32             tot_est += account.est_expenses
33         return tot_est
34
35     def _get_total_invoiced(self, account):
36         total_invoiced = super(account_analytic_account, self)._get_total_invoiced(account)
37         if account.charge_expenses:
38             total_invoiced += account.expense_invoiced
39         return total_invoiced
40
41     def _get_total_remaining(self, account):
42         total_remaining = super(account_analytic_account, self)._get_total_remaining(account)
43         if account.charge_expenses:
44             total_remaining += account.remaining_expense
45         return total_remaining
46
47     def _get_total_toinvoice(self, account):
48         total_toinvoice = super(account_analytic_account, self)._get_total_toinvoice(account)
49         if account.charge_expenses:
50             total_toinvoice += account.expense_to_invoice
51         return total_toinvoice
52
53     def _remaining_expnse_calc(self, cr, uid, ids, name, arg, context=None):
54         res = {}
55         for account in self.browse(cr, uid, ids, context=context):
56             if account.est_expenses != 0:
57                 res[account.id] = max(account.est_expenses - account.expense_invoiced, account.expense_to_invoice)
58             else:
59                 res[account.id]=0.0
60         return res
61
62     def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
63         res = {}
64         #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
65         for account in self.pool.get('account.analytic.account').browse(cr, uid, ids, context=context):
66             cr.execute("""
67                 SELECT product_id, sum(amount), user_id, to_invoice, sum(unit_amount), product_uom_id, line.name
68                 FROM account_analytic_line line
69                     LEFT JOIN account_analytic_journal journal ON (journal.id = line.journal_id)
70                 WHERE account_id = %s
71                     AND journal.type = 'purchase'
72                     AND invoice_id IS NULL
73                     AND to_invoice IS NOT NULL
74                 GROUP BY product_id, user_id, to_invoice, product_uom_id, line.name""", (account.id,))
75
76             res[account.id] = 0.0
77             for product_id, price, user_id, factor_id, qty, uom, line_name in cr.fetchall():
78                 #the amount to reinvoice is the real cost. We don't use the pricelist
79                 price = -price
80                 factor = self.pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context=context)
81                 res[account.id] += price * qty * (100 - factor.factor or 0.0) / 100.0
82         return res
83
84     def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
85         lines_obj = self.pool.get('account.analytic.line')
86         res = {}
87         for account in self.browse(cr, uid, ids, context=context):
88             res[account.id] = 0.0
89             line_ids = lines_obj.search(cr, uid, [('account_id','=', account.id), ('invoice_id','!=',False), ('to_invoice','!=', False), ('journal_id.type', '=', 'purchase')], context=context)
90             for line in lines_obj.browse(cr, uid, line_ids, context=context):
91                 res[account.id] += line.invoice_id.amount_untaxed
92         return res
93
94
95     _columns = {
96         'charge_expenses' : fields.boolean('Charge Expenses'),
97         'expense_invoiced' : fields.function(_expense_invoiced_calc, type="float"),
98         'expense_to_invoice' : fields.function(_expense_to_invoice_calc, type='float'),
99         'remaining_expense' : fields.function(_remaining_expnse_calc, type="float"), 
100         'est_expenses': fields.float('Estimation of Expenses to Invoice'),
101     }
102
103     def on_change_template(self, cr, uid, id, template_id, context=None):
104         res = super(account_analytic_account, self).on_change_template(cr, uid, id, template_id, context=context)
105         if template_id and 'value' in res:
106             template = self.browse(cr, uid, template_id, context=context)
107             res['value']['charge_expenses'] = template.charge_expenses
108             res['value']['est_expenses'] = template.est_expenses
109         return res
110
111     def open_hr_expense(self, cr, uid, ids, context=None):
112         mod_obj = self.pool.get('ir.model.data')
113         act_obj = self.pool.get('ir.actions.act_window')
114
115         dummy, act_window_id = mod_obj.get_object_reference(cr, uid, 'hr_expense', 'expense_all')
116         result = act_obj.read(cr, uid, act_window_id, context=context)
117
118         line_ids = self.pool.get('hr.expense.line').search(cr,uid,[('analytic_account', 'in', ids)])
119         result['domain'] = [('line_ids', 'in', line_ids)]
120         names = [account.name for account in self.browse(cr, uid, ids, context=context)]
121         result['name'] = _('Expenses of %s') % ','.join(names)
122         result['context'] = {'analytic_account': ids[0]}
123         result['view_type'] = 'form'
124         return result
125
126     def hr_to_invoice_expense(self, cr, uid, ids, context=None):
127         domain = [('invoice_id','=',False),('to_invoice','!=',False), ('journal_id.type', '=', 'purchase'), ('account_id', 'in', ids)]
128         names = [record.name for record in self.browse(cr, uid, ids, context=context)]
129         name = _('Expenses to Invoice of %s') % ','.join(names)
130         return {
131             'type': 'ir.actions.act_window',
132             'name': name,
133             'view_type': 'form',
134             'view_mode': 'tree,form',
135             'domain' : domain,
136             'res_model': 'account.analytic.line',
137             'nodestroy': True,
138         }
139
140 account_analytic_account()
141
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: