1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
28 from report import report_sxw
30 class third_party_ledger(rml_parse.rml_parse):
31 def __init__(self, cr, uid, name, context):
33 self.date_lst_string = ''
34 super(third_party_ledger, self).__init__(cr, uid, name, context)
35 self.localcontext.update( {
38 'sum_debit_partner': self._sum_debit_partner,
39 'sum_credit_partner': self._sum_credit_partner,
40 'sum_debit': self._sum_debit,
41 'sum_credit': self._sum_credit,
42 'get_company': self._get_company,
43 'get_currency': self._get_currency,
44 'comma_me' : self.comma_me,
46 def date_range(self,start,end):
47 if not start or not end:
49 start = datetime.date.fromtimestamp(time.mktime(time.strptime(start,"%Y-%m-%d")))
50 end = datetime.date.fromtimestamp(time.mktime(time.strptime(end,"%Y-%m-%d")))
53 r = (end+datetime.timedelta(days=1)-start).days
55 date_array = [start+datetime.timedelta(days=i) for i in range(r)]
56 for date in date_array:
57 full_str_date.append(str(date))
61 def transform_period_into_date_array(self,data):
62 ## Get All Period Date
63 if not data['form']['periods'][0][2] :
64 periods_id = self.pool.get('account.period').search(self.cr, self.uid, [('fiscalyear_id','=',data['form']['fiscalyear'])])
66 periods_id = data['form']['periods'][0][2]
68 for period_id in periods_id:
69 period_obj = self.pool.get('account.period').browse(self.cr, self.uid, period_id)
70 date_array = date_array + self.date_range(period_obj.date_start,period_obj.date_stop)
71 self.date_lst = date_array
74 def transform_date_into_date_array(self,data):
75 return_array = self.date_range(data['form']['date1'],data['form']['date2'])
76 self.date_lst = return_array
79 def transform_both_into_date_array(self,data):
81 if not data['form']['periods'][0][2] :
82 periods_id = self.pool.get('account.period').search(self.cr, self.uid, [('fiscalyear_id','=',data['form']['fiscalyear'])])
84 periods_id = data['form']['periods'][0][2]
86 for period_id in periods_id:
87 period_obj = self.pool.get('account.period').browse(self.cr, self.uid, period_id)
88 date_array = date_array + self.date_range(period_obj.date_start,period_obj.date_stop)
90 period_start_date = date_array[0]
91 date_start_date = data['form']['date1']
92 period_stop_date = date_array[-1]
93 date_stop_date = data['form']['date2']
95 if period_start_date<date_start_date:
96 start_date = period_start_date
98 start_date = date_start_date
100 if date_stop_date<period_stop_date:
101 stop_date = period_stop_date
103 stop_date = date_stop_date
104 final_date_array = []
105 final_date_array = final_date_array + self.date_range(start_date, stop_date)
106 self.date_lst = final_date_array
109 def transform_none_into_date_array(self,data):
110 sql = "SELECT min(date) as start_date from account_move_line"
112 start_date = self.cr.fetchone()[0]
113 sql = "SELECT max(date) as start_date from account_move_line"
115 stop_date = self.cr.fetchone()[0]
117 array = array + self.date_range(start_date, stop_date)
118 self.date_lst = array
122 def comma_me(self,amount):
123 if type(amount) is float :
124 amount = str('%.2f'%amount)
130 new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
134 return self.comma_me(new)
135 def special_map(self):
137 for date_string in self.date_lst:
138 string_map = date_string + ','
141 def set_context(self, objects, data, ids, report_type = None):
143 if (data['model'] == 'res.partner'):
144 ## Si on imprime depuis les partenaires
146 PARTNER_REQUEST = "AND line.partner_id IN (" + ','.join(map(str, ids)) + ")"
147 # Transformation des date
150 # if data['form']['fiscalyear']:
151 # self.transform_period_into_date_array(data)
153 # self.transform_date_into_date_array(data)
155 if data['form']['state'] == 'none':
156 self.transform_none_into_date_array(data)
157 elif data['form']['state'] == 'bydate':
158 self.transform_date_into_date_array(data)
159 elif data['form']['state'] == 'byperiod':
160 self.transform_period_into_date_array(data)
161 elif data['form']['state'] == 'all':
162 self.transform_both_into_date_array(data)
164 self.date_lst_string = ''
166 self.date_lst_string = '\'' + '\',\''.join(map(str,self.date_lst)) + '\''
168 #new_ids = [id for (id,) in self.cr.fetchall()]
169 if data['form']['result_selection'] == 'supplier':
170 self.ACCOUNT_TYPE = "('receivable')"
171 elif data['form']['result_selection'] == 'customer':
172 self.ACCOUNT_TYPE = "('payable')"
173 elif data['form']['result_selection'] == 'all':
174 self.ACCOUNT_TYPE = "('payable','receivable')"
178 "FROM account_account a " \
179 "LEFT JOIN account_account_type t " \
180 "ON (a.type=t.code) " \
181 "WHERE a.company_id = %s " \
182 'AND a.type IN ' + self.ACCOUNT_TYPE + " " \
183 "AND a.active", (data['form']['company_id'],))
184 self.account_ids = ','.join([str(a) for (a,) in self.cr.fetchall()])
186 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
189 if self.date_lst and data['form']['soldeinit'] :
192 "SELECT DISTINCT line.partner_id " \
193 "FROM account_move_line AS line, account_account AS account " \
194 "WHERE line.partner_id IS NOT NULL " \
195 "AND line.account_id = account.id " \
196 "AND line.date >= %s " \
197 "AND line.date <= %s " \
198 "AND line.reconcile_id IS NULL " \
199 "AND line.account_id IN (" + self.account_ids + ") " \
200 " " + PARTNER_REQUEST + " " \
201 "AND account.company_id = %s " \
202 "AND account.active " ,
203 (self.date_lst[0],self.date_lst[len(self.date_lst)-1],data['form']['company_id']))
207 # "SELECT DISTINCT line.partner_id " \
208 # "FROM account_move_line AS line, account_account AS account " \
209 # "WHERE line.partner_id IS NOT NULL " \
210 # "AND line.account_id = account.id " \
211 # "AND line.date IN (" + self.date_lst_string + ") " \
212 # "AND line.account_id IN (" + self.account_ids + ") " \
213 # " " + PARTNER_REQUEST + " " \
214 # "AND account.company_id = %s " \
215 # "AND account.active " ,
216 # (data['form']['company_id']))
218 res = self.cr.dictfetchall()
221 partner_to_use.append(res_line['partner_id'])
222 new_ids = partner_to_use
224 self.partner_ids = ','.join(map(str, new_ids))
225 objects = self.pool.get('res.partner').browse(self.cr, self.uid, new_ids)
226 super(third_party_ledger, self).set_context(objects, data, new_ids, report_type)
228 def lines(self, partner,data):
229 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
231 if data['form']['reconcil'] :
234 RECONCILE_TAG = "AND l.reconcile_id IS NULL"
236 # if data['form']['soldeinit'] :
239 # "SELECT l.id,l.date,j.code, l.ref, l.name, l.debit, l.credit " \
240 # "FROM account_move_line l " \
241 # "LEFT JOIN account_journal j " \
242 # "ON (l.journal_id = j.id) " \
243 # "WHERE l.partner_id = %s " \
244 # "AND l.account_id IN (" + self.account_ids + ") " \
245 # "AND l.date <= %s " \
246 # "AND l.reconcile_id IS NULL "
248 # (partner.id, self.date_lst[0]))
249 # res = self.cr.dictfetchall()
250 # print"----res----",res
253 # sum = r['debit'] - r['credit']
254 # r['progress'] = sum
255 # full_account.append(r)
256 if self.date_lst_string:
258 "SELECT l.id,l.date,j.code, l.ref, l.name, l.debit, l.credit " \
259 "FROM account_move_line l " \
260 "LEFT JOIN account_journal j " \
261 "ON (l.journal_id = j.id) " \
262 "WHERE l.partner_id = %s " \
263 "AND l.account_id IN (" + self.account_ids + ") " \
264 "AND l.date IN (" + self.date_lst_string + ") " \
265 " " + RECONCILE_TAG + " "\
268 res = self.cr.dictfetchall()
271 sum = r['debit'] - r['credit']
273 full_account.append(r)
277 def _sum_debit_partner(self, partner,data):
279 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
281 if data['form']['reconcil'] :
284 RECONCILE_TAG = "AND reconcile_id IS NULL"
285 if self.date_lst and data['form']['soldeinit'] :
287 "SELECT sum(debit) " \
288 "FROM account_move_line " \
289 "WHERE partner_id = %s " \
290 "AND account_id IN (" + self.account_ids + ") " \
291 "AND reconcile_id IS NULL " \
293 (partner.id, self.date_lst[0],))
294 contemp = self.cr.fetchone()
296 result_tmp = contemp[0] or 0.0
298 result_tmp = result_tmp + 0.0
300 if self.date_lst_string:
302 "SELECT sum(debit) " \
303 "FROM account_move_line " \
304 "WHERE partner_id = %s " \
305 "AND account_id IN (" + self.account_ids + ") " \
306 " " + RECONCILE_TAG + " " \
307 "AND date IN (" + self.date_lst_string + ") " ,
310 contemp = self.cr.fetchone()
312 result_tmp = contemp[0] or 0.0
314 result_tmp = result_tmp + 0.0
317 def _sum_credit_partner(self, partner,data):
318 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
320 if data['form']['reconcil'] :
323 RECONCILE_TAG = "AND reconcile_id IS NULL"
324 if self.date_lst and data['form']['soldeinit'] :
326 "SELECT sum(credit) " \
327 "FROM account_move_line " \
328 "WHERE partner_id=%s " \
329 "AND account_id IN (" + self.account_ids + ") " \
330 "AND reconcile_id IS NULL " \
332 (partner.id,self.date_lst[0],))
333 contemp = self.cr.fetchone()
335 result_tmp = contemp[0] or 0.0
337 result_tmp = result_tmp + 0.0
339 if self.date_lst_string:
341 "SELECT sum(credit) " \
342 "FROM account_move_line " \
343 "WHERE partner_id=%s " \
344 "AND account_id IN (" + self.account_ids + ") " \
345 " " + RECONCILE_TAG + " " \
346 "AND date IN (" + self.date_lst_string + ") " ,
349 contemp = self.cr.fetchone()
351 result_tmp = contemp[0] or 0.0
353 result_tmp = result_tmp + 0.0
356 def _sum_debit(self,data):
359 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
361 if data['form']['reconcil'] :
364 RECONCILE_TAG = "AND reconcile_id IS NULL"
365 if self.date_lst and data['form']['soldeinit'] :
367 "SELECT sum(debit) " \
368 "FROM account_move_line " \
369 "WHERE partner_id IN (" + self.partner_ids + ") " \
370 "AND account_id IN (" + self.account_ids + ") " \
371 "AND reconcile_id IS NULL " \
374 contemp = self.cr.fetchone()
376 result_tmp = contemp[0] or 0.0
378 result_tmp = result_tmp + 0.0
380 if self.date_lst_string:
382 "SELECT sum(debit) " \
383 "FROM account_move_line " \
384 "WHERE partner_id IN (" + self.partner_ids + ") " \
385 "AND account_id IN (" + self.account_ids + ") " \
386 " " + RECONCILE_TAG + " " \
387 "AND date IN (" + self.date_lst_string + ") "
390 contemp = self.cr.fetchone()
392 result_tmp = contemp[0] or 0.0
394 result_tmp = result_tmp + 0.0
399 def _sum_credit(self,data):
402 account_move_line_obj = pooler.get_pool(self.cr.dbname).get('account.move.line')
404 if data['form']['reconcil'] :
407 RECONCILE_TAG = "AND reconcile_id IS NULL"
408 if self.date_lst and data['form']['soldeinit'] :
410 "SELECT sum(credit) " \
411 "FROM account_move_line " \
412 "WHERE partner_id IN (" + self.partner_ids + ") " \
413 "AND account_id IN (" + self.account_ids + ") " \
414 "AND reconcile_id IS NULL " \
417 contemp = self.cr.fetchone()
419 result_tmp = contemp[0] or 0.0
421 result_tmp = result_tmp + 0.0
423 if self.date_lst_string:
425 "SELECT sum(credit) " \
426 "FROM account_move_line " \
427 "WHERE partner_id IN (" + self.partner_ids + ") " \
428 "AND account_id IN (" + self.account_ids + ") " \
429 " " + RECONCILE_TAG + " " \
430 "AND date IN (" + self.date_lst_string + ") "
432 contemp = self.cr.fetchone()
434 result_tmp = contemp[0] or 0.0
436 result_tmp = result_tmp + 0.0
440 def _get_company(self, form):
441 return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).name
443 def _get_currency(self, form):
444 return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).currency_id.name
446 report_sxw.report_sxw('report.account.third_party_ledger', 'res.partner',
447 'addons/account/report/third_party_ledger.rml',parser=third_party_ledger,
450 report_sxw.report_sxw('report.account.third_party_ledger_other', 'res.partner',
451 'addons/account/report/third_party_ledger_other.rml',parser=third_party_ledger,
454 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: