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.

247 lines
10 KiB

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