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.

101 lines
3.4 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. _inherit = "account_financial_report_abstract_wizard"
  9. date_range_id = fields.Many2one(comodel_name="date.range", string="Date range")
  10. date_from = fields.Date("Start Date", required=True)
  11. date_to = fields.Date("End Date", required=True)
  12. based_on = fields.Selection(
  13. [("taxtags", "Tax Tags"), ("taxgroups", "Tax Groups")],
  14. string="Based On",
  15. required=True,
  16. default="taxtags",
  17. )
  18. tax_detail = fields.Boolean("Detail Taxes")
  19. target_move = fields.Selection(
  20. [("posted", "All Posted Entries"), ("all", "All Entries")],
  21. string="Target Moves",
  22. required=True,
  23. default="posted",
  24. )
  25. @api.onchange("company_id")
  26. def onchange_company_id(self):
  27. if (
  28. self.company_id
  29. and self.date_range_id.company_id
  30. and self.date_range_id.company_id != self.company_id
  31. ):
  32. self.date_range_id = False
  33. res = {"domain": {"date_range_id": []}}
  34. if not self.company_id:
  35. return res
  36. else:
  37. res["domain"]["date_range_id"] += [
  38. "|",
  39. ("company_id", "=", self.company_id.id),
  40. ("company_id", "=", False),
  41. ]
  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.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. def _print_report(self, report_type):
  63. self.ensure_one()
  64. data = self._prepare_vat_report()
  65. if report_type == "xlsx":
  66. report_name = "a_f_r.report_vat_report_xlsx"
  67. else:
  68. report_name = "account_financial_report.vat_report"
  69. return (
  70. self.env["ir.actions.report"]
  71. .search(
  72. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  73. limit=1,
  74. )
  75. .report_action(self, data=data)
  76. )
  77. def _prepare_vat_report(self):
  78. self.ensure_one()
  79. return {
  80. "wizard_id": self.id,
  81. "company_id": self.company_id.id,
  82. "date_from": self.date_from,
  83. "date_to": self.date_to,
  84. "based_on": self.based_on,
  85. "only_posted_moves": self.target_move == "posted",
  86. "tax_detail": self.tax_detail,
  87. "account_financial_report_lang": self.env.lang,
  88. }
  89. def _export(self, report_type):
  90. """Default export is PDF."""
  91. return self._print_report(report_type)