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.

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