Renamed "journal entry" to "voucher line item", and "entry type" to "side".

This commit is contained in:
2023-03-19 21:00:11 +08:00
parent 25c45b16ae
commit c1235608d8
58 changed files with 1961 additions and 1926 deletions

View File

@ -27,7 +27,7 @@ from flask.testing import FlaskCliRunner
from test_site import db
from testlib import create_test_app, get_client
from testlib_offset import TestData, JournalEntryData, VoucherData, \
from testlib_offset import TestData, VoucherLineItemData, VoucherData, \
CurrencyData
from testlib_voucher import Accounts, match_voucher_detail
@ -49,7 +49,7 @@ class OffsetTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -63,7 +63,7 @@ class OffsetTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
self.data: TestData = TestData(self.app, self.client, self.csrf_token)
@ -84,33 +84,34 @@ class OffsetTestCase(unittest.TestCase):
self.data.e_r_or3d.voucher.days, [CurrencyData(
"USD",
[],
[JournalEntryData(Accounts.RECEIVABLE,
self.data.e_r_or1d.summary, "300",
original_entry=self.data.e_r_or1d),
JournalEntryData(Accounts.RECEIVABLE,
self.data.e_r_or1d.summary, "100",
original_entry=self.data.e_r_or1d),
JournalEntryData(Accounts.RECEIVABLE,
self.data.e_r_or3d.summary, "100",
original_entry=self.data.e_r_or3d)])])
[VoucherLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or1d.summary, "300",
original_line_item=self.data.e_r_or1d),
VoucherLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or1d.summary, "100",
original_line_item=self.data.e_r_or1d),
VoucherLineItemData(Accounts.RECEIVABLE,
self.data.e_r_or3d.summary, "100",
original_line_item=self.data.e_r_or3d)])])
# Non-existing original entry ID
# Non-existing original line item ID
form = voucher_data.new_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = "9999"
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 side
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-original_line_item_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"
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# The original entry does not need offset
# The original line item does not need offset
with self.app.app_context():
account = Account.find_by_code(Accounts.RECEIVABLE)
account.is_need_offset = False
@ -124,9 +125,10 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = True
db.session.commit()
# The original entry is also an offset
# The original line item is also an offset
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-original_line_item_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)
self.assertEqual(response.status_code, 302)
@ -164,7 +166,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Not before the original entries
# 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)
@ -181,7 +183,7 @@ class OffsetTestCase(unittest.TestCase):
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].credit:
self.assertIsNotNone(offset.original_entry_id)
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_receivable_offset(self) -> None:
"""Tests to edit the receivable offset.
@ -201,16 +203,17 @@ class OffsetTestCase(unittest.TestCase):
voucher_data.currencies[0].debit[2].amount = Decimal("600")
voucher_data.currencies[0].credit[2].amount = Decimal("600")
# Non-existing original entry ID
# Non-existing original line item ID
form = voucher_data.update_form(self.csrf_token)
form["currency-1-credit-1-original_entry_id"] = "9999"
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 side
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-original_line_item_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"
form["currency-1-credit-1-amount"] = "100"
@ -218,7 +221,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# The original entry does not need offset
# The original line item does not need offset
with self.app.app_context():
account = Account.find_by_code(Accounts.RECEIVABLE)
account.is_need_offset = False
@ -232,9 +235,10 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = True
db.session.commit()
# The original entry is also an offset
# The original line item is also an offset
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-original_line_item_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)
self.assertEqual(response.status_code, 302)
@ -276,7 +280,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not before the original entries
# 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)
@ -292,8 +296,8 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"],
f"{PREFIX}/{voucher_data.id}?next=%2F_next")
def test_edit_receivable_original_entry(self) -> None:
"""Tests to edit the receivable original entry.
def test_edit_receivable_original_line_item(self) -> None:
"""Tests to edit the receivable original line item.
:return: None.
"""
@ -346,7 +350,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not after the offset entries
# 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)
@ -355,7 +359,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
# Not deleting matched original entries
# Not deleting matched original line items
form = voucher_data.update_form(self.csrf_token)
del form["currency-1-debit-1-eid"]
response = self.client.post(update_uri, data=form)
@ -369,8 +373,8 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"],
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.
# 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)
@ -395,34 +399,35 @@ class OffsetTestCase(unittest.TestCase):
voucher_data: VoucherData = VoucherData(
self.data.e_p_or3c.voucher.days, [CurrencyData(
"USD",
[JournalEntryData(Accounts.PAYABLE,
self.data.e_p_or1c.summary, "500",
original_entry=self.data.e_p_or1c),
JournalEntryData(Accounts.PAYABLE,
self.data.e_p_or1c.summary, "300",
original_entry=self.data.e_p_or1c),
JournalEntryData(Accounts.PAYABLE,
self.data.e_p_or3c.summary, "120",
original_entry=self.data.e_p_or3c)],
[VoucherLineItemData(Accounts.PAYABLE,
self.data.e_p_or1c.summary, "500",
original_line_item=self.data.e_p_or1c),
VoucherLineItemData(Accounts.PAYABLE,
self.data.e_p_or1c.summary, "300",
original_line_item=self.data.e_p_or1c),
VoucherLineItemData(Accounts.PAYABLE,
self.data.e_p_or3c.summary, "120",
original_line_item=self.data.e_p_or3c)],
[])])
# Non-existing original entry ID
# Non-existing original line item ID
form = voucher_data.new_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = "9999"
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 side
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-original_line_item_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"
response = self.client.post(store_uri, data=form)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# The original entry does not need offset
# The original line item does not need offset
with self.app.app_context():
account = Account.find_by_code(Accounts.PAYABLE)
account.is_need_offset = False
@ -436,9 +441,10 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = True
db.session.commit()
# The original entry is also an offset
# The original line item is also an offset
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-original_line_item_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)
self.assertEqual(response.status_code, 302)
@ -474,7 +480,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Not before the original entries
# 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)
@ -491,7 +497,7 @@ class OffsetTestCase(unittest.TestCase):
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
self.assertIsNotNone(offset.original_entry_id)
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_payable_offset(self) -> None:
"""Tests to edit the payable offset.
@ -511,16 +517,17 @@ class OffsetTestCase(unittest.TestCase):
voucher_data.currencies[0].debit[2].amount = Decimal("900")
voucher_data.currencies[0].credit[2].amount = Decimal("900")
# Non-existing original entry ID
# Non-existing original line item ID
form = voucher_data.update_form(self.csrf_token)
form["currency-1-debit-1-original_entry_id"] = "9999"
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 side
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-original_line_item_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"
form["currency-1-credit-1-amount"] = "100"
@ -528,7 +535,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# The original entry does not need offset
# The original line item does not need offset
with self.app.app_context():
account = Account.find_by_code(Accounts.PAYABLE)
account.is_need_offset = False
@ -542,9 +549,10 @@ class OffsetTestCase(unittest.TestCase):
account.is_need_offset = True
db.session.commit()
# The original entry is also an offset
# The original line item is also an offset
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-original_line_item_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)
self.assertEqual(response.status_code, 302)
@ -586,7 +594,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not before the original entries
# 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)
@ -603,10 +611,10 @@ class OffsetTestCase(unittest.TestCase):
with self.app.app_context():
voucher = db.session.get(Voucher, voucher_id)
for offset in voucher.currencies[0].debit:
self.assertIsNotNone(offset.original_entry_id)
self.assertIsNotNone(offset.original_line_item_id)
def test_edit_payable_original_entry(self) -> None:
"""Tests to edit the payable original entry.
def test_edit_payable_original_line_item(self) -> None:
"""Tests to edit the payable original line item.
:return: None.
"""
@ -659,7 +667,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Not after the offset entries
# 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)
@ -668,7 +676,7 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], edit_uri)
voucher_data.days = old_days
# Not deleting matched original entries
# Not deleting matched original line items
form = voucher_data.update_form(self.csrf_token)
del form["currency-1-credit-1-eid"]
response = self.client.post(update_uri, data=form)
@ -682,8 +690,8 @@ class OffsetTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"],
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
# 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)

