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