[IMP] account_analytic_analysis : Add recurring field to product and add product...
[odoo/odoo.git] / addons / hr_timesheet_invoice / report / account_analytic_profit.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 openerp.report import report_sxw
23 from openerp.osv import osv
24
25
26 class account_analytic_profit(report_sxw.rml_parse):
27     def __init__(self, cr, uid, name, context):
28         super(account_analytic_profit, self).__init__(cr, uid, name, context=context)
29         self.localcontext.update({
30             'lines': self._lines,
31             'user_ids': self._user_ids,
32             'journal_ids': self._journal_ids,
33             'line': self._line,
34         })
35
36     def _user_ids(self, lines):
37         user_obj = self.pool['res.users']
38         ids=list(set([b.user_id.id for b in lines]))
39         return user_obj.browse(self.cr, self.uid, ids)
40
41     def _journal_ids(self, form, user_id):
42         line_obj = self.pool['account.analytic.line']
43         journal_obj = self.pool['account.analytic.journal']
44         line_ids=line_obj.search(self.cr, self.uid, [
45             ('date', '>=', form['date_from']),
46             ('date', '<=', form['date_to']),
47             ('journal_id', 'in', form['journal_ids'][0][2]),
48             ('user_id', '=', user_id),
49             ])
50         ids=list(set([b.journal_id.id for b in line_obj.browse(self.cr, self.uid, line_ids)]))
51         return journal_obj.browse(self.cr, self.uid, ids)
52
53     def _line(self, form, journal_ids, user_ids):
54         line_obj = self.pool['account.analytic.line']
55         product_obj = self.pool['product.product']
56         price_obj = self.pool['product.pricelist']
57         ids=line_obj.search(self.cr, self.uid, [
58                 ('date', '>=', form['date_from']),
59                 ('date', '<=', form['date_to']),
60                 ('journal_id', 'in', journal_ids),
61                 ('user_id', 'in', user_ids),
62                 ])
63         res={}
64         for line in line_obj.browse(self.cr, self.uid, ids):
65             if line.account_id.pricelist_id:
66                 if line.account_id.to_invoice:
67                     if line.to_invoice:
68                         id=line.to_invoice.id
69                         name=line.to_invoice.name
70                         discount=line.to_invoice.factor
71                     else:
72                         name="/"
73                         discount=1.0
74                         id = -1
75                 else:
76                     name="Fixed"
77                     discount=0.0
78                     id=0
79                 pl=line.account_id.pricelist_id.id
80                 price=price_obj.price_get(self.cr, self.uid, [pl], line.product_id.id, line.unit_amount or 1.0, line.account_id.partner_id.id)[pl]
81             else:
82                 name="/"
83                 discount=1.0
84                 id = -1
85                 price=0.0
86             if id not in res:
87                 res[id]={'name': name, 'amount': 0, 'cost':0, 'unit_amount':0,'amount_th':0}
88             xxx = round(price * line.unit_amount * (1-(discount or 0.0)), 2)
89             res[id]['amount_th']+=xxx
90             if line.invoice_id:
91                 self.cr.execute('select id from account_analytic_line where invoice_id=%s', (line.invoice_id.id,))
92                 tot = 0
93                 for lid in self.cr.fetchall():
94                     lid2 = line_obj.browse(self.cr, self.uid, lid[0])
95                     pl=lid2.account_id.pricelist_id.id
96                     price=price_obj.price_get(self.cr, self.uid, [pl], lid2.product_id.id, lid2.unit_amount or 1.0, lid2.account_id.partner_id.id)[pl]
97                     tot += price * lid2.unit_amount * (1-(discount or 0.0))
98                 if tot:
99                     procent = line.invoice_id.amount_untaxed / tot
100                     res[id]['amount'] +=  xxx * procent
101                 else:
102                     res[id]['amount'] += xxx
103             else:
104                 res[id]['amount'] += xxx
105             res[id]['cost']+=line.amount
106             res[id]['unit_amount']+=line.unit_amount
107         for id in res:
108             res[id]['profit']=res[id]['amount']+res[id]['cost']
109             res[id]['eff']=res[id]['cost'] and '%d' % (-res[id]['amount'] / res[id]['cost'] * 100,) or 0.0
110         return res.values()
111
112     def _lines(self, form):
113         line_obj = self.pool['account.analytic.line']
114         ids=line_obj.search(self.cr, self.uid, [
115             ('date', '>=', form['date_from']),
116             ('date', '<=', form['date_to']),
117             ('journal_id', 'in', form['journal_ids'][0][2]),
118             ('user_id', 'in', form['employee_ids'][0][2]),
119             ])
120         return line_obj.browse(self.cr, self.uid, ids)
121
122
123 class report_account_analytic_profit(osv.AbstractModel):
124     _name = 'report.hr_timesheet_invoice.report_analyticprofit'
125     _inherit = 'report.abstract_report'
126     _template = 'hr_timesheet_invoice.report_analyticprofit'
127     _wrapped_report_class = account_analytic_profit
128
129 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: