~serpentcs/openobject-addons/trunk-webkit-reports-addons

« back to all changes in this revision

Viewing changes to hr_timesheet_invoice/report/account_analytic_profit.py

  • Committer: Hemangini Patel
  • Date: 2013-10-16 10:19:06 UTC
  • Revision ID: h.patel.serpentcs@gmail.com-20131016101906-9bqrmodzzepjw5zs
[ADD] Added hr_timesheet_invoice module with webkit reports.

Show diffs side-by-side

added added

removed removed

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