Skip to content
Snippets Groups Projects
Commit 94ea5dd460f7 authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

Fix period overlaps / wrong years during tests/populate

parent d52326576a47
No related branches found
No related tags found
2 merge requests!30Add period_id in aml tree view (cf account_consult MO16-190),!29🚑 fix populate
Pipeline #60512 failed
This commit is part of merge request !30. Comments created here will be created in the context of that merge request.
from . import demo, models, populate, wizards
from . import demo, models, wizards
......@@ -17,7 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from odoo import _, exceptions, fields, models, tools
......@@ -21,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."""
......@@ -40,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()
......@@ -46,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:
......@@ -52,6 +45,5 @@
today = fields.Date.today()
for accdoc in self:
# Cache some data.
acc_date = accdoc.date or today
company = accdoc.company_id
......@@ -56,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(
......@@ -58,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:
......@@ -67,9 +59,8 @@
limit=1,
)
if not period:
# if doing tests or installing demo data, and any matching period not
# closed
# Create missing periods on-the-fly when running tests / DB populate.
if (
tools.config["test_enable"]
or tools.config["test_file"]
or self.env.context.get("install_demo", False)
......@@ -72,7 +63,8 @@
if (
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(
......@@ -76,12 +68,7 @@
) and (
not self.env.context.get("period_close_enable")
or not 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,
)
):
......@@ -85,14 +72,20 @@
limit=1,
)
):
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; ?
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),
("date_from", "<=", year_start),
("date_to", ">=", year_end),
],
limit=1,
)
)
if not fiscalyear:
fiscalyear = (
self.env["account.fiscal.year"]
......@@ -100,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()
......@@ -106,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(
......
from . import account_move
##############################################################################
#
# Part of Accounting periods, for Odoo
# Copyright © 2023 XCG Consulting <https://xcg-consulting.fr/>
#
# Accounting periods is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Accounting periods is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Accounting periods. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo import models
class AccountMove(models.Model):
_inherit = "account.move"
def _populate(self, size):
return super(AccountMove, self.with_context(install_demo=True))._populate(size)
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