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
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(
  313. self.cr, self.uid, account_ids, 100, self.context)
  314. account_ids = _get_children_and_consol(self.cr, self.uid, account_ids, form[
  315. 'display_account_level'] and form['display_account_level'] or 100, self.context)
  316. credit_account_ids = _get_children_and_consol(
  317. self.cr, self.uid, credit_account_ids, 100, self.context, change_sign=True)
  318. debit_account_ids = _get_children_and_consol(
  319. self.cr, self.uid, debit_account_ids, 100, self.context, change_sign=True)
  320. credit_account_ids = list(set(
  321. credit_account_ids) - set(debit_account_ids))
  322. #
  323. # Generate the report lines (checking each account)
  324. #
  325. tot_check = False
  326. if not form['periods']:
  327. form['periods'] = period_obj.search(self.cr, self.uid, [(
  328. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  329. if not form['periods']:
  330. raise osv.except_osv(_('UserError'), _(
  331. 'The Selected Fiscal Year Does not have Regular Periods'))
  332. if form['columns'] == 'qtr':
  333. period_ids = period_obj.search(self.cr, self.uid, [(
  334. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  335. a = 0
  336. l = []
  337. p = []
  338. for x in period_ids:
  339. a += 1
  340. if a < 3:
  341. l.append(x)
  342. else:
  343. l.append(x)
  344. p.append(l)
  345. l = []
  346. a = 0
  347. tot_bal1 = 0.0
  348. tot_bal2 = 0.0
  349. tot_bal3 = 0.0
  350. tot_bal4 = 0.0
  351. tot_bal5 = 0.0
  352. elif form['columns'] == 'thirteen':
  353. period_ids = period_obj.search(self.cr, self.uid, [(
  354. 'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
  355. tot_bal1 = 0.0
  356. tot_bal1 = 0.0
  357. tot_bal2 = 0.0
  358. tot_bal3 = 0.0
  359. tot_bal4 = 0.0
  360. tot_bal5 = 0.0
  361. tot_bal6 = 0.0
  362. tot_bal7 = 0.0
  363. tot_bal8 = 0.0
  364. tot_bal9 = 0.0
  365. tot_bal10 = 0.0
  366. tot_bal11 = 0.0
  367. tot_bal12 = 0.0
  368. tot_bal13 = 0.0
  369. else:
  370. ctx_end = _ctx_end(self.context.copy())
  371. tot_bin = 0.0
  372. tot_deb = 0.0
  373. tot_crd = 0.0
  374. tot_ytd = 0.0
  375. tot_eje = 0.0
  376. res = {}
  377. result_acc = []
  378. tot = {}
  379. ###############################################################
  380. # Calculations of credit, debit and balance,
  381. # without repeating operations.
  382. ###############################################################
  383. account_black_ids = account_obj.search(self.cr, self.uid, (
  384. [('id', 'in', [i[0] for i in all_account_ids]),
  385. ('type', 'not in',
  386. ('view', 'consolidation'))]))
  387. account_not_black_ids = account_obj.search(self.cr, self.uid, ([('id', 'in', [
  388. i[0] for i in all_account_ids]), ('type', 'in', ('view', 'consolidation'))]))
  389. # This could be done quickly with a sql sentence
  390. account_not_black = account_obj.browse(
  391. self.cr, self.uid, account_not_black_ids)
  392. account_not_black.sort(key=lambda x: x.level)
  393. account_not_black.reverse()
  394. account_not_black_ids = [i.id for i in account_not_black]
  395. all_account_period = {} # All accounts per period
  396. # Iteration limit depending on the number of columns
  397. if form['columns'] == 'thirteen':
  398. limit = 13
  399. elif form['columns'] == 'qtr':
  400. limit = 5
  401. else:
  402. limit = 1
  403. for p_act in range(limit):
  404. if limit != 1:
  405. if p_act == limit-1:
  406. form['periods'] = period_ids
  407. else:
  408. if form['columns'] == 'thirteen':
  409. form['periods'] = [period_ids[p_act]]
  410. elif form['columns'] == 'qtr':
  411. form['periods'] = p[p_act]
  412. if form['inf_type'] == 'IS':
  413. ctx_to_use = _ctx_end(self.context.copy())
  414. else:
  415. ctx_i = _ctx_init(self.context.copy())
  416. ctx_to_use = _ctx_end(self.context.copy())
  417. account_black = account_obj.browse(
  418. self.cr, self.uid, account_black_ids, ctx_to_use)
  419. if form['inf_type'] == 'BS':
  420. account_black_init = account_obj.browse(
  421. self.cr, self.uid, account_black_ids, ctx_i)
  422. #~ Black
  423. dict_black = {}
  424. for i in account_black:
  425. d = i.debit
  426. c = i.credit
  427. dict_black[i.id] = {
  428. 'obj': i,
  429. 'debit': d,
  430. 'credit': c,
  431. 'balance': d-c
  432. }
  433. if form['inf_type'] == 'BS':
  434. dict_black.get(i.id)['balanceinit'] = 0.0
  435. # If the report is a balance sheet
  436. # Balanceinit values are added to the dictionary
  437. if form['inf_type'] == 'BS':
  438. for i in account_black_init:
  439. dict_black.get(i.id)['balanceinit'] = i.balance
  440. #~ Not black
  441. dict_not_black = {}
  442. for i in account_not_black:
  443. dict_not_black[i.id] = {
  444. 'obj': i, 'debit': 0.0, 'credit': 0.0, 'balance': 0.0}
  445. if form['inf_type'] == 'BS':
  446. dict_not_black.get(i.id)['balanceinit'] = 0.0
  447. all_account = dict_black.copy(
  448. ) #It makes a copy because they modify
  449. for acc_id in account_not_black_ids:
  450. acc_childs = dict_not_black.get(acc_id).get('obj').child_id
  451. for child_id in acc_childs:
  452. dict_not_black.get(acc_id)['debit'] += all_account.get(
  453. child_id.id).get('debit')
  454. dict_not_black.get(acc_id)['credit'] += all_account.get(
  455. child_id.id).get('credit')
  456. dict_not_black.get(acc_id)['balance'] += all_account.get(
  457. child_id.id).get('balance')
  458. if form['inf_type'] == 'BS':
  459. dict_not_black.get(acc_id)['balanceinit'] += all_account.get(
  460. child_id.id).get('balanceinit')
  461. all_account[acc_id] = dict_not_black[acc_id]
  462. if p_act == limit-1:
  463. all_account_period['all'] = all_account
  464. else:
  465. if form['columns'] == 'thirteen':
  466. all_account_period[p_act] = all_account
  467. elif form['columns'] == 'qtr':
  468. all_account_period[p_act] = all_account
  469. ###############################################################
  470. # End of the calculations of credit, debit and balance
  471. #
  472. ###############################################################
  473. for aa_id in account_ids:
  474. id = aa_id[0]
  475. #
  476. # Check if we need to include this level
  477. #
  478. if not form['display_account_level'] or aa_id[3].level <= form['display_account_level']:
  479. res = {
  480. 'id': id,
  481. 'type': aa_id[3].type,
  482. 'code': aa_id[3].code,
  483. 'name': (aa_id[2] and not aa_id[1]) and 'TOTAL %s' % (aa_id[3].name.upper()) or aa_id[3].name,
  484. 'parent_id': aa_id[3].parent_id and aa_id[3].parent_id.id,
  485. 'level': aa_id[3].level,
  486. 'label': aa_id[1],
  487. 'total': aa_id[2],
  488. 'change_sign': credit_account_ids and (id in credit_account_ids and -1 or 1) or 1
  489. }
  490. if form['columns'] == 'qtr':
  491. for pn in range(1, 5):
  492. if form['inf_type'] == 'IS':
  493. d, c, b = map(z, [
  494. all_account_period.get(pn-1).get(id).get('debit', 0.0), all_account_period.get(pn-1).get(id).get('credit', 0.0), all_account_period.get(pn-1).get(id).get('balance', 0.0)])
  495. res.update({
  496. 'dbr%s' % pn: self.exchange(d),
  497. 'cdr%s' % pn: self.exchange(c),
  498. 'bal%s' % pn: self.exchange(b),
  499. })
  500. else:
  501. i, d, c = map(z, [
  502. all_account_period.get(pn-1).get(id).get('balanceinit', 0.0), all_account_period.get(pn-1).get(id).get('debit', 0.0), all_account_period.get(pn-1).get(id).get('credit', 0.0)])
  503. b = z(i+d-c)
  504. res.update({
  505. 'dbr%s' % pn: self.exchange(d),
  506. 'cdr%s' % pn: self.exchange(c),
  507. 'bal%s' % pn: self.exchange(b),
  508. })
  509. if form['inf_type'] == 'IS':
  510. d, c, b = map(z, [
  511. all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit',0.0), all_account_period.get('all').get(id).get('balance')])
  512. res.update({
  513. 'dbr5': self.exchange(d),
  514. 'cdr5': self.exchange(c),
  515. 'bal5': self.exchange(b),
  516. })
  517. else:
  518. i, d, c = map(z, [
  519. all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit',0.0)])
  520. b = z(i+d-c)
  521. res.update({
  522. 'dbr5': self.exchange(d),
  523. 'cdr5': self.exchange(c),
  524. 'bal5': self.exchange(b),
  525. })
  526. elif form['columns'] == 'thirteen':
  527. pn = 1
  528. for p_num in range(12):
  529. if form['inf_type'] == 'IS':
  530. d, c, b = map(z, [
  531. all_account_period.get(p_num).get(id).get('debit', 0.0), all_account_period.get(p_num).get(id).get('credit', 0.0), all_account_period.get(p_num).get(id).get('balance', 0.0)])
  532. res.update({
  533. 'dbr%s' % pn: self.exchange(d),
  534. 'cdr%s' % pn: self.exchange(c),
  535. 'bal%s' % pn: self.exchange(b),
  536. })
  537. else:
  538. i, d, c = map(z, [
  539. all_account_period.get(p_num).get(id).get('balanceinit', 0.0), all_account_period.get(p_num).get(id).get('debit', 0.0), all_account_period.get(p_num).get(id).get('credit', 0.0)])
  540. b = z(i+d-c)
  541. res.update({
  542. 'dbr%s' % pn: self.exchange(d),
  543. 'cdr%s' % pn: self.exchange(c),
  544. 'bal%s' % pn: self.exchange(b),
  545. })
  546. pn += 1
  547. if form['inf_type'] == 'IS':
  548. d, c, b = map(z, [
  549. all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0), all_account_period.get('all').get(id).get('balance', 0.0)])
  550. res.update({
  551. 'dbr13': self.exchange(d),
  552. 'cdr13': self.exchange(c),
  553. 'bal13': self.exchange(b),
  554. })
  555. else:
  556. i, d, c = map(z, [
  557. all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0)])
  558. b = z(i+d-c)
  559. res.update({
  560. 'dbr13': self.exchange(d),
  561. 'cdr13': self.exchange(c),
  562. 'bal13': self.exchange(b),
  563. })
  564. else:
  565. i, d, c = map(z, [
  566. all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0)])
  567. b = z(i+d-c)
  568. res.update({
  569. 'balanceinit': self.exchange(i),
  570. 'debit': self.exchange(d),
  571. 'credit': self.exchange(c),
  572. 'ytd': self.exchange(d-c),
  573. })
  574. if form['inf_type'] == 'IS' and form['columns'] == 'one':
  575. res.update({
  576. 'balance': self.exchange(d-c),
  577. })
  578. else:
  579. res.update({
  580. 'balance': self.exchange(b),
  581. })
  582. #
  583. # Check whether we must include this line in the report or not
  584. #
  585. to_include = False
  586. if form['columns'] in ('thirteen', 'qtr'):
  587. to_test = [False]
  588. if form['display_account'] == 'mov' and aa_id[3].parent_id:
  589. # Include accounts with movements
  590. for x in range(pn-1):
  591. to_test.append(res.get(
  592. 'dbr%s' % x, 0.0) >= 0.005 and True or False)
  593. to_test.append(res.get(
  594. 'cdr%s' % x, 0.0) >= 0.005 and True or False)
  595. if any(to_test):
  596. to_include = True
  597. elif form['display_account'] == 'bal' and aa_id[3].parent_id:
  598. # Include accounts with balance
  599. for x in range(pn-1):
  600. to_test.append(res.get(
  601. 'bal%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_mov' and aa_id[3].parent_id:
  605. # Include accounts with balance or movements
  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. to_test.append(res.get(
  610. 'dbr%s' % x, 0.0) >= 0.005 and True or False)
  611. to_test.append(res.get(
  612. 'cdr%s' % x, 0.0) >= 0.005 and True or False)
  613. if any(to_test):
  614. to_include = True
  615. else:
  616. # Include all accounts
  617. to_include = True
  618. else:
  619. if form['display_account'] == 'mov' and aa_id[3].parent_id:
  620. # Include accounts with movements
  621. if abs(d) >= 0.005 or abs(c) >= 0.005:
  622. to_include = True
  623. elif form['display_account'] == 'bal' and aa_id[3].parent_id:
  624. # Include accounts with balance
  625. if abs(b) >= 0.005:
  626. to_include = True
  627. elif form['display_account'] == 'bal_mov' and aa_id[3].parent_id:
  628. # Include accounts with balance or movements
  629. if abs(b) >= 0.005 or abs(d) >= 0.005 or abs(c) >= 0.005:
  630. to_include = True
  631. else:
  632. # Include all accounts
  633. to_include = True
  634. #~ ANALYTIC LEDGER
  635. if to_include and form['analytic_ledger'] and form['columns'] == 'four' and form['inf_type'] == 'BS' and res['type'] in ('other', 'liquidity', 'receivable', 'payable'):
  636. res['mayor'] = self._get_analytic_ledger(res, ctx=ctx_end)
  637. else:
  638. res['mayor'] = []
  639. if to_include:
  640. result_acc.append(res)
  641. #
  642. # Check whether we must sumarize this line in the report or not
  643. #
  644. if form['tot_check'] and res['type'] == 'view' and res['level'] == 1 and (res['id'] not in tot):
  645. if form['columns'] == 'qtr':
  646. tot_check = True
  647. #~ tot[res['id']] = True
  648. tot_bal1 += res.get('bal1', 0.0)
  649. tot_bal2 += res.get('bal2', 0.0)
  650. tot_bal3 += res.get('bal3', 0.0)
  651. tot_bal4 += res.get('bal4', 0.0)
  652. tot_bal5 += res.get('bal5', 0.0)
  653. elif form['columns'] == 'thirteen':
  654. tot_check = True
  655. #~ tot[res['id']] = True
  656. tot_bal1 += res.get('bal1', 0.0)
  657. tot_bal2 += res.get('bal2', 0.0)
  658. tot_bal3 += res.get('bal3', 0.0)
  659. tot_bal4 += res.get('bal4', 0.0)
  660. tot_bal5 += res.get('bal5', 0.0)
  661. tot_bal6 += res.get('bal6', 0.0)
  662. tot_bal7 += res.get('bal7', 0.0)
  663. tot_bal8 += res.get('bal8', 0.0)
  664. tot_bal9 += res.get('bal9', 0.0)
  665. tot_bal10 += res.get('bal10', 0.0)
  666. tot_bal11 += res.get('bal11', 0.0)
  667. tot_bal12 += res.get('bal12', 0.0)
  668. tot_bal13 += res.get('bal13', 0.0)
  669. else:
  670. tot_check = True
  671. #~ tot[res['id']] = True
  672. tot_bin += res['balanceinit']
  673. tot_deb += res['debit']
  674. tot_crd += res['credit']
  675. tot_ytd += res['ytd']
  676. tot_eje += res['balance']
  677. if tot_check:
  678. str_label = form['lab_str']
  679. res2 = {
  680. 'type': 'view',
  681. 'name': 'TOTAL %s' % (str_label),
  682. 'label': False,
  683. 'total': True,
  684. }
  685. if form['columns'] == 'qtr':
  686. res2.update(dict(
  687. bal1=z(tot_bal1),
  688. bal2=z(tot_bal2),
  689. bal3=z(tot_bal3),
  690. bal4=z(tot_bal4),
  691. bal5=z(tot_bal5),))
  692. elif form['columns'] == 'thirteen':
  693. res2.update(dict(
  694. bal1=z(tot_bal1),
  695. bal2=z(tot_bal2),
  696. bal3=z(tot_bal3),
  697. bal4=z(tot_bal4),
  698. bal5=z(tot_bal5),
  699. bal6=z(tot_bal6),
  700. bal7=z(tot_bal7),
  701. bal8=z(tot_bal8),
  702. bal9=z(tot_bal9),
  703. bal10=z(tot_bal10),
  704. bal11=z(tot_bal11),
  705. bal12=z(tot_bal12),
  706. bal13=z(tot_bal13),))
  707. else:
  708. res2.update({
  709. 'balanceinit': tot_bin,
  710. 'debit': tot_deb,
  711. 'credit': tot_crd,
  712. 'ytd': tot_ytd,
  713. 'balance': tot_eje,
  714. })
  715. result_acc.append(res2)
  716. return result_acc
  717. report_sxw.report_sxw('report.afr.1cols',
  718. 'wizard.report',
  719. 'account_financial_report/report/balance_full.rml',
  720. parser=account_balance,
  721. header=False)
  722. report_sxw.report_sxw('report.afr.2cols',
  723. 'wizard.report',
  724. 'account_financial_report/report/balance_full_2_cols.rml',
  725. parser=account_balance,
  726. header=False)
  727. report_sxw.report_sxw('report.afr.4cols',
  728. 'wizard.report',
  729. 'account_financial_report/report/balance_full_4_cols.rml',
  730. parser=account_balance,
  731. header=False)
  732. report_sxw.report_sxw('report.afr.analytic.ledger',
  733. 'wizard.report',
  734. 'account_financial_report/report/balance_full_4_cols_analytic_ledger.rml',
  735. parser=account_balance,
  736. header=False)
  737. report_sxw.report_sxw('report.afr.5cols',
  738. 'wizard.report',
  739. 'account_financial_report/report/balance_full_5_cols.rml',
  740. parser=account_balance,
  741. header=False)
  742. report_sxw.report_sxw('report.afr.qtrcols',
  743. 'wizard.report',
  744. 'account_financial_report/report/balance_full_qtr_cols.rml',
  745. parser=account_balance,
  746. header=False)
  747. report_sxw.report_sxw('report.afr.13cols',
  748. 'wizard.report',
  749. 'account_financial_report/report/balance_full_13_cols.rml',
  750. parser=account_balance,
  751. header=False)