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