You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

870 lines
38 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # -*- encoding: utf-8 -*-
  2. ###########################################################################
  3. # Module Writen to OpenERP, Open Source Management Solution
  4. # Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
  5. # All Rights Reserved
  6. # Credits######################################################
  7. # Coded by: Humberto Arocha humberto@openerp.com.ve
  8. # Angelica Barrios angelicaisabelb@gmail.com
  9. # Jordi Esteve <jesteve@zikzakmedia.com>
  10. # Javier Duran <javieredm@gmail.com>
  11. # Planified by: Humberto Arocha
  12. # Finance by: LUBCAN COL S.A.S http://www.lubcancol.com
  13. # Audited by: Humberto Arocha humberto@openerp.com.ve
  14. #############################################################################
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. ##############################################################################
  28. import xml
  29. import copy
  30. from operator import itemgetter
  31. import time
  32. import datetime
  33. from report import report_sxw
  34. from tools import config
  35. from tools.translate import _
  36. from osv import osv
  37. import pdb
  38. class account_balance(report_sxw.rml_parse):
  39. def __init__(self, cr, uid, name, context):
  40. super(account_balance, self).__init__(cr, uid, name, context)
  41. self.sum_debit = 0.00
  42. self.sum_credit = 0.00
  43. self.sum_balance = 0.00
  44. self.sum_debit_fy = 0.00
  45. self.sum_credit_fy = 0.00
  46. self.sum_balance_fy = 0.00
  47. self.date_lst = []
  48. self.date_lst_string = ''
  49. self.localcontext.update({
  50. 'time': time,
  51. 'lines': self.lines,
  52. 'get_fiscalyear_text': self.get_fiscalyear_text,
  53. 'get_periods_and_date_text': self.get_periods_and_date_text,
  54. 'get_informe_text': self.get_informe_text,
  55. 'get_month': self.get_month,
  56. 'exchange_name': self.exchange_name,
  57. })
  58. self.context = context
  59. def get_fiscalyear_text(self, form):
  60. """
  61. Returns the fiscal year text used on the report.
  62. """
  63. fiscalyear_obj = self.pool.get('account.fiscalyear')
  64. fiscalyear = None
  65. if form.get('fiscalyear'):
  66. fiscalyear = fiscalyear_obj.browse(
  67. self.cr, self.uid, form['fiscalyear'])
  68. return fiscalyear.name or fiscalyear.code
  69. else:
  70. fiscalyear = fiscalyear_obj.browse(
  71. self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
  72. return "%s*" % (fiscalyear.name or fiscalyear.code)
  73. def get_informe_text(self, form):
  74. """
  75. Returns the header text used on the report.
  76. """
  77. afr_id = form['afr_id'] and type(form['afr_id']) in (
  78. list, tuple) and form['afr_id'][0] or form['afr_id']
  79. if afr_id:
  80. name = self.pool.get('afr').browse(self.cr, self.uid, afr_id).name
  81. elif form['analytic_ledger'] and form['columns'] == 'four' and form['inf_type'] == 'BS':
  82. name = _('Analytic Ledger')
  83. elif form['inf_type'] == 'BS':
  84. name = _('Balance Sheet')
  85. elif form['inf_type'] == 'IS':
  86. name = _('Income Statement')
  87. return name
  88. def get_month(self, form):
  89. '''
  90. return day, year and month
  91. '''
  92. if form['filter'] in ['bydate', 'all']:
  93. months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
  94. "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]
  95. mes = months[time.strptime(form['date_to'], "%Y-%m-%d")[1]-1]
  96. ano = time.strptime(form['date_to'], "%Y-%m-%d")[0]
  97. dia = time.strptime(form['date_to'], "%Y-%m-%d")[2]
  98. return _('From ')+self.formatLang(form['date_from'], date=True) + _(' to ')+self.formatLang(form['date_to'], date=True)
  99. elif form['filter'] in ['byperiod', 'all']:
  100. aux = []
  101. period_obj = self.pool.get('account.period')
  102. for period in period_obj.browse(self.cr, self.uid, form['periods']):
  103. aux.append(period.date_start)
  104. aux.append(period.date_stop)
  105. sorted(aux)
  106. return _('From ')+self.formatLang(aux[0], date=True)+_(' to ')+self.formatLang(aux[-1], date=True)
  107. def get_periods_and_date_text(self, form):
  108. """
  109. Returns the text with the periods/dates used on the report.
  110. """
  111. period_obj = self.pool.get('account.period')
  112. periods_str = None
  113. fiscalyear_id = form[
  114. 'fiscalyear'] or fiscalyear_obj.find(self.cr, self.uid)
  115. period_ids = period_obj.search(self.cr, self.uid, [(
  116. 'fiscalyear_id', '=', fiscalyear_id), ('special', '=', False)])
  117. if form['filter'] in ['byperiod', 'all']:
  118. period_ids = form['periods']
  119. periods_str = ', '.join([period.name or period.code for period in period_obj.browse(
  120. self.cr, self.uid, period_ids)])
  121. dates_str = None
  122. if form['filter'] in ['bydate', 'all']:
  123. dates_str = self.formatLang(form[
  124. 'date_from'], date=True) + ' - ' + self.formatLang(form['date_to'], date=True) + ' '
  125. return {'periods': periods_str, 'date': dates_str}
  126. def special_period(self, periods):
  127. period_obj = self.pool.get('account.period')
  128. period_brw = period_obj.browse(self.cr, self.uid, periods)
  129. period_counter = [True for i in period_brw if not i.special]
  130. if not period_counter:
  131. return True
  132. return False
  133. def exchange_name(self, form):
  134. self.from_currency_id = self.get_company_currency(form['company_id'] and type(form[
  135. 'company_id']) in (list, tuple) and form['company_id'][0] or form['company_id'])
  136. if not form['currency_id']:
  137. self.to_currency_id = self.from_currency_id
  138. else:
  139. self.to_currency_id = form['currency_id'] and type(form['currency_id']) in (
  140. list, tuple) and form['currency_id'][0] or form['currency_id']
  141. return self.pool.get('res.currency').browse(self.cr, self.uid, self.to_currency_id).name
  142. def exchange(self, from_amount):
  143. if self.from_currency_id == self.to_currency_id:
  144. return from_amount
  145. curr_obj = self.pool.get('res.currency')
  146. return curr_obj.compute(self.cr, self.uid, self.from_currency_id, self.to_currency_id, from_amount)
  147. def get_company_currency(self, company_id):
  148. rc_obj = self.pool.get('res.company')
  149. return rc_obj.browse(self.cr, self.uid, company_id).currency_id.id
  150. def get_company_accounts(self, company_id, acc='credit'):
  151. rc_obj = self.pool.get('res.company')
  152. if acc == 'credit':
  153. return [brw.id for brw in rc_obj.browse(self.cr, self.uid, company_id).credit_account_ids]
  154. else:
  155. return [brw.id for brw in rc_obj.browse(self.cr, self.uid, company_id).debit_account_ids]
  156. def _get_analytic_ledger(self, account, ctx={}):
  157. res = []
  158. if account['type'] in ('other', 'liquidity', 'receivable', 'payable'):
  159. #~ TODO: CUANDO EL PERIODO ESTE VACIO LLENARLO CON LOS PERIODOS DEL EJERCICIO
  160. #~ FISCAL, SIN LOS PERIODOS ESPECIALES
  161. periods = ', '.join([str(i) for i in ctx['periods']])
  162. #~ periods = str(tuple(ctx['periods']))
  163. where = """where aml.period_id in (%s) and aa.id = %s and aml.state <> 'draft'""" % (
  164. periods, account['id'])
  165. sql_detalle = """select aml.id as id, aj.name as diario, aa.name as descripcion,
  166. (select name from res_partner where aml.partner_id = id) as partner,
  167. aa.code as cuenta, aml.name as name,
  168. aml.ref as ref,
  169. case when aml.debit is null then 0.00 else aml.debit end as debit,
  170. case when aml.credit is null then 0.00 else aml.credit end as credit,
  171. (select code from account_analytic_account where aml.analytic_account_id = id) as analitica,
  172. aml.date as date, ap.name as periodo,
  173. am.name as asiento
  174. from account_move_line aml
  175. inner join account_journal aj on aj.id = aml.journal_id
  176. inner join account_account aa on aa.id = aml.account_id
  177. inner join account_period ap on ap.id = aml.period_id
  178. inner join account_move am on am.id = aml.move_id """ + where +\
  179. """ order by date, am.name"""
  180. self.cr.execute(sql_detalle)
  181. resultat = self.cr.dictfetchall()
  182. balance = account['balanceinit']
  183. for det in resultat:
  184. balance += det['debit'] - det['credit']
  185. res.append({
  186. 'id': det['id'],
  187. 'date': det['date'],
  188. 'journal': det['diario'],
  189. 'partner': det['partner'],
  190. 'name': det['name'],
  191. 'entry': det['asiento'],
  192. 'ref': det['ref'],
  193. 'debit': det['debit'],
  194. 'credit': det['credit'],
  195. 'analytic': det['analitica'],
  196. 'period': det['periodo'],
  197. 'balance': balance,
  198. })
  199. return res
  200. def lines(self, form, level=0):
  201. """
  202. Returns all the data needed for the report lines
  203. (account info plus debit/credit/balance in the selected period
  204. and the full year)
  205. """
  206. account_obj = self.pool.get('account.account')
  207. period_obj = self.pool.get('account.period')
  208. fiscalyear_obj = self.pool.get('account.fiscalyear')
  209. def _get_children_and_consol(cr, uid, ids, level, context={}, change_sign=False):
  210. aa_obj = self.pool.get('account.account')
  211. ids2 = []
  212. for aa_brw in aa_obj.browse(cr, uid, ids, context):
  213. if aa_brw.child_id and aa_brw.level < level and aa_brw.type != 'consolidation':
  214. if not change_sign:
  215. ids2.append([aa_brw.id, True, False, aa_brw])
  216. ids2 += _get_children_and_consol(cr, uid, [
  217. x.id for x in aa_brw.child_id], level, context, change_sign=change_sign)
  218. if change_sign:
  219. ids2.append(aa_brw.id)
  220. else:
  221. ids2.append([aa_brw.id, False, True, aa_brw])
  222. else:
  223. if change_sign:
  224. ids2.append(aa_brw.id)
  225. else:
  226. ids2.append([aa_brw.id, True, True, aa_brw])
  227. return ids2
  228. #############################################################################
  229. # CONTEXT FOR ENDIND BALANCE #
  230. #######################################################################
  231. def _ctx_end(ctx):
  232. ctx_end = ctx
  233. ctx_end['filter'] = form.get('filter', 'all')
  234. ctx_end['fiscalyear'] = fiscalyear.id
  235. #~ ctx_end['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)])
  236. if ctx_end['filter'] not in ['bydate', 'none']:
  237. special = self.special_period(form['periods'])
  238. else:
  239. special = False
  240. if form['filter'] in ['byperiod', 'all']:
  241. if special:
  242. ctx_end['periods'] = period_obj.search(self.cr, self.uid, [(
  243. 'id', 'in', form['periods'] or ctx_end.get('periods', False))])
  244. else:
  245. ctx_end['periods'] = period_obj.search(self.cr, self.uid, [('id', 'in', form[
  246. 'periods'] or ctx_end.get('periods', False)), ('special', '=', False)])
  247. if form['filter'] in ['bydate', 'all', 'none']:
  248. ctx_end['date_from'] = form['date_from']
  249. ctx_end['date_to'] = form['date_to']
  250. return ctx_end.copy()
  251. def missing_period(ctx_init):
  252. ctx_init['fiscalyear'] = fiscalyear_obj.search(self.cr, self.uid, [('date_stop', '<', fiscalyear.date_start)], order='date_stop') and \
  253. fiscalyear_obj.search(self.cr, self.uid, [(
  254. 'date_stop', '<', fiscalyear.date_start)], order='date_stop')[-1] or []
  255. ctx_init['periods'] = period_obj.search(self.cr, self.uid, [(
  256. 'fiscalyear_id', '=', ctx_init['fiscalyear']), ('date_stop', '<', fiscalyear.date_start)])
  257. return ctx_init
  258. #############################################################################
  259. # CONTEXT FOR INITIAL BALANCE #
  260. #######################################################################
  261. def _ctx_init(ctx):
  262. ctx_init = self.context.copy()
  263. ctx_init['filter'] = form.get('filter', 'all')
  264. ctx_init['fiscalyear'] = fiscalyear.id
  265. if form['filter'] in ['byperiod', 'all']:
  266. ctx_init['periods'] = form['periods']
  267. if not ctx_init['periods']:
  268. ctx_init = missing_period(ctx_init.copy())
  269. date_start = min([period.date_start for period in period_obj.browse(
  270. self.cr, self.uid, ctx_init['periods'])])
  271. ctx_init['periods'] = period_obj.search(self.cr, self.uid, [(
  272. 'fiscalyear_id', '=', fiscalyear.id), ('date_stop', '<=', date_start)])
  273. elif form['filter'] in ['bydate']:
  274. ctx_init['date_from'] = fiscalyear.date_start
  275. ctx_init['date_to'] = form['date_from']
  276. ctx_init['periods'] = period_obj.search(self.cr, self.uid, [(
  277. 'fiscalyear_id', '=', fiscalyear.id), ('date_stop', '<=', ctx_init['date_to'])])
  278. elif form['filter'] == 'none':
  279. ctx_init['periods'] = period_obj.search(self.cr, self.uid, [(
  280. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', True)])
  281. date_start = min([period.date_start for period in period_obj.browse(
  282. self.cr, self.uid, ctx_init['periods'])])
  283. ctx_init['periods'] = period_obj.search(self.cr, self.uid, [(
  284. 'fiscalyear_id', '=', fiscalyear.id), ('date_start', '<=', date_start), ('special', '=', True)])
  285. return ctx_init.copy()
  286. def z(n):
  287. return abs(n) < 0.005 and 0.0 or n
  288. self.from_currency_id = self.get_company_currency(form['company_id'] and type(form[
  289. 'company_id']) in (list, tuple) and form['company_id'][0] or form['company_id'])
  290. if not form['currency_id']:
  291. self.to_currency_id = self.from_currency_id
  292. else:
  293. self.to_currency_id = form['currency_id'] and type(form['currency_id']) in (
  294. list, tuple) and form['currency_id'][0] or form['currency_id']
  295. if 'account_list' in form and form['account_list']:
  296. account_ids = form['account_list']
  297. del form['account_list']
  298. credit_account_ids = self.get_company_accounts(form['company_id'] and type(form[
  299. 'company_id']) in (list, tuple) and form['company_id'][0] or form['company_id'], 'credit')
  300. debit_account_ids = self.get_company_accounts(form['company_id'] and type(form[
  301. 'company_id']) in (list, tuple) and form['company_id'][0] or form['company_id'], 'debit')
  302. if form.get('fiscalyear'):
  303. if type(form.get('fiscalyear')) in (list, tuple):
  304. fiscalyear = form['fiscalyear'] and form['fiscalyear'][0]
  305. elif type(form.get('fiscalyear')) in (int,):
  306. fiscalyear = form['fiscalyear']
  307. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear)
  308. ################################################################
  309. # Get the accounts #
  310. ################################################################
  311. start_time = time.clock()
  312. account_ids = _get_children_and_consol(self.cr, self.uid, account_ids, form[
  313. 'display_account_level'] and form['display_account_level'] or 100, self.context)
  314. credit_account_ids = _get_children_and_consol(
  315. self.cr, self.uid, credit_account_ids, 100, self.context, change_sign=True)
  316. debit_account_ids = _get_children_and_consol(
  317. self.cr, self.uid, debit_account_ids, 100, self.context, change_sign=True)
  318. credit_account_ids = list(set(
  319. credit_account_ids) - set(debit_account_ids))
  320. print time.clock() - start_time, "seconds"
  321. #
  322. # Generate the report lines (checking each account)
  323. #
  324. tot_check = False
  325. if not form['periods']:
  326. form['periods'] = period_obj.search(self.cr, self.uid, [(
  327. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  328. if not form['periods']:
  329. raise osv.except_osv(_('UserError'), _(
  330. 'The Selected Fiscal Year Does not have Regular Periods'))
  331. if form['columns'] == 'qtr':
  332. period_ids = period_obj.search(self.cr, self.uid, [(
  333. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  334. a = 0
  335. l = []
  336. p = []
  337. for x in period_ids:
  338. a += 1
  339. if a < 3:
  340. l.append(x)
  341. else:
  342. l.append(x)
  343. p.append(l)
  344. l = []
  345. a = 0
  346. #~ period_ids = p
  347. elif form['columns'] == 'thirteen':
  348. period_ids = period_obj.search(self.cr, self.uid, [(
  349. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  350. if form['columns'] == 'qtr':
  351. tot_bal1 = 0.0
  352. tot_bal2 = 0.0
  353. tot_bal3 = 0.0
  354. tot_bal4 = 0.0
  355. tot_bal5 = 0.0
  356. elif form['columns'] == 'thirteen':
  357. tot_bal1 = 0.0
  358. tot_bal2 = 0.0
  359. tot_bal3 = 0.0
  360. tot_bal4 = 0.0
  361. tot_bal5 = 0.0
  362. tot_bal6 = 0.0
  363. tot_bal7 = 0.0
  364. tot_bal8 = 0.0
  365. tot_bal9 = 0.0
  366. tot_bal10 = 0.0
  367. tot_bal11 = 0.0
  368. tot_bal12 = 0.0
  369. tot_bal13 = 0.0
  370. else:
  371. ctx_init = _ctx_init(self.context.copy())
  372. ctx_end = _ctx_end(self.context.copy())
  373. tot_bin = 0.0
  374. tot_deb = 0.0
  375. tot_crd = 0.0
  376. tot_ytd = 0.0
  377. tot_eje = 0.0
  378. res = {}
  379. result_acc = []
  380. tot = {}
  381. start_time = time.clock()
  382. ###############################################################
  383. #
  384. #
  385. ###############################################################
  386. pdb.set_trace()
  387. print period_ids
  388. for p_id in period_ids:
  389. form['periods'] = [p_id]
  390. ctx_init = _ctx_init(self.context.copy())
  391. account_black_ids = account_obj.search(self.cr, self.uid, ([('id', 'in', [i[0] for i in account_ids]),('type','not in',('view','consolidation'))]))
  392. account_black = account_obj.browse(self.cr, self.uid, account_black_ids, ctx_init)
  393. account_black.sort(key=lambda x: x.level)
  394. account_black.reverse()
  395. account_black_ids = [i.id for i in account_black]
  396. account_not_black_ids = account_obj.search(self.cr, self.uid, ([('id', 'in', [i[0] for i in account_ids]),('type','in',('view','consolidation'))]))
  397. account_not_black = account_obj.browse(self.cr, self.uid, account_not_black_ids, ctx_init)
  398. account_not_black.sort(key=lambda x: x.level)
  399. account_not_black.reverse()
  400. account_not_black_ids = [i.id for i in account_not_black]
  401. dict_black = {}
  402. print "Negros"
  403. for i in account_black:
  404. black_data = {}
  405. black_data['obj'] = i
  406. black_data['debit'] = i.debit
  407. black_data['credit'] = i.credit
  408. black_data['balance'] = i.balance
  409. black_data['parent_id'] = i.parent_id
  410. print i.id, i.name
  411. #print i.level
  412. #print black_data
  413. dict_black[i.id] = black_data
  414. dict_not_black = {}
  415. print "No Negros"
  416. for i in account_not_black:
  417. not_black_data = {}
  418. not_black_data['obj'] = i
  419. not_black_data['debit'] = 0.0
  420. not_black_data['credit'] = 0.0
  421. not_black_data['balance'] = 0.0
  422. not_black_data['parent_id'] = i.parent_id
  423. dict_not_black[i.id] = not_black_data
  424. all_account = dict_black.copy() #se hace una copia, porque se modificara
  425. print "##################"
  426. for i in dict_not_black:
  427. print dict_not_black[i].get('obj').name , dict_not_black[i].get('debit')
  428. for acc_id in account_not_black_ids:
  429. print dict_not_black[acc_id].get('obj').name
  430. acc_childs = dict_not_black[acc_id].get('obj')._get_child_ids(self.cr, self.uid).popitem()[1] #hijos de la cuenta i (id)
  431. for child_id in acc_childs:
  432. dict_not_black[acc_id]['debit'] += all_account[child_id].get('debit')
  433. all_account[acc_id] = dict_not_black[acc_id]
  434. print "##################"
  435. for i in all_account:
  436. print all_account[i].get('obj').name , all_account[i].get('debit')
  437. #import pdb
  438. #pdb.set_trace()
  439. print time.clock() - start_time, "seconds"
  440. #
  441. ###############################################################
  442. #
  443. #
  444. ###############################################################
  445. start_time = time.clock()
  446. for aa_id in account_ids:
  447. id = aa_id[0]
  448. #
  449. # Check if we need to include this level
  450. #
  451. if not form['display_account_level'] or aa_id[3].level <= form['display_account_level']:
  452. res = {
  453. 'id': id,
  454. 'type': aa_id[3].type,
  455. 'code': aa_id[3].code,
  456. 'name': (aa_id[2] and not aa_id[1]) and 'TOTAL %s' % (aa_id[3].name.upper()) or aa_id[3].name,
  457. 'parent_id': aa_id[3].parent_id and aa_id[3].parent_id.id,
  458. 'level': aa_id[3].level,
  459. 'label': aa_id[1],
  460. 'total': aa_id[2],
  461. 'change_sign': credit_account_ids and (id in credit_account_ids and -1 or 1) or 1
  462. }
  463. if form['columns'] == 'qtr':
  464. pn = 1
  465. for p_id in p:
  466. form['periods'] = p_id
  467. ctx_init = _ctx_init(self.context.copy())
  468. aa_brw_init = account_obj.browse(
  469. self.cr, self.uid, id, ctx_init)
  470. ctx_end = _ctx_end(self.context.copy())
  471. aa_brw_end = account_obj.browse(
  472. self.cr, self.uid, id, ctx_end)
  473. if form['inf_type'] == 'IS':
  474. d, c, b = map(z, [
  475. aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
  476. res.update({
  477. 'dbr%s' % pn: self.exchange(d),
  478. 'cdr%s' % pn: self.exchange(c),
  479. 'bal%s' % pn: self.exchange(b),
  480. })
  481. else:
  482. i, d, c = map(z, [
  483. aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
  484. b = z(i+d-c)
  485. res.update({
  486. 'dbr%s' % pn: self.exchange(d),
  487. 'cdr%s' % pn: self.exchange(c),
  488. 'bal%s' % pn: self.exchange(b),
  489. })
  490. pn += 1
  491. form['periods'] = period_ids
  492. ctx_init = _ctx_init(self.context.copy())
  493. aa_brw_init = account_obj.browse(
  494. self.cr, self.uid, id, ctx_init)
  495. ctx_end = _ctx_end(self.context.copy())
  496. aa_brw_end = account_obj.browse(
  497. self.cr, self.uid, id, ctx_end)
  498. if form['inf_type'] == 'IS':
  499. d, c, b = map(z, [
  500. aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
  501. res.update({
  502. 'dbr5': self.exchange(d),
  503. 'cdr5': self.exchange(c),
  504. 'bal5': self.exchange(b),
  505. })
  506. else:
  507. i, d, c = map(z, [
  508. aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
  509. b = z(i+d-c)
  510. res.update({
  511. 'dbr5': self.exchange(d),
  512. 'cdr5': self.exchange(c),
  513. 'bal5': self.exchange(b),
  514. })
  515. elif form['columns'] == 'thirteen':
  516. pn = 1
  517. for p_id in period_ids:
  518. form['periods'] = [p_id]
  519. ctx_init = _ctx_init(self.context.copy())
  520. aa_brw_init = account_obj.browse(
  521. self.cr, self.uid, id, ctx_init)
  522. ctx_end = _ctx_end(self.context.copy())
  523. aa_brw_end = account_obj.browse(
  524. self.cr, self.uid, id, ctx_end)
  525. if form['inf_type'] == 'IS':
  526. d, c, b = map(z, [
  527. aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
  528. res.update({
  529. 'dbr%s' % pn: self.exchange(d),
  530. 'cdr%s' % pn: self.exchange(c),
  531. 'bal%s' % pn: self.exchange(b),
  532. })
  533. else:
  534. i, d, c = map(z, [
  535. aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
  536. b = z(i+d-c)
  537. res.update({
  538. 'dbr%s' % pn: self.exchange(d),
  539. 'cdr%s' % pn: self.exchange(c),
  540. 'bal%s' % pn: self.exchange(b),
  541. })
  542. pn += 1
  543. form['periods'] = period_ids
  544. ctx_init = _ctx_init(self.context.copy())
  545. aa_brw_init = account_obj.browse(
  546. self.cr, self.uid, id, ctx_init)
  547. ctx_end = _ctx_end(self.context.copy())
  548. aa_brw_end = account_obj.browse(
  549. self.cr, self.uid, id, ctx_end)
  550. if form['inf_type'] == 'IS':
  551. d, c, b = map(z, [
  552. aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
  553. res.update({
  554. 'dbr13': self.exchange(d),
  555. 'cdr13': self.exchange(c),
  556. 'bal13': self.exchange(b),
  557. })
  558. else:
  559. i, d, c = map(z, [
  560. aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
  561. b = z(i+d-c)
  562. res.update({
  563. 'dbr13': self.exchange(d),
  564. 'cdr13': self.exchange(c),
  565. 'bal13': self.exchange(b),
  566. })
  567. else:
  568. aa_brw_init = account_obj.browse(
  569. self.cr, self.uid, id, ctx_init)
  570. aa_brw_end = account_obj.browse(
  571. self.cr, self.uid, id, ctx_end)
  572. i, d, c = map(z, [
  573. aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
  574. b = z(i+d-c)
  575. res.update({
  576. 'balanceinit': self.exchange(i),
  577. 'debit': self.exchange(d),
  578. 'credit': self.exchange(c),
  579. 'ytd': self.exchange(d-c),
  580. })
  581. if form['inf_type'] == 'IS' and form['columns'] == 'one':
  582. res.update({
  583. 'balance': self.exchange(d-c),
  584. })
  585. else:
  586. res.update({
  587. 'balance': self.exchange(b),
  588. })
  589. #
  590. # Check whether we must include this line in the report or not
  591. #
  592. to_include = False
  593. if form['columns'] in ('thirteen', 'qtr'):
  594. to_test = [False]
  595. if form['display_account'] == 'mov' and aa_id[3].parent_id:
  596. # Include accounts with movements
  597. for x in range(pn-1):
  598. to_test.append(res.get(
  599. 'dbr%s' % x, 0.0) >= 0.005 and True or False)
  600. to_test.append(res.get(
  601. 'cdr%s' % x, 0.0) >= 0.005 and True or False)
  602. if any(to_test):
  603. to_include = True
  604. elif form['display_account'] == 'bal' and aa_id[3].parent_id:
  605. # Include accounts with balance
  606. for x in range(pn-1):
  607. to_test.append(res.get(
  608. 'bal%s' % x, 0.0) >= 0.005 and True or False)
  609. if any(to_test):
  610. to_include = True
  611. elif form['display_account'] == 'bal_mov' and aa_id[3].parent_id:
  612. # Include accounts with balance or movements
  613. for x in range(pn-1):
  614. to_test.append(res.get(
  615. 'bal%s' % x, 0.0) >= 0.005 and True or False)
  616. to_test.append(res.get(
  617. 'dbr%s' % x, 0.0) >= 0.005 and True or False)
  618. to_test.append(res.get(
  619. 'cdr%s' % x, 0.0) >= 0.005 and True or False)
  620. if any(to_test):
  621. to_include = True
  622. else:
  623. # Include all accounts
  624. to_include = True
  625. else:
  626. if form['display_account'] == 'mov' and aa_id[3].parent_id:
  627. # Include accounts with movements
  628. if abs(d) >= 0.005 or abs(c) >= 0.005:
  629. to_include = True
  630. elif form['display_account'] == 'bal' and aa_id[3].parent_id:
  631. # Include accounts with balance
  632. if abs(b) >= 0.005:
  633. to_include = True
  634. elif form['display_account'] == 'bal_mov' and aa_id[3].parent_id:
  635. # Include accounts with balance or movements
  636. if abs(b) >= 0.005 or abs(d) >= 0.005 or abs(c) >= 0.005:
  637. to_include = True
  638. else:
  639. # Include all accounts
  640. to_include = True
  641. #~ ANALYTIC LEDGER
  642. if to_include and form['analytic_ledger'] and form['columns'] == 'four' and form['inf_type'] == 'BS' and res['type'] in ('other', 'liquidity', 'receivable', 'payable'):
  643. res['mayor'] = self._get_analytic_ledger(res, ctx=ctx_end)
  644. else:
  645. res['mayor'] = []
  646. if to_include:
  647. result_acc.append(res)
  648. #
  649. # Check whether we must sumarize this line in the report or not
  650. #
  651. if form['tot_check'] and res['type'] == 'view' and res['level'] == 1 and (res['id'] not in tot):
  652. if form['columns'] == 'qtr':
  653. tot_check = True
  654. #~ tot[res['id']] = True
  655. tot_bal1 += res.get('bal1', 0.0)
  656. tot_bal2 += res.get('bal2', 0.0)
  657. tot_bal3 += res.get('bal3', 0.0)
  658. tot_bal4 += res.get('bal4', 0.0)
  659. tot_bal5 += res.get('bal5', 0.0)
  660. elif form['columns'] == 'thirteen':
  661. tot_check = True
  662. #~ tot[res['id']] = True
  663. tot_bal1 += res.get('bal1', 0.0)
  664. tot_bal2 += res.get('bal2', 0.0)
  665. tot_bal3 += res.get('bal3', 0.0)
  666. tot_bal4 += res.get('bal4', 0.0)
  667. tot_bal5 += res.get('bal5', 0.0)
  668. tot_bal6 += res.get('bal6', 0.0)
  669. tot_bal7 += res.get('bal7', 0.0)
  670. tot_bal8 += res.get('bal8', 0.0)
  671. tot_bal9 += res.get('bal9', 0.0)
  672. tot_bal10 += res.get('bal10', 0.0)
  673. tot_bal11 += res.get('bal11', 0.0)
  674. tot_bal12 += res.get('bal12', 0.0)
  675. tot_bal13 += res.get('bal13', 0.0)
  676. else:
  677. tot_check = True
  678. #~ tot[res['id']] = True
  679. tot_bin += res['balanceinit']
  680. tot_deb += res['debit']
  681. tot_crd += res['credit']
  682. tot_ytd += res['ytd']
  683. tot_eje += res['balance']
  684. print time.clock() - start_time, "seconds"
  685. if tot_check:
  686. str_label = form['lab_str']
  687. res2 = {
  688. 'type': 'view',
  689. 'name': 'TOTAL %s' % (str_label),
  690. 'label': False,
  691. 'total': True,
  692. }
  693. if form['columns'] == 'qtr':
  694. res2.update(dict(
  695. bal1=tot_bal1,
  696. bal2=tot_bal2,
  697. bal3=tot_bal3,
  698. bal4=tot_bal4,
  699. bal5=tot_bal5,))
  700. elif form['columns'] == 'thirteen':
  701. res2.update(dict(
  702. bal1=tot_bal1,
  703. bal2=tot_bal2,
  704. bal3=tot_bal3,
  705. bal4=tot_bal4,
  706. bal5=tot_bal5,
  707. bal6=tot_bal6,
  708. bal7=tot_bal7,
  709. bal8=tot_bal8,
  710. bal9=tot_bal9,
  711. bal10=tot_bal10,
  712. bal11=tot_bal11,
  713. bal12=tot_bal12,
  714. bal13=tot_bal13,))
  715. else:
  716. res2.update({
  717. 'balanceinit': tot_bin,
  718. 'debit': tot_deb,
  719. 'credit': tot_crd,
  720. 'ytd': tot_ytd,
  721. 'balance': tot_eje,
  722. })
  723. result_acc.append(res2)
  724. return result_acc
  725. report_sxw.report_sxw('report.afr.1cols',
  726. 'wizard.report',
  727. 'account_financial_report/report/balance_full.rml',
  728. parser=account_balance,
  729. header=False)
  730. report_sxw.report_sxw('report.afr.2cols',
  731. 'wizard.report',
  732. 'account_financial_report/report/balance_full_2_cols.rml',
  733. parser=account_balance,
  734. header=False)
  735. report_sxw.report_sxw('report.afr.4cols',
  736. 'wizard.report',
  737. 'account_financial_report/report/balance_full_4_cols.rml',
  738. parser=account_balance,
  739. header=False)
  740. report_sxw.report_sxw('report.afr.analytic.ledger',
  741. 'wizard.report',
  742. 'account_financial_report/report/balance_full_4_cols_analytic_ledger.rml',
  743. parser=account_balance,
  744. header=False)
  745. report_sxw.report_sxw('report.afr.5cols',
  746. 'wizard.report',
  747. 'account_financial_report/report/balance_full_5_cols.rml',
  748. parser=account_balance,
  749. header=False)
  750. report_sxw.report_sxw('report.afr.qtrcols',
  751. 'wizard.report',
  752. 'account_financial_report/report/balance_full_qtr_cols.rml',
  753. parser=account_balance,
  754. header=False)
  755. report_sxw.report_sxw('report.afr.13cols',
  756. 'wizard.report',
  757. 'account_financial_report/report/balance_full_13_cols.rml',
  758. parser=account_balance,
  759. header=False)