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.

150 lines
5.8 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, fields, api
  6. class OpenItemsReportWizard(models.TransientModel):
  7. """Open items report wizard."""
  8. _name = "open.items.report.wizard"
  9. _description = "Open Items Report Wizard"
  10. _inherit = 'account_financial_report_abstract_wizard'
  11. company_id = fields.Many2one(
  12. comodel_name='res.company',
  13. default=lambda self: self.env.user.company_id,
  14. required=False,
  15. string='Company'
  16. )
  17. date_at = fields.Date(required=True,
  18. default=fields.Date.context_today)
  19. date_from = fields.Date(string='Date From')
  20. target_move = fields.Selection([('posted', 'All Posted Entries'),
  21. ('all', 'All Entries')],
  22. string='Target Moves',
  23. required=True,
  24. default='posted')
  25. account_ids = fields.Many2many(
  26. comodel_name='account.account',
  27. string='Filter accounts',
  28. domain=[('reconcile', '=', True)],
  29. )
  30. hide_account_at_0 = fields.Boolean(
  31. string='Hide account ending balance at 0', default=True,
  32. help='Use this filter to hide an account or a partner '
  33. 'with an ending balance at 0. '
  34. 'If partners are filtered, '
  35. 'debits and credits totals will not match the trial balance.'
  36. )
  37. receivable_accounts_only = fields.Boolean()
  38. payable_accounts_only = fields.Boolean()
  39. partner_ids = fields.Many2many(
  40. comodel_name='res.partner',
  41. string='Filter partners',
  42. default=lambda self: self._default_partners(),
  43. )
  44. foreign_currency = fields.Boolean(
  45. string='Show foreign currency',
  46. help='Display foreign currency for move lines, unless '
  47. 'account currency is not setup through chart of accounts '
  48. 'will display initial and final balance in that currency.',
  49. default=lambda self: self._default_foreign_currency(),
  50. )
  51. show_partner_details = fields.Boolean(
  52. string='Show Partner Details',
  53. default=True,
  54. )
  55. def _default_foreign_currency(self):
  56. return self.env.user.has_group('base.group_multi_currency')
  57. @api.onchange('company_id')
  58. def onchange_company_id(self):
  59. """Handle company change."""
  60. if self.company_id and self.partner_ids:
  61. self.partner_ids = self.partner_ids.filtered(
  62. lambda p: p.company_id == self.company_id or
  63. not p.company_id)
  64. if self.company_id and self.account_ids:
  65. if self.receivable_accounts_only or self.payable_accounts_only:
  66. self.onchange_type_accounts_only()
  67. else:
  68. self.account_ids = self.account_ids.filtered(
  69. lambda a: a.company_id == self.company_id)
  70. res = {'domain': {'account_ids': [],
  71. 'partner_ids': []}}
  72. if not self.company_id:
  73. return res
  74. else:
  75. res['domain']['account_ids'] += [
  76. ('company_id', '=', self.company_id.id)]
  77. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  78. return res
  79. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  80. def onchange_type_accounts_only(self):
  81. """Handle receivable/payable accounts only change."""
  82. domain = [('company_id', '=', self.company_id.id)]
  83. if self.receivable_accounts_only or self.payable_accounts_only:
  84. if self.receivable_accounts_only and self.payable_accounts_only:
  85. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  86. elif self.receivable_accounts_only:
  87. domain += [('internal_type', '=', 'receivable')]
  88. elif self.payable_accounts_only:
  89. domain += [('internal_type', '=', 'payable')]
  90. elif not self.receivable_accounts_only and not self.payable_accounts_only:
  91. domain += [('reconcile', '=', True)]
  92. self.account_ids = self.env['account.account'].search(domain)
  93. @api.multi
  94. def _print_report(self, report_type):
  95. self.ensure_one()
  96. data = self._prepare_report_open_items()
  97. if report_type == 'xlsx':
  98. report_name = 'a_f_r.report_open_items_xlsx'
  99. else:
  100. report_name = 'account_financial_report.open_items'
  101. return self.env['ir.actions.report'].search(
  102. [('report_name', '=', report_name),
  103. ('report_type', '=', report_type)], limit=1).report_action(
  104. self, data=data)
  105. @api.multi
  106. def button_export_html(self):
  107. self.ensure_one()
  108. report_type = 'qweb-html'
  109. return self._export(report_type)
  110. @api.multi
  111. def button_export_pdf(self):
  112. self.ensure_one()
  113. report_type = 'qweb-pdf'
  114. return self._export(report_type)
  115. @api.multi
  116. def button_export_xlsx(self):
  117. self.ensure_one()
  118. report_type = 'xlsx'
  119. return self._export(report_type)
  120. def _prepare_report_open_items(self):
  121. self.ensure_one()
  122. return {
  123. 'wizard_id': self.id,
  124. 'date_at': fields.Date.to_string(self.date_at),
  125. 'date_from': self.date_from or False,
  126. 'only_posted_moves': self.target_move == 'posted',
  127. 'hide_account_at_0': self.hide_account_at_0,
  128. 'foreign_currency': self.foreign_currency,
  129. 'show_partner_details': self.show_partner_details,
  130. 'company_id': self.company_id.id,
  131. 'target_move': self.target_move,
  132. 'account_ids': self.account_ids.ids,
  133. 'partner_ids': self.partner_ids.ids or [],
  134. }
  135. def _export(self, report_type):
  136. return self._print_report(report_type)