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.

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