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.

251 lines
11 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. import pooler
  25. from report import report_sxw
  26. from common_report_header import common_report_header
  27. from tools.translate import _
  28. class report_pl_account_horizontal(report_sxw.rml_parse, common_report_header):
  29. def __init__(self, cr, uid, name, context=None):
  30. super(report_pl_account_horizontal, self).__init__(
  31. cr, uid, name, context=context)
  32. self.result_sum_dr = 0.0
  33. self.result_sum_cr = 0.0
  34. self.res_pl = {}
  35. self.result = {}
  36. self.result_temp = []
  37. self.localcontext.update({
  38. 'time': time,
  39. 'get_lines': self.get_lines,
  40. 'get_lines_another': self.get_lines_another,
  41. 'get_data': self.get_data,
  42. 'sum_dr': self.sum_dr,
  43. 'sum_cr': self.sum_cr,
  44. 'final_result': self.final_result,
  45. 'get_fiscalyear': self._get_fiscalyear,
  46. 'get_account': self._get_account,
  47. 'get_start_period': self.get_start_period,
  48. 'get_end_period': self.get_end_period,
  49. 'get_sortby': self._get_sortby,
  50. 'get_filter': self._get_filter,
  51. 'get_start_date': self._get_start_date,
  52. 'get_end_date': self._get_end_date,
  53. 'get_target_move': self._get_target_move,
  54. 'get_trans': self._get_trans
  55. })
  56. self.context = context
  57. def set_context(self, objects, data, ids, report_type=None):
  58. new_ids = ids
  59. if (data['model'] == 'ir.ui.menu'):
  60. new_ids = 'chart_account_id' in data['form'] and data['form'][
  61. 'chart_account_id'] and [data['form'][
  62. 'chart_account_id'][0]] or []
  63. objects = self.pool.get('account.account').browse(
  64. self.cr, self.uid, new_ids)
  65. lang_dict = self.pool.get('res.users').read(
  66. self.cr, self.uid, self.uid, ['context_lang'])
  67. data['lang'] = lang_dict.get('context_lang') or False
  68. return super(report_pl_account_horizontal, self).set_context(
  69. objects, data, new_ids, report_type=report_type)
  70. def final_result(self):
  71. return self.res_pl
  72. def sum_dr(self):
  73. if self.res_pl['type'] == _('Net Profit'):
  74. self.result_sum_dr += self.res_pl['balance']
  75. return self.result_sum_dr
  76. def sum_cr(self):
  77. if self.res_pl['type'] == _('Net Loss'):
  78. self.result_sum_cr += self.res_pl['balance']
  79. return self.result_sum_cr
  80. def _get_trans(self, source):
  81. return _(source)
  82. def get_data(self, data):
  83. def get_account_repr(account, account_type):
  84. return {
  85. 'code': account.code,
  86. 'name': account.name,
  87. 'level': account.level,
  88. 'balance': account.balance and (
  89. account_type == 'income' and -1 or 1) * account.balance,
  90. 'type': account.type,
  91. }
  92. cr, uid = self.cr, self.uid
  93. db_pool = pooler.get_pool(self.cr.dbname)
  94. account_pool = db_pool.get('account.account')
  95. currency_pool = db_pool.get('res.currency')
  96. types = [
  97. 'expense',
  98. 'income'
  99. ]
  100. ctx = self.context.copy()
  101. ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
  102. if ctx['fiscalyear']:
  103. ctx['fiscalyear'] = ctx['fiscalyear'][0]
  104. if data['form']['filter'] == 'filter_period':
  105. ctx['periods'] = data['form'].get('periods', False)
  106. elif data['form']['filter'] == 'filter_date':
  107. ctx['date_from'] = data['form'].get('date_from', False)
  108. ctx['date_to'] = data['form'].get('date_to', False)
  109. ctx['state'] = data['form'].get('target_move', 'all')
  110. cal_list = {}
  111. account_id = data['form'].get('chart_account_id', False)
  112. if account_id:
  113. account_id = account_id[0]
  114. account_ids = account_pool._get_children_and_consol(
  115. cr, uid, account_id, context=ctx)
  116. accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
  117. for typ in types:
  118. accounts_temp = []
  119. for account in accounts:
  120. if (account.user_type.report_type) and (
  121. account.user_type.report_type == typ
  122. ):
  123. currency = (
  124. account.currency_id and account.currency_id
  125. or account.company_id.currency_id)
  126. if typ == 'expense' and account.type != 'view' and (
  127. account.debit != account.credit
  128. ):
  129. self.result_sum_dr += account.debit - account.credit
  130. if typ == 'income' and account.type != 'view' and (
  131. account.debit != account.credit
  132. ):
  133. self.result_sum_cr += account.credit - account.debit
  134. if data['form']['display_account'] == 'bal_movement':
  135. if (
  136. not currency_pool.is_zero(
  137. self.cr, self.uid, currency, account.credit)
  138. ) or (
  139. not currency_pool.is_zero(
  140. self.cr, self.uid, currency, account.debit)
  141. ) or (
  142. not currency_pool.is_zero(
  143. self.cr, self.uid, currency, account.balance)
  144. ):
  145. accounts_temp.append(
  146. get_account_repr(account, typ))
  147. elif data['form']['display_account'] == 'bal_solde':
  148. if not currency_pool.is_zero(
  149. self.cr, self.uid, currency, account.balance
  150. ):
  151. accounts_temp.append(
  152. get_account_repr(account, typ))
  153. else:
  154. accounts_temp.append(get_account_repr(account, typ))
  155. if self.result_sum_dr > self.result_sum_cr:
  156. self.res_pl['type'] = _('Net Loss')
  157. self.res_pl['balance'] = (
  158. self.result_sum_dr - self.result_sum_cr)
  159. else:
  160. self.res_pl['type'] = _('Net Profit')
  161. self.res_pl['balance'] = (
  162. self.result_sum_cr - self.result_sum_dr)
  163. self.result[typ] = accounts_temp
  164. cal_list[typ] = self.result[typ]
  165. if cal_list:
  166. temp = {}
  167. for i in range(
  168. 0, max(len(cal_list['expense']), len(cal_list['income']))
  169. ):
  170. if i < len(cal_list['expense']) and i < len(
  171. cal_list['income']
  172. ):
  173. temp = {
  174. 'code': cal_list['expense'][i]['code'],
  175. 'name': cal_list['expense'][i]['name'],
  176. 'level': cal_list['expense'][i]['level'],
  177. 'balance': cal_list['expense'][i]['balance'],
  178. 'type': cal_list['expense'][i]['type'],
  179. 'code1': cal_list['income'][i]['code'],
  180. 'name1': cal_list['income'][i]['name'],
  181. 'level1': cal_list['income'][i]['level'],
  182. 'balance1': cal_list['income'][i]['balance'],
  183. 'type1': cal_list['income'][i]['type'],
  184. }
  185. self.result_temp.append(temp)
  186. else:
  187. if i < len(cal_list['income']):
  188. temp = {
  189. 'code': '',
  190. 'name': '',
  191. 'level': False,
  192. 'balance': False,
  193. 'type': False,
  194. 'code1': cal_list['income'][i]['code'],
  195. 'name1': cal_list['income'][i]['name'],
  196. 'level1': cal_list['income'][i]['level'],
  197. 'balance1': cal_list['income'][i]['balance'],
  198. 'type1': cal_list['income'][i]['type'],
  199. }
  200. self.result_temp.append(temp)
  201. if i < len(cal_list['expense']):
  202. temp = {
  203. 'code': cal_list['expense'][i]['code'],
  204. 'name': cal_list['expense'][i]['name'],
  205. 'level': cal_list['expense'][i]['level'],
  206. 'balance': cal_list['expense'][i]['balance'],
  207. 'type': cal_list['expense'][i]['type'],
  208. 'code1': '',
  209. 'name1': '',
  210. 'level1': False,
  211. 'balance1': False,
  212. 'type1': False,
  213. }
  214. self.result_temp.append(temp)
  215. return None
  216. def get_lines(self):
  217. return self.result_temp
  218. def get_lines_another(self, group):
  219. return self.result.get(group, [])
  220. report_sxw.report_sxw(
  221. 'report.account.profit_horizontal', 'account.account',
  222. 'addons/account_financial_report_horizontal/report/'
  223. 'account_profit_horizontal.rml',
  224. parser=report_pl_account_horizontal, header='internal landscape')
  225. report_sxw.report_sxw(
  226. 'report.account.profit_loss', 'account.account',
  227. 'addons/account_financial_report_horizontal/report/'
  228. 'account_profit_loss.rml',
  229. parser=report_pl_account_horizontal, header='internal')
  230. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: