1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
26 from report import report_sxw
27 from common_report_header import common_report_header
29 class aged_trial_report(rml_parse.rml_parse, common_report_header):
31 def __init__(self, cr, uid, name, context):
32 super(aged_trial_report, self).__init__(cr, uid, name, context=context)
34 self.total_account = []
35 self.localcontext.update({
37 'get_lines_with_out_partner': self._get_lines_with_out_partner,
38 'get_lines': self._get_lines,
39 'get_total': self._get_total,
40 'get_direction': self._get_direction,
41 'get_for_period': self._get_for_period,
42 'get_company': self._get_company,
43 'get_currency': self._get_currency,
44 'get_partners':self._get_partners,
45 'get_account': self._get_account,
46 'get_fiscalyear': self._get_fiscalyear,
49 def set_context(self, objects, data, ids, report_type=None):
50 self.query = data['form'].get('query_line', '')
51 self.direction_selection = data['form'].get('direction_selection', 'past')
52 self.date_from = data['form'].get('date_from', time.strftime('%Y-%m-%d'))
53 if (data['form']['result_selection'] == 'customer' ):
54 self.ACCOUNT_TYPE = ['receivable']
55 elif (data['form']['result_selection'] == 'supplier'):
56 self.ACCOUNT_TYPE = ['payable']
58 self.ACCOUNT_TYPE = ['payable','receivable']
59 return super(aged_trial_report, self).set_context(objects, data, ids, report_type=report_type)
61 def _get_lines(self, form):
63 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
64 self.cr.execute('SELECT DISTINCT res_partner.id AS id,\
65 res_partner.name AS name \
66 FROM res_partner,account_move_line AS l, account_account\
67 WHERE (l.account_id=account_account.id)\
68 AND ((reconcile_id IS NULL)\
69 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
70 AND (l.partner_id=res_partner.id)\
71 AND ' + self.query + ' \
72 ORDER BY res_partner.name' , (self.date_from,))
73 partners = self.cr.dictfetchall()
76 self.total_account.append(0)
78 # Build a string like (1,2,3) for easy use in SQL query
79 partner_ids = [x['id'] for x in partners]
81 # This dictionary will store the debit-credit for all partners, using partner_id as key.
83 self.cr.execute('SELECT partner_id, SUM(debit-credit) \
84 FROM account_move_line AS l, account_account\
85 WHERE (l.account_id = account_account.id)\
86 AND (account_account.type IN %s)\
87 AND (partner_id IN %s)\
88 AND ((reconcile_id IS NULL)\
89 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
90 AND ' + self.query + '\
91 AND account_account.active\
92 GROUP BY partner_id ' , (tuple(self.ACCOUNT_TYPE), tuple(partner_ids), self.date_from,))
93 t = self.cr.fetchall()
97 # This dictionary will store the future or past of all partners
99 if self.direction_selection == 'future':
100 self.cr.execute('SELECT partner_id, SUM(debit-credit) \
101 FROM account_move_line AS l, account_account\
102 WHERE (l.account_id=account_account.id)\
103 AND (account_account.type IN %s)\
104 AND (COALESCE(date_maturity, date) < %s)\
105 AND (partner_id IN %s)\
106 AND ((reconcile_id IS NULL)\
107 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
108 AND '+ self.query + '\
109 AND account_account.active\
110 GROUP BY partner_id', (tuple(self.ACCOUNT_TYPE), self.date_from, tuple(partner_ids),self.date_from,))
111 t = self.cr.fetchall()
113 future_past[i[0]] = i[1]
114 elif self.direction_selection == 'past': # Using elif so people could extend without this breaking
115 self.cr.execute('SELECT partner_id, SUM(debit-credit) \
116 FROM account_move_line AS l, account_account\
117 WHERE (l.account_id=account_account.id)\
118 AND (account_account.type IN %s)\
119 AND (COALESCE(date_maturity,date) > %s)\
120 AND (partner_id IN %s)\
121 AND ((reconcile_id IS NULL)\
122 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
123 AND '+ self.query + '\
124 AND account_account.active\
125 GROUP BY partner_id' , (tuple(self.ACCOUNT_TYPE), self.date_from, tuple(partner_ids), self.date_from,))
126 t = self.cr.fetchall()
128 future_past[i[0]] = i[1]
130 # Use one query per period and store results in history (a list variable)
131 # Each history will contain : history[1] = {'<partner_id>': <partner_debit-credit>}
134 args_list = (tuple(self.ACCOUNT_TYPE), tuple(partner_ids) ,self.date_from,)
135 dates_query = '(COALESCE(date_maturity,date)'
136 if form[str(i)]['start'] and form[str(i)]['stop']:
137 dates_query += ' BETWEEN %s AND %s)'
138 args_list += (form[str(i)]['start'], form[str(i)]['stop'])
139 elif form[str(i)]['start']:
140 dates_query += ' > %s)'
141 args_list += (form[str(i)]['start'],)
143 dates_query += ' < %s)'
144 args_list += (form[str(i)]['stop'],)
145 self.cr.execute('SELECT partner_id, SUM(debit-credit)\
146 FROM account_move_line AS l, account_account\
147 WHERE (l.account_id = account_account.id)\
148 AND (account_account.type IN %s)\
149 AND (partner_id IN %s)\
150 AND ((reconcile_id IS NULL)\
151 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
152 AND '+ self.query + '\
153 AND account_account.active\
154 AND ' + dates_query + '\
155 GROUP BY partner_id' , args_list)
156 t = self.cr.fetchall()
162 for partner in partners:
164 ## If choise selection is in the future
165 if self.direction_selection == 'future':
166 # Query here is replaced by one query which gets the all the partners their 'before' value
168 if future_past.has_key(partner['id']):
169 before = [ future_past[partner['id']] ]
170 self.total_account[6] = self.total_account[6] + (before and before[0] or 0.0)
171 values['direction'] = before and before[0] or 0.0
172 elif self.direction_selection == 'past': # Changed this so people could in the future create new direction_selections
173 # Query here is replaced by one query which gets the all the partners their 'after' value
175 if future_past.has_key(partner['id']): # Making sure this partner actually was found by the query
176 after = [ future_past[partner['id']] ]
178 self.total_account[6] = self.total_account[6] + (after and after[0] or 0.0)
179 values['direction'] = after and after[0] or 0.0
183 if history[i].has_key(partner['id']):
184 during = [ history[i][partner['id']] ]
186 self.total_account[(i)] = self.total_account[(i)] + (during and during[0] or 0)
187 values[str(i)] = during and during[0] or 0.0
189 if totals.has_key( partner['id'] ):
190 total = [ totals[partner['id']] ]
191 values['total'] = total and total[0] or 0.0
193 self.total_account[(i+1)] = self.total_account[(i+1)] + (total and total[0] or 0.0)
194 values['name'] = partner['name']
202 total += float(r['total'] or 0.0)
203 for i in range(5)+['direction']:
204 totals.setdefault(str(i), 0.0)
205 totals[str(i)] += float(r[str(i)] or 0.0)
208 def _get_lines_with_out_partner(self, form):
210 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
213 self.total_account.append(0)
215 self.cr.execute('SELECT SUM(debit-credit) \
216 FROM account_move_line AS l, account_account\
217 WHERE (l.account_id = account_account.id)\
218 AND (l.partner_id IS NULL)\
219 AND (account_account.type IN %s)\
220 AND ((reconcile_id IS NULL) \
221 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
222 AND ' + self.query + '\
223 AND account_account.active ' ,(tuple(self.ACCOUNT_TYPE), self.date_from, ))
224 t = self.cr.fetchall()
226 totals['No Partner Defined'] = i[0]
228 if self.direction_selection == 'future':
229 self.cr.execute('SELECT SUM(debit-credit) \
230 FROM account_move_line AS l, account_account\
231 WHERE (l.account_id=account_account.id)\
232 AND (l.partner_id IS NULL)\
233 AND (account_account.type IN %s)\
234 AND (COALESCE(date_maturity, date) < %s)\
235 AND ((reconcile_id IS NULL)\
236 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
237 AND '+ self.query + '\
238 AND account_account.active ', (tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from, ))
239 t = self.cr.fetchall()
241 future_past['No Partner Defined'] = i[0]
242 elif self.direction_selection == 'past': # Using elif so people could extend without this breaking
243 self.cr.execute('SELECT SUM(debit-credit) \
244 FROM account_move_line AS l, account_account\
245 WHERE (l.account_id=account_account.id)\
246 AND (l.partner_id IS NULL)\
247 AND (account_account.type IN %s)\
248 AND (COALESCE(date_maturity,date) > %s)\
249 AND ((reconcile_id IS NULL)\
250 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
251 AND '+ self.query + '\
252 AND account_account.active ' , (tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from,))
253 t = self.cr.fetchall()
255 future_past['No Partner Defined'] = i[0]
258 self.cr.execute('SELECT SUM(debit-credit)\
259 FROM account_move_line AS l, account_account\
260 WHERE (l.account_id = account_account.id)\
261 AND (l.partner_id IS NULL)\
262 AND (account_account.type IN %s)\
263 AND (COALESCE(date_maturity,date) BETWEEN %s AND %s)\
264 AND ((reconcile_id IS NULL)\
265 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
266 AND '+ self.query + '\
267 AND account_account.active ' , (tuple(self.ACCOUNT_TYPE), form[str(i)]['start'], form[str(i)]['stop'], self.date_from,))
268 t = self.cr.fetchall()
271 d['No Partner Defined'] = i[0]
275 if self.direction_selection == 'future':
277 if future_past.has_key('No Partner Defined'):
278 before = [ future_past['No Partner Defined'] ]
279 self.total_account[6] = self.total_account[6] + (before and before[0] or 0.0)
280 values['direction'] = before and before[0] or 0.0
281 elif self.direction_selection == 'past':
283 if future_past.has_key('No Partner Defined'):
284 after = [ future_past['No Partner Defined'] ]
285 self.total_account[6] = self.total_account[6] + (after and after[0] or 0.0)
286 values['direction'] = after and after[0] or ""
290 if history[i].has_key('No Partner Defined'):
291 during = [ history[i]['No Partner Defined'] ]
292 self.total_account[(i)] = self.total_account[(i)] + (during and during[0] or 0)
293 values[str(i)] = during and during[0] or ""
296 if totals.has_key( 'No Partner Defined' ):
297 total = [ totals['No Partner Defined'] ]
298 values['total'] = total and total[0] or 0.0
300 self.total_account[(i+1)] = self.total_account[(i+1)] + (total and total[0] or 0.0)
301 values['name'] = 'No Partner Defined'
309 total += float(r['total'] or 0.0)
310 for i in range(5)+['direction']:
311 totals.setdefault(str(i), 0.0)
312 totals[str(i)] += float(r[str(i)] or 0.0)
315 def _get_total(self,pos):
316 period = self.total_account[int(pos)]
319 def _get_direction(self,pos):
320 period = self.total_account[int(pos)]
323 def _get_for_period(self,pos):
324 period = self.total_account[int(pos)]
327 def _get_partners(self,data):
328 if data['form']['result_selection'] == 'customer':
329 return 'Receivable Accounts'
330 elif data['form']['result_selection'] == 'supplier':
331 return 'Payable Accounts'
332 elif data['form']['result_selection'] == 'customer_supplier':
333 return 'Receivable and Payable Accounts'
336 report_sxw.report_sxw('report.account.aged_trial_balance', 'res.partner',
337 'addons/account/report/account_aged_partner_balance.rml',parser=aged_trial_report, header="internal landscape")
340 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: