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.

113 lines
3.6 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.exceptions import ValidationError
  5. class VATReportWizard(models.TransientModel):
  6. _name = "vat.report.wizard"
  7. _description = "VAT Report Wizard"
  8. company_id = fields.Many2one(
  9. comodel_name="res.company",
  10. default=lambda self: self.env.user.company_id,
  11. required=False,
  12. string="Company",
  13. )
  14. date_range_id = fields.Many2one(comodel_name="date.range", string="Date range")
  15. date_from = fields.Date("Start Date", required=True)
  16. date_to = fields.Date("End Date", required=True)
  17. based_on = fields.Selection(
  18. [("taxtags", "Tax Tags"), ("taxgroups", "Tax Groups")],
  19. string="Based On",
  20. required=True,
  21. default="taxtags",
  22. )
  23. tax_detail = fields.Boolean("Detail Taxes")
  24. @api.onchange("company_id")
  25. def onchange_company_id(self):
  26. if (
  27. self.company_id
  28. and self.date_range_id.company_id
  29. and self.date_range_id.company_id != self.company_id
  30. ):
  31. self.date_range_id = False
  32. res = {"domain": {"date_range_id": []}}
  33. if not self.company_id:
  34. return res
  35. else:
  36. res["domain"]["date_range_id"] += [
  37. "|",
  38. ("company_id", "=", self.company_id.id),
  39. ("company_id", "=", False),
  40. ]
  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.constrains("company_id", "date_range_id")
  48. def _check_company_id_date_range_id(self):
  49. for rec in self.sudo():
  50. if (
  51. rec.company_id
  52. and rec.date_range_id.company_id
  53. and rec.company_id != rec.date_range_id.company_id
  54. ):
  55. raise ValidationError(
  56. _(
  57. "The Company in the Vat Report Wizard and in "
  58. "Date Range must be the same."
  59. )
  60. )
  61. def _print_report(self, report_type):
  62. self.ensure_one()
  63. data = self._prepare_vat_report()
  64. if report_type == "xlsx":
  65. report_name = "a_f_r.report_vat_report_xlsx"
  66. else:
  67. report_name = "account_financial_report.vat_report"
  68. return (
  69. self.env["ir.actions.report"]
  70. .search(
  71. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  72. limit=1,
  73. )
  74. .report_action(self, data=data)
  75. )
  76. def button_export_html(self):
  77. self.ensure_one()
  78. report_type = "qweb-html"
  79. return self._export(report_type)
  80. def button_export_pdf(self):
  81. self.ensure_one()
  82. report_type = "qweb-pdf"
  83. return self._export(report_type)
  84. def button_export_xlsx(self):
  85. self.ensure_one()
  86. report_type = "xlsx"
  87. return self._export(report_type)
  88. def _prepare_vat_report(self):
  89. self.ensure_one()
  90. return {
  91. "wizard_id": self.id,
  92. "company_id": self.company_id.id,
  93. "date_from": self.date_from,
  94. "date_to": self.date_to,
  95. "based_on": self.based_on,
  96. "tax_detail": self.tax_detail,
  97. }
  98. def _export(self, report_type):
  99. """Default export is PDF."""
  100. return self._print_report(report_type)