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.

197 lines
6.0 KiB

  1. # Copyright 2017 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. class JournalLedgerReportWizard(models.TransientModel):
  5. """Journal Ledger report wizard."""
  6. _name = 'journal.ledger.report.wizard'
  7. _description = "Journal Ledger Report Wizard"
  8. company_id = fields.Many2one(
  9. comodel_name='res.company',
  10. default=lambda self: self.env.user.company_id,
  11. string='Company',
  12. required=False,
  13. ondelete='cascade',
  14. )
  15. date_range_id = fields.Many2one(
  16. comodel_name='date.range',
  17. string='Date range',
  18. )
  19. date_from = fields.Date(
  20. string="Start date",
  21. required=True
  22. )
  23. date_to = fields.Date(
  24. string="End date",
  25. required=True
  26. )
  27. journal_ids = fields.Many2many(
  28. comodel_name='account.journal',
  29. string="Journals",
  30. required=False,
  31. )
  32. move_target = fields.Selection(
  33. selection='_get_move_targets',
  34. default='all',
  35. required=True,
  36. )
  37. foreign_currency = fields.Boolean()
  38. sort_option = fields.Selection(
  39. selection='_get_sort_options',
  40. string="Sort entries by",
  41. default='move_name',
  42. required=True,
  43. )
  44. group_option = fields.Selection(
  45. selection='_get_group_options',
  46. string="Group entries by",
  47. default='journal',
  48. required=True,
  49. )
  50. with_account_name = fields.Boolean(
  51. default=False,
  52. )
  53. @api.model
  54. def _get_move_targets(self):
  55. return [
  56. ('all', _("All")),
  57. ('posted', _("Posted")),
  58. ('draft', _("Not Posted"))
  59. ]
  60. @api.model
  61. def _get_sort_options(self):
  62. return [
  63. ('move_name', _("Entry number")),
  64. ('date', _("Date")),
  65. ]
  66. @api.model
  67. def _get_group_options(self):
  68. return [
  69. ('journal', _("Journal")),
  70. ('none', _("No group")),
  71. ]
  72. @api.onchange('date_range_id')
  73. def onchange_date_range_id(self):
  74. self.date_from = self.date_range_id.date_start
  75. self.date_to = self.date_range_id.date_end
  76. @api.onchange('company_id')
  77. def onchange_company_id(self):
  78. """Handle company change."""
  79. if self.company_id and self.date_range_id.company_id and \
  80. self.date_range_id.company_id != self.company_id:
  81. self.date_range_id = False
  82. if self.company_id and self.journal_ids:
  83. self.journal_ids = self.journal_ids.filtered(
  84. lambda p: p.company_id == self.company_id or not p.company_id)
  85. res = {'domain': {'journal_ids': []}}
  86. if not self.company_id:
  87. return res
  88. else:
  89. res['domain']['journal_ids'] += [
  90. ('company_id', '=', self.company_id.id)]
  91. return res
  92. @api.multi
  93. def _print_report(self, report_type):
  94. self.ensure_one()
  95. data = self._prepare_report_journal_ledger()
  96. if report_type == 'xlsx':
  97. report_name = 'a_f_r.report_journal_ledger_xlsx'
  98. else:
  99. report_name = 'account_financial_report.journal_ledger'
  100. return self.env['ir.actions.report'].search(
  101. [('report_name', '=', report_name),
  102. ('report_type', '=', report_type)], limit=1).report_action(
  103. self, data=data)
  104. @api.multi
  105. def button_export_html(self):
  106. self.ensure_one()
  107. report_type = 'qweb-html'
  108. return self._export(report_type)
  109. @api.multi
  110. def button_export_pdf(self):
  111. report_type = 'qweb-pdf'
  112. return self._export(report_type)
  113. @api.multi
  114. def button_export_xlsx(self):
  115. self.ensure_one()
  116. report_type = 'xlsx'
  117. return self._export(report_type)
  118. @api.multi
  119. def _prepare_report_journal_ledger(self):
  120. self.ensure_one()
  121. journals = self.journal_ids
  122. if not journals:
  123. # Not selecting a journal means that we'll display all journals
  124. journals = self.env['account.journal'].search(
  125. [('company_id', '=', self.company_id.id)])
  126. return {
  127. 'wizard_id': self.id,
  128. 'date_from': self.date_from,
  129. 'date_to': self.date_to,
  130. 'move_target': self.move_target,
  131. 'foreign_currency': self.foreign_currency,
  132. 'company_id': self.company_id.id,
  133. 'journal_ids': journals.ids,
  134. 'sort_option': self.sort_option,
  135. 'group_option': self.group_option,
  136. 'with_account_name': self.with_account_name,
  137. }
  138. def _export(self, report_type):
  139. """Default export is PDF."""
  140. self.ensure_one()
  141. return self._print_report(report_type)
  142. @api.model
  143. def _get_ml_tax_description(
  144. self, move_line_data, tax_line_data, move_line_taxes_data):
  145. taxes_description = ''
  146. if move_line_data['tax_line_id']:
  147. taxes_description = tax_line_data['description'] or \
  148. tax_line_data['name']
  149. elif move_line_taxes_data:
  150. tax_names = []
  151. for tax_key in move_line_taxes_data:
  152. tax = move_line_taxes_data[tax_key]
  153. tax_names.append(
  154. tax['description'] or tax['name'])
  155. taxes_description = ','.join(tax_names)
  156. return taxes_description
  157. @api.model
  158. def _get_partner_name(self, partner_id, partner_data):
  159. if str(partner_id) in partner_data.keys():
  160. return partner_data[str(partner_id)]['name']
  161. else:
  162. return ''
  163. @api.model
  164. def _get_atr_from_dict(self, obj_id, data, key):
  165. try:
  166. return data[obj_id][key]
  167. except KeyError:
  168. return data[str(obj_id)][key]
  169. @api.model
  170. def _get_data_from_dict(self, obj_id, data):
  171. if data:
  172. if isinstance(list(data.keys())[0], int):
  173. return data.get(obj_id, False)
  174. else:
  175. return data.get(obj_id(obj_id), False)
  176. else:
  177. return False