Skip to content
Snippets Groups Projects
  • Houzefa Abbasbhay's avatar
    121a1cd601e8
    Migrate to Odoo 13.0 · 121a1cd601e8
    Houzefa Abbasbhay authored
    Specifics to play better with accounting changes:
    
    * No longer change the base ``date`` field; add ``accounting_date`` instead.
    * Build periods when loading accounting data for a new company.
    121a1cd601e8
    History
    Migrate to Odoo 13.0
    Houzefa Abbasbhay authored
    Specifics to play better with accounting changes:
    
    * No longer change the base ``date`` field; add ``accounting_date`` instead.
    * Build periods when loading accounting data for a new company.
test_account_period.py 6.41 KiB
##############################################################################
#
#    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/>.
#
##############################################################################

import uuid

import odoo.fields
import odoo.tests


@odoo.tests.common.at_install(False)
@odoo.tests.common.post_install(True)
class Test(odoo.tests.TransactionCase):
    """Test fiscal years & accounting periods.

    These tests assume an Odoo database with demo data.
    """

    def setUp(self):
        """Set up base elements we are going to use in these tests.
        """

        super(Test, self).setUp()

        self.client = self._createPartner()

        self.product = self.env.ref("product.product_product_1")
        self.product_account = self._createAccount("other")

    def test_invoice_dates(self):
        """Check invoice date propagation to accounting documents.
        """

        # Find the period around today. We should have one in demo data.
        today = odoo.fields.Date.today()
        period_today = self._getTodayPeriod()

        # Control: Default dates & period around today.
        accdoc = self._makeInvoice()
        self._validateInvoice(accdoc)
        self.assertEqual(accdoc.accounting_date, today)
        self.assertEqual(accdoc.date, today)
        self.assertEqual(accdoc.transaction_date, today)
        self.assertEqual(accdoc.period_id.id, period_today.id)
        accentries = accdoc.line_ids
        self.assertEqual(set(accentries.mapped("transaction_date")), {today})
        self.assertEqual(accentries.mapped("period_id").id, period_today.id)

        # Force a date on the invoice; check its propagation.
        forced_date = odoo.fields.Date.to_date("2017-01-15")
        accdoc = self._makeInvoice(invoice_date=forced_date)
        self._validateInvoice(accdoc)
        self.assertEqual(accdoc.accounting_date, today)
        self.assertEqual(accdoc.date, forced_date)  # unchanged odoo field
        self.assertEqual(accdoc.transaction_date, forced_date)
        self.assertEqual(accdoc.period_id.id, period_today.id)
        accentries = accdoc.line_ids
        self.assertEqual(
            set(accentries.mapped("transaction_date")), {forced_date}
        )
        self.assertEqual(accentries.mapped("period_id").id, period_today.id)

    def test_close_period(self):
        """Check the "Close period" dialog box.
        """

        # Find the period around today. We should have one in demo data.
        period_today = self._getTodayPeriod()

        self.assertEqual(period_today.state, "draft")

        # Close the period.
        self.env["account.period.close"].with_context(
            active_ids=period_today.ids, active_model=period_today._name
        ).create({"validated": True}).close()

        self.assertEqual(period_today.state, "done")

        # Can no longer create an accounting document on this period.
        invoice = self._makeInvoice()
        with self.assertRaises(odoo.exceptions.Warning):
            self._validateInvoice(invoice)

    def _createAccount(self, account_type):
        """Create an account.

        :type account_type: String.
        :rtype: Odoo "account.account" record.
        """

        return self.env["account.account"].create(
            {
                "code": uuid.uuid4().hex[:8],
                "internal_type": account_type,
                "name": uuid.uuid4().hex,
                "reconcile": True,
                "user_type_id": (
                    self.env["account.account.type"]
                    .search([("type", "=", account_type)], limit=1)
                    .id
                ),
            }
        )

    def _createPartner(self):
        """Create a client.

        :rtype: Odoo "res.partner" record.
        """

        account = self._createAccount("receivable")
        return self.env["res.partner"].create(
            {
                "is_company": True,
                "name": "TEST-PARTNER-NAME",
                "property_account_receivable_id": account.id,
            }
        )

    def _getTodayPeriod(self):
        """Find the period around today. We should have one in demo data.

        :return: The period.
        :rtype: Odoo "account.period" record set.
        """

        today = odoo.fields.Date.today()
        period_today = self.env["account.period"].search(
            [
                ("date_start", "<=", today),
                ("date_stop", ">=", today),
                ("state", "=", "draft"),
            ]
        )
        self.assertEqual(len(period_today), 1)
        return period_today

    def _makeInvoice(self, **invoice_values):
        """Create an invoice and return it.

        :param invoice_values: kwargs to customize invoice creation values.

        :return: The invoice.
        :rtype: Odoo "account.move" record.
        """

        local_invoice_values = {
            "invoice_line_ids": [
                (
                    0,  # 0: Create.
                    0,
                    {
                        "account_id": self.product_account.id,
                        "name": "TEST-INVLINE",
                        "price_unit": 42.0,
                        "product_id": self.product.id,
                        "quantity": 1.0,
                    },
                )
            ],
            "partner_id": self.client.id,
            "type": "out_invoice",
        }
        local_invoice_values.update(invoice_values)
        return self.env["account.move"].create(local_invoice_values)

    def _validateInvoice(self, invoice):
        """Validate the specified invoice.

        :type invoice: Odoo "account.move" record.
        """

        self.assertEqual(invoice.state, "draft")
        invoice.post()
        self.assertEqual(invoice.state, "posted")