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.

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