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.

162 lines
6.2 KiB

6 years ago
6 years ago
  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. from odoo.tools.safe_eval import safe_eval
  7. from odoo.tools import pycompat
  8. class OpenItemsReportWizard(models.TransientModel):
  9. """Open items report wizard."""
  10. _name = "open.items.report.wizard"
  11. _description = "Open Items Report Wizard"
  12. company_id = fields.Many2one(
  13. comodel_name='res.company',
  14. default=lambda self: self.env.user.company_id,
  15. required=False,
  16. string='Company'
  17. )
  18. date_at = fields.Date(required=True,
  19. default=fields.Date.context_today)
  20. target_move = fields.Selection([('posted', 'All Posted Entries'),
  21. ('all', 'All Entries')],
  22. string='Target Moves',
  23. required=True,
  24. default='all')
  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. def _default_foreign_currency(self):
  52. return self.env.user.has_group('base.group_multi_currency')
  53. def _default_partners(self):
  54. context = self.env.context
  55. if context.get('active_ids') and context.get('active_model') \
  56. == 'res.partner':
  57. partner_ids = context['active_ids']
  58. corp_partners = self.env['res.partner'].browse(partner_ids). \
  59. filtered(lambda p: p.parent_id)
  60. partner_ids = set(partner_ids) - set(corp_partners.ids)
  61. partner_ids |= set(corp_partners.mapped('parent_id.id'))
  62. return list(partner_ids)
  63. @api.onchange('company_id')
  64. def onchange_company_id(self):
  65. """Handle company change."""
  66. if self.company_id and self.partner_ids:
  67. self.partner_ids = self.partner_ids.filtered(
  68. lambda p: p.company_id == self.company_id or
  69. not p.company_id)
  70. if self.company_id and self.account_ids:
  71. if self.receivable_accounts_only or self.payable_accounts_only:
  72. self.onchange_type_accounts_only()
  73. else:
  74. self.account_ids = self.account_ids.filtered(
  75. lambda a: a.company_id == self.company_id)
  76. res = {'domain': {'account_ids': [],
  77. 'partner_ids': []}}
  78. if not self.company_id:
  79. return res
  80. else:
  81. res['domain']['account_ids'] += [
  82. ('company_id', '=', self.company_id.id)]
  83. res['domain']['partner_ids'] += [
  84. '&',
  85. '|', ('company_id', '=', self.company_id.id),
  86. ('company_id', '=', False),
  87. ('parent_id', '=', False)]
  88. return res
  89. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  90. def onchange_type_accounts_only(self):
  91. """Handle receivable/payable accounts only change."""
  92. if self.receivable_accounts_only or self.payable_accounts_only:
  93. domain = [('company_id', '=', self.company_id.id)]
  94. if self.receivable_accounts_only and self.payable_accounts_only:
  95. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  96. elif self.receivable_accounts_only:
  97. domain += [('internal_type', '=', 'receivable')]
  98. elif self.payable_accounts_only:
  99. domain += [('internal_type', '=', 'payable')]
  100. self.account_ids = self.env['account.account'].search(domain)
  101. else:
  102. self.account_ids = None
  103. @api.multi
  104. def button_export_html(self):
  105. self.ensure_one()
  106. action = self.env.ref(
  107. 'account_financial_report.action_report_open_items')
  108. vals = action.read()[0]
  109. context1 = vals.get('context', {})
  110. if isinstance(context1, pycompat.string_types):
  111. context1 = safe_eval(context1)
  112. model = self.env['report_open_items']
  113. report = model.create(self._prepare_report_open_items())
  114. report.compute_data_for_report()
  115. context1['active_id'] = report.id
  116. context1['active_ids'] = report.ids
  117. vals['context'] = context1
  118. return vals
  119. @api.multi
  120. def button_export_pdf(self):
  121. self.ensure_one()
  122. report_type = 'qweb-pdf'
  123. return self._export(report_type)
  124. @api.multi
  125. def button_export_xlsx(self):
  126. self.ensure_one()
  127. report_type = 'xlsx'
  128. return self._export(report_type)
  129. def _prepare_report_open_items(self):
  130. self.ensure_one()
  131. return {
  132. 'date_at': self.date_at,
  133. 'only_posted_moves': self.target_move == 'posted',
  134. 'hide_account_at_0': self.hide_account_at_0,
  135. 'foreign_currency': self.foreign_currency,
  136. 'company_id': self.company_id.id,
  137. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  138. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  139. }
  140. def _export(self, report_type):
  141. """Default export is PDF."""
  142. model = self.env['report_open_items']
  143. report = model.create(self._prepare_report_open_items())
  144. report.compute_data_for_report()
  145. return report.print_report(report_type)