~noviat/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_cashflow/report/account_cashflow_report.py

  • Committer: root
  • Date: 2012-04-10 20:37:39 UTC
  • Revision ID: root@oerp61-20120410203739-flykplw8jzpqa15c
update noviat v6.0 accounting modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    
 
6
#    Copyright (c) 2011 Noviat nv/sa (www.noviat.be). All rights reserved.
 
7
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import time
 
24
from datetime import datetime, date, timedelta
 
25
from math import ceil
 
26
from report import report_sxw
 
27
from osv import osv
 
28
from tools.translate import _
 
29
import netsvc
 
30
logger=netsvc.Logger()
 
31
 
 
32
class account_cashflow_report(report_sxw.rml_parse):
 
33
           
 
34
    def __init__(self, cr, uid, name, context):
 
35
        super(account_cashflow_report, self).__init__(cr, uid, name, context=context)
 
36
        #logger.notifyChannel('addons.'+__name__, netsvc.LOG_WARNING, '_init, name = %s, context = %s' % (name, context)) 
 
37
        self.context = context
 
38
        cr = self.cr
 
39
        uid = self.uid
 
40
 
 
41
        if not context.get('date_start', None):
 
42
            raise osv.except_osv(_('Warning!'), _('This report is only available from the Cash Flow Chart!'))
 
43
            return {}
 
44
 
 
45
        format_date = self.pool.get('account.cashflow.code').format_date
 
46
        date_start = datetime.strptime(context.get('date_start'), '%Y-%m-%d').date()            
 
47
        nbr_days = int(context.get('nbr_days'))
 
48
        days = [date_start.isoformat()] + [(date_start + timedelta(days=x)).isoformat() for x in range(1, nbr_days)]        
 
49
 
 
50
        self.localcontext.update({
 
51
            'time': time,
 
52
            'timedelta': timedelta,
 
53
            'ceil': ceil,
 
54
            'cr': cr,
 
55
            'uid': uid,
 
56
            'name_get': self._name_get,
 
57
            'lang': context.get('lang', 'en_US'),
 
58
            'date_start': days[0],
 
59
            'date_stop': days[-1],
 
60
            'nbr_days': nbr_days,
 
61
            'days': days,
 
62
            'balance_period': self._balance_period,            
 
63
        })
 
64
 
 
65
    def set_context(self, objects, data, ids, report_type=None):
 
66
        cr = self.cr
 
67
        uid = self.uid
 
68
        context = self.context
 
69
        cfc_obj = self.pool.get('account.cashflow.code')
 
70
        active_cfc = objects[0]
 
71
        
 
72
        def _get_toplevel_cfc(record):
 
73
            toplevel_cfc = record
 
74
            parent = record.parent_id
 
75
            if parent:
 
76
                toplevel_cfc = _get_toplevel_cfc(parent)
 
77
            return toplevel_cfc
 
78
        toplevel_cfc = _get_toplevel_cfc(active_cfc)       
 
79
        cfc_tree_ids = cfc_obj.search(cr, uid, [('parent_id', 'child_of', [toplevel_cfc.id])], context=context)
 
80
        cfc_tree = cfc_obj.browse(cr, uid, cfc_tree_ids, context=context)
 
81
        
 
82
        cfc_level = 0
 
83
        cfc_levels = {toplevel_cfc.id:0}
 
84
        for cfc in cfc_tree[1:]:
 
85
            if cfc.parent_id.id in cfc_levels:
 
86
                cfc_level = cfc_levels[cfc.parent_id.id] + 1
 
87
            else:
 
88
                cfc_level += 1                
 
89
            cfc_levels[cfc.id] = cfc_level
 
90
        data['cfc_levels'] = cfc_levels
 
91
        super(account_cashflow_report, self).set_context(cfc_tree, data, cfc_tree_ids, report_type=report_type)
 
92
            
 
93
    def _name_get(self, object):
 
94
        res = self.pool.get(object._name).name_get(self.cr, self.uid, [object.id], self.context)
 
95
        return res[0][1]
 
96
 
 
97
    def _balance_period(self, ids, date_start=None, date_stop=None, day=None):
 
98
        res = self.pool.get('account.cashflow.code')._balance_period(self.cr, self.uid, ids, context=self.context, date_start=date_start, date_stop=date_stop, day=day)
 
99
        balance = res.values()[0]
 
100
        return balance
 
101
 
 
102
report_sxw.report_sxw('report.account.cashflow.report',
 
103
                       'account.cashflow.code', 
 
104
                       'account_cashflow/report/account_cashflow_report.mako',
 
105
                       parser=account_cashflow_report)
 
106
 
 
107
report_sxw.report_sxw('report.account.cashflow.summary.report',
 
108
                       'account.cashflow.code', 
 
109
                       'account_cashflow/report/account_cashflow_summary_report.mako',
 
110
                       parser=account_cashflow_report)