Bugfixes and improvement
[odoo/odoo.git] / addons / hr_timesheet_invoice / report / account_analytic_profit.py
1 ##############################################################################
2 #
3 # Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting FROM its eventual inadequacies AND bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees AND support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it AND/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from report import report_sxw
30 import pooler
31
32 class account_analytic_profit(report_sxw.rml_parse):
33         def __init__(self, cr, uid, name, context):
34                 super(account_analytic_profit, self).__init__(cr, uid, name, context)
35                 self.localcontext.update({
36                         'lines': self._lines,
37                         'user_ids': self._user_ids,
38                         'journal_ids': self._journal_ids,
39                         'line': self._line,
40                 })
41         def _user_ids(self, lines):
42                 user_obj=pooler.get_pool(self.cr.dbname).get('res.users')
43                 ids=list(set([b.user_id.id for b in lines]))
44                 res=user_obj.browse(self.cr, self.uid, ids)
45                 return res
46
47         def _journal_ids(self, form, user_id):
48                 line_obj=pooler.get_pool(self.cr.dbname).get('account.analytic.line')
49                 journal_obj=pooler.get_pool(self.cr.dbname).get('account.analytic.journal')
50                 line_ids=line_obj.search(self.cr, self.uid, [
51                         ('date', '>=', form['date_from']),
52                         ('date', '<=', form['date_to']),
53                         ('journal_id', 'in', form['journal_ids'][0][2]),
54                         ('user_id', '=', user_id),
55                         ])
56                 ids=list(set([b.journal_id.id for b in line_obj.browse(self.cr, self.uid, line_ids)]))
57                 res=journal_obj.browse(self.cr, self.uid, ids)
58                 return res
59
60         def _line(self, form, journal_ids, user_ids):
61                 pool=pooler.get_pool(self.cr.dbname)
62                 line_obj=pool.get('account.analytic.line')
63                 product_obj=pool.get('product.product')
64                 price_obj=pool.get('product.pricelist')
65                 ids=line_obj.search(self.cr, self.uid, [
66                                 ('date', '>=', form['date_from']),
67                                 ('date', '<=', form['date_to']),
68                                 ('journal_id', 'in', journal_ids),
69                                 ('user_id', 'in', user_ids),
70                                 ])
71                 res={}
72                 for line in line_obj.browse(self.cr, self.uid, ids):
73                         if line.account_id.pricelist_id:
74                                 if line.account_id.to_invoice:
75                                         if line.to_invoice:
76                                                 id=line.to_invoice.id
77                                                 name=line.to_invoice.name
78                                                 discount=line.to_invoice.factor
79                                         else:
80                                                 name="/"
81                                                 discount=1.0
82                                                 id = -1
83                                 else:
84                                         name="Fixed"
85                                         discount=0.0
86                                         id=0
87                                 pl=line.account_id.pricelist_id.id
88                                 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]
89                         else:
90                                 name="/"
91                                 discount=1.0
92                                 id = -1
93                                 price=0.0
94                         if id not in res:
95                                 res[id]={'name': name, 'amount': 0, 'cost':0, 'unit_amount':0,'amount_th':0}
96                         xxx = round(price * line.unit_amount * (1-(discount or 0.0)), 2)
97                         res[id]['amount_th']+=xxx
98                         if line.invoice_id:
99                                 self.cr.execute('select id from account_analytic_line where invoice_id=%d', (line.invoice_id.id,))
100                                 tot = 0
101                                 for lid in self.cr.fetchall():
102                                         lid2 = line_obj.browse(self.cr, self.uid, lid[0])
103                                         pl=lid2.account_id.pricelist_id.id
104                                         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]
105                                         tot += price * lid2.unit_amount * (1-(discount or 0.0))
106                                 if tot:
107                                         procent = line.invoice_id.amount_untaxed / tot
108                                         res[id]['amount'] +=  xxx * procent
109                                 else:
110                                         res[id]['amount'] += xxx
111                         else:
112                                 res[id]['amount'] += xxx
113
114                         res[id]['cost']+=line.amount
115                         res[id]['unit_amount']+=line.unit_amount
116                 for id in res:
117                         res[id]['profit']=res[id]['amount']+res[id]['cost']
118                         res[id]['eff']='%d' % (-res[id]['amount'] / res[id]['cost'] * 100,)
119                 return res.values()
120
121         def _lines(self, form):
122                 line_obj=pooler.get_pool(self.cr.dbname).get('account.analytic.line')
123                 ids=line_obj.search(self.cr, self.uid, [
124                         ('date', '>=', form['date_from']),
125                         ('date', '<=', form['date_to']),
126                         ('journal_id', 'in', form['journal_ids'][0][2]),
127                         ('user_id', 'in', form['employee_ids'][0][2]),
128                         ])
129                 res=line_obj.browse(self.cr, self.uid, ids)
130                 return res
131
132 report_sxw.report_sxw('report.account.analytic.profit', 'account.analytic.line', 'addons/hr_timesheet_invoice/report/account_analytic_profit.rml', parser=account_analytic_profit)