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.

324 lines
15 KiB

11 years ago
11 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import xlwt
  23. from openerp.addons.report_xls.report_xls import report_xls
  24. from openerp.addons.report_xls.utils import rowcol_to_cell
  25. from openerp.addons.account_financial_report_webkit.report.trial_balance \
  26. import TrialBalanceWebkit
  27. from openerp.tools.translate import _
  28. # import logging
  29. # _logger = logging.getLogger(__name__)
  30. class trial_balance_xls(report_xls):
  31. column_sizes = [12, 60, 17, 17, 17, 17, 17, 17]
  32. def generate_xls_report(self, _p, _xs, data, objects, wb):
  33. ws = wb.add_sheet(_p.report_name[:31])
  34. ws.panes_frozen = True
  35. ws.remove_splits = True
  36. ws.portrait = 0 # Landscape
  37. ws.fit_width_to_pages = 1
  38. row_pos = 0
  39. # set print header/footer
  40. ws.header_str = self.xls_headers['standard']
  41. ws.footer_str = self.xls_footers['standard']
  42. # cf. account_report_trial_balance.mako
  43. initial_balance_text = {'initial_balance': _('Computed'),
  44. 'opening_balance': _('Opening Entries'),
  45. False: _('No')}
  46. # Title
  47. cell_style = xlwt.easyxf(_xs['xls_title'])
  48. report_name = ' - '.join([_p.report_name.upper(),
  49. _p.company.partner_id.name,
  50. _p.company.currency_id.name])
  51. c_specs = [
  52. ('report_name', 1, 0, 'text', report_name),
  53. ]
  54. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  55. row_pos = self.xls_write_row(
  56. ws, row_pos, row_data, row_style=cell_style)
  57. # write empty row to define column sizes
  58. c_sizes = self.column_sizes
  59. c_specs = [('empty%s' % i, 1, c_sizes[i], 'text', None)
  60. for i in range(0, len(c_sizes))]
  61. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  62. row_pos = self.xls_write_row(
  63. ws, row_pos, row_data, set_column_size=True)
  64. # Header Table
  65. cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  66. cell_style = xlwt.easyxf(cell_format)
  67. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  68. c_specs = [
  69. ('fy', 1, 0, 'text', _('Fiscal Year')),
  70. ('af', 2, 0, 'text', _('Accounts Filter')),
  71. ('df', 1, 0, 'text', _p.filter_form(data) ==
  72. 'filter_date' and _('Dates Filter') or _('Periods Filter')),
  73. ('tm', 2, 0, 'text', _('Target Moves'), None, cell_style_center),
  74. ('ib', 1, 0, 'text', _('Initial Balance'),
  75. None, cell_style_center),
  76. ('coa', 1, 0, 'text', _('Chart of Account'),
  77. None, cell_style_center),
  78. ]
  79. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  80. row_pos = self.xls_write_row(
  81. ws, row_pos, row_data, row_style=cell_style)
  82. cell_format = _xs['borders_all'] + _xs['wrap'] + _xs['top']
  83. cell_style = xlwt.easyxf(cell_format)
  84. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  85. c_specs = [
  86. ('fy', 1, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-'),
  87. ('af', 2, 0, 'text', _p.accounts(data) and ', '.join(
  88. [account.code for account in _p.accounts(data)]) or _('All')),
  89. ]
  90. df = _('From') + ': '
  91. if _p.filter_form(data) == 'filter_date':
  92. df += _p.start_date if _p.start_date else u''
  93. else:
  94. df += _p.start_period.name if _p.start_period else u''
  95. df += ' ' + _('\nTo') + ': '
  96. if _p.filter_form(data) == 'filter_date':
  97. df += _p.stop_date if _p.stop_date else u''
  98. else:
  99. df += _p.stop_period.name if _p.stop_period else u''
  100. c_specs += [
  101. ('df', 1, 0, 'text', df),
  102. ('tm', 2, 0, 'text', _p.display_target_move(
  103. data), None, cell_style_center),
  104. ('ib', 1, 0, 'text', initial_balance_text[
  105. _p.initial_balance_mode], None, cell_style_center),
  106. ('coa', 1, 0, 'text', _p.chart_account.name,
  107. None, cell_style_center),
  108. ]
  109. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  110. row_pos = self.xls_write_row(
  111. ws, row_pos, row_data, row_style=cell_style)
  112. # comparison header table
  113. if _p.comparison_mode in ('single', 'multiple'):
  114. row_pos += 1
  115. cell_format_ct = _xs['bold'] + \
  116. _xs['fill_blue'] + _xs['borders_all']
  117. cell_style_ct = xlwt.easyxf(cell_format_ct)
  118. c_specs = [('ct', 8, 0, 'text', _('Comparisons'))]
  119. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  120. row_pos = self.xls_write_row(
  121. ws, row_pos, row_data, row_style=cell_style_ct)
  122. cell_style_center = xlwt.easyxf(cell_format)
  123. for index, params in enumerate(_p.comp_params):
  124. c_specs = [
  125. ('c', 3, 0, 'text', _('Comparison') + str(index + 1) +
  126. ' (C' + str(index + 1) + ')')]
  127. if params['comparison_filter'] == 'filter_date':
  128. c_specs += [('f', 3, 0, 'text', _('Dates Filter') + ': ' +
  129. _p.formatLang(
  130. params['start'], date=True) + ' - ' +
  131. _p.formatLang(params['stop'], date=True))]
  132. elif params['comparison_filter'] == 'filter_period':
  133. c_specs += [('f', 3, 0, 'text', _('Periods Filter') +
  134. ': ' + params['start'].name + ' - ' +
  135. params['stop'].name)]
  136. else:
  137. c_specs += [('f', 3, 0, 'text', _('Fiscal Year') +
  138. ': ' + params['fiscalyear'].name)]
  139. c_specs += [('ib', 2, 0, 'text', _('Initial Balance') +
  140. ': ' +
  141. initial_balance_text[
  142. params['initial_balance_mode']])]
  143. row_data = self.xls_row_template(
  144. c_specs, [x[0] for x in c_specs])
  145. row_pos = self.xls_write_row(
  146. ws, row_pos, row_data, row_style=cell_style_center)
  147. row_pos += 1
  148. # Column Header Row
  149. cell_format = _xs['bold'] + _xs['fill_blue'] + \
  150. _xs['borders_all'] + _xs['wrap'] + _xs['top']
  151. cell_style = xlwt.easyxf(cell_format)
  152. cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  153. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  154. if len(_p.comp_params) == 2:
  155. account_span = 3
  156. else:
  157. account_span = _p.initial_balance_mode and 2 or 3
  158. c_specs = [
  159. ('code', 1, 0, 'text', _('Code')),
  160. ('account', account_span, 0, 'text', _('Account')),
  161. ]
  162. if _p.comparison_mode == 'no_comparison':
  163. if _p.initial_balance_mode:
  164. c_specs += [('init_bal', 1, 0, 'text',
  165. _('Initial Balance'), None, cell_style_right)]
  166. c_specs += [
  167. ('debit', 1, 0, 'text', _('Debit'), None, cell_style_right),
  168. ('credit', 1, 0, 'text', _('Credit'), None, cell_style_right),
  169. ]
  170. if _p.comparison_mode == 'no_comparison' or not _p.fiscalyear:
  171. c_specs += [('balance', 1, 0, 'text',
  172. _('Balance'), None, cell_style_right)]
  173. else:
  174. c_specs += [('balance_fy', 1, 0, 'text', _('Balance %s') %
  175. _p.fiscalyear.name, None, cell_style_right)]
  176. if _p.comparison_mode in ('single', 'multiple'):
  177. for index in range(_p.nb_comparison):
  178. if _p.comp_params[index][
  179. 'comparison_filter'] == 'filter_year' \
  180. and _p.comp_params[index].get('fiscalyear', False):
  181. c_specs += [('balance_%s' % index, 1, 0, 'text',
  182. _('Balance %s') %
  183. _p.comp_params[index]['fiscalyear'].name,
  184. None, cell_style_right)]
  185. else:
  186. c_specs += [('balance_%s' % index, 1, 0, 'text',
  187. _('Balance C%s') % (index + 1), None,
  188. cell_style_right)]
  189. if _p.comparison_mode == 'single':
  190. c_specs += [
  191. ('diff', 1, 0, 'text', _('Difference'),
  192. None, cell_style_right),
  193. ('diff_percent', 1, 0, 'text',
  194. _('% Difference'), None, cell_style_center),
  195. ]
  196. c_specs += [('type', 1, 0, 'text', _('Type'), None, cell_style_center)]
  197. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  198. row_pos = self.xls_write_row(
  199. ws, row_pos, row_data, row_style=cell_style)
  200. ws.set_horz_split_pos(row_pos)
  201. last_child_consol_ids = []
  202. # cell styles for account data
  203. view_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  204. view_cell_style = xlwt.easyxf(view_cell_format)
  205. view_cell_style_center = xlwt.easyxf(view_cell_format + _xs['center'])
  206. view_cell_style_decimal = xlwt.easyxf(
  207. view_cell_format + _xs['right'],
  208. num_format_str=report_xls.decimal_format)
  209. view_cell_style_pct = xlwt.easyxf(
  210. view_cell_format + _xs['center'], num_format_str='0')
  211. regular_cell_format = _xs['borders_all']
  212. regular_cell_style = xlwt.easyxf(regular_cell_format)
  213. regular_cell_style_center = xlwt.easyxf(
  214. regular_cell_format + _xs['center'])
  215. regular_cell_style_decimal = xlwt.easyxf(
  216. regular_cell_format + _xs['right'],
  217. num_format_str=report_xls.decimal_format)
  218. regular_cell_style_pct = xlwt.easyxf(
  219. regular_cell_format + _xs['center'], num_format_str='0')
  220. for current_account in objects:
  221. if not _p['to_display_accounts'][current_account.id]:
  222. continue
  223. if current_account.type == 'view':
  224. cell_style = view_cell_style
  225. cell_style_center = view_cell_style_center
  226. cell_style_decimal = view_cell_style_decimal
  227. cell_style_pct = view_cell_style_pct
  228. else:
  229. cell_style = regular_cell_style
  230. cell_style_center = regular_cell_style_center
  231. cell_style_decimal = regular_cell_style_decimal
  232. cell_style_pct = regular_cell_style_pct
  233. comparisons = _p['comparisons_accounts'][current_account.id]
  234. if current_account.id not in last_child_consol_ids:
  235. # current account is a not a consolidation child: use its own
  236. # level
  237. last_child_consol_ids = [
  238. child_consol_id.id for child_consol_id in
  239. current_account.child_consol_ids]
  240. c_specs = [
  241. ('code', 1, 0, 'text', current_account.code),
  242. ('account', account_span, 0, 'text', current_account.name),
  243. ]
  244. if _p.comparison_mode == 'no_comparison':
  245. debit_cell = rowcol_to_cell(row_pos, 4)
  246. credit_cell = rowcol_to_cell(row_pos, 5)
  247. bal_formula = debit_cell + '-' + credit_cell
  248. if _p.initial_balance_mode:
  249. init_cell = rowcol_to_cell(row_pos, 3)
  250. debit_cell = rowcol_to_cell(row_pos, 4)
  251. credit_cell = rowcol_to_cell(row_pos, 5)
  252. bal_formula = init_cell + '+' + \
  253. debit_cell + '-' + credit_cell
  254. c_specs += [('init_bal', 1, 0, 'number',
  255. _p['init_balance_'
  256. 'accounts'][current_account.id],
  257. None,
  258. cell_style_decimal)]
  259. c_specs += [
  260. ('debit', 1, 0, 'number',
  261. _p['debit_accounts'][current_account.id],
  262. None, cell_style_decimal),
  263. ('credit', 1, 0, 'number',
  264. _p['credit_accounts'][current_account.id],
  265. None, cell_style_decimal),
  266. ]
  267. c_specs += [('balance', 1, 0, 'number', None,
  268. bal_formula, cell_style_decimal)]
  269. else:
  270. c_specs += [('balance', 1, 0, 'number',
  271. _p['balance_accounts'][current_account.id],
  272. None, cell_style_decimal)]
  273. if _p.comparison_mode in ('single', 'multiple'):
  274. c = 1
  275. for comp_account in comparisons:
  276. c_specs += [('balance_%s' % c, 1, 0, 'number',
  277. comp_account['balance'], None,
  278. cell_style_decimal)]
  279. c += 1
  280. if _p.comparison_mode == 'single':
  281. c_specs += [
  282. ('diff', 1, 0, 'number', comp_account[
  283. 'diff'], None, cell_style_decimal),
  284. ('diff_percent', 1, 0, 'number', comp_account[
  285. 'percent_diff'] and
  286. comp_account['percent_diff'] or 0, None,
  287. cell_style_pct),
  288. ]
  289. c_specs += [('type', 1, 0, 'text',
  290. current_account.type, None, cell_style_center)]
  291. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  292. row_pos = self.xls_write_row(
  293. ws, row_pos, row_data, row_style=cell_style)
  294. trial_balance_xls('report.account.account_report_trial_balance_xls',
  295. 'account.account',
  296. parser=TrialBalanceWebkit)