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.

118 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.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.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 (
  52. rec.company_id
  53. and rec.date_range_id.company_id
  54. and rec.company_id != rec.date_range_id.company_id
  55. ):
  56. raise ValidationError(
  57. _(
  58. "The Company in the Vat Report Wizard and in "
  59. "Date Range must be the same."
  60. )
  61. )
  62. @api.multi
  63. def _print_report(self, report_type):
  64. self.ensure_one()
  65. data = self._prepare_vat_report()
  66. if report_type == "xlsx":
  67. report_name = "a_f_r.report_vat_report_xlsx"
  68. else:
  69. report_name = "account_financial_report.vat_report"
  70. return (
  71. self.env["ir.actions.report"]
  72. .search(
  73. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  74. limit=1,
  75. )
  76. .report_action(self, data=data)
  77. )
  78. @api.multi
  79. def button_export_html(self):
  80. self.ensure_one()
  81. report_type = "qweb-html"
  82. return self._export(report_type)
  83. @api.multi
  84. def button_export_pdf(self):
  85. self.ensure_one()
  86. report_type = "qweb-pdf"
  87. return self._export(report_type)
  88. @api.multi
  89. def button_export_xlsx(self):
  90. self.ensure_one()
  91. report_type = "xlsx"
  92. return self._export(report_type)
  93. def _prepare_vat_report(self):
  94. self.ensure_one()
  95. return {
  96. "wizard_id": self.id,
  97. "company_id": self.company_id.id,
  98. "date_from": self.date_from,
  99. "date_to": self.date_to,
  100. "based_on": self.based_on,
  101. "tax_detail": self.tax_detail,
  102. }
  103. def _export(self, report_type):
  104. """Default export is PDF."""
  105. return self._print_report(report_type)