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.

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