2021-03-31 10:31:33 +02:00
|
|
|
# Copyright (C) 2021 - Today: GRAP (http://www.grap.coop)
|
|
|
|
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
|
|
from datetime import date, datetime
|
|
|
|
|
2022-11-04 00:00:49 +01:00
|
|
|
from odoo.tests.common import TransactionCase
|
2021-03-31 10:31:33 +02:00
|
|
|
|
|
|
|
|
2022-11-04 00:00:49 +01:00
|
|
|
class TestModule(TransactionCase):
|
2021-03-31 10:31:33 +02:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.AccountFiscalYear = self.env["account.fiscal.year"]
|
2022-11-04 00:00:49 +01:00
|
|
|
self.company = self.env["res.company"].create(
|
|
|
|
{
|
|
|
|
"name": "Demo Company (account_fiscal_year_auto_create)",
|
|
|
|
}
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
|
|
|
|
# create a fiscal year
|
|
|
|
self.last_year = datetime.now().year - 1
|
2022-11-04 00:00:49 +01:00
|
|
|
self.last_fiscal_year = self.AccountFiscalYear.create(
|
|
|
|
{
|
|
|
|
"name": "FY %d" % (self.last_year),
|
|
|
|
"date_from": date(self.last_year, 1, 1).strftime("%Y-%m-%d"),
|
|
|
|
"date_to": date(self.last_year, 12, 31).strftime("%Y-%m-%d"),
|
|
|
|
"company_id": self.company.id,
|
|
|
|
}
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
|
|
|
|
def test_cron(self):
|
2022-11-04 00:10:13 +01:00
|
|
|
# Run cron should create a new fiscal year
|
2021-03-31 10:31:33 +02:00
|
|
|
existing_fiscal_years = self.AccountFiscalYear.search([])
|
|
|
|
self.AccountFiscalYear.cron_auto_create()
|
|
|
|
|
2022-11-04 00:00:49 +01:00
|
|
|
new_fiscal_year = self.AccountFiscalYear.search(
|
|
|
|
[("id", "not in", existing_fiscal_years.ids)]
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
self.assertTrue(new_fiscal_year)
|
2022-11-04 00:00:49 +01:00
|
|
|
self.assertEqual(new_fiscal_year.name, "FY %d" % (self.last_year + 1))
|
|
|
|
self.assertEqual(new_fiscal_year.date_from, date(self.last_year + 1, 1, 1))
|
|
|
|
self.assertEqual(new_fiscal_year.date_from, date(self.last_year + 1, 1, 1))
|
|
|
|
self.assertEqual(new_fiscal_year.name, "FY %d" % (self.last_year + 1))
|
2021-03-31 10:31:33 +02:00
|
|
|
|
|
|
|
# Rerun cron should not create a new fiscal year
|
|
|
|
existing_fiscal_years = self.AccountFiscalYear.search([])
|
|
|
|
self.AccountFiscalYear.cron_auto_create()
|
|
|
|
|
2022-11-04 00:00:49 +01:00
|
|
|
new_fiscal_year = self.AccountFiscalYear.search(
|
|
|
|
[("id", "not in", existing_fiscal_years.ids)]
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
self.assertFalse(new_fiscal_year)
|