Added the sample data generation and database reset on the test site for live demonstration.
This commit is contained in:
1 parent
3ca246d3e0
commit
20b0412091
7 files changed
+566
-2
No files matched your search
Executable
+306
@@ -0,0 +1,306 @@
|
||||
#! env python3
|
||||
# The Mia! Accounting Project.
|
||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/9
|
||||
|
||||
# Copyright (c) 2023 imacat.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""The sample data generation.
|
||||
|
||||
"""
|
||||
from datetime import date, timedelta
|
||||
|
||||
import click
|
||||
|
||||
from testlib import Accounts, create_test_app, JournalEntryLineItemData, \
|
||||
JournalEntryCurrencyData, JournalEntryData, \
|
||||
BaseTestData
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("file")
|
||||
def main(file) -> None:
|
||||
"""Creates the sample data and output to a file."""
|
||||
data: SampleData = SampleData(create_test_app(), "editor")
|
||||
with open(file, "wt") as fp:
|
||||
fp.write(data.json())
|
||||
|
||||
|
||||
class SampleData(BaseTestData):
|
||||
"""The sample data."""
|
||||
|
||||
def _init_data(self) -> None:
|
||||
self.__add_recurring()
|
||||
self.__add_offsets()
|
||||
self.__add_meals()
|
||||
|
||||
def __add_recurring(self) -> None:
|
||||
"""Adds the recurring data.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
self.__add_usd_recurring()
|
||||
self.__add_twd_recurring()
|
||||
|
||||
def __add_usd_recurring(self) -> None:
|
||||
"""Adds the recurring data in USD.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
today: date = date.today()
|
||||
days: int
|
||||
year: int
|
||||
month: int
|
||||
|
||||
# Recurring in USD
|
||||
j_date: date = date(today.year - 5, today.month, today.day)
|
||||
j_date = j_date + timedelta(days=(4 - j_date.weekday()))
|
||||
days = (today - j_date).days
|
||||
while True:
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "2600",
|
||||
Accounts.BANK, "Transfer", Accounts.SERVICE, "Payroll")
|
||||
|
||||
days = days - 1
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "1200",
|
||||
Accounts.CASH, None, Accounts.BANK, "Withdraw")
|
||||
days = days - 13
|
||||
|
||||
year = today.year - 5
|
||||
month = today.month
|
||||
while True:
|
||||
month = month + 1
|
||||
if month > 12:
|
||||
year = year + 1
|
||||
month = 1
|
||||
days = (today - date(year, month, 1)).days
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "1800",
|
||||
Accounts.RENT_EXPENSE, "Rent", Accounts.BANK, "Transfer")
|
||||
|
||||
def __add_twd_recurring(self) -> None:
|
||||
"""Adds the recurring data in TWD.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
today: date = date.today()
|
||||
|
||||
year: int = today.year - 5
|
||||
month: int = today.month
|
||||
while True:
|
||||
days: int = (today - date(year, month, 5)).days
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "50000",
|
||||
Accounts.BANK, "薪資轉帳", Accounts.SERVICE, "薪水")
|
||||
|
||||
days = days - 1
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "25000",
|
||||
Accounts.CASH, None, Accounts.BANK, "提款")
|
||||
|
||||
days = days - 4
|
||||
if days < 0:
|
||||
break
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "18000",
|
||||
Accounts.RENT_EXPENSE, "房租", Accounts.BANK, "轉帳")
|
||||
|
||||
month = month + 1
|
||||
if month > 12:
|
||||
year = year + 1
|
||||
month = 1
|
||||
|
||||
def __add_offsets(self) -> None:
|
||||
"""Adds the offset data.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
days: int
|
||||
year: int
|
||||
month: int
|
||||
description: str
|
||||
line_item_or: JournalEntryLineItemData
|
||||
line_item_of: JournalEntryLineItemData
|
||||
|
||||
# Full offset and unmatched in USD
|
||||
description = "Speaking—Institute"
|
||||
line_item_or = JournalEntryLineItemData(
|
||||
Accounts.RECEIVABLE, description, "120")
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
40, [JournalEntryCurrencyData(
|
||||
"USD", [line_item_or], [JournalEntryLineItemData(
|
||||
Accounts.SERVICE, description, "120")])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.RECEIVABLE, description, "120",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
5, [JournalEntryCurrencyData(
|
||||
"USD", [JournalEntryLineItemData(
|
||||
Accounts.BANK, description, "120")],
|
||||
[line_item_of])]))
|
||||
self.__add_journal_entry(
|
||||
30, "USD", "120",
|
||||
Accounts.BANK, description, Accounts.SERVICE, description)
|
||||
|
||||
# Partial offset in USD
|
||||
line_item_or = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "Computer", "1600")
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
60, [JournalEntryCurrencyData(
|
||||
"USD", [JournalEntryLineItemData(
|
||||
Accounts.MACHINERY, "Computer", "1600")],
|
||||
[line_item_or])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "Computer", "800",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
35, [JournalEntryCurrencyData(
|
||||
"USD", [line_item_of], [JournalEntryLineItemData(
|
||||
Accounts.BANK, "Computer", "800")])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "Computer", "400",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
10, [JournalEntryCurrencyData(
|
||||
"USD", [line_item_of], [JournalEntryLineItemData(
|
||||
Accounts.CASH, "Computer", "400")])]))
|
||||
|
||||
# Full offset and unmatched in TWD
|
||||
description = "演講費—母校"
|
||||
line_item_or = JournalEntryLineItemData(
|
||||
Accounts.RECEIVABLE, description, "3000")
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
45, [JournalEntryCurrencyData(
|
||||
"TWD", [line_item_or], [JournalEntryLineItemData(
|
||||
Accounts.SERVICE, description, "3000")])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.RECEIVABLE, description, "3000",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
6, [JournalEntryCurrencyData(
|
||||
"TWD", [JournalEntryLineItemData(
|
||||
Accounts.BANK, description, "3000")],
|
||||
[line_item_of])]))
|
||||
self.__add_journal_entry(
|
||||
25, "TWD", "3000",
|
||||
Accounts.BANK, description, Accounts.SERVICE, description)
|
||||
|
||||
# Partial offset in TWD
|
||||
line_item_or = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "手機", "30000")
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
55, [JournalEntryCurrencyData(
|
||||
"TWD", [JournalEntryLineItemData(
|
||||
Accounts.MACHINERY, "手機", "30000")],
|
||||
[line_item_or])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "手機", "16000",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
27, [JournalEntryCurrencyData(
|
||||
"TWD", [line_item_of], [JournalEntryLineItemData(
|
||||
Accounts.BANK, "手機", "16000")])]))
|
||||
line_item_of = JournalEntryLineItemData(
|
||||
Accounts.PAYABLE, "手機", "6000",
|
||||
original_line_item=line_item_or)
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
8, [JournalEntryCurrencyData(
|
||||
"TWD", [line_item_of], [JournalEntryLineItemData(
|
||||
Accounts.CASH, "手機", "6000")])]))
|
||||
|
||||
def __add_meals(self) -> None:
|
||||
"""Adds the meal data.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
days = 60
|
||||
while days >= 0:
|
||||
# Meals in USD
|
||||
if days % 4 == 2:
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "2.9",
|
||||
Accounts.MEAL, "Lunch—Coffee", Accounts.CASH, None)
|
||||
else:
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "3.9",
|
||||
Accounts.MEAL, "Lunch—Coffee", Accounts.CASH, None)
|
||||
|
||||
if days % 15 == 3:
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "5.45",
|
||||
Accounts.MEAL, "Dinner—Pizza",
|
||||
Accounts.PAYABLE, "Dinner—Pizza")
|
||||
else:
|
||||
self.__add_journal_entry(
|
||||
days, "USD", "5.9",
|
||||
Accounts.MEAL, "Dinner—Pasta", Accounts.CASH, None)
|
||||
|
||||
# Meals in TWD
|
||||
if days % 5 == 3:
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "125",
|
||||
Accounts.MEAL, "午餐—鄰家咖啡", Accounts.CASH, None)
|
||||
else:
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "80",
|
||||
Accounts.MEAL, "午餐—便當", Accounts.CASH, None)
|
||||
|
||||
if days % 15 == 3:
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "320",
|
||||
Accounts.MEAL, "晚餐—牛排", Accounts.PAYABLE, "晚餐—牛排")
|
||||
else:
|
||||
self.__add_journal_entry(
|
||||
days, "TWD", "100",
|
||||
Accounts.MEAL, "晚餐—自助餐", Accounts.CASH, None)
|
||||
|
||||
days = days - 1
|
||||
|
||||
def __add_journal_entry(
|
||||
self, days: int, currency: str, amount: str,
|
||||
debit_account: str, debit_description: str | None,
|
||||
credit_account: str, credit_description: str | None) -> None:
|
||||
"""Adds a simple journal entry.
|
||||
|
||||
:param days: The number of days before today.
|
||||
:param currency: The currency code.
|
||||
:param amount: The amount.
|
||||
:param debit_account: The debit account code.
|
||||
:param debit_description: The debit description.
|
||||
:param credit_account: The credit account code.
|
||||
:param credit_description: The credit description.
|
||||
:return: None.
|
||||
"""
|
||||
self._add_journal_entry(JournalEntryData(
|
||||
days,
|
||||
[JournalEntryCurrencyData(
|
||||
currency,
|
||||
[JournalEntryLineItemData(
|
||||
debit_account, debit_description, amount)],
|
||||
[JournalEntryLineItemData(
|
||||
credit_account, credit_description, amount)])]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -71,6 +71,9 @@ def create_app(is_testing: bool = False) -> Flask:
|
||||
from . import auth
|
||||
auth.init_app(app)
|
||||
|
||||
from . import reset
|
||||
reset.init_app(app)
|
||||
|
||||
class UserUtilities(accounting.UserUtilityInterface[auth.User]):
|
||||
|
||||
def can_view(self) -> bool:
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
[[[253158430,1825,1,null],[501877731,1824,1,null],[460366072,1811,1,null],[554667026,1810,1,null],[702118770,1797,1,null],[550526924,1796,1,null],[421329611,1783,1,null],[979177690,1782,1,null],[835285096,1769,1,null],[601049725,1768,1,null],[598355863,1755,1,null],[971081093,1754,1,null],[526799211,1741,1,null],[999626050,1740,1,null],[941420495,1727,1,null],[552147625,1726,1,null],[782405730,1713,1,null],[573083370,1712,1,null],[945564030,1699,1,null],[425042638,1698,1,null],[824745041,1685,1,null],[404965306,1684,1,null],[892559865,1671,1,null],[129992464,1670,1,null],[741138754,1657,1,null],[982468055,1656,1,null],[907597486,1643,1,null],[176175705,1642,1,null],[780722984,1629,1,null],[861666286,1628,1,null],[323324813,1615,1,null],[290006384,1614,1,null],[230007734,1601,1,null],[457479017,1600,1,null],[391314349,1587,1,null],[943792184,1586,1,null],[650995854,1573,1,null],[399286484,1572,1,null],[441393336,1559,1,null],[218775281,1558,1,null],[311684126,1545,1,null],[849146815,1544,1,null],[607216321,1531,1,null],[837009926,1530,1,null],[225609272,1517,1,null],[369261797,1516,1,null],[523663392,1503,1,null],[980305433,1502,1,null],[295924612,1489,1,null],[996375561,1488,1,null],[609232101,1475,1,null],[134436830,1474,1,null],[709315353,1461,1,null],[421331133,1460,1,null],[884289804,1447,1,null],[684672567,1446,1,null],[530494819,1433,1,null],[376645414,1432,1,null],[699140121,1419,1,null],[207607057,1418,1,null],[756036282,1405,1,null],[411672052,1404,1,null],[297427105,1391,1,null],[779481962,1390,1,null],[319805938,1377,1,null],[935639254,1376,1,null],[820408310,1363,1,null],[463378592,1362,1,null],[417853800,1349,1,null],[192573182,1348,1,null],[861579557,1335,1,null],[287829575,1334,1,null],[116006789,1321,1,null],[695119790,1320,1,null],[814985554,1307,1,null],[662655319,1306,1,null],[705468189,1293,1,null],[385101494,1292,1,null],[395780763,1279,1,null],[198718243,1278,1,null],[509855955,1265,1,null],[619240819,1264,1,null],[854581870,1251,1,null],[770378308,1250,1,null],[104310630,1237,1,null],[901022088,1236,1,null],[225836497,1223,1,null],[548722546,1222,1,null],[470798282,1209,1,null],[503494972,1208,1,null],[122467162,1195,1,null],[243949196,1194,1,null],[778028734,1181,1,null],[399004197,1180,1,null],[317235882,1167,1,null],[923613790,1166,1,null],[127179414,1153,1,null],[961138616,1152,1,null],[269486137,1139,1,null],[450699202,1138,1,null],[141910829,1125,1,null],[285004420,1124,1,null],[267104520,1111,1,null],[751150140,1110,1,null],[647203184,1097,1,null],[578215382,1096,1,null],[875642894,1083,1,null],[393526860,1082,1,null],[697719225,1069,1,null],[757314224,1068,1,null],[535887305,1055,1,null],[835911862,1054,1,null],[426462969,1041,1,null],[165420001,1040,1,null],[381041678,1027,1,null],[361224513,1026,1,null],[959468490,1013,1,null],[698845847,1012,1,null],[419168678,999,1,null],[965141621,998,1,null],[187043935,985,1,null],[495395757,984,1,null],[650712663,971,1,null],[184837703,970,1,null],[905114139,957,1,null],[679435843,956,1,null],[926412500,943,1,null],[779213278,942,1,null],[933197526,929,1,null],[968988396,928,1,null],[457733099,915,1,null],[747103638,914,1,null],[587000640,901,1,null],[348901327,900,1,null],[601996516,887,1,null],[975357738,886,1,null],[892159546,873,1,null],[240506334,872,1,null],[158934461,859,1,null],[926353339,858,1,null],[419292067,845,1,null],[592700748,844,1,null],[499570617,831,1,null],[440712581,830,1,null],[307495448,817,1,null],[659922383,816,1,null],[400761950,803,1,null],[256075005,802,1,null],[654405336,789,1,null],[463813104,788,1,null],[658449826,775,1,null],[642448727,774,1,null],[922361675,761,1,null],[260697412,760,1,null],[210360781,747,1,null],[830709793,746,1,null],[372899620,733,1,null],[814514954,732,1,null],[475514794,719,1,null],[979620534,718,1,null],[573728363,705,1,null],[750271534,704,1,null],[330280926,691,1,null],[795258561,690,1,null],[374806098,677,1,null],[466016585,676,1,null],[641544947,663,1,null],[479820125,662,1,null],[556458833,649,1,null],[519338197,648,1,null],[570813218,635,1,null],[926924928,634,1,null],[398264067,621,1,null],[868647777,620,1,null],[856215680,607,1,null],[232915605,606,1,null],[549497179,593,1,null],[303987776,592,1,null],[528403200,579,1,null],[462665875,578,1,null],[250766497,565,1,null],[941497943,564,1,null],[543704555,551,1,null],[856593345,550,1,null],[635853159,537,1,null],[637438371,536,1,null],[471101487,523,1,null],[378456964,522,1,null],[585765045,509,1,null],[470520170,508,1,null],[738450125,495,1,null],[719191928,494,1,null],[635637148,481,1,null],[497944705,480,1,null],[689631912,467,1,null],[258346266,466,1,null],[987873720,453,1,null],[796762854,452,1,null],[199009028,439,1,null],[174921759,438,1,null],[241879347,425,1,null],[924233611,424,1,null],[830194620,411,1,null],[101021398,410,1,null],[725864650,397,1,null],[302039212,396,1,null],[144421767,383,1,null],[145220702,382,1,null],[687939010,369,1,null],[377539407,368,1,null],[732443521,355,1,null],[318333173,354,1,null],[80Line truncated
|
||||
@@ -0,0 +1,153 @@
|
||||
# The Mia! Accounting Demonstration Website.
|
||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/12
|
||||
|
||||
# Copyright (c) 2023 imacat.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""The data reset for the Mia! Accounting demonstration website.
|
||||
|
||||
"""
|
||||
import json
|
||||
import typing as t
|
||||
from datetime import date, timedelta
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
import sqlalchemy as sa
|
||||
from flask import Flask, Blueprint, url_for, flash, redirect, session, \
|
||||
render_template
|
||||
from flask_babel import lazy_gettext
|
||||
|
||||
from accounting.utils.cast import s
|
||||
from . import db
|
||||
from .auth import User, current_user
|
||||
|
||||
bp: Blueprint = Blueprint("reset", __name__, url_prefix="/")
|
||||
|
||||
|
||||
@bp.get("reset", endpoint="reset-page")
|
||||
def reset() -> str:
|
||||
"""Resets the sample data.
|
||||
|
||||
:return: Redirection to the accounting application.
|
||||
"""
|
||||
return render_template("reset.html")
|
||||
|
||||
|
||||
@bp.post("sample", endpoint="sample")
|
||||
def reset_sample() -> redirect:
|
||||
"""Resets the sample data.
|
||||
|
||||
:return: Redirection to the accounting application.
|
||||
"""
|
||||
__reset_database()
|
||||
__populate_sample_data()
|
||||
flash(s(lazy_gettext(
|
||||
"The sample data are emptied and reset successfully.")), "success")
|
||||
return redirect(url_for("accounting-report.default"))
|
||||
|
||||
|
||||
@bp.post("reset", endpoint="clean-up")
|
||||
def clean_up() -> redirect:
|
||||
"""Clean-up the database data.
|
||||
|
||||
:return: Redirection to the accounting application.
|
||||
"""
|
||||
__reset_database()
|
||||
db.session.commit()
|
||||
flash(s(lazy_gettext("The database is emptied successfully.")), "success")
|
||||
return redirect(url_for("accounting-report.default"))
|
||||
|
||||
|
||||
def __populate_sample_data() -> None:
|
||||
"""Populates the sample data.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
from accounting.models import Account, JournalEntry, JournalEntryLineItem
|
||||
file: Path = Path(__file__).parent / "data" / "sample.json"
|
||||
with open(file) as fp:
|
||||
json_data = json.load(fp)
|
||||
today: date = date.today()
|
||||
user: User | None = current_user()
|
||||
assert user is not None
|
||||
|
||||
def filter_journal_entry(data: list[t.Any]) -> dict[str, t.Any]:
|
||||
"""Filters the journal entry data from JSON.
|
||||
|
||||
:param data: The journal entry data.
|
||||
:return: The journal entry data from JSON.
|
||||
"""
|
||||
return {"id": data[0],
|
||||
"date": today - timedelta(days=data[1]),
|
||||
"no": data[2],
|
||||
"note": data[3],
|
||||
"created_by_id": user.id,
|
||||
"updated_by_id": user.id}
|
||||
|
||||
def filter_line_item(data: list[t.Any]) -> dict[str, t.Any]:
|
||||
"""Filters the journal entry line item data from JSON.
|
||||
|
||||
:param data: The journal entry line item data.
|
||||
:return: The journal entry line item data from JSON.
|
||||
"""
|
||||
return {"id": data[0],
|
||||
"journal_entry_id": data[1],
|
||||
"original_line_item_id": data[2],
|
||||
"is_debit": data[3],
|
||||
"no": data[4],
|
||||
"account_id": Account.find_by_code(data[5]).id,
|
||||
"currency_code": data[6],
|
||||
"description": data[7],
|
||||
"amount": Decimal(data[8])}
|
||||
|
||||
db.session.execute(sa.insert(JournalEntry),
|
||||
[filter_journal_entry(x) for x in json_data[0]])
|
||||
db.session.execute(sa.insert(JournalEntryLineItem),
|
||||
[filter_line_item(x) for x in json_data[1]])
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def __reset_database() -> None:
|
||||
"""Resets the database.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
from accounting.models import Currency, CurrencyL10n, BaseAccount, \
|
||||
BaseAccountL10n, Account, AccountL10n, JournalEntry, \
|
||||
JournalEntryLineItem
|
||||
from accounting.base_account import init_base_accounts_command
|
||||
from accounting.account import init_accounts_command
|
||||
from accounting.currency import init_currencies_command
|
||||
|
||||
JournalEntryLineItem.query.delete()
|
||||
JournalEntry.query.delete()
|
||||
CurrencyL10n.query.delete()
|
||||
Currency.query.delete()
|
||||
AccountL10n.query.delete()
|
||||
Account.query.delete()
|
||||
BaseAccountL10n.query.delete()
|
||||
BaseAccount.query.delete()
|
||||
init_base_accounts_command()
|
||||
init_accounts_command(session["user"])
|
||||
init_currencies_command(session["user"])
|
||||
|
||||
|
||||
def init_app(app: Flask) -> None:
|
||||
"""Initialize the localization.
|
||||
|
||||
:param app: The Flask application.
|
||||
:return: None.
|
||||
"""
|
||||
app.register_blueprint(bp)
|
||||
|
||||
@@ -72,6 +72,14 @@ First written: 2023/1/27
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
{% if current_user().username == "admin" %}
|
||||
<li>
|
||||
<a class="dropdown-item {% if request.endpoint and request.endpoint.startswith("reset.") %} active {% endif %}" href="{{ url_for("reset.reset-page") }}">
|
||||
<i class="fa-solid fa-rotate-right"></i>
|
||||
{{ _("Reset") }}
|
||||
</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</li>
|
||||
{% else %}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
{#
|
||||
The Mia! Accounting Demonstration Website
|
||||
reset.html: The reset page.
|
||||
|
||||
Copyright (c) 2023 imacat.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Author: imacat@mail.imacat.idv.tw (imacat)
|
||||
First written: 2023/4/12
|
||||
#}
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block header %}{% block title %}{{ _("Reset Database") }}{% endblock %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<p>{{ _("Warning: All the current accounting data will be deleted. This cannot be undone. Please backup your database first.") }}</p>
|
||||
|
||||
<p>{{ _("Database reset is provided by the live demonstration. This is not part of the Mia! Accounting project.") }}</p>
|
||||
|
||||
<form class="mb-2" action="{{ url_for("reset.clean-up") }}" method="post">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
{% if request.args.next %}
|
||||
<input type="hidden" name="next" value="{{ request.args.next }}">
|
||||
{% endif %}
|
||||
<button class="btn btn-primary" type="submit">{{ _("Empty the Database") }}</button>
|
||||
</form>
|
||||
|
||||
<form class="mb-2" action="{{ url_for("reset.sample") }}" method="post">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
{% if request.args.next %}
|
||||
<input type="hidden" name="next" value="{{ request.args.next }}">
|
||||
{% endif %}
|
||||
<button class="btn btn-primary" type="submit">{{ _("Empty and reset the Sample Data") }}</button>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
+47
-2
@@ -19,6 +19,7 @@
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import typing as t
|
||||
from abc import ABC, abstractmethod
|
||||
@@ -47,6 +48,7 @@ class Accounts:
|
||||
BANK: str = "1113-001"
|
||||
NOTES_RECEIVABLE: str = "1131-001"
|
||||
RECEIVABLE: str = "1141-001"
|
||||
MACHINERY: str = "1441-001"
|
||||
PREPAID: str = "1258-001"
|
||||
NOTES_PAYABLE: str = "2131-001"
|
||||
PAYABLE: str = "2141-001"
|
||||
@@ -166,7 +168,7 @@ def match_journal_entry_detail(location: str) -> int:
|
||||
class JournalEntryLineItemData:
|
||||
"""The journal entry line item data."""
|
||||
|
||||
def __init__(self, account: str, description: str, amount: str,
|
||||
def __init__(self, account: str, description: str | None, amount: str,
|
||||
original_line_item: JournalEntryLineItemData | None = None):
|
||||
"""Constructs the journal entry line item data.
|
||||
|
||||
@@ -181,7 +183,7 @@ class JournalEntryLineItemData:
|
||||
self.original_line_item: JournalEntryLineItemData | None \
|
||||
= original_line_item
|
||||
self.account: str = account
|
||||
self.description: str = description
|
||||
self.description: str | None = description
|
||||
self.amount: Decimal = Decimal(amount)
|
||||
|
||||
def form(self, prefix: str, debit_credit: str, index: int,
|
||||
@@ -332,6 +334,49 @@ class BaseTestData(ABC):
|
||||
self.__line_items)
|
||||
db.session.commit()
|
||||
|
||||
def json(self) -> str:
|
||||
"""Returns the data as JSON.
|
||||
|
||||
:return: The JSON string.
|
||||
"""
|
||||
from accounting.models import Account
|
||||
today: date = date.today()
|
||||
|
||||
def filter_journal_entry(data: dict[str, t.Any]) -> list[t.Any]:
|
||||
"""Filters the journal entry data for JSON encoding.
|
||||
|
||||
:param data: The journal entry data.
|
||||
:return: The journal entry data for JSON encoding.
|
||||
"""
|
||||
data = data.copy()
|
||||
data["date"] = (today - data["date"]).days
|
||||
del data["created_by_id"]
|
||||
del data["updated_by_id"]
|
||||
return [data[x] for x in ["id", "date", "no", "note"]]
|
||||
|
||||
def filter_line_item(data: dict[str, t.Any]) -> list[t.Any]:
|
||||
"""Filters the journal entry line item data for JSON encoding.
|
||||
|
||||
:param data: The journal entry line item data.
|
||||
:return: The journal entry line item data for JSON encoding.
|
||||
"""
|
||||
data = data.copy()
|
||||
with self.__app.app_context():
|
||||
data["account_id"] \
|
||||
= db.session.get(Account, data["account_id"]).code
|
||||
data["amount"] = str(data["amount"])
|
||||
if "original_line_item_id" not in data:
|
||||
data["original_line_item_id"] = None
|
||||
return [data[x] for x in ["id", "journal_entry_id",
|
||||
"original_line_item_id", "is_debit",
|
||||
"no", "account_id", "currency_code",
|
||||
"description", "amount"]]
|
||||
|
||||
return json.dumps(
|
||||
[[filter_journal_entry(x) for x in self.__journal_entries],
|
||||
[filter_line_item(x) for x in self.__line_items]],
|
||||
ensure_ascii=False, separators=(",", ":"))
|
||||
|
||||
@staticmethod
|
||||
def _couple(description: str, amount: str, debit: str, credit: str) \
|
||||
-> tuple[JournalEntryLineItemData, JournalEntryLineItemData]:
|
||||
|
||||
Reference in new issue
Block a user