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.

182 lines
7.1 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. required=True,
  30. )
  31. hide_account_at_0 = fields.Boolean(
  32. string='Hide account ending balance at 0', default=True,
  33. help='Use this filter to hide an account or a partner '
  34. 'with an ending balance at 0. '
  35. 'If partners are filtered, '
  36. 'debits and credits totals will not match the trial balance.'
  37. )
  38. receivable_accounts_only = fields.Boolean()
  39. payable_accounts_only = fields.Boolean()
  40. partner_ids = fields.Many2many(
  41. comodel_name='res.partner',
  42. string='Filter partners',
  43. default=lambda self: self._default_partners(),
  44. )
  45. foreign_currency = fields.Boolean(
  46. string='Show foreign currency',
  47. help='Display foreign currency for move lines, unless '
  48. 'account currency is not setup through chart of accounts '
  49. 'will display initial and final balance in that currency.',
  50. default=lambda self: self._default_foreign_currency(),
  51. )
  52. show_partner_details = fields.Boolean(
  53. string='Show Partner Details',
  54. default=True,
  55. )
  56. account_code_from = fields.Many2one(
  57. comodel_name='account.account',
  58. string='Account Code From',
  59. help='Starting account in a range')
  60. account_code_to = fields.Many2one(
  61. comodel_name='account.account',
  62. string='Account Code To',
  63. help='Ending account in a range')
  64. @api.onchange('account_code_from', 'account_code_to')
  65. def on_change_account_range(self):
  66. if self.account_code_from and self.account_code_from.code.isdigit() and \
  67. self.account_code_to and self.account_code_to.code.isdigit():
  68. start_range = int(self.account_code_from.code)
  69. end_range = int(self.account_code_to.code)
  70. self.account_ids = self.env['account.account'].search([
  71. ('code', 'in', [
  72. x for x in range(start_range, end_range + 1)]),
  73. ('reconcile', '=', True)
  74. ])
  75. if self.company_id:
  76. self.account_ids = self.account_ids.filtered(
  77. lambda a: a.company_id == self.company_id)
  78. return {'domain': {
  79. 'account_code_from': [('reconcile', '=', True)],
  80. 'account_code_to': [('reconcile', '=', True)]
  81. }}
  82. def _default_foreign_currency(self):
  83. return self.env.user.has_group('base.group_multi_currency')
  84. @api.onchange('company_id')
  85. def onchange_company_id(self):
  86. """Handle company change."""
  87. if self.company_id and self.partner_ids:
  88. self.partner_ids = self.partner_ids.filtered(
  89. lambda p: p.company_id == self.company_id or
  90. not p.company_id)
  91. if self.company_id and self.account_ids:
  92. if self.receivable_accounts_only or self.payable_accounts_only:
  93. self.onchange_type_accounts_only()
  94. else:
  95. self.account_ids = self.account_ids.filtered(
  96. lambda a: a.company_id == self.company_id)
  97. res = {'domain': {'account_ids': [],
  98. 'partner_ids': []}}
  99. if not self.company_id:
  100. return res
  101. else:
  102. res['domain']['account_ids'] += [
  103. ('company_id', '=', self.company_id.id)]
  104. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  105. return res
  106. @api.onchange('account_ids')
  107. def onchange_account_ids(self):
  108. return {'domain': {'account_ids': [('reconcile', '=', True)]}}
  109. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  110. def onchange_type_accounts_only(self):
  111. """Handle receivable/payable accounts only change."""
  112. domain = [('company_id', '=', self.company_id.id)]
  113. if self.receivable_accounts_only or self.payable_accounts_only:
  114. if self.receivable_accounts_only and self.payable_accounts_only:
  115. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  116. elif self.receivable_accounts_only:
  117. domain += [('internal_type', '=', 'receivable')]
  118. elif self.payable_accounts_only:
  119. domain += [('internal_type', '=', 'payable')]
  120. self.account_ids = self.env['account.account'].search(domain)
  121. else:
  122. self.account_ids = None
  123. @api.multi
  124. def _print_report(self, report_type):
  125. self.ensure_one()
  126. data = self._prepare_report_open_items()
  127. if report_type == 'xlsx':
  128. report_name = 'a_f_r.report_open_items_xlsx'
  129. else:
  130. report_name = 'account_financial_report.open_items'
  131. return self.env['ir.actions.report'].search(
  132. [('report_name', '=', report_name),
  133. ('report_type', '=', report_type)], limit=1).report_action(
  134. self, data=data)
  135. @api.multi
  136. def button_export_html(self):
  137. self.ensure_one()
  138. report_type = 'qweb-html'
  139. return self._export(report_type)
  140. @api.multi
  141. def button_export_pdf(self):
  142. self.ensure_one()
  143. report_type = 'qweb-pdf'
  144. return self._export(report_type)
  145. @api.multi
  146. def button_export_xlsx(self):
  147. self.ensure_one()
  148. report_type = 'xlsx'
  149. return self._export(report_type)
  150. def _prepare_report_open_items(self):
  151. self.ensure_one()
  152. return {
  153. 'wizard_id': self.id,
  154. 'date_at': fields.Date.to_string(self.date_at),
  155. 'date_from': self.date_from or False,
  156. 'only_posted_moves': self.target_move == 'posted',
  157. 'hide_account_at_0': self.hide_account_at_0,
  158. 'foreign_currency': self.foreign_currency,
  159. 'show_partner_details': self.show_partner_details,
  160. 'company_id': self.company_id.id,
  161. 'target_move': self.target_move,
  162. 'account_ids': self.account_ids.ids,
  163. 'partner_ids': self.partner_ids.ids or [],
  164. }
  165. def _export(self, report_type):
  166. return self._print_report(report_type)