Skip to content
Snippets Groups Projects
Commit 359f94301358 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:sparkles: add periods when using populate

parent b2f548b007a1
No related branches found
No related tags found
1 merge request!11✨ add periods when using populate
variables:
ODOO_POPULATE_MODEL: account.move
ODOO_POPULATE_PREREQ: l10n_us
include:
- project: xcg/ci-templates
file: /odoo/15.0/gitlab-ci.yaml
......@@ -19,6 +19,7 @@
##############################################################################
import datetime
import logging
from typing import Optional
from dateutil.relativedelta import relativedelta
......@@ -169,9 +170,11 @@
:return: relativedelta of duration of a fiscal year
"""
# This is a method so it can be changed in the case of needing to
# handle shorter or longer fiscal year
# handle shorter or longer fiscal year.
# In that case the constraints on fiscalyear_last_day/fiscalyear_last_month
# needs to be changed too, and so does the populate factory.
return relativedelta(months=12, days=-1)
def action_realloc_period_am(self):
"""Call by the "Reallocate Periods" action.
......@@ -173,10 +176,9 @@
return relativedelta(months=12, days=-1)
def action_realloc_period_am(self):
"""Call by the "Reallocate Periods" action.
Look up all accounts move where periods is empty and
try to reallocate it.
Look up all accounts move where periods is empty and try to reallocate it.
"""
for record in self:
# Only administrator can reallocate periods.
......@@ -216,10 +218,13 @@
move.write({"period_id": period.id, "transaction_date": move.date})
def find(self, dt=None):
"""
:param dt: date (odoo format so string), if None, use today
def find(
self, date: Optional[datetime.date] = None, create: bool = False
) -> models.Model:
"""Return the fiscal years for the provided date.
:param date: date, if None, use today
:param create: indicate to create the fiscal year if set to True
:return: fiscal years that include the indicated date, using either the
company_id in the context or if not set, the user company_id
"""
......@@ -222,9 +227,13 @@
:return: fiscal years that include the indicated date, using either the
company_id in the context or if not set, the user company_id
"""
if not dt:
dt = fields.Date.context_today(self)
company_id = self.env.context.get("company_id") or self.env.company.id
return self.search(
if not date:
date = fields.Date.context_today(self)
company = (
self.env["res.company"].browse(self.env.context.get("company_id"))
if "company_id" in self.env.context
else self.env.company
)
result = self.search(
[
......@@ -230,6 +239,6 @@
[
("date_start", "<=", dt),
("date_stop", ">=", dt),
("company_id", "=", company_id),
("date_start", "<=", date),
("date_stop", ">=", date),
("company_id", "=", company.id),
]
)
......@@ -234,2 +243,20 @@
]
)
if not result and create:
# assume fiscal year duration is one year
month = int(company.fiscalyear_last_month)
day = company.fiscalyear_last_day
if date < datetime.date(date.year, month, day):
year = date.year
else:
year = date.year + 1
result = self.create(
{
"name": str(year),
"code": str(year),
"date_stop": datetime.date(year, month, day),
"company_id": company.id,
}
)
result.create_period()
return result
......@@ -17,4 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from math import log10
......@@ -20,5 +22,9 @@
from odoo import _, api, exceptions, fields, models
from dateutil.relativedelta import relativedelta
from odoo import _, api, exceptions, fields, models, tools
_logger = logging.getLogger(__name__)
class AccountMove(models.Model):
......@@ -53,7 +59,6 @@
@api.model_create_multi
def create(self, vals_list):
"""Override to set a transaction date from invoices."""
for vals in vals_list:
invoice_date = vals.get("invoice_date")
if invoice_date:
......@@ -130,3 +135,34 @@
accdoc.write(accdoc_values)
return True
def _populate_factories(self) -> list:
"""Add periods to the generated account.move"""
today = fields.Date.today()
def get_accounting_date(values, counter, random):
"""return an accounting date"""
accounting_date = (
values["date"].date() if values["date"].date() < today else None
)
# make sure target period exists (or action_post will throw an exception)
if accounting_date:
# make it random between the date and today
seconds_after = (today - accounting_date).total_seconds()
accounting_date = accounting_date + relativedelta(
seconds=seconds_after * -log10(0.001 + 0.999 * random.random()) / 3
)
# make sure that period exists
self.env["account.period"].with_context(
company_id=values["company_id"]
).find(accounting_date.date(), True)
return accounting_date
result = super()._populate_factories()
result.append(
# set some accounting_date so that when posting everything does not end up
# on the current period
("accounting_date", tools.populate.compute(get_accounting_date))
)
return result
......@@ -17,6 +17,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import datetime
from typing import Optional
from odoo import _, api, exceptions, fields, models
......@@ -105,5 +107,5 @@
)
]
def find(self, dt=None):
def find(self, date: Optional[datetime.date] = None, create: bool = False):
......@@ -109,7 +111,7 @@
if not dt:
dt = fields.Date.context_today(self)
if not date:
date = fields.Date.context_today(self)
company_id = self.env.context.get("company_id") or self.env.company.id
result = self.search(
[
......@@ -112,12 +114,12 @@
company_id = self.env.context.get("company_id") or self.env.company.id
result = self.search(
[
("date_start", "<=", dt),
("date_stop", ">=", dt),
("date_start", "<=", date),
("date_stop", ">=", date),
("company_id", "=", company_id),
]
)
if not result:
......@@ -118,9 +120,13 @@
("company_id", "=", company_id),
]
)
if not result:
if create:
self.env["account.fiscalyear"].find(date, True)
return self.find(date, False)
action = self.env.ref(
"account_period.redirect_warning_account_period_action"
)
......@@ -129,7 +135,7 @@
"There is no period defined for this date: %s.\n"
"Please go to Configuration/Periods."
)
% dt
% date
)
raise exceptions.RedirectWarning(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment