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.

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