Added Populator to the quickly populates the accounting data, and applied it in the accounting_sample command in the accounting application.

This commit is contained in:
依瑪貓 2020-07-22 21:02:04 +08:00
parent 9d188ff96b
commit 554a195b5a
2 changed files with 195 additions and 339 deletions

View File

@ -23,9 +23,8 @@ import random
from django.core.management import BaseCommand, CommandParser from django.core.management import BaseCommand, CommandParser
from django.utils import timezone from django.utils import timezone
from accounting.models import Record, Account, Transaction from accounting.populate import Populator
from mia_core.models import User from mia_core.models import User
from mia_core.utils import new_sn
class Command(BaseCommand): class Command(BaseCommand):
@ -54,165 +53,59 @@ class Command(BaseCommand):
user.updated_by = user user.updated_by = user
user.save() user.save()
Account(sn=new_sn(Account), code="1", title_zh_hant="資產", p = Populator(user)
title_en="assets", title_zh_hans="资产", created_by=user, p.add_accounts((
updated_by=user).save() (1, "資產", "assets", "资产"),
Account(sn=new_sn(Account), code="2", title_zh_hant="負債", (2, "負債", "liabilities", "负债"),
title_en="liabilities", title_zh_hans="负债", created_by=user, (3, "業主權益", "owners equity", "业主权益"),
updated_by=user).save() (4, "營業收入", "operating revenue", "营业收入"),
Account(sn=new_sn(Account), code="3", title_zh_hant="業主權益", (5, "營業成本", "operating costs", "营业成本"),
title_en="owners equity", title_zh_hans="业主权益", (6, "營業費用", "operating expenses", "营业费用"),
created_by=user, updated_by=user).save() (7, "營業外收入及費用",
Account(sn=new_sn(Account), code="4", title_zh_hant="營業收入", "non-operating revenue and expenses, other income (expense)",
title_en="operating revenue", title_zh_hans="营业收入", "营业外收入及费用"),
created_by=user, updated_by=user).save() (8, "所得稅費用(或利益)", "income tax expense (or benefit)",
Account(sn=new_sn(Account), code="5", title_zh_hant="營業成本", "所得税费用(或利益)"),
title_en="operating costs", title_zh_hans="营业成本", (9, "非經常營業損益", "nonrecurring gain or loss", "非经常营业损益"),
created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), code="6", title_zh_hant="營業費用",
title_en="operating expenses", title_zh_hans="营业费用",
created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), code="7", title_zh_hant="營業外收入及費用",
title_en=("non-operating revenue and expenses, "
"other income (expense)"),
title_zh_hans="营业外收入及费用", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), code="8", title_zh_hant="所得稅費用(或利益)",
title_en="income tax expense (or benefit)",
title_zh_hans="所得税费用(或利益)", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), code="9", title_zh_hant="非經常營業損益",
title_en="nonrecurring gain or loss", title_zh_hans="非经常营业损益",
created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="1"), (11, "流動資產", "current assets", "流动资产"),
code="11", title_zh_hant="流動資產", title_en="current assets", (111, "現金及約當現金", "cash and cash equivalents", "现金及约当现金"),
title_zh_hans="流动资产", created_by=user, updated_by=user).save() (1111, "庫存現金", "petty cash/revolving funds", "库存现金"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="11"), (1112, "零用金/週轉金", "cash on hand", "零用金/周转金"),
code="111", title_zh_hant="現金及約當現金", (1113, "銀行存款", "cash in banks", "银行存款"),
title_en="cash and cash equivalents", title_zh_hans="现金及约当现金", (12, "流動資產", "current assets", "流动资产"),
created_by=user, updated_by=user).save() (125, "預付費用", "prepaid expenses", "预付费用"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="111"), (1255, "預付所得稅", "prepaid income tax", "预付所得税"),
code="1111", title_zh_hant="庫存現金", (13, "基金及長期投資", "funds and long-term investments",
title_en="petty cash/revolving funds", title_zh_hans="库存现金", "基金及长期投资"),
created_by=user, updated_by=user).save() (131, "基金", "funds", "基金"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="111"), (1314, "退休基金", "pension fund", "退休基金"),
code="1112", title_zh_hant="零用金/週轉金", title_en="cash on hand",
title_zh_hans="零用金/周转金", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="111"),
code="1113", title_zh_hant="銀行存款", title_en="cash in banks",
title_zh_hans="银行存款", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="1"),
code="12", title_zh_hant="流動資產", title_en="current assets",
title_zh_hans="流动资产", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="12"),
code="125", title_zh_hant="預付費用", title_en="prepaid expenses",
title_zh_hans="预付费用", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="125"),
code="1255", title_zh_hant="預付所得稅", title_en="prepaid income tax",
title_zh_hans="预付所得税", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="1"),
code="13", title_zh_hant="基金及長期投資",
title_en="funds and long-term investments",
title_zh_hans="基金及长期投资",
created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="13"),
code="131", title_zh_hant="基金", title_en="funds",
title_zh_hans="基金", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="131"),
code="1314", title_zh_hant="退休基金", title_en="pension fund",
title_zh_hans="退休基金", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="2"), (21, "流動負債", "current liabilities", "流动负债"),
code="21", title_zh_hant="流動負債", title_en="current liabilities", (214, "應付帳款", "accounts payable", "应付帐款"),
title_zh_hans="流动负债", created_by=user,updated_by=user).save() (2141, "應付帳款", "accounts payable", "应付帐款"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="21"),
code="214", title_zh_hant="應付帳款", title_en="accounts payable",
title_zh_hans="应付帐款", created_by=user,updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="214"),
code="2141", title_zh_hant="應付帳款", title_en="accounts payable",
title_zh_hans="应付帐款", created_by=user,updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="4"), (33, "保留盈餘(或累積虧損)",
code="46", title_zh_hant="勞務收入", title_en="service revenue", "retained earnings (accumulated deficit)", "保留盈余(或累积亏损)"),
title_zh_hans="劳务收入", created_by=user,updated_by=user).save() (335, "未分配盈餘(或累積虧損)",
Account(sn=new_sn(Account), parent=Account.objects.get(code="46"), "retained earnings-unappropriated (or accumulated deficit)",
code="461", title_zh_hant="勞務收入", title_en="service revenue", "未分配盈余(或累积亏损)"),
title_zh_hans="劳务收入", created_by=user,updated_by=user).save() (3351, "累積盈虧", "accumulated profit or loss", "累积盈亏"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="461"), (3353, "本期損益", "net income or loss for current period", "本期损益"),
code="4611", title_zh_hant="勞務收入", title_en="service revenue",
title_zh_hans="劳务收入", created_by=user,updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="3"), (46, "勞務收入", "service revenue", "劳务收入"),
code="33", title_zh_hant="保留盈餘(或累積虧損)", (461, "勞務收入", "service revenue", "劳务收入"),
title_en="retained earnings (accumulated deficit)", (4611, "勞務收入", "service revenue", "劳务收入"),
title_zh_hans="保留盈余(或累积亏损)", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="33"),
code="335", title_zh_hant="未分配盈餘(或累積虧損)",
title_en=("retaid earnings-unappropriated "
"(or accumulated deficit)"),
title_zh_hans="未分配盈余(或累积亏损)", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="335"),
code="3351", title_zh_hant="累積盈虧",
title_en="accumulated profit or loss", title_zh_hans="累积盈亏",
created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="335"),
code="3353", title_zh_hant="本期損益",
title_en="net income or loss for current period",
title_zh_hans="本期损益", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="6"), (62, "管理及總務費用", "general & administrative expenses", "管理及总务费用"),
code="62", title_zh_hant="管理及總務費用", (625, "管理及總務費用", "general & administrative expenses", "管理及总务费用"),
title_en="general & administrative expenses", (6254, "旅費", "travelling expense, travel", "旅费"),
title_zh_hans="管理及总务费用", created_by=user, (626, "管理及總務費用", "general & administrative expenses", "管理及总务费用"),
updated_by=user).save() (6262, "保險費", "insurance (expense)", "保险费"),
Account(sn=new_sn(Account), parent=Account.objects.get(code="62"), (627, "管理及總務費用", "general & administrative expenses", "管理及总务费用"),
code="625", title_zh_hant="管理及總務費用", (6272, "伙食費", "meal (expenses)", "伙食费"),
title_en="general & administrative expenses", (6273, "職工福利", "employee benefits/welfare", "职工福利"),
title_zh_hans="管理及总务费用", created_by=user, ))
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="625"),
code="6254", title_zh_hant="旅費",
title_en="travelling expense, travel",
title_zh_hans="旅费", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="62"),
code="626", title_zh_hant="管理及總務費用",
title_en="general & administrative expenses",
title_zh_hans="管理及总务费用", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="626"),
code="6262", title_zh_hant="保險費",
title_en="insurance (expense)",
title_zh_hans="保险费", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="62"),
code="627", title_zh_hant="管理及總務費用",
title_en="general & administrative expenses",
title_zh_hans="管理及总务费用", created_by=user,
updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="627"),
code="6272", title_zh_hant="伙食費", title_en="meal (enses)",
title_zh_hans="伙食费", created_by=user, updated_by=user).save()
Account(sn=new_sn(Account), parent=Account.objects.get(code="627"),
code="6273", title_zh_hant="職工福利", title_en="employee benefits/welfare",
title_zh_hans="职工福利", created_by=user, updated_by=user).save()
today = timezone.localdate()
cash_account = Account.objects.get(code="1111")
bank_account = Account.objects.get(code="1113")
tax_account = Account.objects.get(code="1255")
pension_account = Account.objects.get(code="1314")
salary_account = Account.objects.get(code="4611")
payable_account = Account.objects.get(code="2141")
travel_account = Account.objects.get(code="6254")
insurance_account = Account.objects.get(code="6262")
meal_account = Account.objects.get(code="6272")
welfare_account = Account.objects.get(code="6273")
income = random.randint(40000, 50000) income = random.randint(40000, 50000)
pension = 882 if income <= 40100\ pension = 882 if income <= 40100\
@ -227,189 +120,45 @@ class Command(BaseCommand):
else 712 else 712
tax = round(income * 0.05) tax = round(income * 0.05)
savings = income - pension - insurance - tax savings = income - pension - insurance - tax
date = today - timezone.timedelta(days=15) date = timezone.localdate() - timezone.timedelta(days=15)
month = (date.replace(day=1) - timezone.timedelta(days=1)).month month = (date.replace(day=1) - timezone.timedelta(days=1)).month
p.add_transfer_transaction(
date,
(("1113", "薪資轉帳", savings),
("1314", F"勞保{month}", pension),
("6262", F"健保{month}", insurance),
("1255", "代扣所得稅", tax)),
(("4611", F"{month}月份薪水", income),))
transaction = Transaction(sn=new_sn(Transaction), p.add_income_transaction(
date=today - timezone.timedelta(days=15), -15,
ord=1, created_by=user, updated_by=user) (("1113", "ATM提款", 2000),))
transaction.save() p.add_transfer_transaction(
transaction.record_set.create(sn=new_sn(Record), is_credit=False, -14,
ord=1, (("6254", "高鐵—台北→左營", 1490),),
account=bank_account, (("2141", "高鐵—台北→左營", 1490),))
summary="薪資轉帳", amount=savings, p.add_transfer_transaction(
created_by=user, updated_by=user) -14,
transaction.record_set.create(sn=new_sn(Record), is_credit=False, (("6273", "電影—復仇者聯盟", 80),),
ord=2, (("2141", "電影—復仇者聯盟", 80),))
account=pension_account, p.add_transfer_transaction(
summary=F"勞保{month}", -11,
amount=pension, (("2141", "電影—復仇者聯盟", 80),),
created_by=user, updated_by=user) (("1113", "電影—復仇者聯盟", 80),))
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=3,
account=insurance_account,
summary=F"健保{month}",
amount=insurance,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=4,
account=tax_account,
summary="代扣所得稅",
amount=tax,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True,
ord=1,
account=salary_account,
summary=F"{month}月份薪水",
amount=income,
created_by=user, updated_by=user)
transaction = Transaction(sn=new_sn(Transaction), p.add_income_transaction(
date=today - timezone.timedelta(days=15), -2,
ord=2, created_by=user, updated_by=user) (("6272", "午餐—排骨飯", random.randint(40, 200)),
transaction.save() ("6272", "飲料—紅茶", random.randint(40, 200))))
transaction.record_set.create(sn=new_sn(Record), is_credit=False, p.add_income_transaction(
ord=1, -1,
account=cash_account, (("6272", "午餐—牛肉麵", random.randint(40, 200)),
amount=2000, ("6272", "飲料—紅茶", random.randint(40, 200))))
created_by=user, updated_by=user) p.add_income_transaction(
transaction.record_set.create(sn=new_sn(Record), is_credit=True, -1,
ord=1, (("6272", "午餐—排骨飯", random.randint(40, 200)),
account=bank_account, ("6272", "飲料—冬瓜茶", random.randint(40, 200))))
summary="ATM提款", p.add_income_transaction(
amount=2000, 0,
created_by=user, updated_by=user) (("6272", "午餐—雞腿飯", random.randint(40, 200)),
("6272", "飲料—咖啡", random.randint(40, 200))))
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=14),
ord=1, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=travel_account,
summary="高鐵—台北→左營",
amount=1490,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True,
ord=1,
account=payable_account,
summary="高鐵—台北→左營",
amount=1490,
created_by=user, updated_by=user)
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=14),
ord=2, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=welfare_account,
summary="電影—復仇者聯盟",
amount=80,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True,
ord=1,
account=payable_account,
summary="電影—復仇者聯盟",
amount=80,
created_by=user, updated_by=user)
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=11),
ord=1, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=payable_account,
summary="電影—復仇者聯盟",
amount=80,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True,
ord=1,
account=bank_account,
summary="電影—復仇者聯盟",
amount=80,
created_by=user, updated_by=user)
amount1 = random.randint(40, 200)
amount2 = random.randint(40, 200)
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=2),
ord=1, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=meal_account,
summary="午餐—排骨飯", amount=amount1,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=2,
account=meal_account,
summary="飲料—咖啡", amount=amount2,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True, ord=1,
account=cash_account,
amount=amount1 + amount2,
created_by=user, updated_by=user)
amount1 = random.randint(40, 200)
amount2 = random.randint(40, 200)
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=1),
ord=1, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=meal_account,
summary="午餐—牛肉麵", amount=amount1,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=2,
account=meal_account,
summary="飲料—紅茶", amount=amount2,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True, ord=1,
account=cash_account,
amount=amount1 + amount2,
created_by=user, updated_by=user)
amount1 = random.randint(40, 200)
amount2 = random.randint(40, 200)
transaction = Transaction(sn=new_sn(Transaction),
date=today - timezone.timedelta(days=1),
ord=2, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=meal_account,
summary="午餐—排骨飯", amount=amount1,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=2,
account=meal_account,
summary="飲料—冬瓜茶", amount=amount2,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True, ord=1,
account=cash_account,
amount=amount1 + amount2,
created_by=user, updated_by=user)
amount1 = random.randint(40, 200)
amount2 = random.randint(40, 200)
transaction = Transaction(sn=new_sn(Transaction), date=today,
ord=1, created_by=user, updated_by=user)
transaction.save()
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=1,
account=meal_account,
summary="午餐—雞腿飯", amount=amount1,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=2,
account=meal_account,
summary="飲料—咖啡", amount=amount2,
created_by=user, updated_by=user)
transaction.record_set.create(sn=new_sn(Record), is_credit=True, ord=1,
account=cash_account,
amount=amount1 + amount2,
created_by=user, updated_by=user)

