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 ##############################################################################
24 from report import report_sxw
25 from common_report_header import common_report_header
26 from tools.translate import _
28 class third_party_ledger(report_sxw.rml_parse, common_report_header):
30 def __init__(self, cr, uid, name, context=None):
31 super(third_party_ledger, self).__init__(cr, uid, name, context=context)
32 self.init_bal_sum = 0.0
33 self.localcontext.update({
36 'sum_debit_partner': self._sum_debit_partner,
37 'sum_credit_partner': self._sum_credit_partner,
38 # 'sum_debit': self._sum_debit,
39 # 'sum_credit': self._sum_credit,
40 'get_currency': self._get_currency,
41 'comma_me': self.comma_me,
42 'get_start_period': self.get_start_period,
43 'get_end_period': self.get_end_period,
44 'get_account': self._get_account,
45 'get_filter': self._get_filter,
46 'get_start_date': self._get_start_date,
47 'get_end_date': self._get_end_date,
48 'get_fiscalyear': self._get_fiscalyear,
49 'get_journal': self._get_journal,
50 'get_partners':self._get_partners,
51 'get_intial_balance':self._get_intial_balance,
52 'display_initial_balance':self._display_initial_balance,
53 'display_currency':self._display_currency,
54 'get_target_move': self._get_target_move,
57 def _get_filter(self, data):
58 if data['form']['filter'] == 'unreconciled':
59 return _('Unreconciled Entries')
60 return super(third_party_ledger, self)._get_filter(data)
62 def set_context(self, objects, data, ids, report_type=None):
63 obj_move = self.pool.get('account.move.line')
64 obj_partner = self.pool.get('res.partner')
65 self.query = obj_move._query_get(self.cr, self.uid, obj='l', context=data['form'].get('used_context', {}))
66 ctx2 = data['form'].get('used_context',{}).copy()
67 self.initial_balance = data['form'].get('initial_balance', True)
68 if self.initial_balance:
69 ctx2.update({'initial_bal': True})
70 self.init_query = obj_move._query_get(self.cr, self.uid, obj='l', context=ctx2)
72 if data['form']['filter'] == 'unreconciled':
74 self.result_selection = data['form'].get('result_selection', 'customer')
75 self.amount_currency = data['form'].get('amount_currency', False)
76 self.target_move = data['form'].get('target_move', 'all')
78 move_state = ['draft','posted']
79 if self.target_move == 'posted':
80 move_state = ['posted']
82 if (data['model'] == 'res.partner'):
83 ## Si on imprime depuis les partenaires
85 PARTNER_REQUEST = "AND line.partner_id IN %s",(tuple(ids),)
86 if self.result_selection == 'supplier':
87 self.ACCOUNT_TYPE = ['payable']
88 elif self.result_selection == 'customer':
89 self.ACCOUNT_TYPE = ['receivable']
91 self.ACCOUNT_TYPE = ['payable','receivable']
95 "FROM account_account a " \
96 "LEFT JOIN account_account_type t " \
97 "ON (a.type=t.code) " \
98 'WHERE a.type IN %s' \
99 "AND a.active", (tuple(self.ACCOUNT_TYPE), ))
100 self.account_ids = [a for (a,) in self.cr.fetchall()]
103 "SELECT DISTINCT l.partner_id " \
104 "FROM account_move_line AS l, account_account AS account, " \
105 " account_move AS am " \
106 "WHERE l.partner_id IS NOT NULL " \
107 "AND l.account_id = account.id " \
108 "AND am.id = l.move_id " \
110 # "AND " + self.query +" " \
111 "AND l.account_id IN %s " \
112 " " + PARTNER_REQUEST + " " \
113 "AND account.active ",
114 (tuple(move_state), tuple(self.account_ids),))
116 res = self.cr.dictfetchall()
118 partner_to_use.append(res_line['partner_id'])
119 new_ids = partner_to_use
120 self.partner_ids = new_ids
121 objects = obj_partner.browse(self.cr, self.uid, new_ids)
122 return super(third_party_ledger, self).set_context(objects, data, new_ids, report_type)
124 def comma_me(self, amount):
125 if type(amount) is float:
126 amount = str('%.2f'%amount)
132 new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
136 return self.comma_me(new)
138 def lines(self, partner):
139 move_state = ['draft','posted']
140 if self.target_move == 'posted':
141 move_state = ['posted']
147 RECONCILE_TAG = "AND l.reconcile_id IS NULL"
149 "SELECT l.id, l.date, j.code, acc.code as a_code, acc.name as a_name, l.ref, m.name as move_name, l.name, l.debit, l.credit, l.amount_currency,l.currency_id, c.symbol AS currency_code " \
150 "FROM account_move_line l " \
151 "LEFT JOIN account_journal j " \
152 "ON (l.journal_id = j.id) " \
153 "LEFT JOIN account_account acc " \
154 "ON (l.account_id = acc.id) " \
155 "LEFT JOIN res_currency c ON (l.currency_id=c.id)" \
156 "LEFT JOIN account_move m ON (m.id=l.move_id)" \
157 "WHERE l.partner_id = %s " \
158 "AND l.account_id IN %s AND " + self.query +" " \
159 "AND m.state IN %s " \
160 " " + RECONCILE_TAG + " "\
162 (partner.id, tuple(self.account_ids), tuple(move_state)))
163 res = self.cr.dictfetchall()
165 if self.initial_balance:
166 sum = self.init_bal_sum
168 sum += r['debit'] - r['credit']
170 full_account.append(r)
173 def _get_intial_balance(self, partner):
174 move_state = ['draft','posted']
175 if self.target_move == 'posted':
176 move_state = ['posted']
180 RECONCILE_TAG = "AND l.reconcile_id IS NULL"
182 "SELECT COALESCE(SUM(l.debit),0.0), COALESCE(SUM(l.credit),0.0), COALESCE(sum(debit-credit), 0.0) " \
183 "FROM account_move_line AS l, " \
185 "WHERE l.partner_id = %s " \
186 "AND m.id = l.move_id " \
188 "AND account_id IN %s" \
189 " " + RECONCILE_TAG + " "\
190 "AND " + self.init_query + " ",
191 (partner.id, tuple(move_state), tuple(self.account_ids)))
192 res = self.cr.fetchall()
193 self.init_bal_sum = res[0][2]
196 def _sum_debit_partner(self, partner):
197 move_state = ['draft','posted']
198 if self.target_move == 'posted':
199 move_state = ['posted']
206 RECONCILE_TAG = "AND reconcile_id IS NULL"
207 if self.initial_balance:
209 "SELECT sum(debit) " \
210 "FROM account_move_line AS l, " \
212 "WHERE l.partner_id = %s" \
213 "AND m.id = l.move_id " \
215 "AND account_id IN %s" \
216 " " + RECONCILE_TAG + " " \
217 "AND " + self.init_query + " ",
218 (partner.id, tuple(move_state), tuple(self.account_ids)))
219 contemp = self.cr.fetchone()
221 result_init = contemp[0] or 0.0
223 result_init = result_tmp + 0.0
226 "SELECT sum(debit) " \
227 "FROM account_move_line AS l, " \
229 "WHERE l.partner_id = %s " \
230 "AND m.id = l.move_id " \
232 "AND account_id IN %s" \
233 " " + RECONCILE_TAG + " " \
234 "AND " + self.query + " ",
235 (partner.id, tuple(move_state), tuple(self.account_ids),))
237 contemp = self.cr.fetchone()
239 result_tmp = contemp[0] or 0.0
241 result_tmp = result_tmp + 0.0
243 return result_tmp + result_init
245 def _sum_credit_partner(self, partner):
246 move_state = ['draft','posted']
247 if self.target_move == 'posted':
248 move_state = ['posted']
255 RECONCILE_TAG = "AND reconcile_id IS NULL"
256 if self.initial_balance:
258 "SELECT sum(credit) " \
259 "FROM account_move_line AS l, " \
261 "WHERE l.partner_id = %s" \
262 "AND m.id = l.move_id " \
264 "AND account_id IN %s" \
265 " " + RECONCILE_TAG + " " \
266 "AND " + self.init_query + " ",
267 (partner.id, tuple(move_state), tuple(self.account_ids)))
268 contemp = self.cr.fetchone()
270 result_init = contemp[0] or 0.0
272 result_init = result_tmp + 0.0
275 "SELECT sum(credit) " \
276 "FROM account_move_line AS l, " \
278 "WHERE l.partner_id=%s " \
279 "AND m.id = l.move_id " \
281 "AND account_id IN %s" \
282 " " + RECONCILE_TAG + " " \
283 "AND " + self.query + " ",
284 (partner.id, tuple(move_state), tuple(self.account_ids),))
286 contemp = self.cr.fetchone()
288 result_tmp = contemp[0] or 0.0
290 result_tmp = result_tmp + 0.0
291 return result_tmp + result_init
294 # def _sum_debit(self):
295 # move_state = ['draft','posted']
296 # if self.target_move == 'posted':
297 # move_state = ['posted']
304 # RECONCILE_TAG = " "
306 # RECONCILE_TAG = "AND reconcile_id IS NULL"
307 # if self.initial_balance:
309 # "SELECT sum(debit) " \
310 # "FROM account_move_line AS l, " \
311 # "account_move AS m "
312 # "WHERE partner_id IN %s" \
313 # "AND m.id = l.move_id " \
314 # "AND m.state IN %s "
315 # "AND account_id IN %s" \
316 # "AND reconcile_id IS NULL " \
317 # "AND " + self.init_query + " ",
318 # (tuple(self.partner_ids), tuple(move_state), tuple(self.account_ids)))
319 # contemp = self.cr.fetchone()
320 # if contemp != None:
321 # result_init = contemp[0] or 0.0
323 # result_init = result_tmp + 0.0
326 # "SELECT sum(debit) " \
327 # "FROM account_move_line AS l, " \
328 # "account_move AS m "
329 # "WHERE partner_id IN %s" \
330 # "AND m.id = l.move_id " \
331 # "AND m.state IN %s "
332 # "AND account_id IN %s" \
333 # " " + RECONCILE_TAG + " " \
334 # "AND " + self.query + " ",
335 # (tuple(self.partner_ids), tuple(move_state) ,tuple(self.account_ids),))
336 # contemp = self.cr.fetchone()
337 # if contemp != None:
338 # result_tmp = contemp[0] or 0.0
340 # result_tmp = result_tmp + 0.0
341 # return result_tmp + result_init
343 # def _sum_credit(self):
344 # move_state = ['draft','posted']
345 # if self.target_move == 'posted':
346 # move_state = ['posted']
353 # RECONCILE_TAG = " "
355 # RECONCILE_TAG = "AND reconcile_id IS NULL"
356 # if self.initial_balance:
358 # "SELECT sum(credit) " \
359 # "FROM account_move_line AS l, " \
360 # "account_move AS m "
361 # "WHERE partner_id IN %s" \
362 # "AND m.id = l.move_id " \
363 # "AND m.state IN %s "
364 # "AND account_id IN %s" \
365 # "AND reconcile_id IS NULL " \
366 # "AND " + self.init_query + " ",
367 # (tuple(self.partner_ids), tuple(move_state), tuple(self.account_ids)))
368 # contemp = self.cr.fetchone()
369 # if contemp != None:
370 # result_init = contemp[0] or 0.0
372 # result_init = result_tmp + 0.0
375 # "SELECT sum(credit) " \
376 # "FROM account_move_line AS l, " \
377 # "account_move AS m "
378 # "WHERE partner_id IN %s" \
379 # "AND m.id = l.move_id " \
380 # "AND m.state IN %s "
381 # "AND account_id IN %s" \
382 # " " + RECONCILE_TAG + " " \
383 # "AND " + self.query + " ",
384 # (tuple(self.partner_ids), tuple(move_state), tuple(self.account_ids),))
385 # contemp = self.cr.fetchone()
386 # if contemp != None:
387 # result_tmp = contemp[0] or 0.0
389 # result_tmp = result_tmp + 0.0
390 # return result_tmp + result_init
392 def _get_partners(self):
393 if self.result_selection == 'customer':
394 return 'Receivable Accounts'
395 elif self.result_selection == 'supplier':
396 return 'Payable Accounts'
397 elif self.result_selection == 'customer_supplier':
398 return 'Receivable and Payable Accounts'
401 def _sum_currency_amount_account(self, account, form):
402 self._set_get_account_currency_code(account.id)
403 self.cr.execute("SELECT sum(aml.amount_currency) FROM account_move_line as aml,res_currency as rc WHERE aml.currency_id = rc.id AND aml.account_id= %s ", (account.id,))
404 total = self.cr.fetchone()
405 if self.account_currency:
406 return_field = str(total[0]) + self.account_currency
409 currency_total = self.tot_currency = 0.0
410 return currency_total
412 def _display_initial_balance(self, data):
413 if self.initial_balance:
417 def _display_currency(self, data):
418 if self.amount_currency:
422 report_sxw.report_sxw('report.account.third_party_ledger', 'res.partner',
423 'addons/account/report/account_partner_ledger.rml',parser=third_party_ledger,
426 report_sxw.report_sxw('report.account.third_party_ledger_other', 'res.partner',
427 'addons/account/report/account_partner_ledger_other.rml',parser=third_party_ledger,
430 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: