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.

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