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.

248 lines
11 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, data=False):
  9. company_id = data.get('company_id', False)
  10. report_name = _('Trial Balance')
  11. if company_id:
  12. company = self.env['res.company'].browse(company_id)
  13. suffix = ' - %s - %s' % (
  14. company.name, company.currency_id.name)
  15. report_name = report_name + suffix
  16. return report_name
  17. def _get_report_columns(self, report):
  18. if not report.show_partner_details:
  19. res = {
  20. 0: {'header': _('Code'), 'field': 'code', 'width': 10},
  21. 1: {'header': _('Account'), 'field': 'name', 'width': 60},
  22. 2: {'header': _('Initial balance'),
  23. 'field': 'initial_balance',
  24. 'type': 'amount',
  25. 'width': 14},
  26. 3: {'header': _('Debit'),
  27. 'field': 'debit',
  28. 'type': 'amount',
  29. 'width': 14},
  30. 4: {'header': _('Credit'),
  31. 'field': 'credit',
  32. 'type': 'amount',
  33. 'width': 14},
  34. 5: {'header': _('Period balance'),
  35. 'field': 'period_balance',
  36. 'type': 'amount',
  37. 'width': 14},
  38. 6: {'header': _('Ending balance'),
  39. 'field': 'final_balance',
  40. 'type': 'amount',
  41. 'width': 14},
  42. }
  43. if report.foreign_currency:
  44. foreign_currency = {
  45. 7: {'header': _('Cur.'),
  46. 'field': 'currency_id',
  47. 'field_currency_balance': 'currency_id',
  48. 'type': 'many2one', 'width': 7},
  49. 8: {'header': _('Initial balance'),
  50. 'field': 'initial_balance_foreign_currency',
  51. 'type': 'amount_currency',
  52. 'width': 14},
  53. 9: {'header': _('Ending balance'),
  54. 'field': 'final_balance_foreign_currency',
  55. 'type': 'amount_currency',
  56. 'width': 14},
  57. }
  58. res = {**res, **foreign_currency}
  59. return res
  60. else:
  61. res = {
  62. 0: {'header': _('Partner'), 'field': 'name', 'width': 70},
  63. 1: {'header': _('Initial balance'),
  64. 'field': 'initial_balance',
  65. 'type': 'amount',
  66. 'width': 14},
  67. 2: {'header': _('Debit'),
  68. 'field': 'debit',
  69. 'type': 'amount',
  70. 'width': 14},
  71. 3: {'header': _('Credit'),
  72. 'field': 'credit',
  73. 'type': 'amount',
  74. 'width': 14},
  75. 4: {'header': _('Period balance'),
  76. 'field': 'balance',
  77. 'type': 'amount',
  78. 'width': 14},
  79. 5: {'header': _('Ending balance'),
  80. 'field': 'ending_balance',
  81. 'type': 'amount',
  82. 'width': 14},
  83. }
  84. if report.foreign_currency:
  85. foreign_currency = {
  86. 6: {'header': _('Cur.'),
  87. 'field': 'currency_id',
  88. 'field_currency_balance': 'currency_id',
  89. 'type': 'many2one', 'width': 7},
  90. 7: {'header': _('Initial balance'),
  91. 'field': 'initial_currency_balance',
  92. 'type': 'amount_currency',
  93. 'width': 14},
  94. 8: {'header': _('Ending balance'),
  95. 'field': 'ending_currency_balance',
  96. 'type': 'amount_currency',
  97. 'width': 14},
  98. }
  99. res = {**res, **foreign_currency}
  100. return res
  101. def _get_report_filters(self, report):
  102. return [
  103. [_('Date range filter'),
  104. _('From: %s To: %s') % (report.date_from, report.date_to)],
  105. [_('Target moves filter'),
  106. _('All posted entries') if report.target_move == 'all' else _(
  107. 'All entries')],
  108. [_('Account at 0 filter'),
  109. _('Hide') if report.hide_account_at_0 else _('Show')],
  110. [_('Show foreign currency'),
  111. _('Yes') if report.foreign_currency else _('No')],
  112. [_('Limit hierarchy levels'),
  113. _('Level %s' % report.show_hierarchy_level) if
  114. report.limit_hierarchy_level else _('No limit')],
  115. ]
  116. def _get_col_count_filter_name(self):
  117. return 2
  118. def _get_col_count_filter_value(self):
  119. return 3
  120. def _generate_report_content(self, workbook, report, data):
  121. res_data = self.env[
  122. 'report.account_financial_report.trial_balance']._get_report_values(
  123. report, data)
  124. trial_balance = res_data['trial_balance']
  125. total_amount = res_data['total_amount']
  126. partners_data = res_data['partners_data']
  127. accounts_data = res_data['accounts_data']
  128. hierarchy_on = res_data['hierarchy_on']
  129. show_partner_details = res_data['show_partner_details']
  130. show_hierarchy_level = res_data['show_hierarchy_level']
  131. foreign_currency = res_data['foreign_currency']
  132. limit_hierarchy_level = res_data['limit_hierarchy_level']
  133. if not show_partner_details:
  134. # Display array header for account lines
  135. self.write_array_header()
  136. # For each account
  137. if not show_partner_details:
  138. for balance in trial_balance:
  139. if hierarchy_on == 'relation':
  140. if limit_hierarchy_level:
  141. if show_hierarchy_level > balance['level']:
  142. # Display account lines
  143. self.write_line_from_dict(balance)
  144. else:
  145. self.write_line_from_dict(balance)
  146. elif hierarchy_on == 'computed':
  147. if balance['type'] == 'account_type':
  148. if limit_hierarchy_level:
  149. if show_hierarchy_level > balance['level']:
  150. # Display account lines
  151. self.write_line_from_dict(balance)
  152. else:
  153. self.write_line_from_dict(balance)
  154. else:
  155. self.write_line_from_dict(balance)
  156. else:
  157. for account_id in total_amount:
  158. # Write account title
  159. self.write_array_title(accounts_data[account_id]['code'] + '- '
  160. + accounts_data[account_id]['name'])
  161. # Display array header for partner lines
  162. self.write_array_header()
  163. # For each partner
  164. for partner_id in total_amount[account_id]:
  165. if isinstance(partner_id, int):
  166. # Display partner lines
  167. self.write_line_from_dict_order(
  168. total_amount[account_id][partner_id],
  169. partners_data[partner_id])
  170. # Display account footer line
  171. accounts_data[account_id].update({
  172. 'initial_balance': total_amount[account_id][
  173. 'initial_balance'],
  174. 'credit': total_amount[account_id]['credit'],
  175. 'debit': total_amount[account_id]['debit'],
  176. 'balance': total_amount[account_id]['balance'],
  177. 'ending_balance': total_amount[account_id]['ending_balance']
  178. })
  179. if foreign_currency:
  180. accounts_data[account_id].update({
  181. 'initial_currency_balance': total_amount[account_id][
  182. 'initial_currency_balance'],
  183. 'ending_currency_balance': total_amount[account_id][
  184. 'ending_currency_balance']
  185. })
  186. self.write_account_footer(accounts_data[account_id],
  187. accounts_data[account_id][
  188. 'code'] + '- '
  189. + accounts_data[account_id]['name'])
  190. # Line break
  191. self.row_pos += 2
  192. def write_line_from_dict_order(self, total_amount, partner_data):
  193. total_amount.update({'name': str(partner_data['name'])})
  194. self.write_line_from_dict(total_amount)
  195. def write_line(self, line_object, type_object):
  196. """Write a line on current line using all defined columns field name.
  197. Columns are defined with `_get_report_columns` method.
  198. """
  199. if type_object == 'partner':
  200. line_object.currency_id = line_object.report_account_id.currency_id
  201. elif type_object == 'account':
  202. line_object.currency_id = line_object.currency_id
  203. super(TrialBalanceXslx, self).write_line(line_object)
  204. def write_account_footer(self, account, name_value):
  205. """Specific function to write account footer for Trial Balance"""
  206. format_amt = self._get_currency_amt_header_format_dict(account)
  207. for col_pos, column in self.columns.items():
  208. if column['field'] == 'name':
  209. value = name_value
  210. else:
  211. value = account[column['field']]
  212. cell_type = column.get('type', 'string')
  213. if cell_type == 'string':
  214. self.sheet.write_string(self.row_pos, col_pos, value or '',
  215. self.format_header_left)
  216. elif cell_type == 'amount':
  217. self.sheet.write_number(self.row_pos, col_pos, float(value),
  218. self.format_header_amount)
  219. elif cell_type == 'many2one' and account['currency_id']:
  220. self.sheet.write_string(
  221. self.row_pos, col_pos, value.name or '',
  222. self.format_header_right)
  223. elif cell_type == 'amount_currency' and account['currency_id']:
  224. self.sheet.write_number(
  225. self.row_pos, col_pos, float(value),
  226. format_amt)
  227. else:
  228. self.sheet.write_string(
  229. self.row_pos, col_pos, '',
  230. self.format_header_right)
  231. self.row_pos += 1