View File

@ -42,7 +42,7 @@ class SummeryEditorTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -56,7 +56,7 @@ class SummeryEditorTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")

View File

@ -53,7 +53,7 @@ class CashReceiptVoucherTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -67,7 +67,7 @@ class CashReceiptVoucherTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -231,7 +231,7 @@ class CashReceiptVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# A receivable entry cannot start from the credit side
# A receivable line item cannot start from the credit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-credit-" in x][0]
@ -391,7 +391,7 @@ class CashReceiptVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# A receivable entry cannot start from the credit side
# A receivable line item cannot start from the credit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-credit-" in x][0]
@ -625,7 +625,7 @@ class CashDisbursementVoucherTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -639,7 +639,7 @@ class CashDisbursementVoucherTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -803,7 +803,7 @@ class CashDisbursementVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# A payable entry cannot start from the debit side
# A payable line item cannot start from the debit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-debit-" in x][0]
@ -966,7 +966,7 @@ class CashDisbursementVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# A payable entry cannot start from the debit side
# A payable line item cannot start from the debit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-debit-" in x][0]
@ -1204,7 +1204,7 @@ class TransferVoucherTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -1218,7 +1218,7 @@ class TransferVoucherTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")
@ -1398,7 +1398,7 @@ class TransferVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# A receivable entry cannot start from the credit side
# A receivable line item cannot start from the credit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-credit-" in x][0]
@ -1407,7 +1407,7 @@ class TransferVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# A payable entry cannot start from the debit side
# A payable line item cannot start from the debit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-debit-" in x][0]
@ -1597,7 +1597,7 @@ class TransferVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# A receivable entry cannot start from the credit side
# A receivable line item cannot start from the credit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-credit-" in x][0]
@ -1606,7 +1606,7 @@ class TransferVoucherTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# A payable entry cannot start from the debit side
# A payable line item cannot start from the debit side
form = self.__get_add_form()
key: str = [x for x in form.keys()
if x.endswith("-account_code") and "-debit-" in x][0]
@ -2056,7 +2056,7 @@ class VoucherReorderTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount, Voucher, \
JournalEntry
VoucherLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
@ -2070,7 +2070,7 @@ class VoucherReorderTestCase(unittest.TestCase):
"-u", "editor"])
self.assertEqual(result.exit_code, 0)
Voucher.query.delete()
JournalEntry.query.delete()
VoucherLineItem.query.delete()
self.client, self.csrf_token = get_client(self.app, "editor")

