[FIX] widget is undefined when press cost and revenus
[odoo/odoo.git] / addons / account / project / report / quantity_cost_ledger.py
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 import time
22
23 import pooler
24 from report import report_sxw
25
26 class account_analytic_quantity_cost_ledger(report_sxw.rml_parse):
27     def __init__(self, cr, uid, name, context):
28         super(account_analytic_quantity_cost_ledger, self).__init__(cr, uid, name, context=context)
29         self.localcontext.update( {
30             'time': time,
31             'lines_g': self._lines_g,
32             'lines_a': self._lines_a,
33             'sum_quantity': self._sum_quantity,
34             'account_sum_quantity': self._account_sum_quantity,
35         })
36
37     def _lines_g(self, account_id, date1, date2, journals):
38         if not journals:
39             self.cr.execute("SELECT sum(aal.unit_amount) AS quantity, \
40                         aa.code AS code, aa.name AS name, aa.id AS id \
41                     FROM account_account AS aa, account_analytic_line AS aal \
42                     WHERE (aal.account_id=%s) AND (aal.date>=%s) \
43                         AND (aal.date<=%s) AND (aal.general_account_id=aa.id) \
44                         AND aa.active \
45                     GROUP BY aa.code, aa.name, aa.id ORDER BY aa.code",
46                     (account_id, date1, date2))
47         else:
48             journal_ids = journals
49             self.cr.execute("SELECT sum(aal.unit_amount) AS quantity, \
50                         aa.code AS code, aa.name AS name, aa.id AS id \
51                     FROM account_account AS aa, account_analytic_line AS aal \
52                     WHERE (aal.account_id=%s) AND (aal.date>=%s) \
53                         AND (aal.date<=%s) AND (aal.general_account_id=aa.id) \
54                         AND aa.active \
55                         AND (aal.journal_id IN %s ) \
56                     GROUP BY aa.code, aa.name, aa.id ORDER BY aa.code",
57                     (account_id, date1, date2, tuple(journal_ids)))
58         res = self.cr.dictfetchall()
59         return res
60
61     def _lines_a(self, general_account_id, account_id, date1, date2, journals):
62         if not journals:
63             self.cr.execute("SELECT aal.name AS name, aal.code AS code, \
64                         aal.unit_amount AS quantity, aal.date AS date, \
65                         aaj.code AS cj \
66                     FROM account_analytic_line AS aal, \
67                         account_analytic_journal AS aaj \
68                     WHERE (aal.general_account_id=%s) AND (aal.account_id=%s) \
69                         AND (aal.date>=%s) AND (aal.date<=%s) \
70                         AND (aal.journal_id=aaj.id) \
71                     ORDER BY aal.date, aaj.code, aal.code",
72                     (general_account_id, account_id, date1, date2))
73         else:
74             journal_ids = journals
75             self.cr.execute("SELECT aal.name AS name, aal.code AS code, \
76                         aal.unit_amount AS quantity, aal.date AS date, \
77                         aaj.code AS cj \
78                     FROM account_analytic_line AS aal, \
79                         account_analytic_journal AS aaj \
80                     WHERE (aal.general_account_id=%s) AND (aal.account_id=%s) \
81                         AND (aal.date>=%s) AND (aal.date<=%s) \
82                         AND (aal.journal_id=aaj.id) AND (aaj.id IN %s) \
83                         ORDER BY aal.date, aaj.code, aal.code",
84                     (general_account_id, account_id, date1, date2,tuple(journal_ids)))
85         res = self.cr.dictfetchall()
86         return res
87
88     def _account_sum_quantity(self, account_id, date1, date2, journals):
89         if not journals:
90             self.cr.execute("SELECT sum(unit_amount) \
91                     FROM account_analytic_line \
92                     WHERE account_id=%s AND date>=%s AND date<=%s",
93                     (account_id, date1, date2))
94         else:
95             journal_ids = journals
96             self.cr.execute("SELECT sum(unit_amount) \
97                     FROM account_analytic_line \
98                     WHERE account_id = %s AND date >= %s AND date <= %s \
99                         AND journal_id IN %s",
100                         (account_id, date1, date2, tuple(journal_ids),))
101         return self.cr.fetchone()[0] or 0.0
102
103     def _sum_quantity(self, accounts, date1, date2, journals):
104         ids = map(lambda x: x.id, accounts)
105         if not ids:
106             return 0.0
107         if not journals:
108             self.cr.execute("SELECT sum(unit_amount) \
109                     FROM account_analytic_line \
110                     WHERE account_id IN %s AND date>=%s AND date<=%s",
111                     (tuple(ids), date1, date2,))
112         else:
113             journal_ids = journals
114             self.cr.execute("SELECT sum(unit_amount) \
115                     FROM account_analytic_line \
116                     WHERE account_id IN %s AND date >= %s AND date <= %s \
117                         AND journal_id IN %s",(tuple(ids), date1, date2, tuple(journal_ids)))
118         return self.cr.fetchone()[0] or 0.0
119
120 report_sxw.report_sxw('report.account.analytic.account.quantity_cost_ledger',
121         'account.analytic.account',
122         'addons/account/project/report/quantity_cost_ledger.rml',
123         parser=account_analytic_quantity_cost_ledger, header="internal")
124
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: