[ADD] uid field in context used when requesting a view, so modifiers processing can...
[odoo/odoo.git] / addons / l10n_fr / report / base_report.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2008 JAILLET Simon - CrysaLEAD - www.crysalead.fr
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 import time
30
31 from report import report_sxw
32
33 class base_report(report_sxw.rml_parse):
34     def __init__(self, cr, uid, name, context=None):
35         super(base_report, self).__init__(cr, uid, name, context=context)
36         self.localcontext.update({
37             'time': time,
38             '_load': self._load,
39             '_get_variable': self._get_variable,
40             '_set_variable': self._set_variable,
41         })
42         self.context = context
43
44     def _load(self, name, form):
45         fiscalyear = self.pool.get('account.fiscalyear').browse(self.cr, self.uid, form['fiscalyear_id'])
46         period_ids=self.pool.get('account.period').search(self.cr, self.uid, [('fiscalyear_id', '=', form['fiscalyear_id'])])
47
48         if period_ids:
49             self.cr.execute("SELECT MIN(date_start) AS date_start, MAX(date_stop) AS date_stop FROM account_period WHERE id = ANY(%s)", (period_ids,))
50             dates = self.cr.dictfetchall()
51         else:
52             dates = False
53         if dates:
54             self._set_variable('date_start', dates[0]['date_start'])
55             self._set_variable('date_stop', dates[0]['date_stop'])
56
57         self.cr.execute("SELECT l10n_fr_line.code,definition FROM l10n_fr_line LEFT JOIN l10n_fr_report ON l10n_fr_report.id=report_id WHERE l10n_fr_report.code=%s",(name,))
58         datas = self.cr.dictfetchall()
59         for line in datas:
60             self._load_accounts(form,line['code'],eval(line['definition']),fiscalyear,period_ids)
61
62     def _set_variable(self, variable, valeur):
63         self.localcontext.update({variable: valeur})
64
65     def _get_variable(self, variable):
66         return self.localcontext[variable]
67
68     def _load_accounts(self, form, code, definition, fiscalyear, period_ids):
69         accounts = {}
70         for x in definition['load']:
71             p = x.split(":")
72             accounts[p[1]] = [p[0],p[2]]
73         sum = 0.0
74         if fiscalyear.state != 'done' or not code.startswith('bpcheck'):
75             query_params = []
76             query_cond = "("
77             for account in accounts:
78                 query_cond += "aa.code LIKE '" + account + "%%' OR "
79             query_cond = query_cond[:-4]+")"
80
81             if len(definition['except']) > 0:
82                 query_cond = query_cond+" and ("
83                 for account in definition['except']:
84                     query_cond += "aa.code NOT LIKE '"+account+"%%' AND "
85                 query_cond = query_cond[:-5]+")"
86
87             closed_cond = ""
88             if fiscalyear.state == 'done':
89                 closed_cond=" AND (aml.move_id NOT IN (SELECT account_move.id as move_id FROM account_move WHERE period_id = ANY(%s) AND journal_id=(SELECT res_id FROM ir_model_data WHERE name='closing_journal' AND module='l10n_fr')) OR (aa.type != 'income' AND aa.type !='expense'))"
90                 query_params.append(list(period_ids))
91
92             query = "SELECT aa.code AS code, SUM(debit) as debit, SUM(credit) as credit " \
93                 " FROM account_move_line aml LEFT JOIN account_account aa ON aa.id=aml.account_id "\
94                 " WHERE "+query_cond+closed_cond+" AND aml.state='valid' AND aml.period_id = ANY(%s) GROUP BY code"
95             query_params.append(list(period_ids))
96             self.cr.execute(query, query_params)
97
98             lines =self.cr.dictfetchall()
99             for line in lines:
100                 for account in accounts:
101                     if(line["code"].startswith(account)):
102                         operator=accounts[account][0]
103                         type=accounts[account][1]
104                         value=0.0
105                         if(type == "S"):
106                             value=line["debit"]-line["credit"]
107                         elif(type == "D"):
108                             value=line["debit"]-line["credit"]
109                             if(value<0.001): value=0.0
110                         elif(type == "C"):
111                             value=line["credit"]-line["debit"]
112                             if(value<0.001): value=0.0
113                         if(operator == '+'):
114                             sum += value
115                         else:
116                             sum -= value
117                         break
118         self._set_variable(code, sum)
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: