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. ])
  16. if not fy:
  17. raise UserError("No fiscal year has been found for %s" %
  18. str(today))
  19. if company_id:
  20. return fy.filtered(lambda r: r.company_id == company_id)
  21. return fy
  22. @api.model
  23. def get_next_fiscal_year(self, date=None, company_id=None):
  24. if not date:
  25. date = fields.Date.today()
  26. nextyear = date + relativedelta(years=+1)
  27. fy = self.env["account.fiscal.year"].search([
  28. ('date_from', '<=', nextyear),
  29. ('date_to', '>=', nextyear),
  30. ])
  31. if not fy:
  32. raise UserError("No next fiscal year has been found for %s" %
  33. str(nextyear))
  34. if company_id:
  35. return fy.filtered(lambda r: r.company_id == company_id)
  36. return fy