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.

847 lines
39 KiB

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