View File

@ -29,63 +29,65 @@ from test_site import db
from testlib_voucher import Accounts, match_voucher_detail, NEXT_URI
class JournalEntryData:
"""The journal entry data."""
class VoucherLineItemData:
"""The voucher line item data."""
def __init__(self, account: str, summary: str, amount: str,
original_entry: JournalEntryData | None = None):
"""Constructs the journal entry data.
original_line_item: VoucherLineItemData | None = None):
"""Constructs the voucher line item data.
:param account: The account code.
:param summary: The summary.
:param amount: The amount.
:param original_entry: The original entry.
:param original_line_item: The original voucher line item.
"""
self.voucher: VoucherData | None = None
self.id: int = -1
self.no: int = -1
self.original_entry: JournalEntryData | None = original_entry
self.original_line_item: VoucherLineItemData | None \
= original_line_item
self.account: str = account
self.summary: str = summary
self.amount: Decimal = Decimal(amount)
def form(self, prefix: str, entry_type: str, index: int, is_update: bool) \
def form(self, prefix: str, side: str, index: int, is_update: bool) \
-> dict[str, str]:
"""Returns the journal entry as form data.
"""Returns the line item as form data.
:param prefix: The prefix of the form fields.
:param entry_type: The entry type, either "debit" or "credit".
:param index: The entry index.
:param side: The side, either "debit" or "credit".
:param index: The line item index.
:param is_update: True for an update operation, or False otherwise
:return: The form data.
"""
prefix = f"{prefix}-{entry_type}-{index}"
prefix = f"{prefix}-{side}-{index}"
form: dict[str, str] = {f"{prefix}-account_code": self.account,
f"{prefix}-summary": self.summary,
f"{prefix}-amount": str(self.amount)}
if is_update and self.id != -1:
form[f"{prefix}-eid"] = str(self.id)
form[f"{prefix}-no"] = str(index) if self.no == -1 else str(self.no)
if self.original_entry is not None:
assert self.original_entry.id != -1
form[f"{prefix}-original_entry_id"] = str(self.original_entry.id)
if self.original_line_item is not None:
assert self.original_line_item.id != -1
form[f"{prefix}-original_line_item_id"] \
= str(self.original_line_item.id)
return form
class CurrencyData:
"""The voucher currency data."""
def __init__(self, currency: str, debit: list[JournalEntryData],
credit: list[JournalEntryData]):
def __init__(self, currency: str, debit: list[VoucherLineItemData],
credit: list[VoucherLineItemData]):
"""Constructs the voucher currency data.
:param currency: The currency code.
:param debit: The debit journal entries.
:param credit: The credit journal entries.
:param debit: The debit line items.
:param credit: The credit line items.
"""
self.code: str = currency
self.debit: list[JournalEntryData] = debit
self.credit: list[JournalEntryData] = credit
self.debit: list[VoucherLineItemData] = debit
self.credit: list[VoucherLineItemData] = credit
def form(self, index: int, is_update: bool) -> dict[str, str]:
"""Returns the currency as form data.
@ -118,10 +120,10 @@ class VoucherData:
self.currencies: list[CurrencyData] = currencies
self.note: str | None = None
for currency in self.currencies:
for entry in currency.debit:
entry.voucher = self
for entry in currency.credit:
entry.voucher = self
for line_item in currency.debit:
line_item.voucher = self
for line_item in currency.credit:
line_item.voucher = self
def new_form(self, csrf_token: str) -> dict[str, str]:
"""Returns the voucher as a creation form.
@ -173,19 +175,19 @@ class TestData:
self.csrf_token: str = csrf_token
def couple(summary: str, amount: str, debit: str, credit: str) \
-> tuple[JournalEntryData, JournalEntryData]:
"""Returns a couple of debit-credit journal entries.
-> tuple[VoucherLineItemData, VoucherLineItemData]:
"""Returns a couple of debit-credit line items.
:param summary: The summary.
:param amount: The amount.
:param debit: The debit account code.
:param credit: The credit account code.
:return: The debit journal entry and credit journal entry.
:return: The debit line item and credit line item.
"""
return JournalEntryData(debit, summary, amount),\
JournalEntryData(credit, summary, amount)
return VoucherLineItemData(debit, summary, amount),\
VoucherLineItemData(credit, summary, amount)
# Receivable original entries
# Receivable original line items
self.e_r_or1d, self.e_r_or1c = couple(
"Accountant", "1200", Accounts.RECEIVABLE, Accounts.SERVICE)
self.e_r_or2d, self.e_r_or2c = couple(
@ -195,7 +197,7 @@ class TestData:
self.e_r_or4d, self.e_r_or4c = couple(
"Interest", "3.4", Accounts.RECEIVABLE, Accounts.INTEREST)
# Payable original entries
# Payable original line items
self.e_p_or1d, self.e_p_or1c = couple(
"Airplane", "2000", Accounts.TRAVEL, Accounts.PAYABLE)
self.e_p_or2d, self.e_p_or2c = couple(
@ -224,39 +226,39 @@ class TestData:
self.__add_voucher(self.v_p_or1)
self.__add_voucher(self.v_p_or2)
# Receivable offset entries
# Receivable offset items
self.e_r_of1d, self.e_r_of1c = couple(
"Accountant", "500", Accounts.CASH, Accounts.RECEIVABLE)
self.e_r_of1c.original_entry = self.e_r_or1d
self.e_r_of1c.original_line_item = self.e_r_or1d
self.e_r_of2d, self.e_r_of2c = couple(
"Accountant", "200", Accounts.CASH, Accounts.RECEIVABLE)
self.e_r_of2c.original_entry = self.e_r_or1d
self.e_r_of2c.original_line_item = self.e_r_or1d
self.e_r_of3d, self.e_r_of3c = couple(
"Accountant", "100", Accounts.CASH, Accounts.RECEIVABLE)
self.e_r_of3c.original_entry = self.e_r_or1d
self.e_r_of3c.original_line_item = self.e_r_or1d
self.e_r_of4d, self.e_r_of4c = couple(
"Toy", "240", Accounts.CASH, Accounts.RECEIVABLE)
self.e_r_of4c.original_entry = self.e_r_or2d
self.e_r_of4c.original_line_item = self.e_r_or2d
self.e_r_of5d, self.e_r_of5c = couple(
"Interest", "3.4", Accounts.CASH, Accounts.RECEIVABLE)
self.e_r_of5c.original_entry = self.e_r_or4d
self.e_r_of5c.original_line_item = self.e_r_or4d
# Payable offset entries
# Payable offset items
self.e_p_of1d, self.e_p_of1c = couple(
"Airplane", "800", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of1d.original_entry = self.e_p_or1c
self.e_p_of1d.original_line_item = self.e_p_or1c
self.e_p_of2d, self.e_p_of2c = couple(
"Airplane", "300", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of2d.original_entry = self.e_p_or1c
self.e_p_of2d.original_line_item = self.e_p_or1c
self.e_p_of3d, self.e_p_of3c = couple(
"Airplane", "100", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of3d.original_entry = self.e_p_or1c
self.e_p_of3d.original_line_item = self.e_p_or1c
self.e_p_of4d, self.e_p_of4c = couple(
"Phone", "400", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of4d.original_entry = self.e_p_or2c
self.e_p_of4d.original_line_item = self.e_p_or2c
self.e_p_of5d, self.e_p_of5c = couple(
"Envelop", "0.9", Accounts.PAYABLE, Accounts.CASH)
self.e_p_of5d.original_entry = self.e_p_or4c
self.e_p_of5d.original_line_item = self.e_p_or4c
# Offset vouchers
self.v_r_of1: VoucherData = VoucherData(

View File

@ -154,35 +154,36 @@ def get_unchanged_update_form(voucher_id: int, app: Flask, csrf_token: str) \
currency_prefix: str = f"currency-{currency_index}"
form[f"{currency_prefix}-no"] = str(currency_no)
form[f"{currency_prefix}-code"] = currency.code
entry_indices_used: set[int]
entry_no: int
line_item_indices_used: set[int]
line_item_no: int
prefix: str
entry_indices_used = set()
entry_no = 0
for entry in currency.debit:
entry_index: int = __get_new_index(entry_indices_used)
entry_no = entry_no + 3 + randbelow(3)
prefix = f"{currency_prefix}-debit-{entry_index}"
form[f"{prefix}-eid"] = str(entry.id)
form[f"{prefix}-no"] = str(entry_no)
form[f"{prefix}-account_code"] = entry.account.code
line_item_indices_used = set()
line_item_no = 0
for line_item in currency.debit:
line_item_index: int = __get_new_index(line_item_indices_used)
line_item_no = line_item_no + 3 + randbelow(3)
prefix = f"{currency_prefix}-debit-{line_item_index}"
form[f"{prefix}-eid"] = str(line_item.id)
form[f"{prefix}-no"] = str(line_item_no)
form[f"{prefix}-account_code"] = line_item.account.code
form[f"{prefix}-summary"] \
= " " if entry.summary is None else f" {entry.summary} "
form[f"{prefix}-amount"] = str(entry.amount)
= " " if line_item.summary is None \
else f" {line_item.summary} "
form[f"{prefix}-amount"] = str(line_item.amount)
entry_indices_used = set()
entry_no = 0
for entry in currency.credit:
entry_index: int = __get_new_index(entry_indices_used)
entry_no = entry_no + 3 + randbelow(3)
prefix = f"{currency_prefix}-credit-{entry_index}"
form[f"{prefix}-eid"] = str(entry.id)
form[f"{prefix}-no"] = str(entry_no)
form[f"{prefix}-account_code"] = entry.account.code
line_item_indices_used = set()
line_item_no = 0
for line_item in currency.credit:
line_item_index: int = __get_new_index(line_item_indices_used)
line_item_no = line_item_no + 3 + randbelow(3)
prefix = f"{currency_prefix}-credit-{line_item_index}"
form[f"{prefix}-eid"] = str(line_item.id)
form[f"{prefix}-no"] = str(line_item_no)
form[f"{prefix}-account_code"] = line_item.account.code
form[f"{prefix}-summary"] \
= " " if entry.summary is None else f" {entry.summary} "
form[f"{prefix}-amount"] = str(entry.amount)
= " " if line_item.summary is None else f" {line_item.summary} "
form[f"{prefix}-amount"] = str(line_item.amount)
return form
@ -216,7 +217,7 @@ def get_update_form(voucher_id: int, app: Flask,
form: dict[str, str] = get_unchanged_update_form(
voucher_id, app, csrf_token)
# Mess up the entries in a currency
# Mess up the line items in a currency
currency_prefix: str = __get_currency_prefix(form, "USD")
if is_debit is None or is_debit:
form = __mess_up_debit(form, currency_prefix)
@ -231,7 +232,7 @@ def get_update_form(voucher_id: int, app: Flask,
def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
-> dict[str, str]:
"""Mess up the debit journal entries in the form data.
"""Mess up the debit line items in the form data.
:param form: The form data.
:param currency_prefix: The key prefix of the currency sub-form.
@ -246,9 +247,9 @@ def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
and form[x] == Accounts.OFFICE][0]
m = re.match(r"^((.+-)\d+-)account_code$", key)
debit_prefix: str = m.group(2)
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)}
line_item_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{line_item_prefix}amount"])
form = {x: form[x] for x in form if not x.startswith(line_item_prefix)}
# Add a new travel disbursement
indices: set[int] = set()
for key in form:
@ -262,15 +263,15 @@ 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_entry_no_key(form, currency_prefix, Accounts.CASH)
key_bank: str = __get_entry_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
def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
-> dict[str, str]:
"""Mess up the credit journal entries in the form data.
"""Mess up the credit line items in the form data.
:param form: The form data.
:param currency_prefix: The key prefix of the currency sub-form.
@ -285,9 +286,9 @@ def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
and form[x] == Accounts.SALES][0]
m = re.match(r"^((.+-)\d+-)account_code$", key)
credit_prefix: str = m.group(2)
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)}
line_item_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{line_item_prefix}amount"])
form = {x: form[x] for x in form if not x.startswith(line_item_prefix)}
# Add a new agency receipt
indices: set[int] = set()
for key in form:
@ -301,8 +302,8 @@ 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_entry_no_key(form, currency_prefix, Accounts.SERVICE)
key_int: str = __get_entry_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
@ -361,14 +362,14 @@ def __mess_up_currencies(form: dict[str, str]) -> dict[str, str]:
return form
def __get_entry_no_key(form: dict[str, str], currency_prefix: str,
code: str) -> str:
"""Returns the key of an entry number in the form data.
def __get_line_item_no_key(form: dict[str, str], currency_prefix: str,
code: str) -> str:
"""Returns the key of a line item number in the form data.
:param form: The form data.
:param currency_prefix: The prefix of the currency.
:param code: The code of the account.
:return: The key of the entry number in the form data.
:return: The key of the line item number in the form data.
"""
key: str = [x for x in form
if x.startswith(currency_prefix)
@ -444,7 +445,7 @@ def set_negative_amount(form: dict[str, str]) -> None:
def remove_debit_in_a_currency(form: dict[str, str]) -> None:
"""Removes credit entries in a currency sub-form.
"""Removes debit line items in a currency sub-form.
:param form: The form data.
:return: None.
@ -457,7 +458,7 @@ def remove_debit_in_a_currency(form: dict[str, str]) -> None:
def remove_credit_in_a_currency(form: dict[str, str]) -> None:
"""Removes credit entries in a currency sub-form.
"""Removes credit line items in a currency sub-form.
:param form: The form data.
:return: None.