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.

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