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.

108 lines
3.7 KiB

  1. # Copyright 2018 Forest and Biomass Romania
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models, _
  4. from odoo.tools.safe_eval import safe_eval
  5. from odoo.tools import pycompat
  6. from odoo.exceptions import ValidationError
  7. class VATReportWizard(models.TransientModel):
  8. _name = "vat.report.wizard"
  9. company_id = fields.Many2one(
  10. comodel_name='res.company',
  11. default=lambda self: self.env.user.company_id,
  12. required=False,
  13. string='Company'
  14. )
  15. date_range_id = fields.Many2one(
  16. comodel_name='date.range',
  17. string='Date range'
  18. )
  19. date_from = fields.Date('Start Date', required=True)
  20. date_to = fields.Date('End Date', required=True)
  21. based_on = fields.Selection([('taxtags', 'Tax Tags'),
  22. ('taxgroups', 'Tax Groups')],
  23. string='Based On',
  24. required=True,
  25. default='taxtags')
  26. tax_detail = fields.Boolean('Detail Taxes')
  27. @api.onchange('company_id')
  28. def onchange_company_id(self):
  29. if self.company_id and self.date_range_id.company_id and \
  30. self.date_range_id.company_id != self.company_id:
  31. self.date_range_id = False
  32. res = {'domain': {'date_range_id': [],
  33. }
  34. }
  35. if not self.company_id:
  36. return res
  37. else:
  38. res['domain']['date_range_id'] += [
  39. '|', ('company_id', '=', self.company_id.id),
  40. ('company_id', '=', False)]
  41. return res
  42. @api.onchange('date_range_id')
  43. def onchange_date_range_id(self):
  44. """Handle date range change."""
  45. self.date_from = self.date_range_id.date_start
  46. self.date_to = self.date_range_id.date_end
  47. @api.multi
  48. @api.constrains('company_id', 'date_range_id')
  49. def _check_company_id_date_range_id(self):
  50. for rec in self.sudo():
  51. if rec.company_id and rec.date_range_id.company_id and\
  52. rec.company_id != rec.date_range_id.company_id:
  53. raise ValidationError(
  54. _('The Company in the Vat Report Wizard and in '
  55. 'Date Range must be the same.'))
  56. @api.multi
  57. def button_export_html(self):
  58. self.ensure_one()
  59. action = self.env.ref(
  60. 'account_financial_report.action_report_vat_report')
  61. vals = action.read()[0]
  62. context1 = vals.get('context', {})
  63. if isinstance(context1, pycompat.string_types):
  64. context1 = safe_eval(context1)
  65. model = self.env['report_vat_report']
  66. report = model.create(self._prepare_vat_report())
  67. report.compute_data_for_report()
  68. context1['active_id'] = report.id
  69. context1['active_ids'] = report.ids
  70. vals['context'] = context1
  71. return vals
  72. @api.multi
  73. def button_export_pdf(self):
  74. self.ensure_one()
  75. report_type = 'qweb-pdf'
  76. return self._export(report_type)
  77. @api.multi
  78. def button_export_xlsx(self):
  79. self.ensure_one()
  80. report_type = 'xlsx'
  81. return self._export(report_type)
  82. def _prepare_vat_report(self):
  83. self.ensure_one()
  84. return {
  85. 'company_id': self.company_id.id,
  86. 'date_from': self.date_from,
  87. 'date_to': self.date_to,
  88. 'based_on': self.based_on,
  89. 'tax_detail': self.tax_detail,
  90. }
  91. def _export(self, report_type):
  92. """Default export is PDF."""
  93. model = self.env['report_vat_report']
  94. report = model.create(self._prepare_vat_report())
  95. report.compute_data_for_report()
  96. return report.print_report(report_type)