2023-04-04 18:17:44 +08:00
|
|
|
# The Mia! Accounting Project.
|
2023-03-17 22:32:01 +08:00
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/3/11
|
|
|
|
|
|
|
|
# Copyright (c) 2023 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""The test for the offset.
|
|
|
|
|
|
|
|
"""
|
2023-04-09 11:00:38 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-03-17 22:32:01 +08:00
|
|
|
import unittest
|
2023-03-18 03:38:07 +08:00
|
|
|
from decimal import Decimal
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
import httpx
|
|
|
|
from flask import Flask
|
|
|
|
|
|
|
|
from test_site import db
|
2023-04-13 08:30:07 +08:00
|
|
|
from test_site.lib import JournalEntryLineItemData, JournalEntryCurrencyData, \
|
|
|
|
JournalEntryData, BaseTestData
|
2023-04-13 08:23:52 +08:00
|
|
|
from testlib import NEXT_URI, Accounts, create_test_app, get_client, \
|
2023-04-13 08:30:07 +08:00
|
|
|
match_journal_entry_detail
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-20 22:08:58 +08:00
|
|
|
PREFIX: str = "/accounting/journal-entries"
|
|
|
|
"""The URL prefix for the journal entry management."""
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OffsetTestCase(unittest.TestCase):
|
|
|
|
"""The offset test case."""
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
"""Sets up the test.
|
|
|
|
This is run once per test.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
self.app: Flask = create_test_app()
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-04-10 23:14:04 +08:00
|
|
|
from accounting.models import JournalEntry, JournalEntryLineItem
|
2023-03-20 22:08:58 +08:00
|
|
|
JournalEntry.query.delete()
|
2023-03-20 20:52:35 +08:00
|
|
|
JournalEntryLineItem.query.delete()
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
self.client, self.csrf_token = get_client(self.app, "editor")
|
2023-04-12 12:12:11 +08:00
|
|
|
self.data: OffsetTestData = OffsetTestData(self.app, "editor")
|
2023-04-12 12:28:34 +08:00
|
|
|
self.data.populate()
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
def test_add_receivable_offset(self) -> None:
|
|
|
|
"""Tests to add the receivable offset.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-20 22:08:58 +08:00
|
|
|
from accounting.models import Account, JournalEntry
|
2023-03-19 13:44:51 +08:00
|
|
|
create_uri: str = f"{PREFIX}/create/receipt?next=%2F_next"
|
|
|
|
store_uri: str = f"{PREFIX}/store/receipt"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
2023-03-18 03:38:07 +08:00
|
|
|
old_amount: Decimal
|
2023-03-17 22:32:01 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data: JournalEntryData = JournalEntryData(
|
2023-04-09 10:04:12 +08:00
|
|
|
self.data.l_r_or3d.journal_entry.days, [JournalEntryCurrencyData(
|
2023-03-17 22:32:01 +08:00
|
|
|
"USD",
|
|
|
|
[],
|
2023-03-20 22:08:58 +08:00
|
|
|
[JournalEntryLineItemData(
|
|
|
|
Accounts.RECEIVABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_r_or1d.description, "300",
|
|
|
|
original_line_item=self.data.l_r_or1d),
|
2023-03-20 22:08:58 +08:00
|
|
|
JournalEntryLineItemData(
|
|
|
|
Accounts.RECEIVABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_r_or1d.description, "100",
|
|
|
|
original_line_item=self.data.l_r_or1d),
|
2023-03-20 22:08:58 +08:00
|
|
|
JournalEntryLineItemData(
|
|
|
|
Accounts.RECEIVABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_r_or3d.description, "100",
|
|
|
|
original_line_item=self.data.l_r_or3d)])])
|
2023-03-19 21:00:11 +08:00
|
|
|
|
|
|
|
# Non-existing original line item ID
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] = "9999"
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-20 20:35:10 +08:00
|
|
|
# The same debit or credit
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_p_or1c.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = self.data.l_p_or1c.account
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-credit-1-amount"] = "100"
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item does not need offset
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.RECEIVABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = False
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
2023-03-19 13:44:51 +08:00
|
|
|
response = self.client.post(
|
2023-04-13 08:23:52 +08:00
|
|
|
store_uri,
|
|
|
|
data=journal_entry_data.new_form(self.csrf_token, NEXT_URI))
|
2023-03-17 22:32:01 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.RECEIVABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = True
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is also an offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_p_of1d.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = self.data.l_p_of1d.account
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = Accounts.NOTES_RECEIVABLE
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[0].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - unmatched
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[2].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not before the original line items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days + 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_id: int \
|
|
|
|
= match_journal_entry_detail(response.headers["Location"])
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry = db.session.get(JournalEntry, journal_entry_id)
|
|
|
|
for offset in journal_entry.currencies[0].credit:
|
2023-03-19 21:00:11 +08:00
|
|
|
self.assertIsNotNone(offset.original_line_item_id)
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
def test_edit_receivable_offset(self) -> None:
|
|
|
|
"""Tests to edit the receivable offset.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data: JournalEntryData = self.data.j_r_of2
|
2023-03-20 22:08:58 +08:00
|
|
|
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
|
|
|
|
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data.days = self.data.j_r_or2.days
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.currencies[0].debit[0].amount = Decimal("600")
|
|
|
|
journal_entry_data.currencies[0].credit[0].amount = Decimal("600")
|
|
|
|
journal_entry_data.currencies[0].debit[2].amount = Decimal("600")
|
|
|
|
journal_entry_data.currencies[0].credit[2].amount = Decimal("600")
|
2023-03-18 03:08:08 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Non-existing original line item ID
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] = "9999"
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-20 20:35:10 +08:00
|
|
|
# The same debit or credit
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_p_or1c.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = self.data.l_p_or1c.account
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-amount"] = "100"
|
|
|
|
form["currency-1-credit-1-amount"] = "100"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item does not need offset
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.RECEIVABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = False
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
2023-03-19 13:44:51 +08:00
|
|
|
response = self.client.post(
|
2023-04-13 08:23:52 +08:00
|
|
|
update_uri,
|
|
|
|
data=journal_entry_data.update_form(self.csrf_token, NEXT_URI))
|
2023-03-17 22:32:01 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.RECEIVABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = True
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is also an offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-credit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_p_of1d.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = self.data.l_p_of1d.account
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = Accounts.NOTES_RECEIVABLE
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[0].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[0].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - unmatched
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[2].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[2].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not before the original line items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days: int = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days + 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-20 22:08:58 +08:00
|
|
|
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
def test_edit_receivable_original_line_item(self) -> None:
|
|
|
|
"""Tests to edit the receivable original line item.
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-20 22:08:58 +08:00
|
|
|
from accounting.models import JournalEntry
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data: JournalEntryData = self.data.j_r_or1
|
2023-03-20 22:08:58 +08:00
|
|
|
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
|
|
|
|
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data.days = self.data.j_r_of1.days
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.currencies[0].debit[0].amount = Decimal("800")
|
|
|
|
journal_entry_data.currencies[0].credit[0].amount = Decimal("800")
|
|
|
|
journal_entry_data.currencies[0].debit[1].amount = Decimal("3.4")
|
|
|
|
journal_entry_data.currencies[0].credit[1].amount = Decimal("3.4")
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = Accounts.NOTES_RECEIVABLE
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not less than offset total - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[0].amount
|
|
|
|
- Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[0].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
- Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not less than offset total - fully offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-2-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[1].amount
|
|
|
|
- Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-2-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[1].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
- Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not after the offset items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days: int = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days - 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not deleting matched original line items
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-20 23:06:57 +08:00
|
|
|
del form["currency-1-debit-1-id"]
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-20 22:08:58 +08:00
|
|
|
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is always before the offset item, even when
|
|
|
|
# they happen in the same day.
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_or: JournalEntry | None = db.session.get(
|
|
|
|
JournalEntry, journal_entry_data.id)
|
|
|
|
self.assertIsNotNone(journal_entry_or)
|
|
|
|
journal_entry_of: JournalEntry | None = db.session.get(
|
2023-04-08 20:30:17 +08:00
|
|
|
JournalEntry, self.data.j_r_of1.id)
|
2023-03-20 22:08:58 +08:00
|
|
|
self.assertIsNotNone(journal_entry_of)
|
|
|
|
self.assertEqual(journal_entry_or.date, journal_entry_of.date)
|
|
|
|
self.assertLess(journal_entry_or.no, journal_entry_of.no)
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
def test_add_payable_offset(self) -> None:
|
|
|
|
"""Tests to add the payable offset.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-20 22:08:58 +08:00
|
|
|
from accounting.models import Account, JournalEntry
|
2023-03-19 13:44:51 +08:00
|
|
|
create_uri: str = f"{PREFIX}/create/disbursement?next=%2F_next"
|
|
|
|
store_uri: str = f"{PREFIX}/store/disbursement"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data: JournalEntryData = JournalEntryData(
|
2023-04-09 10:04:12 +08:00
|
|
|
self.data.l_p_or3c.journal_entry.days, [JournalEntryCurrencyData(
|
2023-03-17 22:32:01 +08:00
|
|
|
"USD",
|
2023-03-20 22:08:58 +08:00
|
|
|
[JournalEntryLineItemData(
|
|
|
|
Accounts.PAYABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_p_or1c.description, "500",
|
|
|
|
original_line_item=self.data.l_p_or1c),
|
2023-03-20 22:08:58 +08:00
|
|
|
JournalEntryLineItemData(
|
|
|
|
Accounts.PAYABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_p_or1c.description, "300",
|
|
|
|
original_line_item=self.data.l_p_or1c),
|
2023-03-20 22:08:58 +08:00
|
|
|
JournalEntryLineItemData(
|
|
|
|
Accounts.PAYABLE,
|
2023-04-08 20:30:17 +08:00
|
|
|
self.data.l_p_or3c.description, "120",
|
|
|
|
original_line_item=self.data.l_p_or3c)],
|
2023-03-17 22:32:01 +08:00
|
|
|
[])])
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Non-existing original line item ID
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] = "9999"
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-20 20:35:10 +08:00
|
|
|
# The same debit or credit
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_r_or1d.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = self.data.l_r_or1d.account
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-amount"] = "100"
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item does not need offset
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.PAYABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = False
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
2023-03-19 13:44:51 +08:00
|
|
|
response = self.client.post(
|
2023-04-13 08:23:52 +08:00
|
|
|
store_uri,
|
|
|
|
data=journal_entry_data.new_form(self.csrf_token, NEXT_URI))
|
2023-03-17 22:32:01 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.PAYABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = True
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is also an offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_r_of1c.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = self.data.l_r_of1c.account
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = Accounts.NOTES_PAYABLE
|
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[0].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - unmatched
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[2].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not before the original line items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days: int = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days + 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.new_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(store_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_id: int \
|
|
|
|
= match_journal_entry_detail(response.headers["Location"])
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry = db.session.get(JournalEntry, journal_entry_id)
|
|
|
|
for offset in journal_entry.currencies[0].debit:
|
2023-03-19 21:00:11 +08:00
|
|
|
self.assertIsNotNone(offset.original_line_item_id)
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
def test_edit_payable_offset(self) -> None:
|
|
|
|
"""Tests to edit the payable offset.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-20 22:08:58 +08:00
|
|
|
from accounting.models import Account, JournalEntry
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data: JournalEntryData = self.data.j_p_of2
|
2023-03-20 22:08:58 +08:00
|
|
|
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
|
|
|
|
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data.days = self.data.j_p_or2.days
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.currencies[0].debit[0].amount = Decimal("1100")
|
|
|
|
journal_entry_data.currencies[0].credit[0].amount = Decimal("1100")
|
|
|
|
journal_entry_data.currencies[0].debit[2].amount = Decimal("900")
|
|
|
|
journal_entry_data.currencies[0].credit[2].amount = Decimal("900")
|
2023-03-18 03:08:08 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Non-existing original line item ID
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] = "9999"
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-20 20:35:10 +08:00
|
|
|
# The same debit or credit
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_r_or1d.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = self.data.l_r_or1d.account
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-amount"] = "100"
|
|
|
|
form["currency-1-credit-1-amount"] = "100"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item does not need offset
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.PAYABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = False
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
2023-03-19 13:44:51 +08:00
|
|
|
response = self.client.post(
|
2023-04-13 08:23:52 +08:00
|
|
|
update_uri,
|
|
|
|
data=journal_entry_data.update_form(self.csrf_token, NEXT_URI))
|
2023-03-17 22:32:01 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
with self.app.app_context():
|
|
|
|
account = Account.find_by_code(Accounts.PAYABLE)
|
2023-03-18 22:52:29 +08:00
|
|
|
account.is_need_offset = True
|
2023-03-17 22:32:01 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is also an offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-19 21:00:11 +08:00
|
|
|
form["currency-1-debit-1-original_line_item_id"] \
|
2023-04-09 11:05:52 +08:00
|
|
|
= str(self.data.l_r_of1c.id)
|
2023-04-08 20:30:17 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = self.data.l_r_of1c.account
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-debit-1-account_code"] = Accounts.NOTES_PAYABLE
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[0].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[0].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not exceeding net balance - unmatched
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[2].amount
|
|
|
|
+ Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-3-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[2].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
+ Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not before the original line items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days: int = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days + 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_id: int \
|
|
|
|
= match_journal_entry_detail(response.headers["Location"])
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry = db.session.get(JournalEntry, journal_entry_id)
|
|
|
|
for offset in journal_entry.currencies[0].debit:
|
2023-03-19 21:00:11 +08:00
|
|
|
self.assertIsNotNone(offset.original_line_item_id)
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
def test_edit_payable_original_line_item(self) -> None:
|
|
|
|
"""Tests to edit the payable original line item.
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-20 22:08:58 +08:00
|
|
|
from accounting.models import JournalEntry
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data: JournalEntryData = self.data.j_p_or1
|
2023-03-20 22:08:58 +08:00
|
|
|
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
|
|
|
|
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
|
2023-03-17 22:32:01 +08:00
|
|
|
form: dict[str, str]
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-04-08 20:30:17 +08:00
|
|
|
journal_entry_data.days = self.data.j_p_of1.days
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.currencies[0].debit[0].amount = Decimal("1200")
|
|
|
|
journal_entry_data.currencies[0].credit[0].amount = Decimal("1200")
|
|
|
|
journal_entry_data.currencies[0].debit[1].amount = Decimal("0.9")
|
|
|
|
journal_entry_data.currencies[0].credit[1].amount = Decimal("0.9")
|
2023-03-17 22:32:01 +08:00
|
|
|
|
|
|
|
# Not the same currency
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-code"] = "EUR"
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not the same account
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
form["currency-1-credit-1-account_code"] = Accounts.NOTES_PAYABLE
|
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not less than offset total - partially offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[0].amount
|
|
|
|
- Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-1-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[0].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
- Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Not less than offset total - fully offset
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-debit-2-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].debit[1].amount
|
|
|
|
- Decimal("0.01"))
|
2023-03-18 03:38:07 +08:00
|
|
|
form["currency-1-credit-2-amount"] \
|
2023-03-20 22:08:58 +08:00
|
|
|
= str(journal_entry_data.currencies[0].credit[1].amount
|
2023-03-19 13:44:51 +08:00
|
|
|
- Decimal("0.01"))
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not after the offset items
|
2023-03-20 22:08:58 +08:00
|
|
|
old_days: int = journal_entry_data.days
|
|
|
|
journal_entry_data.days = old_days - 1
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_data.days = old_days
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# Not deleting matched original line items
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-20 23:06:57 +08:00
|
|
|
del form["currency-1-credit-1-id"]
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Success
|
2023-04-13 08:23:52 +08:00
|
|
|
form = journal_entry_data.update_form(self.csrf_token, NEXT_URI)
|
2023-03-17 22:32:01 +08:00
|
|
|
response = self.client.post(update_uri, data=form)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-20 22:08:58 +08:00
|
|
|
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
|
2023-03-17 22:32:01 +08:00
|
|
|
|
2023-03-19 21:00:11 +08:00
|
|
|
# The original line item is always before the offset item, even when
|
|
|
|
# they happen in the same day
|
2023-03-17 22:32:01 +08:00
|
|
|
with self.app.app_context():
|
2023-03-20 22:08:58 +08:00
|
|
|
journal_entry_or: JournalEntry | None = db.session.get(
|
|
|
|
JournalEntry, journal_entry_data.id)
|
|
|
|
self.assertIsNotNone(journal_entry_or)
|
|
|
|
journal_entry_of: JournalEntry | None = db.session.get(
|
2023-04-08 20:30:17 +08:00
|
|
|
JournalEntry, self.data.j_p_of1.id)
|
2023-03-20 22:08:58 +08:00
|
|
|
self.assertIsNotNone(journal_entry_of)
|
|
|
|
self.assertEqual(journal_entry_or.date, journal_entry_of.date)
|
|
|
|
self.assertLess(journal_entry_or.no, journal_entry_of.no)
|
2023-04-09 11:00:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OffsetTestData(BaseTestData):
|
|
|
|
"""The offset test data."""
|
|
|
|
|
|
|
|
def _init_data(self) -> None:
|
|
|
|
# Receivable original line items
|
|
|
|
self.l_r_or1d, self.l_r_or1c = self._couple(
|
|
|
|
"Accountant", "1200", Accounts.RECEIVABLE, Accounts.SERVICE)
|
|
|
|
self.l_r_or2d, self.l_r_or2c = self._couple(
|
|
|
|
"Toy", "600", Accounts.RECEIVABLE, Accounts.SALES)
|
|
|
|
self.l_r_or3d, self.l_r_or3c = self._couple(
|
|
|
|
"Noodles", "100", Accounts.RECEIVABLE, Accounts.SALES)
|
|
|
|
self.l_r_or4d, self.l_r_or4c = self._couple(
|
|
|
|
"Interest", "3.4", Accounts.RECEIVABLE, Accounts.INTEREST)
|
|
|
|
|
|
|
|
# Payable original line items
|
|
|
|
self.l_p_or1d, self.l_p_or1c = self._couple(
|
|
|
|
"Airplane", "2000", Accounts.TRAVEL, Accounts.PAYABLE)
|
|
|
|
self.l_p_or2d, self.l_p_or2c = self._couple(
|
|
|
|
"Phone", "900", Accounts.OFFICE, Accounts.PAYABLE)
|
|
|
|
self.l_p_or3d, self.l_p_or3c = self._couple(
|
|
|
|
"Steak", "120", Accounts.MEAL, Accounts.PAYABLE)
|
|
|
|
self.l_p_or4d, self.l_p_or4c = self._couple(
|
|
|
|
"Envelop", "0.9", Accounts.OFFICE, Accounts.PAYABLE)
|
|
|
|
|
|
|
|
# Original journal entries
|
|
|
|
self.j_r_or1: JournalEntryData = JournalEntryData(
|
|
|
|
50, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_r_or1d, self.l_r_or4d],
|
|
|
|
[self.l_r_or1c, self.l_r_or4c])])
|
|
|
|
self.j_r_or2: JournalEntryData = JournalEntryData(
|
|
|
|
30, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_r_or2d, self.l_r_or3d],
|
|
|
|
[self.l_r_or2c, self.l_r_or3c])])
|
|
|
|
self.j_p_or1: JournalEntryData = JournalEntryData(
|
|
|
|
40, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_p_or1d, self.l_p_or4d],
|
|
|
|
[self.l_p_or1c, self.l_p_or4c])])
|
|
|
|
self.j_p_or2: JournalEntryData = JournalEntryData(
|
|
|
|
20, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_p_or2d, self.l_p_or3d],
|
|
|
|
[self.l_p_or2c, self.l_p_or3c])])
|
|
|
|
|
|
|
|
self._add_journal_entry(self.j_r_or1)
|
|
|
|
self._add_journal_entry(self.j_r_or2)
|
|
|
|
self._add_journal_entry(self.j_p_or1)
|
|
|
|
self._add_journal_entry(self.j_p_or2)
|
|
|
|
|
|
|
|
# Receivable offset items
|
|
|
|
self.l_r_of1d, self.l_r_of1c = self._couple(
|
|
|
|
"Accountant", "500", Accounts.CASH, Accounts.RECEIVABLE)
|
|
|
|
self.l_r_of1c.original_line_item = self.l_r_or1d
|
|
|
|
self.l_r_of2d, self.l_r_of2c = self._couple(
|
|
|
|
"Accountant", "200", Accounts.CASH, Accounts.RECEIVABLE)
|
|
|
|
self.l_r_of2c.original_line_item = self.l_r_or1d
|
|
|
|
self.l_r_of3d, self.l_r_of3c = self._couple(
|
|
|
|
"Accountant", "100", Accounts.CASH, Accounts.RECEIVABLE)
|
|
|
|
self.l_r_of3c.original_line_item = self.l_r_or1d
|
|
|
|
self.l_r_of4d, self.l_r_of4c = self._couple(
|
|
|
|
"Toy", "240", Accounts.CASH, Accounts.RECEIVABLE)
|
|
|
|
self.l_r_of4c.original_line_item = self.l_r_or2d
|
|
|
|
self.l_r_of5d, self.l_r_of5c = self._couple(
|
|
|
|
"Interest", "3.4", Accounts.CASH, Accounts.RECEIVABLE)
|
|
|
|
self.l_r_of5c.original_line_item = self.l_r_or4d
|
|
|
|
|
|
|
|
# Payable offset items
|
|
|
|
self.l_p_of1d, self.l_p_of1c = self._couple(
|
|
|
|
"Airplane", "800", Accounts.PAYABLE, Accounts.CASH)
|
|
|
|
self.l_p_of1d.original_line_item = self.l_p_or1c
|
|
|
|
self.l_p_of2d, self.l_p_of2c = self._couple(
|
|
|
|
"Airplane", "300", Accounts.PAYABLE, Accounts.CASH)
|
|
|
|
self.l_p_of2d.original_line_item = self.l_p_or1c
|
|
|
|
self.l_p_of3d, self.l_p_of3c = self._couple(
|
|
|
|
"Airplane", "100", Accounts.PAYABLE, Accounts.CASH)
|
|
|
|
self.l_p_of3d.original_line_item = self.l_p_or1c
|
|
|
|
self.l_p_of4d, self.l_p_of4c = self._couple(
|
|
|
|
"Phone", "400", Accounts.PAYABLE, Accounts.CASH)
|
|
|
|
self.l_p_of4d.original_line_item = self.l_p_or2c
|
|
|
|
self.l_p_of5d, self.l_p_of5c = self._couple(
|
|
|
|
"Envelop", "0.9", Accounts.PAYABLE, Accounts.CASH)
|
|
|
|
self.l_p_of5d.original_line_item = self.l_p_or4c
|
|
|
|
|
|
|
|
# Offset journal entries
|
|
|
|
self.j_r_of1: JournalEntryData = JournalEntryData(
|
|
|
|
25, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_r_of1d], [self.l_r_of1c])])
|
|
|
|
self.j_r_of2: JournalEntryData = JournalEntryData(
|
|
|
|
20, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_r_of2d, self.l_r_of3d, self.l_r_of4d],
|
|
|
|
[self.l_r_of2c, self.l_r_of3c, self.l_r_of4c])])
|
|
|
|
self.j_r_of3: JournalEntryData = JournalEntryData(
|
|
|
|
15, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_r_of5d], [self.l_r_of5c])])
|
|
|
|
self.j_p_of1: JournalEntryData = JournalEntryData(
|
|
|
|
15, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_p_of1d], [self.l_p_of1c])])
|
|
|
|
self.j_p_of2: JournalEntryData = JournalEntryData(
|
|
|
|
10, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_p_of2d, self.l_p_of3d, self.l_p_of4d],
|
|
|
|
[self.l_p_of2c, self.l_p_of3c, self.l_p_of4c])])
|
|
|
|
self.j_p_of3: JournalEntryData = JournalEntryData(
|
|
|
|
5, [JournalEntryCurrencyData(
|
|
|
|
"USD", [self.l_p_of5d], [self.l_p_of5c])])
|
|
|
|
|
|
|
|
self._add_journal_entry(self.j_r_of1)
|
|
|
|
self._add_journal_entry(self.j_r_of2)
|
|
|
|
self._add_journal_entry(self.j_r_of3)
|
|
|
|
self._add_journal_entry(self.j_p_of1)
|
|
|
|
self._add_journal_entry(self.j_p_of2)
|
|
|
|
self._add_journal_entry(self.j_p_of3)
|