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.

334 lines
14 KiB

  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. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ##############################################################################
  20. import xml
  21. import copy
  22. from operator import itemgetter
  23. import time
  24. import datetime
  25. from report import report_sxw
  26. from tools import config
  27. class account_balance(report_sxw.rml_parse):
  28. def __init__(self, cr, uid, name, context):
  29. super(account_balance, self).__init__(cr, uid, name, context)
  30. self.sum_debit = 0.00
  31. self.sum_credit = 0.00
  32. self.sum_balance = 0.00
  33. self.sum_debit_fy = 0.00
  34. self.sum_credit_fy = 0.00
  35. self.sum_balance_fy = 0.00
  36. self.date_lst = []
  37. self.date_lst_string = ''
  38. self.localcontext.update({
  39. 'time': time,
  40. 'lines': self.lines,
  41. 'get_fiscalyear_text': self.get_fiscalyear_text,
  42. 'get_periods_and_date_text': self.get_periods_and_date_text,
  43. 'get_inf_text': self.get_informe_text,
  44. })
  45. self.context = context
  46. def get_fiscalyear_text(self, form):
  47. """
  48. Returns the fiscal year text used on the report.
  49. """
  50. fiscalyear_obj = self.pool.get('account.fiscalyear')
  51. fiscalyear = None
  52. if form.get('fiscalyear'):
  53. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, form['fiscalyear'])
  54. return fiscalyear.name or fiscalyear.code
  55. else:
  56. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
  57. return "%s*" % (fiscalyear.name or fiscalyear.code)
  58. def get_informe_text(self, form):
  59. """
  60. Returns the header text used on the report.
  61. """
  62. inf_type = {
  63. 'bgen' : ' Balance General',
  64. 'bcom' : ' Balance de Comprobacion',
  65. 'edogp': 'Estado de Ganancias y Perdidas'
  66. }
  67. return inf_type[form['inf_type']]
  68. def get_periods_and_date_text(self, form):
  69. """
  70. Returns the text with the periods/dates used on the report.
  71. """
  72. period_obj = self.pool.get('account.period')
  73. periods_str = None
  74. fiscalyear_id = form['fiscalyear'] or fiscalyear_obj.find(self.cr, self.uid)
  75. period_ids = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear_id),('special','=',False)])
  76. if form['filter'] in ['byperiod', 'all']:
  77. period_ids = form['periods']
  78. periods_str = ', '.join([period.name or period.code for period in period_obj.browse(self.cr, self.uid, period_ids)])
  79. dates_str = None
  80. if form['filter'] in ['bydate', 'all']:
  81. dates_str = self.formatLang(form['date_from'], date=True) + ' - ' + self.formatLang(form['date_to'], date=True) + ' '
  82. return {'periods':periods_str, 'date':dates_str}
  83. def es_especial(self, periods):
  84. period_obj = self.pool.get('account.period')
  85. period_brws = period_obj.browse(self.cr, self.uid, periods)
  86. tod=0
  87. for p_b in period_brws:
  88. if p_b.special:
  89. tod = tod + 1
  90. if tod == len(periods):
  91. return True
  92. else:
  93. return False
  94. def lines(self, form, ids={}, done=None, level=0):
  95. """
  96. Returns all the data needed for the report lines
  97. (account info plus debit/credit/balance in the selected period
  98. and the full year)
  99. """
  100. tot_bin = 0.0
  101. tot_deb = 0.0
  102. tot_crd = 0.0
  103. tot_eje = 0.0
  104. if not ids:
  105. ids = self.ids
  106. if not ids:
  107. return []
  108. if not done:
  109. done = {}
  110. if form.has_key('account_list') and form['account_list']:
  111. account_ids = form['account_list']
  112. del form['account_list']
  113. res = {}
  114. result_acc = []
  115. accounts_levels = {}
  116. account_obj = self.pool.get('account.account')
  117. period_obj = self.pool.get('account.period')
  118. fiscalyear_obj = self.pool.get('account.fiscalyear')
  119. # Get the fiscal year
  120. fiscalyear = None
  121. if form.get('fiscalyear'):
  122. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, form['fiscalyear'])
  123. else:
  124. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
  125. #
  126. # Get the accounts
  127. #
  128. def _get_children_and_consol(cr, uid, ids, level, context={}):
  129. aa_obj = self.pool.get('account.account')
  130. ids2=[]
  131. temp=[]
  132. read_data= aa_obj.read(cr, uid, ids,['id','child_id','level','type'], context)
  133. for data in read_data:
  134. if data['child_id'] and data['level'] < level and data['type']!='consolidation':
  135. #ids2.append([data['id'],'Label', 'Total'])
  136. ids2.append([data['id'],True, False])
  137. temp=[]
  138. for x in data['child_id']:
  139. temp.append(x)
  140. ids2 += _get_children_and_consol(cr, uid, temp, level, context)
  141. ids2.append([data['id'],False,True])
  142. else:
  143. ids2.append([data['id'],True,True])
  144. return ids2
  145. child_ids = _get_children_and_consol(self.cr, self.uid, account_ids, form['display_account_level'] and form['display_account_level'] or 100,self.context)
  146. if child_ids:
  147. account_ids = child_ids
  148. account_obj = self.pool.get('account.account')
  149. period_obj = self.pool.get('account.period')
  150. fiscalyear_obj = self.pool.get('account.fiscalyear')
  151. #############################################################################
  152. # Calculate the period Debit/Credit #
  153. # (from the selected period or all the non special periods in the fy) #
  154. #############################################################################
  155. ctx = self.context.copy()
  156. ctx['filter'] = form.get('filter','all')
  157. ctx['fiscalyear'] = fiscalyear.id
  158. #~ ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)])
  159. if ctx['filter'] not in ['bydate','none']:
  160. especial = self.es_especial(form['periods'])
  161. else:
  162. especial = False
  163. if form['filter'] in ['byperiod', 'all']:
  164. if especial:
  165. ctx['periods'] = period_obj.search(self.cr, self.uid, [('id','in',form['periods'] or ctx['periods'])])
  166. else:
  167. ctx['periods'] = period_obj.search(self.cr, self.uid, [('id','in',form['periods'] or ctx['periods']),('special','=',False)])
  168. if form['filter'] in ['bydate','all','none']:
  169. ctx['date_from'] = form['date_from']
  170. ctx['date_to'] = form['date_to']
  171. if form.get('state'):
  172. ctx['state'] = form['state']
  173. accounts=[]
  174. val = account_obj.browse(self.cr, self.uid, [aa_id[0] for aa_id in account_ids], ctx)
  175. c = 0
  176. for aa_id in account_ids:
  177. new_acc = {
  178. 'id' :val[c].id,
  179. 'type' :val[c].type,
  180. 'code' :val[c].code,
  181. 'name' :val[c].name,
  182. 'debit' :val[c].debit,
  183. 'credit' :val[c].credit,
  184. 'parent_id' :val[c].parent_id and val[c].parent_id.id,
  185. 'level' :val[c].level,
  186. 'label' :aa_id[1],
  187. 'total' :aa_id[2],
  188. }
  189. c += 1
  190. accounts.append(new_acc)
  191. def missing_period():
  192. ctx['fiscalyear'] = fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop') and \
  193. fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop')[-1] or []
  194. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',ctx['fiscalyear']),('date_stop','<',fiscalyear.date_start)])
  195. #############################################################################
  196. # Calculate the period initial Balance #
  197. # (fy balance minus the balance from the start of the selected period #
  198. # to the end of the year) #
  199. #############################################################################
  200. ctx = self.context.copy()
  201. ctx['filter'] = form.get('filter','all')
  202. ctx['fiscalyear'] = fiscalyear.id
  203. if form['filter'] in ['byperiod', 'all']:
  204. ctx['periods'] = form['periods']
  205. date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx['periods'])])
  206. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',date_start)])
  207. if not ctx['periods']:
  208. missing_period()
  209. elif form['filter'] in ['bydate']:
  210. ctx['date_from'] = fiscalyear.date_start
  211. ctx['date_to'] = form['date_from']
  212. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',ctx['date_to'])])
  213. elif form['filter'] == 'none':
  214. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',True)])
  215. date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx['periods'])])
  216. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_start','<=',date_start),('special','=',True)])
  217. period_balanceinit = {}
  218. for acc in account_obj.browse(self.cr, self.uid, [x[0] for x in account_ids], ctx):
  219. if especial:
  220. period_balanceinit[acc['id']] = 0.0
  221. else:
  222. period_balanceinit[acc['id']] = acc.balance
  223. #
  224. # Generate the report lines (checking each account)
  225. #
  226. tot = {}
  227. for account in accounts:
  228. account_id = account['id']
  229. if account_id in done:
  230. pass
  231. done[account_id] = 1
  232. accounts_levels[account_id] = account['level']
  233. #
  234. # Check if we need to include this level
  235. #
  236. if not form['display_account_level'] or account['level'] <= form['display_account_level']:
  237. #
  238. # Copy the account values
  239. #
  240. res = {
  241. 'id' : account_id,
  242. 'type' : account['type'],
  243. 'code': account['code'],
  244. 'name': (account['total'] and not account['label']) and 'TOTAL %s'%(account['name'].upper()) or account['name'],
  245. 'level': account['level'],
  246. 'balanceinit': period_balanceinit[account_id],
  247. 'debit': account['debit'],
  248. 'credit': account['credit'],
  249. 'balance': period_balanceinit[account_id]+account['debit']-account['credit'],
  250. 'parent_id': account['parent_id'],
  251. 'bal_type': '',
  252. 'label': account['label'],
  253. 'total': account['total'],
  254. }
  255. #
  256. # Round the values to zero if needed (-0.000001 ~= 0)
  257. #
  258. if abs(res['balance']) < 0.5 * 10**-4:
  259. res['balance'] = 0.0
  260. #
  261. # Check whether we must include this line in the report or not
  262. #
  263. if form['display_account'] == 'con_movimiento' and account['parent_id']:
  264. # Include accounts with movements
  265. if abs(res['debit']) >= 0.5 * 10**-int(2) or abs(res['credit']) >= 0.5 * 10**-int(2):
  266. result_acc.append(res)
  267. elif form['display_account'] == 'con_balance' and account['parent_id']:
  268. # Include accounts with balance
  269. if abs(res['balance']) >= 0.5 * 10**-4:
  270. result_acc.append(res)
  271. else:
  272. # Include all accounts
  273. result_acc.append(res)
  274. if form['tot_check'] and res['type'] == 'view' and res['level'] == 1 and (res['id'] not in tot):
  275. tot[res['id']] = True
  276. tot_bin += res['balanceinit']
  277. tot_deb += res['debit']
  278. tot_crd += res['credit']
  279. tot_eje += res['balance']
  280. if form['tot_check']:
  281. str_label = form['lab_str']
  282. res2 = {
  283. 'type' : 'view',
  284. 'name': 'TOTAL %s'%(str_label),
  285. 'balanceinit': tot_bin,
  286. 'debit': tot_deb,
  287. 'credit': tot_crd,
  288. 'balance': tot_eje,
  289. 'label': False,
  290. 'total': True,
  291. }
  292. result_acc.append(res2)
  293. return result_acc
  294. report_sxw.report_sxw('report.wizard.reporte.comprobacion.cuatro.col',
  295. 'wizard.reporte.comprobacion',
  296. 'l10n_ve_account_financial_report/report/balance_full_4_cols_h.rml',
  297. parser=account_balance,
  298. header=False)