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.

231 lines
11 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>),
  6. # Copyright (C) 2012 Therp BV (<http://therp.nl>),
  7. # Copyright (C) 2013 Agile Business Group sagl
  8. # (<http://www.agilebg.com>) (<lorenzo.battistini@agilebg.com>)
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as
  12. # published by the Free Software Foundation, either version 3 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. import time
  25. import pooler
  26. from report import report_sxw
  27. from openerp.addons.account_financial_report_horizontal.report import account_profit_loss
  28. from common_report_header import common_report_header
  29. from tools.translate import _
  30. class report_balancesheet_horizontal(report_sxw.rml_parse, common_report_header):
  31. def __init__(self, cr, uid, name, context=None):
  32. super(report_balancesheet_horizontal, self).__init__(cr, uid, name, context=context)
  33. self.obj_pl = account_profit_loss.report_pl_account_horizontal(cr, uid, name, context=context)
  34. self.result_sum_dr = 0.0
  35. self.result_sum_cr = 0.0
  36. self.result = {}
  37. self.res_bl = {}
  38. self.result_temp = []
  39. self.localcontext.update({
  40. 'time': time,
  41. 'get_lines': self.get_lines,
  42. 'get_lines_another': self.get_lines_another,
  43. 'sum_dr': self.sum_dr,
  44. 'sum_cr': self.sum_cr,
  45. 'get_data':self.get_data,
  46. 'get_pl_balance':self.get_pl_balance,
  47. 'get_fiscalyear': self._get_fiscalyear,
  48. 'get_account': self._get_account,
  49. 'get_start_period': self.get_start_period,
  50. 'get_end_period': self.get_end_period,
  51. 'get_sortby': self._get_sortby,
  52. 'get_filter': self._get_filter,
  53. 'get_start_date':self._get_start_date,
  54. 'get_end_date':self._get_end_date,
  55. 'get_target_move': self._get_target_move,
  56. })
  57. self.context = context
  58. def set_context(self, objects, data, ids, report_type=None):
  59. new_ids = ids
  60. if (data['model'] == 'ir.ui.menu'):
  61. new_ids = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] and [data['form']['chart_account_id'][0]] or []
  62. objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
  63. lang_dict = self.pool.get('res.users').read(self.cr,self.uid,self.uid,['context_lang'])
  64. data['lang'] = lang_dict.get('context_lang') or False
  65. return super(report_balancesheet_horizontal, self).set_context(objects, data, new_ids, report_type=report_type)
  66. def sum_dr(self):
  67. if self.res_bl['type'] == _('Net Profit'):
  68. self.result_sum_dr += self.res_bl['balance']
  69. return self.result_sum_dr
  70. def sum_cr(self):
  71. if self.res_bl['type'] == _('Net Loss'):
  72. self.result_sum_cr += self.res_bl['balance']
  73. return self.result_sum_cr
  74. def get_pl_balance(self):
  75. return self.res_bl
  76. def get_data(self,data):
  77. cr, uid = self.cr, self.uid
  78. db_pool = pooler.get_pool(self.cr.dbname)
  79. #Getting Profit or Loss Balance from profit and Loss report
  80. self.obj_pl.get_data(data)
  81. self.res_bl = self.obj_pl.final_result()
  82. account_pool = db_pool.get('account.account')
  83. currency_pool = db_pool.get('res.currency')
  84. types = [
  85. 'liability',
  86. 'asset'
  87. ]
  88. ctx = self.context.copy()
  89. ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
  90. if ctx['fiscalyear']:
  91. ctx['fiscalyear'] = ctx['fiscalyear'][0]
  92. if data['form']['filter'] == 'filter_period':
  93. ctx['periods'] = data['form'].get('periods', False)
  94. elif data['form']['filter'] == 'filter_date':
  95. ctx['date_from'] = data['form'].get('date_from', False)
  96. ctx['date_to'] = data['form'].get('date_to', False)
  97. ctx['state'] = data['form'].get('target_move', 'all')
  98. cal_list = {}
  99. pl_dict = {}
  100. account_dict = {}
  101. account_id = data['form'].get('chart_account_id', False)
  102. if account_id:
  103. account_id = account_id[0]
  104. account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
  105. accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
  106. if not self.res_bl:
  107. self.res_bl['type'] = _('Net Profit')
  108. self.res_bl['balance'] = 0.0
  109. if self.res_bl['type'] == _('Net Profit'):
  110. self.res_bl['type'] = _('Net Profit')
  111. else:
  112. self.res_bl['type'] = _('Net Loss')
  113. pl_dict = {
  114. 'code': self.res_bl['type'],
  115. 'name': self.res_bl['type'],
  116. 'level': False,
  117. 'balance':self.res_bl['balance'],
  118. 'type':self.res_bl['type'],
  119. }
  120. for typ in types:
  121. accounts_temp = []
  122. for account in accounts:
  123. if (account.user_type.report_type) and (account.user_type.report_type == typ):
  124. account_dict = {
  125. 'id': account.id,
  126. 'code': account.code,
  127. 'name': account.name,
  128. 'level': account.level,
  129. 'balance': (account.balance and typ == 'liability' and -1 or 1 ) * account.balance,
  130. 'type': account.type,
  131. }
  132. currency = account.currency_id and account.currency_id or account.company_id.currency_id
  133. if typ == 'liability' and account.type <> 'view' and (account.debit <> account.credit):
  134. self.result_sum_dr += account_dict['balance']
  135. if typ == 'asset' and account.type <> 'view' and (account.debit <> account.credit):
  136. self.result_sum_cr += account_dict['balance']
  137. if data['form']['display_account'] == 'bal_movement':
  138. if (not currency_pool.is_zero(self.cr, self.uid, currency, account.credit)) or (not currency_pool.is_zero(self.cr, self.uid, currency, account.debit)) or (not currency_pool.is_zero(self.cr, self.uid, currency, account.balance)):
  139. accounts_temp.append(account_dict)
  140. elif data['form']['display_account'] == 'bal_solde':
  141. if not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
  142. accounts_temp.append(account_dict)
  143. else:
  144. accounts_temp.append(account_dict)
  145. self.result[typ] = accounts_temp
  146. cal_list[typ]=self.result[typ]
  147. if pl_dict['code'] == _('Net Loss'):
  148. self.result['asset'].append(pl_dict)
  149. else:
  150. self.result['liability'].append(pl_dict)
  151. if cal_list:
  152. temp = {}
  153. for i in range(0,max(len(cal_list['liability']),len(cal_list['asset']))):
  154. if i < len(cal_list['liability']) and i < len(cal_list['asset']):
  155. temp={
  156. 'code': cal_list['liability'][i]['code'],
  157. 'name': cal_list['liability'][i]['name'],
  158. 'level': cal_list['liability'][i]['level'],
  159. 'balance':cal_list['liability'][i]['balance'],
  160. 'type':cal_list['liability'][i]['type'],
  161. 'code1': cal_list['asset'][i]['code'],
  162. 'name1': cal_list['asset'][i]['name'],
  163. 'level1': cal_list['asset'][i]['level'],
  164. 'balance1':cal_list['asset'][i]['balance'],
  165. 'type1':cal_list['asset'][i]['type'],
  166. }
  167. self.result_temp.append(temp)
  168. else:
  169. if i < len(cal_list['asset']):
  170. temp={
  171. 'code': '',
  172. 'name': '',
  173. 'level': False,
  174. 'balance':False,
  175. 'type':False,
  176. 'code1': cal_list['asset'][i]['code'],
  177. 'name1': cal_list['asset'][i]['name'],
  178. 'level1': cal_list['asset'][i]['level'],
  179. 'balance1':cal_list['asset'][i]['balance'],
  180. 'type1':cal_list['asset'][i]['type'],
  181. }
  182. self.result_temp.append(temp)
  183. if i < len(cal_list['liability']):
  184. temp={
  185. 'code': cal_list['liability'][i]['code'],
  186. 'name': cal_list['liability'][i]['name'],
  187. 'level': cal_list['liability'][i]['level'],
  188. 'balance':cal_list['liability'][i]['balance'],
  189. 'type':cal_list['liability'][i]['type'],
  190. 'code1': '',
  191. 'name1': '',
  192. 'level1': False,
  193. 'balance1':False,
  194. 'type1':False,
  195. }
  196. self.result_temp.append(temp)
  197. return None
  198. def get_lines(self):
  199. return self.result_temp
  200. def get_lines_another(self, group):
  201. return self.result.get(group, [])
  202. report_sxw.report_sxw('report.account.balancesheet.horizontal', 'account.account',
  203. 'addons/account_financial_report_horizontal/report/account_balance_sheet_horizontal.rml',parser=report_balancesheet_horizontal,
  204. header='internal landscape')
  205. report_sxw.report_sxw('report.account.balancesheet', 'account.account',
  206. 'addons/account_financial_report_horizontal/report/account_balance_sheet.rml',parser=report_balancesheet_horizontal,
  207. header='internal')
  208. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: