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 ##############################################################################
23 from report import report_sxw
24 from common_report_header import common_report_header
26 class aged_trial_report(report_sxw.rml_parse, common_report_header):
28 def __init__(self, cr, uid, name, context):
29 super(aged_trial_report, self).__init__(cr, uid, name, context=context)
30 self.total_account = []
31 self.localcontext.update({
33 'get_lines_with_out_partner': self._get_lines_with_out_partner,
34 'get_lines': self._get_lines,
35 'get_total': self._get_total,
36 'get_direction': self._get_direction,
37 'get_for_period': self._get_for_period,
38 'get_company': self._get_company,
39 'get_currency': self._get_currency,
40 'get_partners':self._get_partners,
41 'get_account': self._get_account,
42 'get_fiscalyear': self._get_fiscalyear,
43 'get_target_move': self._get_target_move,
46 def set_context(self, objects, data, ids, report_type=None):
47 obj_move = self.pool.get('account.move.line')
48 ctx = data['form'].get('used_context', {})
49 ctx.update({'fiscalyear': False, 'all_fiscalyear': True})
50 self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx)
51 self.direction_selection = data['form'].get('direction_selection', 'past')
52 self.target_move = data['form'].get('target_move', 'all')
53 self.date_from = data['form'].get('date_from', time.strftime('%Y-%m-%d'))
54 if (data['form']['result_selection'] == 'customer' ):
55 self.ACCOUNT_TYPE = ['receivable']
56 elif (data['form']['result_selection'] == 'supplier'):
57 self.ACCOUNT_TYPE = ['payable']
59 self.ACCOUNT_TYPE = ['payable','receivable']
60 lang_dict = self.pool.get('res.users').read(self.cr,self.uid,self.uid,['context_lang'])
61 data['lang'] = lang_dict.get('context_lang') or False
62 return super(aged_trial_report, self).set_context(objects, data, ids, report_type=report_type)
64 def _get_lines(self, form):
66 move_state = ['draft','posted']
67 if self.target_move == 'posted':
68 move_state = ['posted']
69 self.cr.execute('SELECT DISTINCT res_partner.id AS id,\
70 res_partner.name AS name \
71 FROM res_partner,account_move_line AS l, account_account, account_move am\
72 WHERE (l.account_id=account_account.id) \
73 AND (l.move_id=am.id) \
75 AND (account_account.type IN %s)\
76 AND account_account.active\
77 AND ((reconcile_id IS NULL)\
78 OR (reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
79 AND (l.partner_id=res_partner.id)\
81 AND ' + self.query + ' \
82 ORDER BY res_partner.name', (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from,))
83 partners = self.cr.dictfetchall()
86 self.total_account.append(0)
88 # Build a string like (1,2,3) for easy use in SQL query
89 partner_ids = [x['id'] for x in partners]
92 # This dictionary will store the debit-credit for all partners, using partner_id as key.
95 self.cr.execute('SELECT l.partner_id, SUM(l.debit-l.credit) \
96 FROM account_move_line AS l, account_account, account_move am \
97 WHERE (l.account_id = account_account.id) AND (l.move_id=am.id) \
99 AND (account_account.type IN %s)\
100 AND (l.partner_id IN %s)\
101 AND ((l.reconcile_id IS NULL)\
102 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
103 AND ' + self.query + '\
104 AND account_account.active\
106 GROUP BY l.partner_id ', (tuple(move_state), tuple(self.ACCOUNT_TYPE), tuple(partner_ids), self.date_from, self.date_from,))
107 t = self.cr.fetchall()
111 # This dictionary will store the future or past of all partners
113 if self.direction_selection == 'future':
114 self.cr.execute('SELECT l.partner_id, SUM(l.debit-l.credit) \
115 FROM account_move_line AS l, account_account, account_move am \
116 WHERE (l.account_id=account_account.id) AND (l.move_id=am.id) \
117 AND (am.state IN %s)\
118 AND (account_account.type IN %s)\
119 AND (COALESCE(l.date_maturity, l.date) < %s)\
120 AND (l.partner_id IN %s)\
121 AND ((l.reconcile_id IS NULL)\
122 OR (l.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\
126 GROUP BY l.partner_id', (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, tuple(partner_ids),self.date_from, self.date_from,))
127 t = self.cr.fetchall()
129 future_past[i[0]] = i[1]
130 elif self.direction_selection == 'past': # Using elif so people could extend without this breaking
131 self.cr.execute('SELECT l.partner_id, SUM(l.debit-l.credit) \
132 FROM account_move_line AS l, account_account, account_move am \
133 WHERE (l.account_id=account_account.id) AND (l.move_id=am.id)\
134 AND (am.state IN %s)\
135 AND (account_account.type IN %s)\
136 AND (COALESCE(l.date_maturity,l.date) > %s)\
137 AND (l.partner_id IN %s)\
138 AND ((l.reconcile_id IS NULL)\
139 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
140 AND '+ self.query + '\
141 AND account_account.active\
143 GROUP BY l.partner_id', (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, tuple(partner_ids), self.date_from, self.date_from,))
144 t = self.cr.fetchall()
146 future_past[i[0]] = i[1]
148 # Use one query per period and store results in history (a list variable)
149 # Each history will contain: history[1] = {'<partner_id>': <partner_debit-credit>}
152 args_list = (tuple(move_state), tuple(self.ACCOUNT_TYPE), tuple(partner_ids),self.date_from,)
153 dates_query = '(COALESCE(l.date_maturity,l.date)'
154 if form[str(i)]['start'] and form[str(i)]['stop']:
155 dates_query += ' BETWEEN %s AND %s)'
156 args_list += (form[str(i)]['start'], form[str(i)]['stop'])
157 elif form[str(i)]['start']:
158 dates_query += ' > %s)'
159 args_list += (form[str(i)]['start'],)
161 dates_query += ' < %s)'
162 args_list += (form[str(i)]['stop'],)
163 args_list += (self.date_from,)
164 self.cr.execute('''SELECT l.partner_id, SUM(l.debit-l.credit)
165 FROM account_move_line AS l, account_account, account_move am
166 WHERE (l.account_id = account_account.id) AND (l.move_id=am.id)
168 AND (account_account.type IN %s)
169 AND (l.partner_id IN %s)
170 AND ((l.reconcile_id IS NULL)
171 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))
172 AND ''' + self.query + '''
173 AND account_account.active
174 AND ''' + dates_query + '''
176 GROUP BY l.partner_id''', args_list)
177 t = self.cr.fetchall()
183 for partner in partners:
185 ## If choise selection is in the future
186 if self.direction_selection == 'future':
187 # Query here is replaced by one query which gets the all the partners their 'before' value
189 if future_past.has_key(partner['id']):
190 before = [ future_past[partner['id']] ]
191 self.total_account[6] = self.total_account[6] + (before and before[0] or 0.0)
192 values['direction'] = before and before[0] or 0.0
193 elif self.direction_selection == 'past': # Changed this so people could in the future create new direction_selections
194 # Query here is replaced by one query which gets the all the partners their 'after' value
196 if future_past.has_key(partner['id']): # Making sure this partner actually was found by the query
197 after = [ future_past[partner['id']] ]
199 self.total_account[6] = self.total_account[6] + (after and after[0] or 0.0)
200 values['direction'] = after and after[0] or 0.0
204 if history[i].has_key(partner['id']):
205 during = [ history[i][partner['id']] ]
207 self.total_account[(i)] = self.total_account[(i)] + (during and during[0] or 0)
208 values[str(i)] = during and during[0] or 0.0
210 if totals.has_key( partner['id'] ):
211 total = [ totals[partner['id']] ]
212 values['total'] = total and total[0] or 0.0
214 self.total_account[(i+1)] = self.total_account[(i+1)] + (total and total[0] or 0.0)
215 values['name'] = partner['name']
222 total += float(r['total'] or 0.0)
223 for i in range(5)+['direction']:
224 totals.setdefault(str(i), 0.0)
225 totals[str(i)] += float(r[str(i)] or 0.0)
228 def _get_lines_with_out_partner(self, form):
230 move_state = ['draft','posted']
231 if self.target_move == 'posted':
232 move_state = ['posted']
236 self.total_account.append(0)
238 self.cr.execute('SELECT SUM(l.debit-l.credit) \
239 FROM account_move_line AS l, account_account, account_move am \
240 WHERE (l.account_id = account_account.id) AND (l.move_id=am.id)\
241 AND (am.state IN %s)\
242 AND (l.partner_id IS NULL)\
243 AND (account_account.type IN %s)\
244 AND ((l.reconcile_id IS NULL) \
245 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
246 AND ' + self.query + '\
248 AND account_account.active ',(tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from,))
249 t = self.cr.fetchall()
251 totals['Unknown Partner'] = i[0]
253 if self.direction_selection == 'future':
254 self.cr.execute('SELECT SUM(l.debit-l.credit) \
255 FROM account_move_line AS l, account_account, account_move am\
256 WHERE (l.account_id=account_account.id) AND (l.move_id=am.id)\
257 AND (am.state IN %s)\
258 AND (l.partner_id IS NULL)\
259 AND (account_account.type IN %s)\
260 AND (COALESCE(l.date_maturity, l.date) < %s)\
261 AND ((l.reconcile_id IS NULL)\
262 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
263 AND '+ self.query + '\
264 AND account_account.active ', (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from))
265 t = self.cr.fetchall()
267 future_past['Unknown Partner'] = i[0]
268 elif self.direction_selection == 'past': # Using elif so people could extend without this breaking
269 self.cr.execute('SELECT SUM(l.debit-l.credit) \
270 FROM account_move_line AS l, account_account, account_move am \
271 WHERE (l.account_id=account_account.id) AND (l.move_id=am.id)\
272 AND (am.state IN %s)\
273 AND (l.partner_id IS NULL)\
274 AND (account_account.type IN %s)\
275 AND (COALESCE(l.date_maturity,l.date) > %s)\
276 AND ((l.reconcile_id IS NULL)\
277 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
278 AND '+ self.query + '\
279 AND account_account.active ', (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from, self.date_from))
280 t = self.cr.fetchall()
282 future_past['Unknown Partner'] = i[0]
286 args_list = (tuple(move_state), tuple(self.ACCOUNT_TYPE), self.date_from,)
287 dates_query = '(COALESCE(l.date_maturity,l.date)'
288 if form[str(i)]['start'] and form[str(i)]['stop']:
289 dates_query += ' BETWEEN %s AND %s)'
290 args_list += (form[str(i)]['start'], form[str(i)]['stop'])
291 elif form[str(i)]['start']:
292 dates_query += ' > %s)'
293 args_list += (form[str(i)]['start'],)
295 dates_query += ' < %s)'
296 args_list += (form[str(i)]['stop'],)
297 args_list += (self.date_from,)
298 self.cr.execute('SELECT SUM(l.debit-l.credit)\
299 FROM account_move_line AS l, account_account, account_move am \
300 WHERE (l.account_id = account_account.id) AND (l.move_id=am.id)\
301 AND (am.state IN %s)\
302 AND (account_account.type IN %s)\
303 AND (l.partner_id IS NULL)\
304 AND ((l.reconcile_id IS NULL)\
305 OR (l.reconcile_id IN (SELECT recon.id FROM account_move_reconcile AS recon WHERE recon.create_date > %s )))\
306 AND '+ self.query + '\
307 AND account_account.active\
308 AND ' + dates_query + '\
310 GROUP BY l.partner_id', args_list)
311 t = self.cr.fetchall()
314 d['Unknown Partner'] = i[0]
318 if self.direction_selection == 'future':
320 if future_past.has_key('Unknown Partner'):
321 before = [ future_past['Unknown Partner'] ]
322 self.total_account[6] = self.total_account[6] + (before and before[0] or 0.0)
323 values['direction'] = before and before[0] or 0.0
324 elif self.direction_selection == 'past':
326 if future_past.has_key('Unknown Partner'):
327 after = [ future_past['Unknown Partner'] ]
328 self.total_account[6] = self.total_account[6] + (after and after[0] or 0.0)
329 values['direction'] = after and after[0] or 0.0
333 if history[i].has_key('Unknown Partner'):
334 during = [ history[i]['Unknown Partner'] ]
335 self.total_account[(i)] = self.total_account[(i)] + (during and during[0] or 0)
336 values[str(i)] = during and during[0] or 0.0
339 if totals.has_key( 'Unknown Partner' ):
340 total = [ totals['Unknown Partner'] ]
341 values['total'] = total and total[0] or 0.0
343 self.total_account[(i+1)] = self.total_account[(i+1)] + (total and total[0] or 0.0)
344 values['name'] = 'Unknown Partner'
352 total += float(r['total'] or 0.0)
353 for i in range(5)+['direction']:
354 totals.setdefault(str(i), 0.0)
355 totals[str(i)] += float(r[str(i)] or 0.0)
358 def _get_total(self,pos):
359 period = self.total_account[int(pos)]
362 def _get_direction(self,pos):
363 period = self.total_account[int(pos)]
366 def _get_for_period(self,pos):
367 period = self.total_account[int(pos)]
370 def _get_partners(self,data):
371 if data['form']['result_selection'] == 'customer':
372 return 'Receivable Accounts'
373 elif data['form']['result_selection'] == 'supplier':
374 return 'Payable Accounts'
375 elif data['form']['result_selection'] == 'customer_supplier':
376 return 'Receivable and Payable Accounts'
379 report_sxw.report_sxw('report.account.aged_trial_balance', 'res.partner',
380 'addons/account/report/account_aged_partner_balance.rml',parser=aged_trial_report, header="internal landscape")
383 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: