Skip to content
Snippets Groups Projects

Add period_id in aml tree view (cf account_consult MO16-190)

Merged arthur.mayer requested to merge topic/16.0/MO16-190 into branch/16.0
4 files
+ 35
67
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 38
75
##############################################################################
#
# Accounting periods, for Odoo
# Copyright © 2018, 2022 XCG Consulting <https://xcg-consulting.fr/>
# Copyright © 2018, 2022, 2023 XCG Consulting <https://xcg-consulting.fr/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -17,10 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from math import log10
from dateutil.relativedelta import relativedelta
from odoo import _, exceptions, fields, models, tools
@@ -24,8 +20,6 @@
from odoo import _, exceptions, fields, models, tools
_logger = logging.getLogger(__name__)
class AccountMove(models.Model):
"""Add a period & dates onto accounting documents."""
@@ -43,7 +37,6 @@
def action_post(self):
"""Override accounting document validation to fill period."""
self.fetch_period()
test = super().action_post()
return test
self.fill_period()
return super().action_post()
@@ -49,9 +42,6 @@
def fetch_period(self):
"""Force the period to always be around the current date.
Only select open periods.
"""
def fill_period(self):
"""Find an open period around move date, set it onto that move."""
today = fields.Date.today()
for accdoc in self:
@@ -55,6 +45,5 @@
today = fields.Date.today()
for accdoc in self:
# Cache some data.
acc_date = accdoc.date or today
company = accdoc.company_id
@@ -59,5 +48,10 @@
acc_date = accdoc.date or today
company = accdoc.company_id
period_domain = [
("company_id", "=", company.id),
("date_start", "<=", acc_date),
("date_effective_cutoff", ">=", acc_date),
]
# Periods are ordered by date so selecting the first one is fine.
period = self.env["account.period"].search(
@@ -61,12 +55,7 @@
# Periods are ordered by date so selecting the first one is fine.
period = self.env["account.period"].search(
[
("company_id", "=", company.id),
("date_start", "<=", acc_date),
("date_effective_cutoff", ">=", acc_date),
("state", "!=", "done"),
],
period_domain + [("state", "!=", "done")],
limit=1,
)
if not period:
@@ -70,5 +59,5 @@
limit=1,
)
if not period:
# if doing tests, and any matching period not closed
# Create missing periods on-the-fly when running tests / DB populate.
if (
@@ -74,7 +63,20 @@
if (
(tools.config["test_enable"] or tools.config["test_file"])
and (
not self.env.context.get("period_close_enable")
or not self.env["account.period"].search(
tools.config["test_enable"]
or tools.config["test_file"]
or self.env.context.get("install_demo", False)
or hasattr(self.env.registry, "populated_models")
) and (
not self.env.context.get("period_close_enable")
or not self.env["account.period"].search(
period_domain + [("state", "=", "done")],
limit=1,
)
):
year_start = acc_date.replace(day=1, month=1)
year_end = acc_date.replace(day=31, month=12)
fiscalyear = (
self.env["account.fiscal.year"]
.sudo()
.search(
[
("company_id", "=", company.id),
@@ -79,9 +81,8 @@
[
("company_id", "=", company.id),
("date_start", "<=", acc_date),
("date_effective_cutoff", ">=", acc_date),
("state", "=", "done"),
("date_from", "<=", year_start),
("date_to", ">=", year_end),
],
limit=1,
)
)
@@ -84,17 +85,7 @@
],
limit=1,
)
)
or self.env.context.get("install_demo", False)
):
start_date = acc_date.replace(month=1)
end_date = acc_date.replace(month=12)
year_str = str(end_date.year)
fiscalyear = self.env["account.fiscal.year"].search(
[("name", "=", year_str)], limit=1
) # Fiscal year already exists in this company; ?
if not fiscalyear:
fiscalyear = (
self.env["account.fiscal.year"]
@@ -102,10 +93,10 @@
.create(
{
"company_id": company.id,
"date_from": start_date,
"date_to": end_date,
"name": str(end_date.year),
"date_from": year_start,
"date_to": year_end,
"name": str(acc_date.year),
}
)
)
fiscalyear.create_periods()
@@ -108,8 +99,11 @@
}
)
)
fiscalyear.create_periods()
period = fiscalyear.period_ids[0]
period = self.env["account.period"].search(
period_domain + [("state", "!=", "done")],
limit=1,
)
else:
raise exceptions.UserError(
@@ -134,34 +128,3 @@
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
Loading