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.

296 lines
9.9 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import _, models
  6. class JournalLedgerXslx(models.AbstractModel):
  7. _name = 'report.a_f_r.report_journal_ledger_xlsx'
  8. _inherit = 'report.account_financial_report.abstract_report_xlsx'
  9. def _get_report_name(self, report, data=False):
  10. company_id = data.get('company_id', False)
  11. report_name = _('Journal Ledger')
  12. if company_id:
  13. company = self.env['res.company'].browse(company_id)
  14. suffix = ' - %s - %s' % (
  15. company.name, company.currency_id.name)
  16. report_name = report_name + suffix
  17. return report_name
  18. def _get_report_columns(self, report):
  19. columns = [
  20. {
  21. 'header': _('Entry'),
  22. 'field': 'entry',
  23. 'width': 18
  24. },
  25. {
  26. 'header': _('Date'),
  27. 'field': 'date',
  28. 'width': 11
  29. },
  30. {
  31. 'header': _('Account'),
  32. 'field': 'account_code',
  33. 'width': 9
  34. },
  35. ]
  36. if report.with_account_name:
  37. columns.append({
  38. 'header': _('Account Name'),
  39. 'field': 'account_name',
  40. 'width': 15
  41. })
  42. columns += [
  43. {
  44. 'header': _('Partner'),
  45. 'field': 'partner',
  46. 'width': 25
  47. },
  48. {
  49. 'header': _('Ref - Label'),
  50. 'field': 'label',
  51. 'width': 40
  52. },
  53. {
  54. 'header': _('Taxes'),
  55. 'field': 'taxes_description',
  56. 'width': 11
  57. },
  58. {
  59. 'header': _('Debit'),
  60. 'field': 'debit',
  61. 'type': 'amount',
  62. 'width': 14,
  63. },
  64. {
  65. 'header': _('Credit'),
  66. 'field': 'credit',
  67. 'type': 'amount',
  68. 'width': 14
  69. }
  70. ]
  71. if report.foreign_currency:
  72. columns += [
  73. {
  74. 'header': _('Currency'),
  75. 'field': 'currency_name',
  76. 'width': 14,
  77. 'type': 'currency_name',
  78. },
  79. {
  80. 'header': _('Amount Currency'),
  81. 'field': 'amount_currency',
  82. 'type': 'amount',
  83. 'width': 18
  84. },
  85. ]
  86. columns_as_dict = {}
  87. for i, column in enumerate(columns):
  88. columns_as_dict[i] = column
  89. return columns_as_dict
  90. def _get_journal_tax_columns(self, report):
  91. return {
  92. 0: {
  93. 'header': _('Name'),
  94. 'field': 'tax_name',
  95. 'width': 35
  96. },
  97. 1: {
  98. 'header': _('Description'),
  99. 'field': 'tax_code',
  100. 'width': 18
  101. },
  102. 2: {
  103. 'header': _('Base Debit'),
  104. 'field': 'base_debit',
  105. 'type': 'amount',
  106. 'width': 14
  107. },
  108. 3: {
  109. 'header': _('Base Credit'),
  110. 'field': 'base_credit',
  111. 'type': 'amount',
  112. 'width': 14
  113. },
  114. 4: {
  115. 'header': _('Base Balance'),
  116. 'field': 'base_balance',
  117. 'type': 'amount',
  118. 'width': 14
  119. },
  120. 5: {
  121. 'header': _('Tax Debit'),
  122. 'field': 'tax_debit',
  123. 'type': 'amount',
  124. 'width': 14
  125. },
  126. 6: {
  127. 'header': _('Tax Credit'),
  128. 'field': 'tax_credit',
  129. 'type': 'amount',
  130. 'width': 14
  131. },
  132. 7: {
  133. 'header': _('Tax Balance'),
  134. 'field': 'tax_balance',
  135. 'type': 'amount',
  136. 'width': 14
  137. },
  138. }
  139. def _get_col_count_filter_name(self):
  140. return 2
  141. def _get_col_count_filter_value(self):
  142. return 3
  143. def _get_report_filters(self, report):
  144. target_label_by_value = {
  145. value: label
  146. for value, label in
  147. self.env['journal.ledger.report.wizard']._get_move_targets()
  148. }
  149. sort_option_label_by_value = {
  150. value: label
  151. for value, label in
  152. self.env['journal.ledger.report.wizard']._get_sort_options()
  153. }
  154. return [
  155. [
  156. _('Company'),
  157. report.company_id.name
  158. ],
  159. [
  160. _('Date range filter'),
  161. _('From: %s To: %s') % (report.date_from, report.date_to)
  162. ],
  163. [
  164. _('Target moves filter'),
  165. _("%s") % target_label_by_value[report.move_target],
  166. ],
  167. [
  168. _('Entries sorted by'),
  169. _("%s") % sort_option_label_by_value[report.sort_option],
  170. ],
  171. [
  172. _('Journals'),
  173. ', '.join([
  174. "%s - %s" % (report_journal.code, report_journal.name)
  175. for report_journal in report.journal_ids
  176. ])
  177. ]
  178. ]
  179. def _generate_report_content(self, workbook, report, data):
  180. res_data = self.env[
  181. 'report.account_financial_report.journal_ledger'
  182. ]._get_report_values(report, data)
  183. group_option = report.group_option
  184. if group_option == 'journal':
  185. for ledger in res_data['Journal_Ledgers']:
  186. self._generate_journal_content(workbook, report, res_data,
  187. ledger)
  188. elif group_option == 'none':
  189. self._generate_no_group_content(workbook, report,
  190. res_data)
  191. def _generate_no_group_content(self, workbook, report, res_data):
  192. self._generate_moves_content(
  193. workbook, "Report", report, res_data, res_data['Moves'])
  194. self._generate_no_group_taxes_summary(workbook, report, res_data)
  195. def _generate_journal_content(self, workbook, report, res_data, ledger):
  196. journal = self.env['account.journal'].browse(ledger['id'])
  197. currency_name = journal.currency_id and journal.currency_id.name or \
  198. journal.company_id.currency_id.name
  199. sheet_name = "%s (%s) - %s" % (
  200. journal.code,
  201. currency_name,
  202. journal.name,
  203. )
  204. self._generate_moves_content(
  205. workbook, sheet_name, report, res_data, ledger['report_moves'])
  206. self._generate_journal_taxes_summary(workbook, ledger)
  207. def _generate_no_group_taxes_summary(self, workbook, report, res_data):
  208. self._generate_taxes_summary(
  209. workbook, "Tax Report", res_data['tax_line_data'])
  210. def _generate_journal_taxes_summary(self, workbook, ledger):
  211. journal = self.env['account.journal'].browse(ledger['id'])
  212. currency_name = journal.currency_id and journal.currency_id.name or \
  213. journal.company_id.currency_id.name
  214. sheet_name = "Tax - %s (%s) - %s" % (
  215. journal.code,
  216. currency_name,
  217. journal.name,
  218. )
  219. self._generate_taxes_summary(
  220. workbook, sheet_name, ledger['tax_lines'])
  221. def _generate_moves_content(self, workbook, sheet_name, report, res_data,
  222. moves):
  223. self.workbook = workbook
  224. self.sheet = workbook.add_worksheet(sheet_name)
  225. self._set_column_width()
  226. self.row_pos = 1
  227. self.write_array_title(sheet_name)
  228. self.row_pos += 2
  229. self.write_array_header()
  230. account_ids_data = res_data['account_ids_data']
  231. partner_ids_data = res_data['partner_ids_data']
  232. currency_ids_data = res_data['currency_ids_data']
  233. move_ids_data = res_data['move_ids_data']
  234. for move in moves:
  235. for line in move['report_move_lines']:
  236. currency_data = currency_ids_data.get(
  237. line['currency_id'], False)
  238. currency_name = currency_data and currency_data['name'] or ''
  239. account_data = account_ids_data.get(line['account_id'], False)
  240. account_name = account_data and account_data['name'] or ''
  241. account_code = account_data and account_data['code'] or ''
  242. move_data = move_ids_data.get(line['move_id'], False)
  243. move_entry = move_data and move_data['entry'] or ''
  244. line['partner'] = self._get_partner_name(line['partner_id'],
  245. partner_ids_data)
  246. line['account_code'] = account_code
  247. line['account_name'] = account_name
  248. line['currency_name'] = currency_name
  249. line['entry'] = move_entry
  250. line['taxes_description'] = \
  251. report._get_ml_tax_description(
  252. line, res_data['tax_line_data'].get(
  253. line['tax_line_id']),
  254. res_data['move_line_ids_taxes_data'].get(
  255. line['move_line_id'], False))
  256. self.write_line_from_dict(line)
  257. self.row_pos += 1
  258. def _generate_taxes_summary(self, workbook, sheet_name, tax_lines_dict):
  259. self.workbook = workbook
  260. self.sheet = workbook.add_worksheet(sheet_name)
  261. self.row_pos = 1
  262. self.write_array_title(sheet_name)
  263. self.row_pos += 2
  264. def _get_partner_name(self, partner_id, partner_data):
  265. if partner_id in partner_data.keys():
  266. return partner_data[partner_id]['name']
  267. else:
  268. return ''