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