Renamed "voucher" to "journal entry".

This commit is contained in:
2023-03-20 22:08:58 +08:00
parent 8f909965a9
commit b1af1d7425
74 changed files with 1956 additions and 1816 deletions

View File

@ -25,7 +25,7 @@ from flask import Flask
from flask.testing import FlaskCliRunner
from testlib import create_test_app, get_client
from testlib_voucher import Accounts, NEXT_URI, add_voucher
from testlib_journal_entry import Accounts, NEXT_URI, add_journal_entry
class DescriptionEditorTestCase(unittest.TestCase):
@ -41,7 +41,7 @@ class DescriptionEditorTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
from accounting.models import BaseAccount, JournalEntry, \
JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
@ -55,7 +55,7 @@ class DescriptionEditorTestCase(unittest.TestCase):
result = runner.invoke(args=["accounting-init-accounts",
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -65,10 +65,10 @@ class DescriptionEditorTestCase(unittest.TestCase):
:return: None.
"""
from accounting.voucher.utils.description_editor import \
from accounting.journal_entry.utils.description_editor import \
DescriptionEditor
for form in get_form_data(self.csrf_token):
add_voucher(self.client, form)
add_journal_entry(self.client, form)
with self.app.app_context():
editor: DescriptionEditor = DescriptionEditor()
@ -160,22 +160,22 @@ class DescriptionEditorTestCase(unittest.TestCase):
def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"""Returns the form data for multiple voucher forms.
"""Returns the form data for multiple journal entry forms.
:param csrf_token: The CSRF token.
:return: A list of the form data.
"""
voucher_date: str = date.today().isoformat()
journal_entry_date: str = date.today().isoformat()
return [{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-credit-0-account_code": Accounts.SERVICE,
"currency-0-credit-0-description": " Salary ",
"currency-0-credit-0-amount": "2500"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-description": " Lunch—Fish ",
@ -197,7 +197,7 @@ def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"currency-0-credit-2-amount": "4.25"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-description": " Lunch—Salad ",
@ -213,7 +213,7 @@ def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"currency-0-credit-1-amount": "8.28"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-description": " Lunch—Pizza ",
@ -229,14 +229,14 @@ def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"currency-0-credit-1-amount": "7.47"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-description": " Airplane—Lake City↔Hill Town",
"currency-0-debit-0-amount": "800"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-description": " Bus—323—Downtown→Museum ",
@ -264,7 +264,7 @@ def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"currency-0-credit-3-amount": "4.4"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-description": " Taxi—Museum→Office ",
@ -310,7 +310,7 @@ def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"currency-0-credit-6-amount": "5.5"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date,
"date": journal_entry_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.PETTY_CASH,
"currency-0-debit-0-description": " Dinner—Steak ",

File diff suppressed because it is too large Load Diff

View File

@ -27,12 +27,12 @@ from flask.testing import FlaskCliRunner
from test_site import db
from testlib import create_test_app, get_client
from testlib_offset import TestData, JournalEntryLineItemData, VoucherData, \
CurrencyData
from testlib_voucher import Accounts, match_voucher_detail
from testlib_journal_entry import Accounts, match_journal_entry_detail
from testlib_offset import TestData, JournalEntryLineItemData, \
JournalEntryData, CurrencyData
PREFIX: str = "/accounting/vouchers"
"""The URL prefix for the voucher management."""
PREFIX: str = "/accounting/journal-entries"
"""The URL prefix for the journal entry management."""
class OffsetTestCase(unittest.TestCase):
@ -48,7 +48,7 @@ class OffsetTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
from accounting.models import BaseAccount, JournalEntry, \
JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
@ -62,7 +62,7 @@ class OffsetTestCase(unittest.TestCase):
result = runner.invoke(args=["accounting-init-accounts",
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -73,36 +73,39 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account, Voucher
from accounting.models import Account, JournalEntry
create_uri: str = f"{PREFIX}/create/receipt?next=%2F_next"
store_uri: str = f"{PREFIX}/store/receipt"
form: dict[str, str]
old_amount: Decimal
response: httpx.Response
voucher_data: VoucherData = VoucherData(
self.data.e_r_or3d.voucher.days, [CurrencyData(
journal_entry_data: JournalEntryData = JournalEntryData(
self.data.e_r_or3d.journal_entry.days, [CurrencyData(
"USD",
[],
[JournalEntryLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or1d.description, "300",
original_line_item=self.data.e_r_or1d),
JournalEntryLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or1d.description, "100",
original_line_item=self.data.e_r_or1d),
JournalEntryLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or3d.description, "100",
original_line_item=self.data.e_r_or3d)])])
[JournalEntryLineItemData(
Accounts.RECEIVABLE,
self.data.e_r_or1d.description, "300",
original_line_item=self.data.e_r_or1d),
JournalEntryLineItemData(
Accounts.RECEIVABLE,
self.data.e_r_or1d.description, "100",
original_line_item=self.data.e_r_or1d),
JournalEntryLineItemData(
Accounts.RECEIVABLE,
self.data.e_r_or3d.description, "100",
original_line_item=self.data.e_r_or3d)])])
# Non-existing original line item ID
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] = "9999"
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# The same debit or credit
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] \
= self.data.e_p_or1c.id
form["currency-1-credit-1-account_code"] = self.data.e_p_or1c.account
@ -117,7 +120,7 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = False
db.session.commit()
response = self.client.post(
store_uri, data=voucher_data.new_form(self.csrf_token))
store_uri, data=journal_entry_data.new_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context():
@ -126,7 +129,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original line item is also an offset
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] \
= self.data.e_p_of1d.id
form["currency-1-credit-1-account_code"] = self.data.e_p_of1d.account
@ -135,54 +138,55 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], create_uri)
# Not the same currency
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-credit-1-amount"] \
= str(voucher_data.currencies[0].credit[0].amount
= str(journal_entry_data.currencies[0].credit[0].amount
+ Decimal("0.01"))
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-credit-3-amount"] \
= str(voucher_data.currencies[0].credit[2].amount
= str(journal_entry_data.currencies[0].credit[2].amount
+ Decimal("0.01"))
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Not before the original line items
old_days = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_data.new_form(self.csrf_token)
old_days = journal_entry_data.days
journal_entry_data.days = old_days + 1
form = journal_entry_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Success
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
voucher_id: int = match_voucher_detail(response.headers["Location"])
journal_entry_id: int \
= match_journal_entry_detail(response.headers["Location"])
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].credit:
journal_entry = db.session.get(JournalEntry, journal_entry_id)
for offset in journal_entry.currencies[0].credit:
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_receivable_offset(self) -> None:
@ -191,27 +195,27 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account
voucher_data: VoucherData = self.data.v_r_of2
edit_uri: str = f"{PREFIX}/{voucher_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{voucher_data.id}/update"
journal_entry_data: JournalEntryData = self.data.v_r_of2
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
form: dict[str, str]
response: httpx.Response
voucher_data.days = self.data.v_r_or2.days
voucher_data.currencies[0].debit[0].amount = Decimal("600")
voucher_data.currencies[0].credit[0].amount = Decimal("600")
voucher_data.currencies[0].debit[2].amount = Decimal("600")
voucher_data.currencies[0].credit[2].amount = Decimal("600")
journal_entry_data.days = self.data.v_r_or2.days
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")
# Non-existing original line item ID
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] = "9999"
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# The same debit or credit
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] \
= self.data.e_p_or1c.id
form["currency-1-credit-1-account_code"] = self.data.e_p_or1c.account
@ -227,7 +231,7 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = False
db.session.commit()
response = self.client.post(
update_uri, data=voucher_data.update_form(self.csrf_token))
update_uri, data=journal_entry_data.update_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
with self.app.app_context():
@ -236,7 +240,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original line item is also an offset
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_line_item_id"] \
= self.data.e_p_of1d.id
form["currency-1-credit-1-account_code"] = self.data.e_p_of1d.account
@ -245,180 +249,187 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
# Not the same currency
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(voucher_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[0].amount
+ Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(voucher_data.currencies[0].credit[0].amount
= str(journal_entry_data.currencies[0].credit[0].amount
+ Decimal("0.01"))
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(voucher_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[2].amount
+ Decimal("0.01"))
form["currency-1-credit-3-amount"] \
= str(voucher_data.currencies[0].credit[2].amount
= str(journal_entry_data.currencies[0].credit[2].amount
+ Decimal("0.01"))
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not before the original line items
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_data.update_form(self.csrf_token)
old_days: int = journal_entry_data.days
journal_entry_data.days = old_days + 1
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Success
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"],
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
def test_edit_receivable_original_line_item(self) -> None:
"""Tests to edit the receivable original line item.
:return: None.
"""
from accounting.models import Voucher
voucher_data: VoucherData = self.data.v_r_or1
edit_uri: str = f"{PREFIX}/{voucher_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{voucher_data.id}/update"
from accounting.models import JournalEntry
journal_entry_data: JournalEntryData = self.data.v_r_or1
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
form: dict[str, str]
response: httpx.Response
voucher_data.days = self.data.v_r_of1.days
voucher_data.currencies[0].debit[0].amount = Decimal("800")
voucher_data.currencies[0].credit[0].amount = Decimal("800")
voucher_data.currencies[0].debit[1].amount = Decimal("3.4")
voucher_data.currencies[0].credit[1].amount = Decimal("3.4")
journal_entry_data.days = self.data.v_r_of1.days
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")
# Not the same currency
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(voucher_data.currencies[0].debit[0].amount - Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[0].amount
- Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(voucher_data.currencies[0].credit[0].amount
= str(journal_entry_data.currencies[0].credit[0].amount
- Decimal("0.01"))
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-2-amount"] \
= str(voucher_data.currencies[0].debit[1].amount - Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[1].amount
- Decimal("0.01"))
form["currency-1-credit-2-amount"] \
= str(voucher_data.currencies[0].credit[1].amount
= str(journal_entry_data.currencies[0].credit[1].amount
- Decimal("0.01"))
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not after the offset items
old_days: int = voucher_data.days
voucher_data.days = old_days - 1
form = voucher_data.update_form(self.csrf_token)
old_days: int = journal_entry_data.days
journal_entry_data.days = old_days - 1
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Not deleting matched original line items
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
del form["currency-1-debit-1-eid"]
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Success
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"],
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
# The original line item is always before the offset item, even when
# they happen in the same day.
with self.app.app_context():
voucher_or: Voucher | None = db.session.get(
Voucher, voucher_data.id)
self.assertIsNotNone(voucher_or)
voucher_of: Voucher | None = db.session.get(
Voucher, self.data.v_r_of1.id)
self.assertIsNotNone(voucher_of)
self.assertEqual(voucher_or.date, voucher_of.date)
self.assertLess(voucher_or.no, voucher_of.no)
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(
JournalEntry, self.data.v_r_of1.id)
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)
def test_add_payable_offset(self) -> None:
"""Tests to add the payable offset.
:return: None.
"""
from accounting.models import Account, Voucher
from accounting.models import Account, JournalEntry
create_uri: str = f"{PREFIX}/create/disbursement?next=%2F_next"
store_uri: str = f"{PREFIX}/store/disbursement"
form: dict[str, str]
response: httpx.Response
voucher_data: VoucherData = VoucherData(
self.data.e_p_or3c.voucher.days, [CurrencyData(
journal_entry_data: JournalEntryData = JournalEntryData(
self.data.e_p_or3c.journal_entry.days, [CurrencyData(
"USD",
[JournalEntryLineItemData(Accounts.PAYABLE,
self.data.e_p_or1c.description, "500",
original_line_item=self.data.e_p_or1c),
JournalEntryLineItemData(Accounts.PAYABLE,
self.data.e_p_or1c.description, "300",
original_line_item=self.data.e_p_or1c),
JournalEntryLineItemData(Accounts.PAYABLE,
self.data.e_p_or3c.description, "120",
original_line_item=self.data.e_p_or3c)],
[JournalEntryLineItemData(
Accounts.PAYABLE,
self.data.e_p_or1c.description, "500",
original_line_item=self.data.e_p_or1c),
JournalEntryLineItemData(
Accounts.PAYABLE,
self.data.e_p_or1c.description, "300",
original_line_item=self.data.e_p_or1c),
JournalEntryLineItemData(
Accounts.PAYABLE,
self.data.e_p_or3c.description, "120",
original_line_item=self.data.e_p_or3c)],
[])])
# Non-existing original line item ID
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] = "9999"
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# The same debit or credit
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] \
= self.data.e_r_or1d.id
form["currency-1-debit-1-account_code"] = self.data.e_r_or1d.account
@ -433,7 +444,7 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = False
db.session.commit()
response = self.client.post(
store_uri, data=voucher_data.new_form(self.csrf_token))
store_uri, data=journal_entry_data.new_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context():
@ -442,7 +453,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original line item is also an offset
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] \
= self.data.e_r_of1c.id
form["currency-1-debit-1-account_code"] = self.data.e_r_of1c.account
@ -451,52 +462,55 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], create_uri)
# Not the same currency
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(voucher_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[0].amount
+ Decimal("0.01"))
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
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(voucher_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[2].amount
+ Decimal("0.01"))
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Not before the original line items
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_data.new_form(self.csrf_token)
old_days: int = journal_entry_data.days
journal_entry_data.days = old_days + 1
form = journal_entry_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Success
form = voucher_data.new_form(self.csrf_token)
form = journal_entry_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
voucher_id: int = match_voucher_detail(response.headers["Location"])
journal_entry_id: int \
= match_journal_entry_detail(response.headers["Location"])
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
journal_entry = db.session.get(JournalEntry, journal_entry_id)
for offset in journal_entry.currencies[0].debit:
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_payable_offset(self) -> None:
@ -504,28 +518,28 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account, Voucher
voucher_data: VoucherData = self.data.v_p_of2
edit_uri: str = f"{PREFIX}/{voucher_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{voucher_data.id}/update"
from accounting.models import Account, JournalEntry
journal_entry_data: JournalEntryData = self.data.v_p_of2
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
form: dict[str, str]
response: httpx.Response
voucher_data.days = self.data.v_p_or2.days
voucher_data.currencies[0].debit[0].amount = Decimal("1100")
voucher_data.currencies[0].credit[0].amount = Decimal("1100")
voucher_data.currencies[0].debit[2].amount = Decimal("900")
voucher_data.currencies[0].credit[2].amount = Decimal("900")
journal_entry_data.days = self.data.v_p_or2.days
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")
# Non-existing original line item ID
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] = "9999"
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# The same debit or credit
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] \
= self.data.e_r_or1d.id
form["currency-1-debit-1-account_code"] = self.data.e_r_or1d.account
@ -541,7 +555,7 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = False
db.session.commit()
response = self.client.post(
update_uri, data=voucher_data.update_form(self.csrf_token))
update_uri, data=journal_entry_data.update_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
with self.app.app_context():
@ -550,7 +564,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original line item is also an offset
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_line_item_id"] \
= self.data.e_r_of1c.id
form["currency-1-debit-1-account_code"] = self.data.e_r_of1c.account
@ -559,58 +573,61 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
# Not the same currency
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(voucher_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[0].amount
+ Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(voucher_data.currencies[0].credit[0].amount
= str(journal_entry_data.currencies[0].credit[0].amount
+ Decimal("0.01"))
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(voucher_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[2].amount
+ Decimal("0.01"))
form["currency-1-credit-3-amount"] \
= str(voucher_data.currencies[0].credit[2].amount
= str(journal_entry_data.currencies[0].credit[2].amount
+ Decimal("0.01"))
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not before the original line items
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_data.update_form(self.csrf_token)
old_days: int = journal_entry_data.days
journal_entry_data.days = old_days + 1
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Success
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
voucher_id: int = match_voucher_detail(response.headers["Location"])
journal_entry_id: int \
= match_journal_entry_detail(response.headers["Location"])
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
journal_entry = db.session.get(JournalEntry, journal_entry_id)
for offset in journal_entry.currencies[0].debit:
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_payable_original_line_item(self) -> None:
@ -618,86 +635,88 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Voucher
voucher_data: VoucherData = self.data.v_p_or1
edit_uri: str = f"{PREFIX}/{voucher_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{voucher_data.id}/update"
from accounting.models import JournalEntry
journal_entry_data: JournalEntryData = self.data.v_p_or1
edit_uri: str = f"{PREFIX}/{journal_entry_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{journal_entry_data.id}/update"
form: dict[str, str]
response: httpx.Response
voucher_data.days = self.data.v_p_of1.days
voucher_data.currencies[0].debit[0].amount = Decimal("1200")
voucher_data.currencies[0].credit[0].amount = Decimal("1200")
voucher_data.currencies[0].debit[1].amount = Decimal("0.9")
voucher_data.currencies[0].credit[1].amount = Decimal("0.9")
journal_entry_data.days = self.data.v_p_of1.days
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")
# Not the same currency
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(voucher_data.currencies[0].debit[0].amount - Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[0].amount
- Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(voucher_data.currencies[0].credit[0].amount
= str(journal_entry_data.currencies[0].credit[0].amount
- Decimal("0.01"))
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
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
form["currency-1-debit-2-amount"] \
= str(voucher_data.currencies[0].debit[1].amount - Decimal("0.01"))
= str(journal_entry_data.currencies[0].debit[1].amount
- Decimal("0.01"))
form["currency-1-credit-2-amount"] \
= str(voucher_data.currencies[0].credit[1].amount
= str(journal_entry_data.currencies[0].credit[1].amount
- Decimal("0.01"))
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not after the offset items
old_days: int = voucher_data.days
voucher_data.days = old_days - 1
form = voucher_data.update_form(self.csrf_token)
old_days: int = journal_entry_data.days
journal_entry_data.days = old_days - 1
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
journal_entry_data.days = old_days
# Not deleting matched original line items
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
del form["currency-1-credit-1-eid"]
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Success
form = voucher_data.update_form(self.csrf_token)
form = journal_entry_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"],
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
f"{PREFIX}/{journal_entry_data.id}?next=%2F_next")
# The original line item is always before the offset item, even when
# they happen in the same day
with self.app.app_context():
voucher_or: Voucher | None = db.session.get(
Voucher, voucher_data.id)
self.assertIsNotNone(voucher_or)
voucher_of: Voucher | None = db.session.get(
Voucher, self.data.v_p_of1.id)
self.assertIsNotNone(voucher_of)
self.assertEqual(voucher_or.date, voucher_of.date)
self.assertLess(voucher_or.no, voucher_of.no)
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(
JournalEntry, self.data.v_p_of1.id)
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)

View File

@ -14,7 +14,7 @@
# 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 common test libraries for the voucher test cases.
"""The common test libraries for the journal entry test cases.
"""
import re
@ -57,10 +57,10 @@ class Accounts:
def get_add_form(csrf_token: str) -> dict[str, str]:
"""Returns the form data to add a new voucher.
"""Returns the form data to add a new journal entry.
:param csrf_token: The CSRF token.
:return: The form data to add a new voucher.
:return: The form data to add a new journal entry.
"""
return {"csrf_token": csrf_token,
"next": NEXT_URI,
@ -124,28 +124,30 @@ def get_add_form(csrf_token: str) -> dict[str, str]:
"note": f"\n \n\n \n{NON_EMPTY_NOTE} \n \n\n "}
def get_unchanged_update_form(voucher_id: int, app: Flask, csrf_token: str) \
-> dict[str, str]:
"""Returns the form data to update a voucher, where the data are not
def get_unchanged_update_form(journal_entry_id: int, app: Flask,
csrf_token: str) -> dict[str, str]:
"""Returns the form data to update a journal entry, where the data are not
changed.
:param voucher_id: The voucher ID.
:param journal_entry_id: The journal entry ID.
:param app: The Flask application.
:param csrf_token: The CSRF token.
:return: The form data to update the voucher, where the data are not
:return: The form data to update the journal entry, where the data are not
changed.
"""
from accounting.models import Voucher, VoucherCurrency
from accounting.models import JournalEntry, JournalEntryCurrency
with app.app_context():
voucher: Voucher | None = db.session.get(Voucher, voucher_id)
assert voucher is not None
currencies: list[VoucherCurrency] = voucher.currencies
journal_entry: JournalEntry | None \
= db.session.get(JournalEntry, journal_entry_id)
assert journal_entry is not None
currencies: list[JournalEntryCurrency] = journal_entry.currencies
form: dict[str, str] = {"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher.date,
"note": " \n \n\n " if voucher.note is None
else f"\n \n\n \n \n{voucher.note} \n\n "}
form: dict[str, str] \
= {"csrf_token": csrf_token,
"next": NEXT_URI,
"date": journal_entry.date,
"note": " \n \n\n " if journal_entry.note is None
else f"\n \n\n \n \n{journal_entry.note} \n\n "}
currency_indices_used: set[int] = set()
currency_no: int = 0
for currency in currencies:
@ -182,7 +184,8 @@ def get_unchanged_update_form(voucher_id: int, app: Flask, csrf_token: str) \
form[f"{prefix}-no"] = str(line_item_no)
form[f"{prefix}-account_code"] = line_item.account.code
form[f"{prefix}-description"] \
= " " if line_item.description is None else f" {line_item.description} "
= " " if line_item.description is None \
else f" {line_item.description} "
form[f"{prefix}-amount"] = str(line_item.amount)
return form
@ -201,21 +204,21 @@ def __get_new_index(indices_used: set[int]) -> int:
return index
def get_update_form(voucher_id: int, app: Flask,
def get_update_form(journal_entry_id: int, app: Flask,
csrf_token: str, is_debit: bool | None) -> dict[str, str]:
"""Returns the form data to update a voucher, where the data are
"""Returns the form data to update a journal entry, where the data are
changed.
:param voucher_id: The voucher ID.
:param journal_entry_id: The journal entry ID.
:param app: The Flask application.
:param csrf_token: The CSRF token.
:param is_debit: True for a cash disbursement voucher, False for a cash
receipt voucher, or None for a transfer voucher
:return: The form data to update the voucher, where the data are
:param is_debit: True for a cash disbursement journal entry, False for a
cash receipt journal entry, or None for a transfer journal entry.
:return: The form data to update the journal entry, where the data are
changed.
"""
form: dict[str, str] = get_unchanged_update_form(
voucher_id, app, csrf_token)
journal_entry_id, app, csrf_token)
# Mess up the line items in a currency
currency_prefix: str = __get_currency_prefix(form, "USD")
@ -263,8 +266,10 @@ def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
form[f"{debit_prefix}{new_index}-amount"] = str(amount)
form[f"{debit_prefix}{new_index}-account_code"] = Accounts.TRAVEL
# Swap the cash and the bank order
key_cash: str = __get_line_item_no_key(form, currency_prefix, Accounts.CASH)
key_bank: str = __get_line_item_no_key(form, currency_prefix, Accounts.BANK)
key_cash: str = __get_line_item_no_key(
form, currency_prefix, Accounts.CASH)
key_bank: str = __get_line_item_no_key(
form, currency_prefix, Accounts.BANK)
form[key_cash], form[key_bank] = form[key_bank], form[key_cash]
return form
@ -302,8 +307,10 @@ def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
form[f"{credit_prefix}{new_index}-amount"] = str(amount)
form[f"{credit_prefix}{new_index}-account_code"] = Accounts.AGENCY
# Swap the service and the interest order
key_srv: str = __get_line_item_no_key(form, currency_prefix, Accounts.SERVICE)
key_int: str = __get_line_item_no_key(form, currency_prefix, Accounts.INTEREST)
key_srv: str = __get_line_item_no_key(
form, currency_prefix, Accounts.SERVICE)
key_int: str = __get_line_item_no_key(
form, currency_prefix, Accounts.INTEREST)
form[key_srv], form[key_int] = form[key_int], form[key_srv]
return form
@ -390,35 +397,35 @@ def __get_currency_prefix(form: dict[str, str], code: str) -> str:
return m.group(1)
def add_voucher(client: httpx.Client, form: dict[str, str]) -> int:
"""Adds a transfer voucher.
def add_journal_entry(client: httpx.Client, form: dict[str, str]) -> int:
"""Adds a transfer journal entry.
:param client: The client.
:param form: The form data.
:return: The newly-added voucher ID.
:return: The newly-added journal entry ID.
"""
prefix: str = "/accounting/vouchers"
voucher_type: str = "transfer"
prefix: str = "/accounting/journal-entries"
journal_entry_type: str = "transfer"
if len({x for x in form if "-debit-" in x}) == 0:
voucher_type = "receipt"
journal_entry_type = "receipt"
elif len({x for x in form if "-credit-" in x}) == 0:
voucher_type = "disbursement"
store_uri = f"{prefix}/store/{voucher_type}"
journal_entry_type = "disbursement"
store_uri = f"{prefix}/store/{journal_entry_type}"
response: httpx.Response = client.post(store_uri, data=form)
assert response.status_code == 302
return match_voucher_detail(response.headers["Location"])
return match_journal_entry_detail(response.headers["Location"])
def match_voucher_detail(location: str) -> int:
"""Validates if the redirect location is the voucher detail, and
returns the voucher ID on success.
def match_journal_entry_detail(location: str) -> int:
"""Validates if the redirect location is the journal entry detail, and
returns the journal entry ID on success.
:param location: The redirect location.
:return: The voucher ID.
:raise AssertionError: When the location is not the voucher detail.
:return: The journal entry ID.
:raise AssertionError: When the location is not the journal entry detail.
"""
m: re.Match = re.match(r"^/accounting/vouchers/(\d+)\?next=%2F_next",
location)
m: re.Match = re.match(
r"^/accounting/journal-entries/(\d+)\?next=%2F_next", location)
assert m is not None
return int(m.group(1))

View File

@ -26,7 +26,8 @@ import httpx
from flask import Flask
from test_site import db
from testlib_voucher import Accounts, match_voucher_detail, NEXT_URI
from testlib_journal_entry import Accounts, match_journal_entry_detail, \
NEXT_URI
class JournalEntryLineItemData:
@ -41,7 +42,7 @@ class JournalEntryLineItemData:
:param amount: The amount.
:param original_line_item: The original journal entry line item.
"""
self.voucher: VoucherData | None = None
self.journal_entry: JournalEntryData | None = None
self.id: int = -1
self.no: int = -1
self.original_line_item: JournalEntryLineItemData | None \
@ -75,11 +76,11 @@ class JournalEntryLineItemData:
class CurrencyData:
"""The voucher currency data."""
"""The journal entry currency data."""
def __init__(self, currency: str, debit: list[JournalEntryLineItemData],
credit: list[JournalEntryLineItemData]):
"""Constructs the voucher currency data.
"""Constructs the journal entry currency data.
:param currency: The currency code.
:param debit: The debit line items.
@ -106,14 +107,14 @@ class CurrencyData:
return form
class VoucherData:
"""The voucher data."""
class JournalEntryData:
"""The journal entry data."""
def __init__(self, days: int, currencies: list[CurrencyData]):
"""Constructs a voucher.
"""Constructs a journal entry.
:param days: The number of days before today.
:param currencies: The voucher currency data.
:param currencies: The journal entry currency data.
"""
self.id: int = -1
self.days: int = days
@ -121,38 +122,38 @@ class VoucherData:
self.note: str | None = None
for currency in self.currencies:
for line_item in currency.debit:
line_item.voucher = self
line_item.journal_entry = self
for line_item in currency.credit:
line_item.voucher = self
line_item.journal_entry = self
def new_form(self, csrf_token: str) -> dict[str, str]:
"""Returns the voucher as a creation form.
"""Returns the journal entry as a creation form.
:param csrf_token: The CSRF token.
:return: The voucher as a creation form.
:return: The journal entry as a creation form.
"""
return self.__form(csrf_token, is_update=False)
def update_form(self, csrf_token: str) -> dict[str, str]:
"""Returns the voucher as a update form.
"""Returns the journal entry as an update form.
:param csrf_token: The CSRF token.
:return: The voucher as a update form.
:return: The journal entry as an update form.
"""
return self.__form(csrf_token, is_update=True)
def __form(self, csrf_token: str, is_update: bool = False) \
-> dict[str, str]:
"""Returns the voucher as a form.
"""Returns the journal entry as a form.
:param csrf_token: The CSRF token.
:param is_update: True for an update operation, or False otherwise
:return: The voucher as a form.
:return: The journal entry as a form.
"""
voucher_date: date = date.today() - timedelta(days=self.days)
journal_entry_date: date = date.today() - timedelta(days=self.days)
form: dict[str, str] = {"csrf_token": csrf_token,
"next": NEXT_URI,
"date": voucher_date.isoformat()}
"date": journal_entry_date.isoformat()}
for i in range(len(self.currencies)):
form.update(self.currencies[i].form(i + 1, is_update))
if self.note is not None:
@ -207,24 +208,24 @@ class TestData:
self.e_p_or4d, self.e_p_or4c = couple(
"Envelop", "0.9", Accounts.OFFICE, Accounts.PAYABLE)
# Original vouchers
self.v_r_or1: VoucherData = VoucherData(
# Original journal entries
self.v_r_or1: JournalEntryData = JournalEntryData(
50, [CurrencyData("USD", [self.e_r_or1d, self.e_r_or4d],
[self.e_r_or1c, self.e_r_or4c])])
self.v_r_or2: VoucherData = VoucherData(
self.v_r_or2: JournalEntryData = JournalEntryData(
30, [CurrencyData("USD", [self.e_r_or2d, self.e_r_or3d],
[self.e_r_or2c, self.e_r_or3c])])
self.v_p_or1: VoucherData = VoucherData(
self.v_p_or1: JournalEntryData = JournalEntryData(
40, [CurrencyData("USD", [self.e_p_or1d, self.e_p_or4d],
[self.e_p_or1c, self.e_p_or4c])])
self.v_p_or2: VoucherData = VoucherData(
self.v_p_or2: JournalEntryData = JournalEntryData(
20, [CurrencyData("USD", [self.e_p_or2d, self.e_p_or3d],
[self.e_p_or2c, self.e_p_or3c])])
self.__add_voucher(self.v_r_or1)
self.__add_voucher(self.v_r_or2)
self.__add_voucher(self.v_p_or1)
self.__add_voucher(self.v_p_or2)
self.__add_journal_entry(self.v_r_or1)
self.__add_journal_entry(self.v_r_or2)
self.__add_journal_entry(self.v_p_or1)
self.__add_journal_entry(self.v_p_or2)
# Receivable offset items
self.e_r_of1d, self.e_r_of1c = couple(
@ -260,52 +261,55 @@ class TestData:
"Envelop", "0.9", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of5d.original_line_item = self.e_p_or4c
# Offset vouchers
self.v_r_of1: VoucherData = VoucherData(
# Offset journal entries
self.v_r_of1: JournalEntryData = JournalEntryData(
25, [CurrencyData("USD", [self.e_r_of1d], [self.e_r_of1c])])
self.v_r_of2: VoucherData = VoucherData(
self.v_r_of2: JournalEntryData = JournalEntryData(
20, [CurrencyData("USD",
[self.e_r_of2d, self.e_r_of3d, self.e_r_of4d],
[self.e_r_of2c, self.e_r_of3c, self.e_r_of4c])])
self.v_r_of3: VoucherData = VoucherData(
self.v_r_of3: JournalEntryData = JournalEntryData(
15, [CurrencyData("USD", [self.e_r_of5d], [self.e_r_of5c])])
self.v_p_of1: VoucherData = VoucherData(
self.v_p_of1: JournalEntryData = JournalEntryData(
15, [CurrencyData("USD", [self.e_p_of1d], [self.e_p_of1c])])
self.v_p_of2: VoucherData = VoucherData(
self.v_p_of2: JournalEntryData = JournalEntryData(
10, [CurrencyData("USD",
[self.e_p_of2d, self.e_p_of3d, self.e_p_of4d],
[self.e_p_of2c, self.e_p_of3c, self.e_p_of4c])])
self.v_p_of3: VoucherData = VoucherData(
self.v_p_of3: JournalEntryData = JournalEntryData(
5, [CurrencyData("USD", [self.e_p_of5d], [self.e_p_of5c])])
self.__add_voucher(self.v_r_of1)
self.__add_voucher(self.v_r_of2)
self.__add_voucher(self.v_r_of3)
self.__add_voucher(self.v_p_of1)
self.__add_voucher(self.v_p_of2)
self.__add_voucher(self.v_p_of3)
self.__add_journal_entry(self.v_r_of1)
self.__add_journal_entry(self.v_r_of2)
self.__add_journal_entry(self.v_r_of3)
self.__add_journal_entry(self.v_p_of1)
self.__add_journal_entry(self.v_p_of2)
self.__add_journal_entry(self.v_p_of3)
def __add_voucher(self, voucher_data: VoucherData) -> None:
"""Adds a voucher.
def __add_journal_entry(self, journal_entry_data: JournalEntryData) \
-> None:
"""Adds a journal entry.
:param voucher_data: The voucher data.
:param journal_entry_data: The journal entry data.
:return: None.
"""
from accounting.models import Voucher
store_uri: str = "/accounting/vouchers/store/transfer"
from accounting.models import JournalEntry
store_uri: str = "/accounting/journal-entries/store/transfer"
response: httpx.Response = self.client.post(
store_uri, data=voucher_data.new_form(self.csrf_token))
store_uri, data=journal_entry_data.new_form(self.csrf_token))
assert response.status_code == 302
voucher_id: int = match_voucher_detail(response.headers["Location"])
voucher_data.id = voucher_id
journal_entry_id: int \
= match_journal_entry_detail(response.headers["Location"])
journal_entry_data.id = journal_entry_id
with self.app.app_context():
voucher: Voucher | None = db.session.get(Voucher, voucher_id)
assert voucher is not None
for i in range(len(voucher.currencies)):
for j in range(len(voucher.currencies[i].debit)):
voucher_data.currencies[i].debit[j].id \
= voucher.currencies[i].debit[j].id
for j in range(len(voucher.currencies[i].credit)):
voucher_data.currencies[i].credit[j].id \
= voucher.currencies[i].credit[j].id
journal_entry: JournalEntry | None \
= db.session.get(JournalEntry, journal_entry_id)
assert journal_entry is not None
for i in range(len(journal_entry.currencies)):
for j in range(len(journal_entry.currencies[i].debit)):
journal_entry_data.currencies[i].debit[j].id \
= journal_entry.currencies[i].debit[j].id
for j in range(len(journal_entry.currencies[i].credit)):
journal_entry_data.currencies[i].credit[j].id \
= journal_entry.currencies[i].credit[j].id