2
0

[IMP] account_fiscal_year_auto_create: black, isort, prettier

This commit is contained in:
Sylvain LE GAL 2022-11-04 00:00:49 +01:00
parent 1e1f60ed92
commit 88840c2df8
6 changed files with 48 additions and 48 deletions

View File

@ -10,7 +10,7 @@
"category": "Accounting",
"author": "GRAP,Odoo Community Association (OCA)",
"maintainers": ["legalsylvain"],
"website": "http://www.github.com/OCA/account-financial-tools",
"website": "https://github.com/OCA/account-financial-tools",
"license": "AGPL-3",
"depends": [
"account",

View File

@ -1,21 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
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).
-->
<odoo noupdate="1">
<record id="cron_fiscal_year_auto_create" model="ir.cron">
<field name="name">Auto Create Fiscal Years</field>
<field name="active" eval="True"/>
<field name="model_id" ref="account.model_account_fiscal_year"/>
<field name="active" eval="True" />
<field name="model_id" ref="account.model_account_fiscal_year" />
<field name="state">code</field>
<field name="code">model.cron_auto_create()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="nextcall" eval="(DateTime.today()).strftime('%Y-%m-%d')"/>
<field name="nextcall" eval="(DateTime.today()).strftime('%Y-%m-%d')" />
<field name="numbercall">-1</field>
<field name="user_id" ref="base.user_root"/>
<field name="user_id" ref="base.user_root" />
</record>
</odoo>

View File

@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from datetime import datetime
from dateutil.relativedelta import relativedelta
from odoo import _, api, models
@ -16,12 +17,11 @@ class AccountFiscalYear(models.Model):
companies = self.env["res.company"].search([])
for company in companies:
last_fiscal_year = self.search(
[('company_id', '=', company.id)],
order="date_to desc", limit=1)
[("company_id", "=", company.id)], order="date_to desc", limit=1
)
if last_fiscal_year and (
last_fiscal_year.date_to <
datetime.now().date() + relativedelta(days=1)
last_fiscal_year.date_to < datetime.now().date() + relativedelta(days=1)
):
self.create(last_fiscal_year._prepare_next_fiscal_year())
@ -33,18 +33,13 @@ class AccountFiscalYear(models.Model):
# - "FY 2018" will be replace by "FY 2019"
# - "FY 2018-2019" will be replace by "FY 2019-2020"
new_name = self.name.replace(
str((self.date_to.year)), str((self.date_to.year + 1))
).replace(
str((self.date_from.year)), str((self.date_from.year + 1))
)
if self.search([
("name", "=", new_name),
("company_id", "=", self.company_id.id)
]):
str(self.date_to.year), str(self.date_to.year + 1)
).replace(str(self.date_from.year), str(self.date_from.year + 1))
if self.search(
[("name", "=", new_name), ("company_id", "=", self.company_id.id)]
):
# the replace process fail to guess a correct unique name
new_name = _("FY %s - %s") % (
str(self.date_to), str(self.date_from)
)
new_name = _("FY %s - %s") % (str(self.date_to), str(self.date_from))
# compute new dates, handling leap years
new_date_from = self.date_to + relativedelta(days=1)
new_date_to = new_date_from + relativedelta(years=1, days=-1)

View File

@ -2,53 +2,52 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from datetime import date, datetime
from odoo.tests.common import TransactionCase
class TestModule(TransactionCase):
def setUp(self):
super().setUp()
self.AccountFiscalYear = self.env["account.fiscal.year"]
self.company = self.env["res.company"].create({
"name": "Demo Company (account_fiscal_year_auto_create)",
})
self.company = self.env["res.company"].create(
{
"name": "Demo Company (account_fiscal_year_auto_create)",
}
)
# create a fiscal year
self.last_year = datetime.now().year - 1
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,
})
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,
}
)
def test_cron(self):
existing_fiscal_years = self.AccountFiscalYear.search([])
self.AccountFiscalYear.cron_auto_create()
# Run cron should create a new fiscal year
new_fiscal_year = self.AccountFiscalYear.search([
("id", "not in", existing_fiscal_years.ids)
])
new_fiscal_year = self.AccountFiscalYear.search(
[("id", "not in", existing_fiscal_years.ids)]
)
self.assertTrue(new_fiscal_year)
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))
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))
# Rerun cron should not create a new fiscal year
existing_fiscal_years = self.AccountFiscalYear.search([])
self.AccountFiscalYear.cron_auto_create()
# Run cron should create a new fiscal year
new_fiscal_year = self.AccountFiscalYear.search([
("id", "not in", existing_fiscal_years.ids)
])
new_fiscal_year = self.AccountFiscalYear.search(
[("id", "not in", existing_fiscal_years.ids)]
)
self.assertFalse(new_fiscal_year)

View File

@ -0,0 +1 @@
../../../../account_fiscal_year_auto_create

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)