Skip to content
Snippets Groups Projects
account_period.py 4.85 KiB
Newer Older
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
##############################################################################
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
#    Accounting periods, for Odoo
#    Copyright (C) 2018 XCG Consulting <http://odoo.consulting>
#
#    This program 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.
#
#    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
##############################################################################
from odoo import _, api, exceptions, fields, models


class AccountPeriod(models.Model):
    """Account period defined an accounting period.

    This module aims to recreate the same behaviour than the account.period
    model in odoo previous versions.
    """

    _name = "account.period"
    _description = "Accounting period"
    _order = "date_start"
    code = fields.Char(
        string="Code",
        help="Unique code of this period.",
        index=True,
        required=True,
    )

    company_id = fields.Many2one(
        related=("fiscalyear_id", "company_id"),
        comodel_name="res.company",
        string="Company",
        help="The company the fiscal year is in.",
        readonly=True,
        store=True,
        index=True,
    date_cutoff = fields.Date(
        string="Cut-off date",
        help=(
            "Optional cut-off date which helps force transaction dates and "
            "this period for a few days after the end of this period."
    @api.depends("date_cutoff", "date_stop")
    @api.multi
    def _get_date_effective_cutoff(self):
        for period in self:
            period.date_effective_cutoff = (
                period.date_cutoff or period.date_stop
            )

    date_effective_cutoff = fields.Date(
        compute=_get_date_effective_cutoff,
        string="Effective cut-off date",
        help="The cut-off date when defined; the ending date otherwise.",
        readonly=True,
        store=True,
    )

    date_start = fields.Date(
        string="Start", help="When this period starts.", required=True
    )

    date_stop = fields.Date(
        string="End", help="When this period ends.", required=True
    fiscalyear_id = fields.Many2one(
        comodel_name="account.fiscalyear",
        string="Fiscal year",
        help="The fiscal year this period is in.",
        ondelete="cascade",
        required=True,
    )

    name = fields.Char(
        string="Name", help="Displayed name of this period.", required=True
    state = fields.Selection(
        selection=[("draft", "Open"), ("done", "Closed")],
        string="State",
        default="draft",
        readonly=True,
        required=True,

    _sql_constraints = [
        (
            "unique_code_per_company",
            "UNIQUE(code, company_id)",
            "The code must be unique.",
        )

    @api.model
    def find(self, dt=None):

        context = dict(self.env.context)

        if not dt:
            dt = fields.Date.context_today(self)

        args = [("date_start", "<=", dt), ("date_stop", ">=", dt)]

        if context.get("company_id", False):
            args.append(("company_id", "=", context["company_id"]))
        else:
            company_id = self.env.user.company_id.id
            args.append(("company_id", "=", company_id))

        result = []

        if not result:
            result = self.search(args)

        if not result:

            model, action_id = self.env["ir.model.data"].get_object_reference(
                "account_period", "redirect_warning_account_period_action"
            )
            msg = (
                _(
                    "There is no period defined for this date: %s.\n"
                    "Please go to Configuration/Periods."
                )
                % dt
            )

            raise exceptions.RedirectWarning(
                msg, action_id, _("Go to the configuration panel")
            )

        return result

    @api.model
    def name_search(self, name="", args=None, operator="ilike", limit=100):
        """Override to add company filter."""

        args.append(("company_id", "=", self.env.user.company_id.id))
        return super(AccountPeriod, self).name_search(
            name=name, args=args, operator=operator, limit=limit
        )

    @api.multi
    def reopen_period(self):
        """Called by a button to re-open a closed period."""

        self.ensure_one()

        self.state = "draft"