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.

121 lines
3.9 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.company,
  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. target_move = fields.Selection(
  25. [("posted", "All Posted Entries"), ("all", "All Entries")],
  26. string="Target Moves",
  27. required=True,
  28. default="posted",
  29. )
  30. @api.onchange("company_id")
  31. def onchange_company_id(self):
  32. if (
  33. self.company_id
  34. and self.date_range_id.company_id
  35. and self.date_range_id.company_id != self.company_id
  36. ):
  37. self.date_range_id = False
  38. res = {"domain": {"date_range_id": []}}
  39. if not self.company_id:
  40. return res
  41. else:
  42. res["domain"]["date_range_id"] += [
  43. "|",
  44. ("company_id", "=", self.company_id.id),
  45. ("company_id", "=", False),
  46. ]
  47. return res
  48. @api.onchange("date_range_id")
  49. def onchange_date_range_id(self):
  50. """Handle date range change."""
  51. self.date_from = self.date_range_id.date_start
  52. self.date_to = self.date_range_id.date_end
  53. @api.constrains("company_id", "date_range_id")
  54. def _check_company_id_date_range_id(self):
  55. for rec in self.sudo():
  56. if (
  57. rec.company_id
  58. and rec.date_range_id.company_id
  59. and rec.company_id != rec.date_range_id.company_id
  60. ):
  61. raise ValidationError(
  62. _(
  63. "The Company in the Vat Report Wizard and in "
  64. "Date Range must be the same."
  65. )
  66. )
  67. def _print_report(self, report_type):
  68. self.ensure_one()
  69. data = self._prepare_vat_report()
  70. if report_type == "xlsx":
  71. report_name = "a_f_r.report_vat_report_xlsx"
  72. else:
  73. report_name = "account_financial_report.vat_report"
  74. return (
  75. self.env["ir.actions.report"]
  76. .search(
  77. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  78. limit=1,
  79. )
  80. .report_action(self, data=data)
  81. )
  82. def button_export_html(self):
  83. self.ensure_one()
  84. report_type = "qweb-html"
  85. return self._export(report_type)
  86. def button_export_pdf(self):
  87. self.ensure_one()
  88. report_type = "qweb-pdf"
  89. return self._export(report_type)
  90. def button_export_xlsx(self):
  91. self.ensure_one()
  92. report_type = "xlsx"
  93. return self._export(report_type)
  94. def _prepare_vat_report(self):
  95. self.ensure_one()
  96. return {
  97. "wizard_id": self.id,
  98. "company_id": self.company_id.id,
  99. "date_from": self.date_from,
  100. "date_to": self.date_to,
  101. "based_on": self.based_on,
  102. "only_posted_moves": self.target_move == "posted",
  103. "tax_detail": self.tax_detail,
  104. "account_financial_report_lang": self.env.lang,
  105. }
  106. def _export(self, report_type):
  107. """Default export is PDF."""
  108. return self._print_report(report_type)