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.

46 lines
1.4 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Houssine BAKKALI <houssine@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from dateutil.relativedelta import relativedelta
  5. from odoo import api, fields, models
  6. from odoo.exceptions import UserError
  7. class AccountFiscalYear(models.Model):
  8. _inherit = "account.fiscal.year"
  9. @api.model
  10. def get_ongoing_fiscal_year(self, company_id=None):
  11. today = fields.Date.today()
  12. fy = self.env["account.fiscal.year"].search([
  13. ('date_from', '>=', today),
  14. ('date_to', '<=', today),
  15. ('company_id', '=', company_id)
  16. ])
  17. if not fy:
  18. raise UserError("No fiscal year has been found for %d",
  19. today)
  20. if company_id:
  21. return fy.filtered(lambda r: r.company_id == company_id)
  22. return fy
  23. @api.model
  24. def get_next_fiscal_year(self, company_id=None):
  25. nextyear = fields.Date.today() + relativedelta(years=+1)
  26. fy = self.env["account.fiscal.year"].search([
  27. ('date_from', '>=', nextyear),
  28. ('date_to', '<=', nextyear),
  29. ('company_id', '=', company_id)
  30. ])
  31. if not fy:
  32. raise UserError("No next fiscal year has been found for %d",
  33. nextyear)
  34. if company_id:
  35. return fy.filtered(lambda r: r.company_id == company_id)
  36. return fy