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", "category": "Accounting",
"author": "GRAP,Odoo Community Association (OCA)", "author": "GRAP,Odoo Community Association (OCA)",
"maintainers": ["legalsylvain"], "maintainers": ["legalsylvain"],
"website": "http://www.github.com/OCA/account-financial-tools", "website": "https://github.com/OCA/account-financial-tools",
"license": "AGPL-3", "license": "AGPL-3",
"depends": [ "depends": [
"account", "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) Copyright (C) 2021 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain) @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
--> -->
<odoo noupdate="1"> <odoo noupdate="1">
<record id="cron_fiscal_year_auto_create" model="ir.cron"> <record id="cron_fiscal_year_auto_create" model="ir.cron">
<field name="name">Auto Create Fiscal Years</field> <field name="name">Auto Create Fiscal Years</field>
<field name="active" eval="True"/> <field name="active" eval="True" />
<field name="model_id" ref="account.model_account_fiscal_year"/> <field name="model_id" ref="account.model_account_fiscal_year" />
<field name="state">code</field> <field name="state">code</field>
<field name="code">model.cron_auto_create()</field> <field name="code">model.cron_auto_create()</field>
<field name="interval_number">1</field> <field name="interval_number">1</field>
<field name="interval_type">days</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="numbercall">-1</field>
<field name="user_id" ref="base.user_root"/> <field name="user_id" ref="base.user_root" />
</record> </record>
</odoo> </odoo>

View File

@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from datetime import datetime from datetime import datetime
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from odoo import _, api, models from odoo import _, api, models
@ -16,12 +17,11 @@ class AccountFiscalYear(models.Model):
companies = self.env["res.company"].search([]) companies = self.env["res.company"].search([])
for company in companies: for company in companies:
last_fiscal_year = self.search( last_fiscal_year = self.search(
[('company_id', '=', company.id)], [("company_id", "=", company.id)], order="date_to desc", limit=1
order="date_to desc", limit=1) )
if last_fiscal_year and ( if last_fiscal_year and (
last_fiscal_year.date_to < last_fiscal_year.date_to < datetime.now().date() + relativedelta(days=1)
datetime.now().date() + relativedelta(days=1)
): ):
self.create(last_fiscal_year._prepare_next_fiscal_year()) 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" will be replace by "FY 2019"
# - "FY 2018-2019" will be replace by "FY 2019-2020" # - "FY 2018-2019" will be replace by "FY 2019-2020"
new_name = self.name.replace( new_name = self.name.replace(
str((self.date_to.year)), str((self.date_to.year + 1)) str(self.date_to.year), str(self.date_to.year + 1)
).replace( ).replace(str(self.date_from.year), str(self.date_from.year + 1))
str((self.date_from.year)), str((self.date_from.year + 1)) if self.search(
) [("name", "=", new_name), ("company_id", "=", self.company_id.id)]
if self.search([ ):
("name", "=", new_name),
("company_id", "=", self.company_id.id)
]):
# the replace process fail to guess a correct unique name # the replace process fail to guess a correct unique name
new_name = _("FY %s - %s") % ( new_name = _("FY %s - %s") % (str(self.date_to), str(self.date_from))
str(self.date_to), str(self.date_from)
)
# compute new dates, handling leap years # compute new dates, handling leap years
new_date_from = self.date_to + relativedelta(days=1) new_date_from = self.date_to + relativedelta(days=1)
new_date_to = new_date_from + relativedelta(years=1, 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) # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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 datetime import date, datetime
from odoo.tests.common import TransactionCase
class TestModule(TransactionCase): class TestModule(TransactionCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.AccountFiscalYear = self.env["account.fiscal.year"] self.AccountFiscalYear = self.env["account.fiscal.year"]
self.company = self.env["res.company"].create({ self.company = self.env["res.company"].create(
"name": "Demo Company (account_fiscal_year_auto_create)", {
}) "name": "Demo Company (account_fiscal_year_auto_create)",
}
)
# create a fiscal year # create a fiscal year
self.last_year = datetime.now().year - 1 self.last_year = datetime.now().year - 1
self.last_fiscal_year = self.AccountFiscalYear.create({ 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"), "name": "FY %d" % (self.last_year),
"date_to": date(self.last_year, 12, 31).strftime("%Y-%m-%d"), "date_from": date(self.last_year, 1, 1).strftime("%Y-%m-%d"),
"company_id": self.company.id, "date_to": date(self.last_year, 12, 31).strftime("%Y-%m-%d"),
}) "company_id": self.company.id,
}
)
def test_cron(self): def test_cron(self):
existing_fiscal_years = self.AccountFiscalYear.search([]) existing_fiscal_years = self.AccountFiscalYear.search([])
self.AccountFiscalYear.cron_auto_create() self.AccountFiscalYear.cron_auto_create()
# Run cron should create a new fiscal year # Run cron should create a new fiscal year
new_fiscal_year = self.AccountFiscalYear.search([ new_fiscal_year = self.AccountFiscalYear.search(
("id", "not in", existing_fiscal_years.ids) [("id", "not in", existing_fiscal_years.ids)]
]) )
self.assertTrue(new_fiscal_year) self.assertTrue(new_fiscal_year)
self.assertEqual( self.assertEqual(new_fiscal_year.name, "FY %d" % (self.last_year + 1))
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( self.assertEqual(new_fiscal_year.date_from, date(self.last_year + 1, 1, 1))
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.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 # Rerun cron should not create a new fiscal year
existing_fiscal_years = self.AccountFiscalYear.search([]) existing_fiscal_years = self.AccountFiscalYear.search([])
self.AccountFiscalYear.cron_auto_create() self.AccountFiscalYear.cron_auto_create()
# Run cron should create a new fiscal year # Run cron should create a new fiscal year
new_fiscal_year = self.AccountFiscalYear.search([ new_fiscal_year = self.AccountFiscalYear.search(
("id", "not in", existing_fiscal_years.ids) [("id", "not in", existing_fiscal_years.ids)]
]) )
self.assertFalse(new_fiscal_year) 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,
)