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.

167 lines
6.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Damien Crier
  3. # Author: Julien Coux
  4. # Copyright 2016 Camptocamp SA
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import models, fields, api
  7. from odoo.tools.safe_eval import safe_eval
  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',
  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. default=lambda self: self._default_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. )
  51. show_reconciliations = fields.Boolean(
  52. string='Display reconciliations',
  53. )
  54. @api.onchange('company_id')
  55. def onchange_company_id(self):
  56. """Handle company change."""
  57. if self.company_id and self.partner_ids:
  58. self.partner_ids = self.partner_ids.filtered(
  59. lambda p: p.company_id == self.company_id or
  60. not p.company_id)
  61. if self.company_id and self.account_ids:
  62. if self.receivable_accounts_only or self.payable_accounts_only:
  63. self.onchange_type_accounts_only()
  64. else:
  65. self.account_ids = self.account_ids.filtered(
  66. lambda a: a.company_id == self.company_id)
  67. res = {'domain': {'account_ids': [],
  68. 'partner_ids': []}}
  69. if not self.company_id:
  70. return res
  71. else:
  72. res['domain']['account_ids'] += [
  73. ('company_id', '=', self.company_id.id)]
  74. res['domain']['partner_ids'] += [
  75. '&',
  76. '|', ('company_id', '=', self.company_id.id),
  77. ('company_id', '=', False),
  78. ('parent_id', '=', False)]
  79. return res
  80. def _default_foreign_currency(self):
  81. if self.env.user.has_group('base.group_multi_currency'):
  82. return True
  83. def _default_partners(self):
  84. context = self.env.context
  85. if context.get('active_ids') and context.get('active_model')\
  86. == 'res.partner':
  87. partner_ids = context['active_ids']
  88. corp_partners = self.env['res.partner'].browse(partner_ids).\
  89. filtered(lambda p: p.parent_id)
  90. partner_ids = set(partner_ids) - set(corp_partners.ids)
  91. partner_ids |= set(corp_partners.mapped('parent_id.id'))
  92. return list(partner_ids)
  93. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  94. def onchange_type_accounts_only(self):
  95. """Handle receivable/payable accounts only change."""
  96. if self.receivable_accounts_only or self.payable_accounts_only:
  97. domain = [('company_id', '=', self.company_id.id)]
  98. if self.receivable_accounts_only and self.payable_accounts_only:
  99. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  100. elif self.receivable_accounts_only:
  101. domain += [('internal_type', '=', 'receivable')]
  102. elif self.payable_accounts_only:
  103. domain += [('internal_type', '=', 'payable')]
  104. self.account_ids = self.env['account.account'].search(domain)
  105. else:
  106. self.account_ids = None
  107. @api.multi
  108. def button_export_html(self):
  109. self.ensure_one()
  110. action = self.env.ref(
  111. 'account_financial_report_qweb.action_report_open_items')
  112. vals = action.read()[0]
  113. context1 = vals.get('context', {})
  114. if isinstance(context1, basestring):
  115. context1 = safe_eval(context1)
  116. model = self.env['report_open_items_qweb']
  117. report = model.create(self._prepare_report_open_items())
  118. report.compute_data_for_report()
  119. context1['active_id'] = report.id
  120. context1['active_ids'] = report.ids
  121. vals['context'] = context1
  122. return vals
  123. @api.multi
  124. def button_export_pdf(self):
  125. self.ensure_one()
  126. report_type = 'qweb-pdf'
  127. return self._export(report_type)
  128. @api.multi
  129. def button_export_xlsx(self):
  130. self.ensure_one()
  131. report_type = 'xlsx'
  132. return self._export(report_type)
  133. def _prepare_report_open_items(self):
  134. self.ensure_one()
  135. return {
  136. 'date_at': self.date_at,
  137. 'only_posted_moves': self.target_move == 'posted',
  138. 'hide_account_at_0': self.hide_account_at_0,
  139. 'foreign_currency': self.foreign_currency,
  140. 'company_id': self.company_id.id,
  141. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  142. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  143. 'show_reconciliations': self.show_reconciliations,
  144. }
  145. def _export(self, report_type):
  146. """Default export is PDF."""
  147. model = self.env['report_open_items_qweb']
  148. report = model.create(self._prepare_report_open_items())
  149. report.compute_data_for_report()
  150. return report.print_report(report_type)