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)