107
accounting/populate.py Normal file
View File

@ -0,0 +1,107 @@
from django.utils import timezone
from accounting.models import Transaction, Record, Account
from mia_core.models import User
from mia_core.utils import new_sn
class Populator:
"""The helper to populate the accounting data.
Args:
user (User): The user in action.
Attributes:
user (User): The user in action.
"""
user = None
def __init__(self, user):
self.user = user
def add_accounts(self, accounts):
"""Adds accounts.
Args:
accounts (tuple[tuple[any]]): Tuples of
(code, Traditional Chinese, English, Simplified Chinese)
of the accounts.
"""
for data in accounts:
code = data[0]
if isinstance(code, int):
code = str(code)
parent = None if len(code) == 1\
else Account.objects.get(code=code[:-1])
Account(sn=new_sn(Account), parent=parent, code=code,
title_zh_hant=data[1], title_en=data[2],
title_zh_hans=data[3],
created_by=self.user, updated_by=self.user).save()
def add_transfer_transaction(self, date, debit, credit):
"""Adds a transfer transaction.
Args:
date (datetime.date|int): The date, or the number of days from
today.
debit (tuple[tuple[any]]): Tuples of (account, summary, amount)
of the debit records.
credit (tuple[tuple[any]]): Tuples of (account, summary, amount)
of the credit records.
"""
if isinstance(date, int):
date = timezone.localdate() + timezone.timedelta(days=date)
order = Transaction.objects.filter(date=date).count() + 1
transaction = Transaction(sn=new_sn(Transaction), date=date, ord=order,
created_by=self.user, updated_by=self.user)
transaction.save()
order = 1
for data in debit:
account = data[0]
if isinstance(account, str):
account = Account.objects.get(code=account)
elif isinstance(account, int):
account = Account.objects.get(code=str(account))
transaction.record_set.create(sn=new_sn(Record), is_credit=False,
ord=order, account=account,
summary=data[1], amount=data[2],
created_by=self.user,
updated_by=self.user)
order = order + 1
order = 1
for data in credit:
account = data[0]
if isinstance(account, str):
account = Account.objects.get(code=account)
elif isinstance(account, int):
account = Account.objects.get(code=str(account))
transaction.record_set.create(sn=new_sn(Record), is_credit=True,
ord=order, account=account,
summary=data[1], amount=data[2],
created_by=self.user,
updated_by=self.user)
order = order + 1
def add_income_transaction(self, date, credit):
"""Adds a cash income transaction.
Args:
date (datetime.date|int): The date, or the number of days from
today.
credit (tuple[tuple[any]]): Tuples of (account, summary, amount) of
the credit records.
"""
amount = sum([x[2] for x in credit])
self.add_transfer_transaction(date, (("1111", None, amount),), credit)
def add_expense_transaction(self, date, debit):
"""Adds a cash income transaction.
Args:
date (datetime.date|int): The date, or the number of days from
today.
debit (tuple[tuple[any]]): Tuples of (account, summary, amount) of
the debit records.
"""
amount = sum([x[2] for x in debit])
self.add_transfer_transaction(date, debit, (("1111", None, amount),))