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.

206 lines
8.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from . import abstract_report_xlsx
  6. from odoo.report import report_sxw
  7. from odoo import _
  8. class TrialBalanceXslx(abstract_report_xlsx.AbstractReportXslx):
  9. def __init__(self, name, table, rml=False, parser=False, header=True,
  10. store=False):
  11. super(TrialBalanceXslx, self).__init__(
  12. name, table, rml, parser, header, store)
  13. def _get_report_name(self, report):
  14. report_name = _('Trial Balance')
  15. return self._get_report_complete_name(report, report_name)
  16. def _get_report_columns(self, report):
  17. if not report.show_partner_details:
  18. res = {
  19. 0: {'header': _('Code'), 'field': 'code', 'width': 10},
  20. 1: {'header': _('Account'), 'field': 'name', 'width': 60},
  21. 2: {'header': _('Initial balance'),
  22. 'field': 'initial_balance',
  23. 'type': 'amount',
  24. 'width': 14},
  25. 3: {'header': _('Debit'),
  26. 'field': 'debit',
  27. 'type': 'amount',
  28. 'width': 14},
  29. 4: {'header': _('Credit'),
  30. 'field': 'credit',
  31. 'type': 'amount',
  32. 'width': 14},
  33. 5: {'header': _('Period balance'),
  34. 'field': 'period_balance',
  35. 'type': 'amount',
  36. 'width': 14},
  37. 6: {'header': _('Ending balance'),
  38. 'field': 'final_balance',
  39. 'type': 'amount',
  40. 'width': 14},
  41. }
  42. if report.foreign_currency:
  43. foreign_currency = {
  44. 7: {'header': _('Cur.'),
  45. 'field': 'currency_id',
  46. 'field_currency_balance': 'currency_id',
  47. 'type': 'many2one', 'width': 7},
  48. 8: {'header': _('Initial balance'),
  49. 'field': 'initial_balance_foreign_currency',
  50. 'type': 'amount_currency',
  51. 'width': 14},
  52. 9: {'header': _('Ending balance'),
  53. 'field': 'final_balance_foreign_currency',
  54. 'type': 'amount_currency',
  55. 'width': 14},
  56. }
  57. res = dict(res.items() + foreign_currency.items())
  58. return res
  59. else:
  60. res = {
  61. 0: {'header': _('Partner'), 'field': 'name', 'width': 70},
  62. 1: {'header': _('Initial balance'),
  63. 'field': 'initial_balance',
  64. 'type': 'amount',
  65. 'width': 14},
  66. 2: {'header': _('Debit'),
  67. 'field': 'debit',
  68. 'type': 'amount',
  69. 'width': 14},
  70. 3: {'header': _('Credit'),
  71. 'field': 'credit',
  72. 'type': 'amount',
  73. 'width': 14},
  74. 4: {'header': _('Period balance'),
  75. 'field': 'period_balance',
  76. 'type': 'amount',
  77. 'width': 14},
  78. 5: {'header': _('Ending balance'),
  79. 'field': 'final_balance',
  80. 'type': 'amount',
  81. 'width': 14},
  82. }
  83. if report.foreign_currency:
  84. foreign_currency = {
  85. 6: {'header': _('Cur.'),
  86. 'field': 'currency_id',
  87. 'field_currency_balance': 'currency_id',
  88. 'type': 'many2one', 'width': 7},
  89. 7: {'header': _('Initial balance'),
  90. 'field': 'initial_balance_foreign_currency',
  91. 'type': 'amount_currency',
  92. 'width': 14},
  93. 8: {'header': _('Ending balance'),
  94. 'field': 'final_balance_foreign_currency',
  95. 'type': 'amount_currency',
  96. 'width': 14},
  97. }
  98. res = dict(res.items() + foreign_currency.items())
  99. return res
  100. def _get_report_filters(self, report):
  101. return [
  102. [_('Date range filter'),
  103. _('From: %s To: %s') % (report.date_from, report.date_to)],
  104. [_('Target moves filter'),
  105. _('All posted entries') if report.only_posted_moves
  106. else _('All entries')],
  107. [_('Account at 0 filter'),
  108. _('Hide') if report.hide_account_at_0 else _('Show')],
  109. [_('Show foreign currency'),
  110. _('Yes') if report.foreign_currency else _('No')],
  111. [_('Limit hierarchy levels'),
  112. _('Level %s' % report.show_hierarchy_level) if
  113. report.limit_hierarchy_level else _('No limit')],
  114. ]
  115. def _get_col_count_filter_name(self):
  116. return 2
  117. def _get_col_count_filter_value(self):
  118. return 3
  119. def _generate_report_content(self, workbook, report):
  120. if not report.show_partner_details:
  121. # Display array header for account lines
  122. self.write_array_header()
  123. # For each account
  124. for account in report.account_ids.filtered(lambda a: not a.hide_line):
  125. if not report.show_partner_details:
  126. # Display account lines
  127. self.write_line(account, 'account')
  128. else:
  129. # Write account title
  130. self.write_array_title(account.code + ' - ' + account.name)
  131. # Display array header for partner lines
  132. self.write_array_header()
  133. # For each partner
  134. for partner in account.partner_ids:
  135. # Display partner lines
  136. self.write_line(partner, 'partner')
  137. # Display account footer line
  138. self.write_account_footer(account,
  139. account.code + ' - ' + account.name)
  140. # Line break
  141. self.row_pos += 2
  142. def write_line(self, line_object, type_object):
  143. """Write a line on current line using all defined columns field name.
  144. Columns are defined with `_get_report_columns` method.
  145. """
  146. if type_object == 'partner':
  147. line_object.currency_id = line_object.report_account_id.currency_id
  148. elif type_object == 'account':
  149. line_object.currency_id = line_object.currency_id
  150. super(TrialBalanceXslx, self).write_line(line_object)
  151. def write_account_footer(self, account, name_value):
  152. """Specific function to write account footer for Trial Balance"""
  153. format_amt = self._get_currency_amt_header_format(account)
  154. for col_pos, column in self.columns.iteritems():
  155. if column['field'] == 'name':
  156. value = name_value
  157. else:
  158. value = getattr(account, column['field'])
  159. cell_type = column.get('type', 'string')
  160. if cell_type == 'string':
  161. self.sheet.write_string(self.row_pos, col_pos, value or '',
  162. self.format_header_left)
  163. elif cell_type == 'amount':
  164. self.sheet.write_number(self.row_pos, col_pos,
  165. self._number_as_decimal(value),
  166. self.format_header_amount)
  167. elif cell_type == 'many2one':
  168. self.sheet.write_string(
  169. self.row_pos, col_pos, value.name or '',
  170. self.format_header_right)
  171. elif cell_type == 'amount_currency' and account.currency_id:
  172. self.sheet.write_number(
  173. self.row_pos, col_pos, self._number_as_decimal(
  174. value, account.currency_id.decimal_places),
  175. format_amt)
  176. else:
  177. self.sheet.write_string(
  178. self.row_pos, col_pos, '',
  179. self.format_header_right)
  180. self.row_pos += 1
  181. TrialBalanceXslx(
  182. 'report.account_financial_report_qweb.report_trial_balance_xlsx',
  183. 'report_trial_balance_qweb',
  184. parser=report_sxw.rml_parse
  185. )