Renamed "transaction" to "voucher", "cash expense transaction" to "cash disbursement voucher", and "cash income transaction" to "cash receipt voucher".

This commit is contained in:
2023-03-19 13:44:51 +08:00
parent 1e286fbeba
commit 5db13393cc
75 changed files with 1812 additions and 1792 deletions

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, JournalEntryData, TransactionData, \
from testlib_offset import TestData, JournalEntryData, VoucherData, \
CurrencyData
from testlib_txn import Accounts, match_txn_detail
from testlib_voucher import Accounts, match_voucher_detail
PREFIX: str = "/accounting/transactions"
"""The URL prefix for the transaction management."""
PREFIX: str = "/accounting/vouchers"
"""The URL prefix for the voucher 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, Transaction, \
from accounting.models import BaseAccount, Voucher, \
JournalEntry
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)
Transaction.query.delete()
Voucher.query.delete()
JournalEntry.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -73,15 +73,15 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account, Transaction
create_uri: str = f"{PREFIX}/create/income?next=%2F_next"
store_uri: str = f"{PREFIX}/store/income"
from accounting.models import Account, Voucher
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
txn_data: TransactionData = TransactionData(
self.data.e_r_or3d.txn.days, [CurrencyData(
voucher_data: VoucherData = VoucherData(
self.data.e_r_or3d.voucher.days, [CurrencyData(
"USD",
[],
[JournalEntryData(Accounts.RECEIVABLE,
@ -95,14 +95,14 @@ class OffsetTestCase(unittest.TestCase):
original_entry=self.data.e_r_or3d)])])
# Non-existing original entry ID
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_entry_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 side
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = self.data.e_p_or1c.id
form["currency-1-credit-1-account_code"] = self.data.e_p_or1c.account
form["currency-1-credit-1-amount"] = "100"
@ -115,8 +115,8 @@ class OffsetTestCase(unittest.TestCase):
account = Account.find_by_code(Accounts.RECEIVABLE)
account.is_need_offset = False
db.session.commit()
response = self.client.post(store_uri,
data=txn_data.new_form(self.csrf_token))
response = self.client.post(
store_uri, data=voucher_data.new_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context():
@ -125,7 +125,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original entry is also an offset
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = self.data.e_p_of1d.id
form["currency-1-credit-1-account_code"] = self.data.e_p_of1d.account
response = self.client.post(store_uri, data=form)
@ -133,52 +133,54 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], create_uri)
# Not the same currency
form = txn_data.new_form(self.csrf_token)
form = voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-1-amount"] \
= str(txn_data.currencies[0].credit[0].amount + Decimal("0.01"))
= str(voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-3-amount"] \
= str(txn_data.currencies[0].credit[2].amount + Decimal("0.01"))
= str(voucher_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 entries
old_days = txn_data.days
txn_data.days = old_days + 1
form = txn_data.new_form(self.csrf_token)
old_days = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Success
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
txn_id: int = match_txn_detail(response.headers["Location"])
voucher_id: int = match_voucher_detail(response.headers["Location"])
with self.app.app_context():
txn = db.session.get(Transaction, txn_id)
for offset in txn.currencies[0].credit:
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].credit:
self.assertIsNotNone(offset.original_entry_id)
def test_edit_receivable_offset(self) -> None:
@ -187,27 +189,27 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account
txn_data: TransactionData = self.data.t_r_of2
edit_uri: str = f"{PREFIX}/{txn_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{txn_data.id}/update"
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"
form: dict[str, str]
response: httpx.Response
txn_data.days = self.data.t_r_or2.days
txn_data.currencies[0].debit[0].amount = Decimal("600")
txn_data.currencies[0].credit[0].amount = Decimal("600")
txn_data.currencies[0].debit[2].amount = Decimal("600")
txn_data.currencies[0].credit[2].amount = Decimal("600")
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")
# Non-existing original entry ID
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_entry_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 side
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = self.data.e_p_or1c.id
form["currency-1-credit-1-account_code"] = self.data.e_p_or1c.account
form["currency-1-debit-1-amount"] = "100"
@ -221,8 +223,8 @@ class OffsetTestCase(unittest.TestCase):
account = Account.find_by_code(Accounts.RECEIVABLE)
account.is_need_offset = False
db.session.commit()
response = self.client.post(update_uri,
data=txn_data.update_form(self.csrf_token))
response = self.client.post(
update_uri, data=voucher_data.update_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
with self.app.app_context():
@ -231,7 +233,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original entry is also an offset
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = self.data.e_p_of1d.id
form["currency-1-credit-1-account_code"] = self.data.e_p_of1d.account
response = self.client.post(update_uri, data=form)
@ -239,155 +241,159 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
# Not the same currency
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(txn_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(voucher_data.currencies[0].debit[0].amount + Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(txn_data.currencies[0].credit[0].amount + Decimal("0.01"))
= str(voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(txn_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(voucher_data.currencies[0].debit[2].amount + Decimal("0.01"))
form["currency-1-credit-3-amount"] \
= str(txn_data.currencies[0].credit[2].amount + Decimal("0.01"))
= str(voucher_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 entries
old_days: int = txn_data.days
txn_data.days = old_days + 1
form = txn_data.update_form(self.csrf_token)
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Success
form = txn_data.update_form(self.csrf_token)
form = voucher_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}/{txn_data.id}?next=%2F_next")
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
def test_edit_receivable_original_entry(self) -> None:
"""Tests to edit the receivable original entry.
:return: None.
"""
from accounting.models import Transaction
txn_data: TransactionData = self.data.t_r_or1
edit_uri: str = f"{PREFIX}/{txn_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{txn_data.id}/update"
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"
form: dict[str, str]
response: httpx.Response
txn_data.days = self.data.t_r_of1.days
txn_data.currencies[0].debit[0].amount = Decimal("800")
txn_data.currencies[0].credit[0].amount = Decimal("800")
txn_data.currencies[0].debit[1].amount = Decimal("3.4")
txn_data.currencies[0].credit[1].amount = Decimal("3.4")
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")
# Not the same currency
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(txn_data.currencies[0].debit[0].amount - Decimal("0.01"))
= str(voucher_data.currencies[0].debit[0].amount - Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(txn_data.currencies[0].credit[0].amount - Decimal("0.01"))
= str(voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-2-amount"] \
= str(txn_data.currencies[0].debit[1].amount - Decimal("0.01"))
= str(voucher_data.currencies[0].debit[1].amount - Decimal("0.01"))
form["currency-1-credit-2-amount"] \
= str(txn_data.currencies[0].credit[1].amount - Decimal("0.01"))
= str(voucher_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 entries
old_days: int = txn_data.days
txn_data.days = old_days - 1
form = txn_data.update_form(self.csrf_token)
old_days: int = voucher_data.days
voucher_data.days = old_days - 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Not deleting matched original entries
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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}/{txn_data.id}?next=%2F_next")
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
# The original entry is always before the offset entry, even when they
# happen in the same day.
with self.app.app_context():
txn_or: Transaction | None = db.session.get(
Transaction, txn_data.id)
self.assertIsNotNone(txn_or)
txn_of: Transaction | None = db.session.get(
Transaction, self.data.t_r_of1.id)
self.assertIsNotNone(txn_of)
self.assertEqual(txn_or.date, txn_of.date)
self.assertLess(txn_or.no, txn_of.no)
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)
def test_add_payable_offset(self) -> None:
"""Tests to add the payable offset.
:return: None.
"""
from accounting.models import Account, Transaction
create_uri: str = f"{PREFIX}/create/expense?next=%2F_next"
store_uri: str = f"{PREFIX}/store/expense"
from accounting.models import Account, Voucher
create_uri: str = f"{PREFIX}/create/disbursement?next=%2F_next"
store_uri: str = f"{PREFIX}/store/disbursement"
form: dict[str, str]
response: httpx.Response
txn_data: TransactionData = TransactionData(
self.data.e_p_or3c.txn.days, [CurrencyData(
voucher_data: VoucherData = VoucherData(
self.data.e_p_or3c.voucher.days, [CurrencyData(
"USD",
[JournalEntryData(Accounts.PAYABLE,
self.data.e_p_or1c.summary, "500",
@ -401,14 +407,14 @@ class OffsetTestCase(unittest.TestCase):
[])])
# Non-existing original entry ID
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_entry_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 side
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = self.data.e_r_or1d.id
form["currency-1-debit-1-account_code"] = self.data.e_r_or1d.account
form["currency-1-debit-1-amount"] = "100"
@ -421,8 +427,8 @@ class OffsetTestCase(unittest.TestCase):
account = Account.find_by_code(Accounts.PAYABLE)
account.is_need_offset = False
db.session.commit()
response = self.client.post(store_uri,
data=txn_data.new_form(self.csrf_token))
response = self.client.post(
store_uri, data=voucher_data.new_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context():
@ -431,7 +437,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original entry is also an offset
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = self.data.e_r_of1c.id
form["currency-1-debit-1-account_code"] = self.data.e_r_of1c.account
response = self.client.post(store_uri, data=form)
@ -439,52 +445,52 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], create_uri)
# Not the same currency
form = txn_data.new_form(self.csrf_token)
form = voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(txn_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(voucher_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 = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(txn_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(voucher_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 entries
old_days: int = txn_data.days
txn_data.days = old_days + 1
form = txn_data.new_form(self.csrf_token)
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Success
form = txn_data.new_form(self.csrf_token)
form = voucher_data.new_form(self.csrf_token)
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
txn_id: int = match_txn_detail(response.headers["Location"])
voucher_id: int = match_voucher_detail(response.headers["Location"])
with self.app.app_context():
txn = db.session.get(Transaction, txn_id)
for offset in txn.currencies[0].debit:
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
self.assertIsNotNone(offset.original_entry_id)
def test_edit_payable_offset(self) -> None:
@ -492,28 +498,28 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Account, Transaction
txn_data: TransactionData = self.data.t_p_of2
edit_uri: str = f"{PREFIX}/{txn_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{txn_data.id}/update"
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"
form: dict[str, str]
response: httpx.Response
txn_data.days = self.data.t_p_or2.days
txn_data.currencies[0].debit[0].amount = Decimal("1100")
txn_data.currencies[0].credit[0].amount = Decimal("1100")
txn_data.currencies[0].debit[2].amount = Decimal("900")
txn_data.currencies[0].credit[2].amount = Decimal("900")
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")
# Non-existing original entry ID
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_entry_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 side
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = self.data.e_r_or1d.id
form["currency-1-debit-1-account_code"] = self.data.e_r_or1d.account
form["currency-1-debit-1-amount"] = "100"
@ -527,8 +533,8 @@ class OffsetTestCase(unittest.TestCase):
account = Account.find_by_code(Accounts.PAYABLE)
account.is_need_offset = False
db.session.commit()
response = self.client.post(update_uri,
data=txn_data.update_form(self.csrf_token))
response = self.client.post(
update_uri, data=voucher_data.update_form(self.csrf_token))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
with self.app.app_context():
@ -537,7 +543,7 @@ class OffsetTestCase(unittest.TestCase):
db.session.commit()
# The original entry is also an offset
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = self.data.e_r_of1c.id
form["currency-1-debit-1-account_code"] = self.data.e_r_of1c.account
response = self.client.post(update_uri, data=form)
@ -545,56 +551,58 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
# Not the same currency
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(txn_data.currencies[0].debit[0].amount + Decimal("0.01"))
= str(voucher_data.currencies[0].debit[0].amount + Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(txn_data.currencies[0].credit[0].amount + Decimal("0.01"))
= str(voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-3-amount"] \
= str(txn_data.currencies[0].debit[2].amount + Decimal("0.01"))
= str(voucher_data.currencies[0].debit[2].amount + Decimal("0.01"))
form["currency-1-credit-3-amount"] \
= str(txn_data.currencies[0].credit[2].amount + Decimal("0.01"))
= str(voucher_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 entries
old_days: int = txn_data.days
txn_data.days = old_days + 1
form = txn_data.update_form(self.csrf_token)
old_days: int = voucher_data.days
voucher_data.days = old_days + 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Success
form = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
response = self.client.post(update_uri, data=form)
self.assertEqual(response.status_code, 302)
txn_id: int = match_txn_detail(response.headers["Location"])
voucher_id: int = match_voucher_detail(response.headers["Location"])
with self.app.app_context():
txn = db.session.get(Transaction, txn_id)
for offset in txn.currencies[0].debit:
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
self.assertIsNotNone(offset.original_entry_id)
def test_edit_payable_original_entry(self) -> None:
@ -602,84 +610,86 @@ class OffsetTestCase(unittest.TestCase):
:return: None.
"""
from accounting.models import Transaction
txn_data: TransactionData = self.data.t_p_or1
edit_uri: str = f"{PREFIX}/{txn_data.id}/edit?next=%2F_next"
update_uri: str = f"{PREFIX}/{txn_data.id}/update"
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"
form: dict[str, str]
response: httpx.Response
txn_data.days = self.data.t_p_of1.days
txn_data.currencies[0].debit[0].amount = Decimal("1200")
txn_data.currencies[0].credit[0].amount = Decimal("1200")
txn_data.currencies[0].debit[1].amount = Decimal("0.9")
txn_data.currencies[0].credit[1].amount = Decimal("0.9")
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")
# Not the same currency
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-amount"] \
= str(txn_data.currencies[0].debit[0].amount - Decimal("0.01"))
= str(voucher_data.currencies[0].debit[0].amount - Decimal("0.01"))
form["currency-1-credit-1-amount"] \
= str(txn_data.currencies[0].credit[0].amount - Decimal("0.01"))
= str(voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-2-amount"] \
= str(txn_data.currencies[0].debit[1].amount - Decimal("0.01"))
= str(voucher_data.currencies[0].debit[1].amount - Decimal("0.01"))
form["currency-1-credit-2-amount"] \
= str(txn_data.currencies[0].credit[1].amount - Decimal("0.01"))
= str(voucher_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 entries
old_days: int = txn_data.days
txn_data.days = old_days - 1
form = txn_data.update_form(self.csrf_token)
old_days: int = voucher_data.days
voucher_data.days = old_days - 1
form = voucher_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)
txn_data.days = old_days
voucher_data.days = old_days
# Not deleting matched original entries
form = txn_data.update_form(self.csrf_token)
form = voucher_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 = txn_data.update_form(self.csrf_token)
form = voucher_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}/{txn_data.id}?next=%2F_next")
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
# The original entry is always before the offset entry, even when they
# happen in the same day
with self.app.app_context():
txn_or: Transaction | None = db.session.get(
Transaction, txn_data.id)
self.assertIsNotNone(txn_or)
txn_of: Transaction | None = db.session.get(
Transaction, self.data.t_p_of1.id)
self.assertIsNotNone(txn_of)
self.assertEqual(txn_or.date, txn_of.date)
self.assertLess(txn_or.no, txn_of.no)
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)

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_txn import Accounts, NEXT_URI, add_txn
from testlib_voucher import Accounts, NEXT_URI, add_voucher
class SummeryEditorTestCase(unittest.TestCase):
@ -41,7 +41,7 @@ class SummeryEditorTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Transaction, \
from accounting.models import BaseAccount, Voucher, \
JournalEntry
result: Result
result = runner.invoke(args="init-db")
@ -55,7 +55,7 @@ class SummeryEditorTestCase(unittest.TestCase):
result = runner.invoke(args=["accounting-init-accounts",
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Transaction.query.delete()
Voucher.query.delete()
JournalEntry.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -65,9 +65,9 @@ class SummeryEditorTestCase(unittest.TestCase):
:return: None.
"""
from accounting.transaction.utils.summary_editor import SummaryEditor
from accounting.voucher.utils.summary_editor import SummaryEditor
for form in get_form_data(self.csrf_token):
add_txn(self.client, form)
add_voucher(self.client, form)
with self.app.app_context():
editor: SummaryEditor = SummaryEditor()
@ -159,22 +159,22 @@ class SummeryEditorTestCase(unittest.TestCase):
def get_form_data(csrf_token: str) -> list[dict[str, str]]:
"""Returns the form data for multiple transaction forms.
"""Returns the form data for multiple voucher forms.
:param csrf_token: The CSRF token.
:return: A list of the form data.
"""
txn_date: str = date.today().isoformat()
voucher_date: str = date.today().isoformat()
return [{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-credit-0-account_code": Accounts.SERVICE,
"currency-0-credit-0-summary": " Salary ",
"currency-0-credit-0-amount": "2500"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-summary": " Lunch—Fish ",
@ -196,7 +196,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": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-summary": " Lunch—Salad ",
@ -212,7 +212,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": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.MEAL,
"currency-0-debit-0-summary": " Lunch—Pizza ",
@ -228,14 +228,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": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-summary": " Airplane—Lake City↔Hill Town ",
"currency-0-debit-0-amount": "800"},
{"csrf_token": csrf_token,
"next": NEXT_URI,
"date": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-summary": " Bus—323—Downtown→Museum ",
@ -263,7 +263,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": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.TRAVEL,
"currency-0-debit-0-summary": " Taxi—Museum→Office ",
@ -309,7 +309,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": txn_date,
"date": voucher_date,
"currency-0-code": "USD",
"currency-0-debit-0-account_code": Accounts.PETTY_CASH,
"currency-0-debit-0-summary": " Dinner—Steak ",

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@ import httpx
from flask import Flask
from test_site import db
from testlib_txn import Accounts, match_txn_detail, NEXT_URI
from testlib_voucher import Accounts, match_voucher_detail, NEXT_URI
class JournalEntryData:
@ -41,7 +41,7 @@ class JournalEntryData:
:param amount: The amount.
:param original_entry: The original entry.
"""
self.txn: TransactionData | None = None
self.voucher: VoucherData | None = None
self.id: int = -1
self.no: int = -1
self.original_entry: JournalEntryData | None = original_entry
@ -73,11 +73,11 @@ class JournalEntryData:
class CurrencyData:
"""The transaction currency data."""
"""The voucher currency data."""
def __init__(self, currency: str, debit: list[JournalEntryData],
credit: list[JournalEntryData]):
"""Constructs the transaction currency data.
"""Constructs the voucher currency data.
:param currency: The currency code.
:param debit: The debit journal entries.
@ -104,14 +104,14 @@ class CurrencyData:
return form
class TransactionData:
"""The transaction data."""
class VoucherData:
"""The voucher data."""
def __init__(self, days: int, currencies: list[CurrencyData]):
"""Constructs a transaction.
"""Constructs a voucher.
:param days: The number of days before today.
:param currencies: The transaction currency data.
:param currencies: The voucher currency data.
"""
self.id: int = -1
self.days: int = days
@ -119,38 +119,38 @@ class TransactionData:
self.note: str | None = None
for currency in self.currencies:
for entry in currency.debit:
entry.txn = self
entry.voucher = self
for entry in currency.credit:
entry.txn = self
entry.voucher = self
def new_form(self, csrf_token: str) -> dict[str, str]:
"""Returns the transaction as a form.
"""Returns the voucher as a creation form.
:param csrf_token: The CSRF token.
:return: The transaction as a form.
:return: The voucher as a creation form.
"""
return self.__form(csrf_token, is_update=False)
def update_form(self, csrf_token: str) -> dict[str, str]:
"""Returns the transaction as a form.
"""Returns the voucher as a update form.
:param csrf_token: The CSRF token.
:return: The transaction as a form.
:return: The voucher as a 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 transaction as a form.
"""Returns the voucher as a form.
:param csrf_token: The CSRF token.
:param is_update: True for an update operation, or False otherwise
:return: The transaction as a form.
:return: The voucher as a form.
"""
txn_date: date = date.today() - timedelta(days=self.days)
voucher_date: date = date.today() - timedelta(days=self.days)
form: dict[str, str] = {"csrf_token": csrf_token,
"next": NEXT_URI,
"date": txn_date.isoformat()}
"date": voucher_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:
@ -205,24 +205,24 @@ class TestData:
self.e_p_or4d, self.e_p_or4c = couple(
"Envelop", "0.9", Accounts.OFFICE, Accounts.PAYABLE)
# Original transactions
self.t_r_or1: TransactionData = TransactionData(
# Original vouchers
self.v_r_or1: VoucherData = VoucherData(
50, [CurrencyData("USD", [self.e_r_or1d, self.e_r_or4d],
[self.e_r_or1c, self.e_r_or4c])])
self.t_r_or2: TransactionData = TransactionData(
self.v_r_or2: VoucherData = VoucherData(
30, [CurrencyData("USD", [self.e_r_or2d, self.e_r_or3d],
[self.e_r_or2c, self.e_r_or3c])])
self.t_p_or1: TransactionData = TransactionData(
self.v_p_or1: VoucherData = VoucherData(
40, [CurrencyData("USD", [self.e_p_or1d, self.e_p_or4d],
[self.e_p_or1c, self.e_p_or4c])])
self.t_p_or2: TransactionData = TransactionData(
self.v_p_or2: VoucherData = VoucherData(
20, [CurrencyData("USD", [self.e_p_or2d, self.e_p_or3d],
[self.e_p_or2c, self.e_p_or3c])])
self.__add_txn(self.t_r_or1)
self.__add_txn(self.t_r_or2)
self.__add_txn(self.t_p_or1)
self.__add_txn(self.t_p_or2)
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)
# Receivable offset entries
self.e_r_of1d, self.e_r_of1c = couple(
@ -258,52 +258,52 @@ class TestData:
"Envelop", "0.9", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of5d.original_entry = self.e_p_or4c
# Offset transactions
self.t_r_of1: TransactionData = TransactionData(
# Offset vouchers
self.v_r_of1: VoucherData = VoucherData(
25, [CurrencyData("USD", [self.e_r_of1d], [self.e_r_of1c])])
self.t_r_of2: TransactionData = TransactionData(
self.v_r_of2: VoucherData = VoucherData(
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.t_r_of3: TransactionData = TransactionData(
self.v_r_of3: VoucherData = VoucherData(
15, [CurrencyData("USD", [self.e_r_of5d], [self.e_r_of5c])])
self.t_p_of1: TransactionData = TransactionData(
self.v_p_of1: VoucherData = VoucherData(
15, [CurrencyData("USD", [self.e_p_of1d], [self.e_p_of1c])])
self.t_p_of2: TransactionData = TransactionData(
self.v_p_of2: VoucherData = VoucherData(
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.t_p_of3: TransactionData = TransactionData(
self.v_p_of3: VoucherData = VoucherData(
5, [CurrencyData("USD", [self.e_p_of5d], [self.e_p_of5c])])
self.__add_txn(self.t_r_of1)
self.__add_txn(self.t_r_of2)
self.__add_txn(self.t_r_of3)
self.__add_txn(self.t_p_of1)
self.__add_txn(self.t_p_of2)
self.__add_txn(self.t_p_of3)
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)
def __add_txn(self, txn_data: TransactionData) -> None:
"""Adds a transaction.
def __add_voucher(self, voucher_data: VoucherData) -> None:
"""Adds a voucher.
:param txn_data: The transaction data.
:param voucher_data: The voucher data.
:return: None.
"""
from accounting.models import Transaction
store_uri: str = "/accounting/transactions/store/transfer"
from accounting.models import Voucher
store_uri: str = "/accounting/vouchers/store/transfer"
response: httpx.Response = self.client.post(
store_uri, data=txn_data.new_form(self.csrf_token))
store_uri, data=voucher_data.new_form(self.csrf_token))
assert response.status_code == 302
txn_id: int = match_txn_detail(response.headers["Location"])
txn_data.id = txn_id
voucher_id: int = match_voucher_detail(response.headers["Location"])
voucher_data.id = voucher_id
with self.app.app_context():
txn: Transaction | None = db.session.get(Transaction, txn_id)
assert txn is not None
for i in range(len(txn.currencies)):
for j in range(len(txn.currencies[i].debit)):
txn_data.currencies[i].debit[j].id \
= txn.currencies[i].debit[j].id
for j in range(len(txn.currencies[i].credit)):
txn_data.currencies[i].credit[j].id \
= txn.currencies[i].credit[j].id
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

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 transaction test cases.
"""The common test libraries for the voucher 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 transaction.
"""Returns the form data to add a new voucher.
:param csrf_token: The CSRF token.
:return: The form data to add a new transaction.
:return: The form data to add a new voucher.
"""
return {"csrf_token": csrf_token,
"next": NEXT_URI,
@ -124,28 +124,28 @@ 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(txn_id: int, app: Flask, csrf_token: str) \
def get_unchanged_update_form(voucher_id: int, app: Flask, csrf_token: str) \
-> dict[str, str]:
"""Returns the form data to update a transaction, where the data are not
"""Returns the form data to update a voucher, where the data are not
changed.
:param txn_id: The transaction ID.
:param voucher_id: The voucher ID.
:param app: The Flask application.
:param csrf_token: The CSRF token.
:return: The form data to update the transaction, where the data are not
:return: The form data to update the voucher, where the data are not
changed.
"""
from accounting.models import Transaction, TransactionCurrency
from accounting.models import Voucher, VoucherCurrency
with app.app_context():
txn: Transaction | None = db.session.get(Transaction, txn_id)
assert txn is not None
currencies: list[TransactionCurrency] = txn.currencies
voucher: Voucher | None = db.session.get(Voucher, voucher_id)
assert voucher is not None
currencies: list[VoucherCurrency] = voucher.currencies
form: dict[str, str] = {"csrf_token": csrf_token,
"next": NEXT_URI,
"date": txn.date,
"note": " \n \n\n " if txn.note is None
else f"\n \n\n \n \n{txn.note} \n\n "}
"date": voucher.date,
"note": " \n \n\n " if voucher.note is None
else f"\n \n\n \n \n{voucher.note} \n\n "}
currency_indices_used: set[int] = set()
currency_no: int = 0
for currency in currencies:
@ -200,21 +200,21 @@ def __get_new_index(indices_used: set[int]) -> int:
return index
def get_update_form(txn_id: int, app: Flask,
def get_update_form(voucher_id: int, app: Flask,
csrf_token: str, is_debit: bool | None) -> dict[str, str]:
"""Returns the form data to update a transaction, where the data are
"""Returns the form data to update a voucher, where the data are
changed.
:param txn_id: The transaction ID.
:param voucher_id: The voucher ID.
:param app: The Flask application.
:param csrf_token: The CSRF token.
:param is_debit: True for a cash expense transaction, False for a cash
income transaction, or None for a transfer transaction
:return: The form data to update the transaction, where the data are
: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
changed.
"""
form: dict[str, str] = get_unchanged_update_form(
txn_id, app, csrf_token)
voucher_id, app, csrf_token)
# Mess up the entries in a currency
currency_prefix: str = __get_currency_prefix(form, "USD")
@ -240,7 +240,7 @@ def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
key: str
m: re.Match
# Remove the office expense
# Remove the office disbursement
key = [x for x in form
if x.startswith(currency_prefix)
and form[x] == Accounts.OFFICE][0]
@ -249,7 +249,7 @@ def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
entry_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{entry_prefix}amount"])
form = {x: form[x] for x in form if not x.startswith(entry_prefix)}
# Add a new travel expense
# Add a new travel disbursement
indices: set[int] = set()
for key in form:
m = re.match(r"^.+-(\d+)-amount$", key)
@ -279,7 +279,7 @@ def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
key: str
m: re.Match
# Remove the sales income
# Remove the sales receipt
key = [x for x in form
if x.startswith(currency_prefix)
and form[x] == Accounts.SALES][0]
@ -288,7 +288,7 @@ def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
entry_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{entry_prefix}amount"])
form = {x: form[x] for x in form if not x.startswith(entry_prefix)}
# Add a new agency income
# Add a new agency receipt
indices: set[int] = set()
for key in form:
m = re.match(r"^.+-(\d+)-amount$", key)
@ -389,36 +389,35 @@ def __get_currency_prefix(form: dict[str, str], code: str) -> str:
return m.group(1)
def add_txn(client: httpx.Client, form: dict[str, str]) -> int:
"""Adds a transfer transaction.
def add_voucher(client: httpx.Client, form: dict[str, str]) -> int:
"""Adds a transfer voucher.
:param client: The client.
:param form: The form data.
:return: The newly-added transaction ID.
:return: The newly-added voucher ID.
"""
prefix: str = "/accounting/transactions"
txn_type: str = "transfer"
prefix: str = "/accounting/vouchers"
voucher_type: str = "transfer"
if len({x for x in form if "-debit-" in x}) == 0:
txn_type = "income"
voucher_type = "receipt"
elif len({x for x in form if "-credit-" in x}) == 0:
txn_type = "expense"
store_uri = f"{prefix}/store/{txn_type}"
voucher_type = "disbursement"
store_uri = f"{prefix}/store/{voucher_type}"
response: httpx.Response = client.post(store_uri, data=form)
assert response.status_code == 302
return match_txn_detail(response.headers["Location"])
return match_voucher_detail(response.headers["Location"])
def match_txn_detail(location: str) -> int:
"""Validates if the redirect location is the transaction detail, and
returns the transaction ID on success.
def match_voucher_detail(location: str) -> int:
"""Validates if the redirect location is the voucher detail, and
returns the voucher ID on success.
:param location: The redirect location.
:return: The transaction ID.
:raise AssertionError: When the location is not the transaction detail.
:return: The voucher ID.
:raise AssertionError: When the location is not the voucher detail.
"""
m: re.Match = re.match(
r"^/accounting/transactions/(\d+)\?next=%2F_next",
location)
m: re.Match = re.match(r"^/accounting/vouchers/(\d+)\?next=%2F_next",
location)
assert m is not None
return int(m.group(1))