2019-01-30 14:27:00 +01:00
|
|
|
# Copyright 2015-2019 ACSONE SA/NV (<http://acsone.eu>)
|
2017-12-19 17:28:44 +01:00
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
2014-08-07 12:58:07 +02:00
|
|
|
|
2017-05-29 16:25:41 +02:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2017-03-13 14:18:36 +01:00
|
|
|
import odoo.tests.common as common
|
|
|
|
from odoo.exceptions import UserError
|
|
|
|
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
|
2014-08-07 12:58:07 +02:00
|
|
|
|
|
|
|
|
2019-01-30 14:27:00 +01:00
|
|
|
class TestAccountConstraintChronology(common.SavepointCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(TestAccountConstraintChronology, cls).setUpClass()
|
2014-08-07 12:58:07 +02:00
|
|
|
|
2017-03-14 19:09:03 +01:00
|
|
|
# Needed to create invoice
|
2017-03-13 14:18:36 +01:00
|
|
|
|
2021-04-05 16:26:59 +02:00
|
|
|
cls.account_type1 = cls.env["account.account.type"].create(
|
|
|
|
{
|
|
|
|
"name": "acc type test 1",
|
|
|
|
"type": "receivable",
|
|
|
|
"include_initial_balance": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.account_type2 = cls.env["account.account.type"].create(
|
|
|
|
{
|
|
|
|
"name": "acc type test 2",
|
|
|
|
"type": "other",
|
|
|
|
"include_initial_balance": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.account_account = cls.env["account.account"].create(
|
|
|
|
{
|
|
|
|
"name": "acc test",
|
|
|
|
"code": "X2020",
|
|
|
|
"user_type_id": cls.account_type1.id,
|
|
|
|
"reconcile": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.account_account_line = cls.env["account.account"].create(
|
|
|
|
{
|
|
|
|
"name": "acc inv line test",
|
|
|
|
"code": "X2021",
|
|
|
|
"user_type_id": cls.account_type2.id,
|
|
|
|
"reconcile": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.sequence = cls.env["ir.sequence"].create(
|
|
|
|
{
|
|
|
|
"name": "Journal Sale",
|
|
|
|
"prefix": "SALE",
|
|
|
|
"padding": 6,
|
|
|
|
"company_id": cls.env.ref("base.main_company").id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.account_journal_sale = cls.env["account.journal"].create(
|
|
|
|
{
|
|
|
|
"name": "Sale journal",
|
|
|
|
"code": "SALE",
|
|
|
|
"type": "sale",
|
|
|
|
"sequence_id": cls.sequence.id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cls.product = cls.env["product.product"].create({"name": "product name"})
|
|
|
|
cls.analytic_account = cls.env["account.analytic.account"].create(
|
|
|
|
{"name": "test account"}
|
|
|
|
)
|
2014-08-07 12:58:07 +02:00
|
|
|
|
2017-03-14 19:09:03 +01:00
|
|
|
def get_journal_check(self, value):
|
|
|
|
journal = self.account_journal_sale.copy()
|
|
|
|
journal.check_chronology = value
|
|
|
|
return journal
|
2014-08-07 12:58:07 +02:00
|
|
|
|
2017-03-14 19:09:03 +01:00
|
|
|
def create_simple_invoice(self, journal_id, date):
|
2021-04-05 16:26:59 +02:00
|
|
|
invoice = self.env["account.invoice"].create(
|
|
|
|
{
|
|
|
|
"partner_id": self.env.ref("base.res_partner_2").id,
|
|
|
|
"account_id": self.account_account.id,
|
|
|
|
"type": "in_invoice",
|
|
|
|
"journal_id": journal_id,
|
|
|
|
"date_invoice": date,
|
|
|
|
"state": "draft",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.env["account.invoice.line"].create(
|
|
|
|
{
|
|
|
|
"product_id": self.product.id,
|
|
|
|
"quantity": 1.0,
|
|
|
|
"price_unit": 100.0,
|
|
|
|
"invoice_id": invoice.id,
|
|
|
|
"name": "product that cost 100",
|
|
|
|
"account_id": self.account_account_line.id,
|
|
|
|
"account_analytic_id": self.analytic_account.id,
|
|
|
|
}
|
|
|
|
)
|
2017-03-14 19:09:03 +01:00
|
|
|
return invoice
|
2014-08-07 12:58:07 +02:00
|
|
|
|
|
|
|
def test_invoice_draft(self):
|
2017-03-14 19:09:03 +01:00
|
|
|
journal = self.get_journal_check(True)
|
2014-08-07 12:58:07 +02:00
|
|
|
today = datetime.now()
|
|
|
|
yesterday = today - timedelta(days=1)
|
2014-08-18 11:06:23 +02:00
|
|
|
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
2017-03-14 19:09:03 +01:00
|
|
|
self.create_simple_invoice(journal.id, date)
|
2014-08-18 11:06:23 +02:00
|
|
|
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
2017-03-14 19:09:03 +01:00
|
|
|
invoice_2 = self.create_simple_invoice(journal.id, date)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.assertTrue(
|
|
|
|
(invoice_2.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
2017-03-13 14:18:36 +01:00
|
|
|
with self.assertRaises(UserError):
|
|
|
|
invoice_2.action_invoice_open()
|
2014-08-07 12:58:07 +02:00
|
|
|
|
2019-04-10 16:37:31 +02:00
|
|
|
def test_invoice_draft_no_check(self):
|
|
|
|
journal = self.get_journal_check(False)
|
|
|
|
today = datetime.now()
|
|
|
|
yesterday = today - timedelta(days=1)
|
|
|
|
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
self.create_simple_invoice(journal.id, date)
|
|
|
|
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
invoice_2 = self.create_simple_invoice(journal.id, date)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.assertTrue(
|
|
|
|
(invoice_2.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
2019-04-10 16:37:31 +02:00
|
|
|
self.assertTrue(invoice_2.action_invoice_open())
|
|
|
|
|
2014-08-07 12:58:07 +02:00
|
|
|
def test_invoice_validate(self):
|
2017-03-14 19:09:03 +01:00
|
|
|
journal = self.get_journal_check(True)
|
2014-08-07 12:58:07 +02:00
|
|
|
today = datetime.now()
|
|
|
|
tomorrow = today + timedelta(days=1)
|
2017-03-13 14:18:36 +01:00
|
|
|
date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
2017-03-14 19:09:03 +01:00
|
|
|
invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.assertTrue(
|
|
|
|
(invoice_1.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
2017-03-13 14:18:36 +01:00
|
|
|
invoice_1.action_invoice_open()
|
2014-08-18 11:06:23 +02:00
|
|
|
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
2017-03-14 19:09:03 +01:00
|
|
|
invoice_2 = self.create_simple_invoice(journal.id, date)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.assertTrue(
|
|
|
|
(invoice_2.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
2017-03-13 14:18:36 +01:00
|
|
|
with self.assertRaises(UserError):
|
|
|
|
invoice_2.action_invoice_open()
|
2014-08-07 12:58:07 +02:00
|
|
|
|
|
|
|
def test_invoice_without_date(self):
|
2017-03-14 19:09:03 +01:00
|
|
|
journal = self.get_journal_check(True)
|
2014-08-07 12:58:07 +02:00
|
|
|
today = datetime.now()
|
|
|
|
yesterday = today - timedelta(days=1)
|
2014-08-18 11:06:23 +02:00
|
|
|
date = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
2017-03-14 19:09:03 +01:00
|
|
|
self.create_simple_invoice(journal.id, date)
|
|
|
|
invoice_2 = self.create_simple_invoice(journal.id, False)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.assertTrue(
|
|
|
|
(invoice_2.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
2017-03-13 14:18:36 +01:00
|
|
|
with self.assertRaises(UserError):
|
|
|
|
invoice_2.action_invoice_open()
|
2019-04-10 16:37:31 +02:00
|
|
|
|
|
|
|
def test_journal_change_type(self):
|
|
|
|
self.account_journal_sale.check_chronology = True
|
|
|
|
self.assertTrue(self.account_journal_sale.check_chronology)
|
2021-04-05 16:26:59 +02:00
|
|
|
self.account_journal_sale.type = "bank"
|
2019-04-10 16:37:31 +02:00
|
|
|
self.account_journal_sale._onchange_type()
|
|
|
|
self.assertFalse(self.account_journal_sale.check_chronology)
|
2021-02-28 00:59:43 +01:00
|
|
|
|
|
|
|
def test_invoice_refund(self):
|
|
|
|
journal = self.get_journal_check(True)
|
|
|
|
today = datetime.now()
|
|
|
|
tomorrow = today + timedelta(days=1)
|
|
|
|
date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow)
|
|
|
|
self.assertTrue(
|
|
|
|
(invoice_1.state == "draft"), "Initial invoice state is not Draft"
|
|
|
|
)
|
|
|
|
invoice_1.action_invoice_open()
|
|
|
|
date = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
refund_invoice_wiz = (
|
|
|
|
self.env["account.invoice.refund"]
|
|
|
|
.with_context(active_ids=[invoice_1.id])
|
|
|
|
.create(
|
|
|
|
{
|
|
|
|
"description": "test_invoice_refund",
|
|
|
|
"filter_refund": "cancel",
|
|
|
|
"date": date,
|
|
|
|
"date_invoice": date,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
with self.assertRaises(UserError):
|
|
|
|
refund_invoice_wiz.invoice_refund()
|
|
|
|
invoice_1.journal_id.refund_sequence = True
|
|
|
|
refund_invoice_wiz.invoice_refund()
|