20
20
# You should have received a copy of the GNU Affero General Public License
21
21
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
################################################################################
22
##########################################################################
26
26
from report import report_sxw
27
from tools.translate import _
27
from openerp.tools.translate import _
28
29
from account.report import common_report_header
30
32
class ledger_report(report_sxw.rml_parse, common_report_header.common_report_header):
31
33
_name = 'report.report.ledger'
35
37
obj_move = self.pool.get('account.move.line')
36
38
self.sortby = data['form'].get('sortby', 'sort_date')
37
self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context',{}))
38
self.query_get = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context',{}))
39
ctx2 = data['form'].get('used_context',{}).copy()
39
self.query = obj_move._query_get(
40
self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
41
self.query_get = obj_move._query_get(
42
self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
43
ctx2 = data['form'].get('used_context', {}).copy()
40
44
ctx2.update({'initial_bal': True})
41
self.init_query = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx2)
42
self.init_query_get = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx2)
45
self.init_query = obj_move._query_get(
46
self.cr, self.uid, obj='l', context=ctx2)
47
self.init_query_get = obj_move._query_get(
48
self.cr, self.uid, obj='l', context=ctx2)
43
49
self.init_balance = data['form']['initial_balance']
44
50
self.display_account = data['form']['display_account']
45
51
self.target_move = data['form'].get('target_move', 'all')
46
52
## Added partner_id
47
53
self.partner_id = data['form']['partner_id']
48
self.query += self.partner_id and (" AND l.partner_id IN (%s) " % (self.partner_id)) or ""
49
self.init_query += self.partner_id and (" AND l.partner_id IN (%s) " % (self.partner_id)) or ""
54
self.query += self.partner_id and (
55
" AND l.partner_id IN (%s) " % (self.partner_id)) or ""
56
self.init_query += self.partner_id and (
57
" AND l.partner_id IN (%s) " % (self.partner_id)) or ""
50
58
ctx = self.context.copy()
51
59
ctx['fiscalyear'] = data['form']['fiscalyear_id']
52
60
if data['form']['filter'] == 'filter_period':
53
ctx['period_from']= data['form'].get('period_from', False)
54
ctx['period_to']= data['form'].get('period_to', False)
61
ctx['period_from'] = data['form'].get('period_from', False)
62
ctx['period_to'] = data['form'].get('period_to', False)
55
63
elif data['form']['filter'] == 'filter_date':
56
64
ctx['date_from'] = data['form']['date_from']
57
ctx['date_to'] = data['form']['date_to']
65
ctx['date_to'] = data['form']['date_to']
58
66
ctx['state'] = data['form']['target_move']
59
67
self.context.update(ctx)
60
68
if (data['model'] == 'ir.ui.menu'):
61
69
new_ids = [data['form']['chart_account_id']]
62
objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
70
objects = self.pool.get('account.account').browse(
71
self.cr, self.uid, new_ids)
63
72
return super(ledger_report, self).set_context(objects, data, new_ids, report_type=report_type)
65
74
def __init__(self, cr, uid, name, context=None):
88
97
'get_filter': self._get_filter,
89
98
'get_sortby': self._get_sortby,
90
99
'print_partner': self._print_partner,
91
'get_start_date':self._get_start_date,
92
'get_end_date':self._get_end_date,
100
'get_start_date': self._get_start_date,
101
'get_end_date': self._get_end_date,
93
102
'get_target_move': self._get_target_move,
95
104
self.context = context
100
109
def _sum_currency_amount_account(self, account):
101
110
self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
102
111
FROM account_move_line l \
103
WHERE l.account_id = %s AND %s' %(account.id, self.query))
112
WHERE l.account_id = %s AND %s' % (account.id, self.query))
104
113
sum_currency = self.cr.fetchone()[0] or 0.0
105
114
if self.init_balance:
106
115
self.cr.execute('SELECT sum(l.amount_currency) AS tot_currency \
107
116
FROM account_move_line l \
108
WHERE l.account_id = %s AND %s '%(account.id, self.init_query))
117
WHERE l.account_id = %s AND %s ' % (account.id, self.init_query))
109
118
sum_currency += self.cr.fetchone()[0] or 0.0
110
119
return sum_currency
112
121
def get_children_accounts(self, account):
114
123
currency_obj = self.pool.get('res.currency')
115
ids_acc = self.pool.get('account.account')._get_children_and_consol(self.cr, self.uid, account.id)
124
ids_acc = self.pool.get('account.account')._get_children_and_consol(
125
self.cr, self.uid, account.id)
116
126
currency = account.currency_id and account.currency_id or account.company_id.currency_id
117
127
for child_account in self.pool.get('account.account').browse(self.cr, self.uid, ids_acc, context=self.context):
125
135
sold_account = self._sum_balance_account(child_account)
126
136
self.sold_accounts[child_account.id] = sold_account
127
137
if self.display_account == 'bal_movement':
128
if child_account.type != 'view' and num_entry <> 0:
138
if child_account.type != 'view' and num_entry != 0:
129
139
res.append(child_account)
130
140
elif self.display_account == 'bal_solde':
131
if child_account.type != 'view' and num_entry <> 0:
141
if child_account.type != 'view' and num_entry != 0:
132
142
if not currency_obj.is_zero(self.cr, self.uid, currency, sold_account):
133
143
res.append(child_account)
166
176
# Then select all account_move_line of this account
167
177
if self.sortby == 'sort_journal_partner':
168
sql_sort='j.code, p.name, l.move_id'
178
sql_sort = 'j.code, p.name, l.move_id'
170
sql_sort='l.date, l.move_id'
180
sql_sort = 'l.date, l.move_id'
172
182
SELECT l.id AS lid, l.date AS ldate, j.code AS lcode, l.currency_id,l.amount_currency,l.ref AS lref, l.name AS lname, COALESCE(l.debit,0) AS debit, COALESCE(l.credit,0) AS credit, l.period_id AS lperiod_id, l.partner_id AS lpartner_id,
173
183
m.name AS move_name, m.id AS mmove_id,per.code as period_code,
182
192
LEFT JOIN account_period per on (per.id=l.period_id)
183
193
JOIN account_journal j on (l.journal_id=j.id)
184
194
WHERE %s AND m.state IN %s AND l.account_id = %%s ORDER by %s
185
""" %(self.query, tuple(move_state), sql_sort)
195
""" % (self.query, tuple(move_state), sql_sort)
186
196
self.cr.execute(sql, (account.id,))
187
197
res_lines = self.cr.dictfetchall()
189
199
if res_lines and self.init_balance:
190
#FIXME: replace the label of lname with a string translatable
200
# FIXME: replace the label of lname with a string translatable
192
202
SELECT 0 AS lid, '' AS ldate, '' AS lcode, COALESCE(SUM(l.amount_currency),0.0) AS amount_currency, '' AS lref, 'Initial Balance' AS lname, COALESCE(SUM(l.debit),0.0) AS debit, COALESCE(SUM(l.credit),0.0) AS credit, '' AS lperiod_id, '' AS lpartner_id,
193
203
'' AS move_name, '' AS mmove_id, '' AS period_code,
202
212
LEFT JOIN account_invoice i on (m.id =i.move_id)
203
213
JOIN account_journal j on (l.journal_id=j.id)
204
214
WHERE %s AND m.state IN %s AND l.account_id = %%s
205
""" %(self.init_query, tuple(move_state))
215
""" % (self.init_query, tuple(move_state))
206
216
self.cr.execute(sql, (account.id,))
207
217
res_init = self.cr.dictfetchall()
208
218
res = res_init + res_lines
209
219
account_sum = 0.0
211
l['move'] = l['move_name'] != '/' and l['move_name'] or ('*'+str(l['mmove_id']))
221
l['move'] = l['move_name'] != '/' and l[
222
'move_name'] or ('*'+str(l['mmove_id']))
212
223
l['partner'] = l['partner_name'] or ''
213
224
account_sum += l['debit'] - l['credit']
214
225
l['progress'] = account_sum
215
l['line_corresp'] = l['mmove_id'] == '' and ' ' or counterpart_accounts[l['mmove_id']].replace(', ',',')
226
l['line_corresp'] = l['mmove_id'] == '' and ' ' or counterpart_accounts[
227
l['mmove_id']].replace(', ', ',')
216
228
# Modification of amount Currency
217
229
if l['credit'] > 0:
218
230
if l['amount_currency'] != None:
224
236
def _sum_debit_account(self, account):
225
237
if account.type == 'view':
226
238
return account.debit
227
move_state = ['draft','posted']
239
move_state = ['draft', 'posted']
228
240
if self.target_move == 'posted':
229
move_state = ['posted','']
241
move_state = ['posted', '']
230
242
self.cr.execute('SELECT sum(debit) \
231
243
FROM account_move_line l \
232
244
JOIN account_move am ON (am.id = l.move_id) \
233
245
WHERE (l.account_id = %s) \
234
246
AND (am.state IN %s) \
235
AND '+ self.query +' '
236
,(account.id, tuple(move_state)))
247
AND ' + self.query + ' ', (account.id, tuple(move_state)))
237
248
sum_debit = self.cr.fetchone()[0] or 0.0
238
249
if self.init_balance:
239
250
self.cr.execute('SELECT sum(debit) \
250
260
def _sum_credit_account(self, account):
251
261
if account.type == 'view':
252
262
return account.credit
253
move_state = ['draft','posted']
263
move_state = ['draft', 'posted']
254
264
if self.target_move == 'posted':
255
move_state = ['posted','']
265
move_state = ['posted', '']
256
266
self.cr.execute('SELECT sum(credit) \
257
267
FROM account_move_line l \
258
268
JOIN account_move am ON (am.id = l.move_id) \
259
269
WHERE (l.account_id = %s) \
260
270
AND (am.state IN %s) \
261
AND '+ self.query +' '
262
,(account.id, tuple(move_state)))
271
AND ' + self.query + ' ', (account.id, tuple(move_state)))
263
272
sum_credit = self.cr.fetchone()[0] or 0.0
264
273
if self.init_balance:
265
274
self.cr.execute('SELECT sum(credit) \
276
284
def _sum_balance_account(self, account):
277
285
if account.type == 'view':
278
286
return account.balance
279
move_state = ['draft','posted']
287
move_state = ['draft', 'posted']
280
288
if self.target_move == 'posted':
281
move_state = ['posted','']
289
move_state = ['posted', '']
282
290
self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
283
291
FROM account_move_line l \
284
292
JOIN account_move am ON (am.id = l.move_id) \
285
293
WHERE (l.account_id = %s) \
286
294
AND (am.state IN %s) \
287
AND '+ self.query +' '
288
,(account.id, tuple(move_state)))
295
AND ' + self.query + ' ', (account.id, tuple(move_state)))
289
296
sum_balance = self.cr.fetchone()[0] or 0.0
290
297
if self.init_balance:
291
298
self.cr.execute('SELECT (sum(debit) - sum(credit)) as tot_balance \
302
308
def _get_account(self, data):
303
309
if data['model'] == 'account.account':
304
310
return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.name
305
return super(ledger_report ,self)._get_account(data)
311
return super(ledger_report, self)._get_account(data)
307
313
def _get_rif(self, data):
308
314
return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.partner_id.vat
314
320
return _('Journal & Partner')
317
#report_sxw.report_sxw('report.account.general.ledger_landscape', 'account.account', 'addons/account/report/account_general_ledger_landscape.rml', parser=general_ledger, header='internal landscape')
323
# report_sxw.report_sxw('report.account.general.ledger_landscape',
325
# 'addons/account/report/account_general_ledger_landscape.rml',
326
# parser=general_ledger, header='internal landscape')
318
327
report_sxw.report_sxw(
319
328
'report.report.ledger',
320
329
'account.account',
321
330
'addons/ledger_report_partner/report/request_ledger_report.rml',
322
331
parser=ledger_report,
325
334
report_sxw.report_sxw(
326
335
'report.report.ledger_partner_field',
327
336
'account.account',
328
337
'addons/ledger_report_partner/report/request_ledger_report_partner_field.rml',
329
338
parser=ledger_report,
333
342
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: