~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to hr_timesheet_invoice/report/account_analytic_profit.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.to_invoice:
 
74
                                id=line.to_invoice.id
 
75
                                name=line.to_invoice.name
 
76
                                discount=line.to_invoice.factor
 
77
                        else:
 
78
                                if line.account_id.pricelist_id:
 
79
                                        name="Fixed Price"
 
80
                                        discount=0.0
 
81
                                        id=0
 
82
                                else:
 
83
                                        name="/"
 
84
                                        discount=1.0
 
85
                                        id = -1
 
86
                        if line.account_id.pricelist_id:
 
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
                                price=0.0
 
91
                        if id not in res:
 
92
                                res[id]={'name': name, 'amount': 0, 'cost':0, 'unit_amount':0,}
 
93
                        res[id]['amount']+=round(price * line.unit_amount * (1-(discount or 0.0)/100.0), 2)
 
94
                        res[id]['cost']+=line.amount
 
95
                        res[id]['unit_amount']+=line.unit_amount
 
96
                for id in res:
 
97
                        res[id]['profit']=res[id]['amount']+res[id]['cost']
 
98
                        res[id]['eff']=abs(round(res[id]['amount'] / res[id]['cost'] * 100, 2))
 
99
                return res.values()
 
100
 
 
101
        def _lines(self, form):
 
102
                line_obj=pooler.get_pool(self.cr.dbname).get('account.analytic.line')
 
103
                ids=line_obj.search(self.cr, self.uid, [
 
104
                        ('date', '>=', form['date_from']),
 
105
                        ('date', '<=', form['date_to']),
 
106
                        ('journal_id', 'in', form['journal_ids'][0][2]),
 
107
                        ('user_id', 'in', form['employee_ids'][0][2]),
 
108
                        ])
 
109
                res=line_obj.browse(self.cr, self.uid, ids)
 
110
                return res
 
111
 
 
112
report_sxw.report_sxw('report.account.analytic.profit', 'account.analytic.line', 'addons/hr_timesheet_invoice/report/account_analytic_profit.rml', parser=account_analytic_profit)