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.

258 lines
14 KiB

  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. import time
  24. from openerp.report import report_sxw
  25. from openerp.addons.report_xls.report_xls import report_xls
  26. from openerp.addons.report_xls.utils import rowcol_to_cell
  27. from openerp.addons.account_financial_report_webkit.report.trial_balance import TrialBalanceWebkit
  28. from openerp.tools.translate import _
  29. #import logging
  30. #_logger = logging.getLogger(__name__)
  31. class trial_balance_xls(report_xls):
  32. column_sizes = [12,60,17,17,17,17,17,17]
  33. def generate_xls_report(self, _p, _xs, data, objects, wb):
  34. ws = wb.add_sheet(_p.report_name[:31])
  35. ws.panes_frozen = True
  36. ws.remove_splits = True
  37. ws.portrait = 0 # Landscape
  38. ws.fit_width_to_pages = 1
  39. row_pos = 0
  40. # set print header/footer
  41. ws.header_str = self.xls_headers['standard']
  42. ws.footer_str = self.xls_footers['standard']
  43. # cf. account_report_trial_balance.mako
  44. initial_balance_text = {'initial_balance': _('Computed'), 'opening_balance': _('Opening Entries'), False: _('No')}
  45. # Title
  46. cell_style = xlwt.easyxf(_xs['xls_title'])
  47. report_name = ' - '.join([_p.report_name.upper(), _p.company.partner_id.name, _p.company.currency_id.name])
  48. c_specs = [
  49. ('report_name', 1, 0, 'text', report_name),
  50. ]
  51. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  52. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  53. # write empty row to define column sizes
  54. c_sizes = self.column_sizes
  55. c_specs = [('empty%s'%i, 1, c_sizes[i], 'text', None) for i in range(0,len(c_sizes))]
  56. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  57. row_pos = self.xls_write_row(ws, row_pos, row_data, set_column_size=True)
  58. # Header Table
  59. cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  60. cell_style = xlwt.easyxf(cell_format)
  61. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  62. c_specs = [
  63. ('fy', 1, 0, 'text', _('Fiscal Year')),
  64. ('af', 2, 0, 'text', _('Accounts Filter')),
  65. ('df', 1, 0, 'text', _p.filter_form(data) == 'filter_date' and _('Dates Filter') or _('Periods Filter')),
  66. ('tm', 2, 0, 'text', _('Target Moves'), None, cell_style_center),
  67. ('ib', 1, 0, 'text', _('Initial Balance'), None, cell_style_center),
  68. ('coa', 1, 0, 'text', _('Chart of Account'), None, cell_style_center),
  69. ]
  70. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  71. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  72. cell_format = _xs['borders_all'] + _xs['wrap'] + _xs['top']
  73. cell_style = xlwt.easyxf(cell_format)
  74. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  75. c_specs = [
  76. ('fy', 1, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-'),
  77. ('af', 2, 0, 'text', _p.accounts(data) and ', '.join([account.code for account in _p.accounts(data)]) or _('All')),
  78. ]
  79. df = _('From') + ': '
  80. if _p.filter_form(data) == 'filter_date':
  81. df += _p.start_date if _p.start_date else u''
  82. else:
  83. df += _p.start_period.name if _p.start_period else u''
  84. df += ' ' + _('\nTo') + ': '
  85. if _p.filter_form(data) == 'filter_date':
  86. df += _p.stop_date if _p.stop_date else u''
  87. else:
  88. df += _p.stop_period.name if _p.stop_period else u''
  89. c_specs += [
  90. ('df', 1, 0, 'text', df),
  91. ('tm', 2, 0, 'text', _p.display_target_move(data), None, cell_style_center),
  92. ('ib', 1, 0, 'text', initial_balance_text[_p.initial_balance_mode], None, cell_style_center),
  93. ('coa', 1, 0, 'text', _p.chart_account.name, None, cell_style_center),
  94. ]
  95. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  96. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  97. # comparison header table
  98. if _p.comparison_mode in ('single', 'multiple'):
  99. row_pos += 1
  100. cell_format_ct = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  101. cell_style_ct = xlwt.easyxf(cell_format_ct)
  102. c_specs = [('ct', 8, 0, 'text', _('Comparisons'))]
  103. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  104. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_ct)
  105. cell_style_center = xlwt.easyxf(cell_format)
  106. for index, params in enumerate(_p.comp_params):
  107. c_specs = [('c', 3, 0, 'text', _('Comparison') + str(index + 1) + ' (C' + str(index + 1) + ')')]
  108. if params['comparison_filter'] == 'filter_date':
  109. c_specs += [('f', 3, 0, 'text', _('Dates Filter') + ': ' + _p.formatLang(params['start'], date=True) + ' - ' + _p.formatLang(params['stop'], date=True))]
  110. elif params['comparison_filter'] == 'filter_period':
  111. c_specs += [('f', 3, 0, 'text', _('Periods Filter') + ': ' + params['start'].name + ' - ' + params['stop'].name)]
  112. else:
  113. c_specs += [('f', 3, 0, 'text', _('Fiscal Year') + ': ' + params['fiscalyear'].name)]
  114. c_specs += [('ib', 2, 0, 'text', _('Initial Balance') + ': ' + initial_balance_text[params['initial_balance_mode']])]
  115. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  116. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_center)
  117. row_pos += 1
  118. # Column Header Row
  119. cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all'] + _xs['wrap'] + _xs['top']
  120. cell_style = xlwt.easyxf(cell_format)
  121. cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  122. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  123. if len(_p.comp_params) == 2:
  124. account_span = 3
  125. else:
  126. account_span = _p.initial_balance_mode and 2 or 3
  127. c_specs = [
  128. ('code', 1, 0, 'text', _('Code')),
  129. ('account', account_span, 0, 'text', _('Account')),
  130. ]
  131. if _p.comparison_mode == 'no_comparison':
  132. if _p.initial_balance_mode:
  133. c_specs += [('init_bal', 1, 0, 'text', _('Initial Balance'), None, cell_style_right)]
  134. c_specs += [
  135. ('debit', 1, 0, 'text', _('Debit'), None, cell_style_right),
  136. ('credit', 1, 0, 'text', _('Credit'), None, cell_style_right),
  137. ]
  138. if _p.comparison_mode == 'no_comparison' or not _p.fiscalyear:
  139. c_specs += [('balance', 1, 0, 'text', _('Balance'), None, cell_style_right)]
  140. else:
  141. c_specs += [('balance_fy', 1, 0, 'text', _('Balance %s') % _p.fiscalyear.name, None, cell_style_right)]
  142. if _p.comparison_mode in ('single', 'multiple'):
  143. for index in range(_p.nb_comparison):
  144. if _p.comp_params[index]['comparison_filter'] == 'filter_year' and _p.comp_params[index].get('fiscalyear', False):
  145. c_specs += [('balance_%s' %index, 1, 0, 'text', _('Balance %s') % _p.comp_params[index]['fiscalyear'].name, None, cell_style_right)]
  146. else:
  147. c_specs += [('balance_%s' %index, 1, 0, 'text', _('Balance C%s') % (index + 1), None, cell_style_right)]
  148. if _p.comparison_mode == 'single':
  149. c_specs += [
  150. ('diff', 1, 0, 'text', _('Difference'), None, cell_style_right),
  151. ('diff_percent', 1, 0, 'text', _('% Difference'), None, cell_style_center),
  152. ]
  153. c_specs += [('type', 1, 0, 'text', _('Type'), None, cell_style_center)]
  154. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  155. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  156. ws.set_horz_split_pos(row_pos)
  157. last_child_consol_ids = []
  158. last_level = False
  159. # cell styles for account data
  160. view_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  161. view_cell_style = xlwt.easyxf(view_cell_format)
  162. view_cell_style_center = xlwt.easyxf(view_cell_format + _xs['center'])
  163. view_cell_style_decimal = xlwt.easyxf(view_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  164. view_cell_style_pct = xlwt.easyxf(view_cell_format + _xs['center'], num_format_str = '0')
  165. regular_cell_format = _xs['borders_all']
  166. regular_cell_style = xlwt.easyxf(regular_cell_format)
  167. regular_cell_style_center = xlwt.easyxf(regular_cell_format + _xs['center'])
  168. regular_cell_style_decimal = xlwt.easyxf(regular_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  169. regular_cell_style_pct = xlwt.easyxf(regular_cell_format + _xs['center'], num_format_str = '0')
  170. for current_account in objects:
  171. if not current_account.to_display:
  172. continue
  173. if current_account.type == 'view':
  174. cell_style = view_cell_style
  175. cell_style_center = view_cell_style_center
  176. cell_style_decimal = view_cell_style_decimal
  177. cell_style_pct = view_cell_style_pct
  178. else:
  179. cell_style = regular_cell_style
  180. cell_style_center = regular_cell_style_center
  181. cell_style_decimal = regular_cell_style_decimal
  182. cell_style_pct = regular_cell_style_pct
  183. comparisons = current_account.comparisons
  184. if current_account.id in last_child_consol_ids:
  185. # current account is a consolidation child of the last account: use the level of last account
  186. level = last_level
  187. level_class = 'account_level_consol'
  188. else:
  189. # current account is a not a consolidation child: use its own level
  190. level = current_account.level or 0
  191. level_class = "account_level_%s" % (level,)
  192. last_child_consol_ids = [child_consol_id.id for child_consol_id in current_account.child_consol_ids]
  193. last_level = current_account.level
  194. c_specs = [
  195. ('code', 1, 0, 'text', current_account.code),
  196. ('account', account_span, 0, 'text', current_account.name),
  197. ]
  198. if _p.comparison_mode == 'no_comparison':
  199. debit_cell = rowcol_to_cell(row_pos, 3)
  200. credit_cell = rowcol_to_cell(row_pos, 4)
  201. bal_formula = debit_cell + '-' + credit_cell
  202. if _p.initial_balance_mode:
  203. init_cell = rowcol_to_cell(row_pos, 3)
  204. debit_cell = rowcol_to_cell(row_pos, 4)
  205. credit_cell = rowcol_to_cell(row_pos, 5)
  206. bal_formula = init_cell + '+' + debit_cell + '-' + credit_cell
  207. c_specs += [('init_bal', 1, 0, 'number', current_account.init_balance, None, cell_style_decimal)]
  208. c_specs += [
  209. ('debit', 1, 0, 'number', current_account.debit, None, cell_style_decimal),
  210. ('credit', 1, 0, 'number', current_account.credit, None, cell_style_decimal),
  211. ]
  212. c_specs += [('balance', 1, 0, 'number', None, bal_formula, cell_style_decimal)]
  213. else:
  214. c_specs += [('balance', 1, 0, 'number', current_account.balance, None, cell_style_decimal)]
  215. if _p.comparison_mode in ('single', 'multiple'):
  216. c = 1
  217. for comp_account in comparisons:
  218. c_specs += [('balance_%s' %c, 1, 0, 'number', comp_account['balance'], None, cell_style_decimal)]
  219. c += 1
  220. if _p.comparison_mode == 'single':
  221. c_specs += [
  222. ('diff', 1, 0, 'number', comp_account['diff'], None, cell_style_decimal),
  223. ('diff_percent', 1, 0, 'number', comp_account['percent_diff'] and comp_account['percent_diff'] or 0, None, cell_style_pct),
  224. ]
  225. c_specs += [('type', 1, 0, 'text', current_account.type, None, cell_style_center)]
  226. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  227. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  228. trial_balance_xls('report.account.account_report_trial_balance_xls', 'account.account',
  229. parser=TrialBalanceWebkit)
  230. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: