Skip to content

Commit

Permalink
[ADD] account_fiscal_year_auto_create : new module to create automati…
Browse files Browse the repository at this point in the history
…cally new fiscal years
  • Loading branch information
legalsylvain committed Nov 3, 2022
1 parent 6c54ae1 commit b8e210a
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 0 deletions.
8 changes: 8 additions & 0 deletions account_fiscal_year_auto_create/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=================================
Fiscal Years - Automatic Creation
=================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 change: 1 addition & 0 deletions account_fiscal_year_auto_create/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions account_fiscal_year_auto_create/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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).

{
"name": "Fiscal Years - Automatic Creation",
"summary": "Automatically create new fiscal years, based on the datas"
" of the last fiscal years",
"version": "12.0.1.0.1",
"category": "Accounting",
"author": "GRAP,Odoo Community Association (OCA)",
"maintainers": ["legalsylvain"],
"website": "http://www.github.com/OCA/account-financial-tools",
"license": "AGPL-3",
"depends": [
"account",
],
"data": [
"data/ir_cron.xml",
],
}
21 changes: 21 additions & 0 deletions account_fiscal_year_auto_create/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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="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="numbercall">-1</field>
<field name="user_id" ref="base.user_root"/>
</record>
</odoo>
35 changes: 35 additions & 0 deletions account_fiscal_year_auto_create/l18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * account_fiscal_year_auto_create
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-03-31 08:41+0000\n"
"PO-Revision-Date: 2021-03-31 08:41+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: account_fiscal_year_auto_create
#: model:ir.actions.server,name:account_fiscal_year_auto_create.cron_fiscal_year_auto_create_ir_actions_server
#: model:ir.cron,cron_name:account_fiscal_year_auto_create.cron_fiscal_year_auto_create
#: model:ir.cron,name:account_fiscal_year_auto_create.cron_fiscal_year_auto_create
msgid "Auto Create Fiscal Years"
msgstr "Création automatique d'année fiscales"

#. module: account_fiscal_year_auto_create
#: code:addons/account_fiscal_year_auto_create/models/account_fiscal_year.py:45
#, python-format
msgid "FY %s - %s"
msgstr "AF %s - %s"

#. module: account_fiscal_year_auto_create
#: model:ir.model,name:account_fiscal_year_auto_create.model_account_fiscal_year
msgid "Fiscal Year"
msgstr "Exercice fiscal"

1 change: 1 addition & 0 deletions account_fiscal_year_auto_create/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_fiscal_year
56 changes: 56 additions & 0 deletions account_fiscal_year_auto_create/models/account_fiscal_year.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 datetime
from dateutil.relativedelta import relativedelta

from odoo import _, api, models


class AccountFiscalYear(models.Model):
_inherit = "account.fiscal.year"

@api.model
def cron_auto_create(self):
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)

if last_fiscal_year and (
last_fiscal_year.date_to <
datetime.now().date() + relativedelta(days=1)
):
self.create(last_fiscal_year._prepare_next_fiscal_year())

@api.multi
def _prepare_next_fiscal_year(self):
self.ensure_one()
# try to generate a new name, based on the previous
# name replacing YYYY pattern by YYYY+1 value
# - "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)
]):
# the replace process fail to guess a correct unique name
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)
return {
"name": new_name,
"company_id": self.company_id.id,
"date_from": new_date_from.strftime("%Y-%m-%d"),
"date_to": new_date_to.strftime("%Y-%m-%d"),
}
1 change: 1 addition & 0 deletions account_fiscal_year_auto_create/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Sylvain LE GAL <https://twitter.com/legalsylvain>
25 changes: 25 additions & 0 deletions account_fiscal_year_auto_create/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This module extends the functionality of Accounting Odoo module, to
auto generate fiscal years.

Once installed, a cron is running each day. It will create, for each company,
a new fiscal year, if it is the last day of the current fiscal year.

This module is interesting specially in multi company context, to avoid annoying setup every year.

The fiscal year created has a classical "12 monthes" duration, but the accountant
can modify it, once created.

Exemple
~~~~~~~

If a company has it last fiscal year, defined as:

- ``name``: FY 2021-2022
- ``date_from``: 2021-06-01
- ``date_to``: 2022-05-31

When the cron will be executed on May 31, 2022, it will create the following fiscal year:

- ``name``: FY 2022-2023
- ``date_from``: 2022-06-01
- ``date_to``: 2023-05-31
1 change: 1 addition & 0 deletions account_fiscal_year_auto_create/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_module
54 changes: 54 additions & 0 deletions account_fiscal_year_auto_create/tests/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 odoo.tests.common import TransactionCase

from datetime import date, datetime


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)",
})

# 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,
})

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)
])
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))

# 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)
])
self.assertFalse(new_fiscal_year)

0 comments on commit b8e210a

Please sign in to comment.