1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
21 from openerp.osv import fields, osv
22 from openerp.osv.orm import intersect
23 from openerp.tools.translate import _
25 class account_analytic_account(osv.osv):
26 _name = "account.analytic.account"
27 _inherit = "account.analytic.account"
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
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
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
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
53 def _remaining_expnse_calc(self, cr, uid, ids, name, arg, context=None):
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)
62 def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
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):
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)
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,))
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
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
84 def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
85 lines_obj = self.pool.get('account.analytic.line')
87 for account in self.browse(cr, uid, ids, context=context):
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
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'),
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
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')
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)
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'
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)
131 'type': 'ir.actions.act_window',
134 'view_mode': 'tree,form',
136 'res_model': 'account.analytic.line',
140 account_analytic_account()
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: