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.

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