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.

339 lines
15 KiB

13 years ago
13 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. class account_balance(report_sxw.rml_parse):
  36. def __init__(self, cr, uid, name, context):
  37. super(account_balance, self).__init__(cr, uid, name, context)
  38. self.sum_debit = 0.00
  39. self.sum_credit = 0.00
  40. self.sum_balance = 0.00
  41. self.sum_debit_fy = 0.00
  42. self.sum_credit_fy = 0.00
  43. self.sum_balance_fy = 0.00
  44. self.date_lst = []
  45. self.date_lst_string = ''
  46. self.localcontext.update({
  47. 'time': time,
  48. 'lines': self.lines,
  49. 'get_fiscalyear_text': self.get_fiscalyear_text,
  50. 'get_periods_and_date_text': self.get_periods_and_date_text,
  51. 'get_informe_text': self.get_informe_text,
  52. 'get_month':self.get_month,
  53. })
  54. self.context = context
  55. def get_fiscalyear_text(self, form):
  56. """
  57. Returns the fiscal year text used on the report.
  58. """
  59. fiscalyear_obj = self.pool.get('account.fiscalyear')
  60. fiscalyear = None
  61. if form.get('fiscalyear'):
  62. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, form['fiscalyear'])
  63. return fiscalyear.name or fiscalyear.code
  64. else:
  65. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
  66. return "%s*" % (fiscalyear.name or fiscalyear.code)
  67. def get_informe_text(self, form):
  68. """
  69. Returns the header text used on the report.
  70. """
  71. inf_type = {
  72. 'bgen' : ' Balance General',
  73. 'bcom' : ' Balance de Comprobacion',
  74. 'edogp': 'Estado de Ganancias y Perdidas',
  75. 'bdl' : 'Diario Legal'
  76. }
  77. return inf_type[form['inf_type']]
  78. def get_month(self, form):
  79. '''
  80. return day, year and month
  81. '''
  82. if form['filter'] in ['bydate', 'all']:
  83. months=["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]
  84. mes = months[time.strptime(form['date_to'],"%Y-%m-%d")[1]-1]
  85. ano = time.strptime(form['date_to'],"%Y-%m-%d")[0]
  86. dia = time.strptime(form['date_to'],"%Y-%m-%d")[2]
  87. return 'Período del '+self.formatLang(form['date_from'], date=True)+' al '+self.formatLang(form['date_to'], date=True)
  88. elif form['filter'] in ['byperiod', 'all']:
  89. aux=[]
  90. period_obj = self.pool.get('account.period')
  91. for period in period_obj.browse(self.cr, self.uid, form['periods']):
  92. aux.append(period.date_start)
  93. aux.append(period.date_stop)
  94. sorted(aux)
  95. return 'Período del '+self.formatLang(aux[0], date=True)+' al '+self.formatLang(aux[-1], date=True)
  96. def get_periods_and_date_text(self, form):
  97. """
  98. Returns the text with the periods/dates used on the report.
  99. """
  100. period_obj = self.pool.get('account.period')
  101. periods_str = None
  102. fiscalyear_id = form['fiscalyear'] or fiscalyear_obj.find(self.cr, self.uid)
  103. period_ids = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear_id),('special','=',False)])
  104. if form['filter'] in ['byperiod', 'all']:
  105. period_ids = form['periods']
  106. periods_str = ', '.join([period.name or period.code for period in period_obj.browse(self.cr, self.uid, period_ids)])
  107. dates_str = None
  108. if form['filter'] in ['bydate', 'all']:
  109. dates_str = self.formatLang(form['date_from'], date=True) + ' - ' + self.formatLang(form['date_to'], date=True) + ' '
  110. return {'periods':periods_str, 'date':dates_str}
  111. def lines(self, form, ids={}, done=None, level=0):
  112. """
  113. Returns all the data needed for the report lines
  114. (account info plus debit/credit/balance in the selected period
  115. and the full year)
  116. """
  117. tot_bin = 0.0
  118. tot_deb = 0.0
  119. tot_crd = 0.0
  120. tot_eje = 0.0
  121. if not ids:
  122. ids = self.ids
  123. if not ids:
  124. return []
  125. if not done:
  126. done = {}
  127. if form.has_key('account_list') and form['account_list']:
  128. account_ids = form['account_list']
  129. del form['account_list']
  130. res = {}
  131. result_acc = []
  132. accounts_levels = {}
  133. account_obj = self.pool.get('account.account')
  134. period_obj = self.pool.get('account.period')
  135. fiscalyear_obj = self.pool.get('account.fiscalyear')
  136. fiscalyear = None
  137. if form.get('fiscalyear'):
  138. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, form['fiscalyear'])
  139. else:
  140. fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
  141. #
  142. # Get the accounts
  143. #
  144. def _get_children_and_consol(cr, uid, ids, level, context={}):
  145. aa_obj = self.pool.get('account.account')
  146. ids2=[]
  147. temp=[]
  148. read_data= aa_obj.read(cr, uid, ids,['id','child_id','level','type'], context)
  149. for data in read_data:
  150. if data['child_id'] and data['level'] < level and data['type']!='consolidation':
  151. ids2.append([data['id'],True, False])
  152. temp=[]
  153. for x in data['child_id']:
  154. temp.append(x)
  155. ids2 += _get_children_and_consol(cr, uid, temp, level, context)
  156. ids2.append([data['id'],False,True])
  157. else:
  158. ids2.append([data['id'],True,True])
  159. return ids2
  160. 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)
  161. if child_ids:
  162. account_ids = child_ids
  163. account_obj = self.pool.get('account.account')
  164. period_obj = self.pool.get('account.period')
  165. fiscalyear_obj = self.pool.get('account.fiscalyear')
  166. #############################################################################
  167. # Calculate the period Debit/Credit #
  168. # (from the selected period or all the non special periods in the fy) #
  169. #############################################################################
  170. ctx = self.context.copy()
  171. ctx['filter'] = form.get('filter','all')
  172. ctx['fiscalyear'] = fiscalyear.id
  173. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)])
  174. if form['filter'] in ['byperiod', 'all']:
  175. ctx['periods'] = period_obj.search(self.cr, self.uid, [('id','in',form['periods'] or ctx['periods']),('special','=',False)])
  176. if form['filter'] in ['bydate', 'all']:
  177. ctx['date_from'] = form['date_from']
  178. ctx['date_to'] = form['date_to']
  179. accounts=[]
  180. val = account_obj.browse(self.cr, self.uid, [aa_id[0] for aa_id in account_ids], ctx)
  181. c = 0
  182. for aa_id in account_ids:
  183. new_acc = {
  184. 'id' :val[c].id,
  185. 'type' :val[c].type,
  186. 'code' :val[c].code,
  187. 'name' :val[c].name,
  188. 'debit' :val[c].debit,
  189. 'credit' :val[c].credit,
  190. 'parent_id' :val[c].parent_id and val[c].parent_id.id,
  191. 'level' :val[c].level,
  192. 'label' :aa_id[1],
  193. 'total' :aa_id[2],
  194. }
  195. c += 1
  196. accounts.append(new_acc)
  197. def missing_period():
  198. ctx['fiscalyear'] = fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop') and \
  199. fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop')[-1] or []
  200. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',ctx['fiscalyear']),('date_stop','<',fiscalyear.date_start)])
  201. #############################################################################
  202. # Calculate the period initial Balance #
  203. # (fy balance minus the balance from the start of the selected period #
  204. # to the end of the year) #
  205. #############################################################################
  206. ctx = self.context.copy()
  207. ctx['filter'] = form.get('filter','all')
  208. ctx['fiscalyear'] = fiscalyear.id
  209. if form['filter'] in ['byperiod', 'all']:
  210. ctx['periods'] = form['periods']
  211. date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx['periods'])])
  212. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',date_start)])
  213. if not ctx['periods']:
  214. missing_period()
  215. elif form['filter'] in ['bydate']:
  216. ctx['date_from'] = fiscalyear.date_start
  217. ctx['date_to'] = form['date_from']
  218. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',ctx['date_to'])])
  219. elif form['filter'] == 'none':
  220. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',True)])
  221. date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx['periods'])])
  222. ctx['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_start','<=',date_start),('special','=',True)])
  223. period_balanceinit = {}
  224. for acc in account_obj.browse(self.cr, self.uid, [x[0] for x in account_ids], ctx):
  225. period_balanceinit[acc['id']] = acc.balance
  226. #
  227. # Generate the report lines (checking each account)
  228. #
  229. tot = {}
  230. for account in accounts:
  231. account_id = account['id']
  232. if account_id in done:
  233. pass
  234. done[account_id] = 1
  235. accounts_levels[account_id] = account['level']
  236. #
  237. # Check if we need to include this level
  238. #
  239. if not form['display_account_level'] or account['level'] <= form['display_account_level']:
  240. #
  241. # Copy the account values
  242. #
  243. res = {
  244. 'id' : account_id,
  245. 'type' : account['type'],
  246. 'code': account['code'],
  247. 'name': (account['total'] and not account['label']) and 'TOTAL %s'%(account['name'].upper()) or account['name'],
  248. 'level': account['level'],
  249. 'balanceinit': period_balanceinit[account_id],
  250. 'debit': account['debit'],
  251. 'credit': account['credit'],
  252. 'balance': period_balanceinit[account_id]+account['debit']-account['credit'],
  253. 'parent_id': account['parent_id'],
  254. 'bal_type': '',
  255. 'label': account['label'],
  256. 'total': account['total'],
  257. }
  258. #
  259. # Round the values to zero if needed (-0.000001 ~= 0)
  260. #
  261. if abs(res['balance']) < 0.5 * 10**-4:
  262. res['balance'] = 0.0
  263. #
  264. # Check whether we must include this line in the report or not
  265. #
  266. if form['display_account'] == 'con_movimiento' and account['parent_id']:
  267. # Include accounts with movements
  268. if abs(res['balance']) >= 0.5 * 10**-int(2):
  269. result_acc.append(res)
  270. elif form['display_account'] == 'con_balance' and account['parent_id']:
  271. # Include accounts with balance
  272. if abs(res['balance']) >= 0.5 * 10**-4:
  273. result_acc.append(res)
  274. else:
  275. # Include all accounts
  276. result_acc.append(res)
  277. if form['tot_check'] and res['type'] == 'view' and res['level'] == 1 and (res['id'] not in tot):
  278. tot[res['id']] = True
  279. tot_bin += res['balanceinit']
  280. tot_deb += res['debit']
  281. tot_crd += res['credit']
  282. tot_eje += res['balance']
  283. if form['tot_check']:
  284. str_label = form['lab_str']
  285. res2 = {
  286. 'type' : 'view',
  287. 'name': 'TOTAL %s'%(str_label),
  288. 'balanceinit': tot_bin,
  289. 'debit': tot_deb,
  290. 'credit': tot_crd,
  291. 'balance': tot_eje,
  292. 'label': False,
  293. 'total': True,
  294. }
  295. result_acc.append(res2)
  296. return result_acc
  297. report_sxw.report_sxw('report.account.account.balance.gene.2',
  298. 'wizard.report.account.balance.gene.2',
  299. 'account_financial_report/report/balance_full_2_cols.rml',
  300. parser=account_balance,
  301. header=False)