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.

180 lines
7.1 KiB

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