Skip to content

Commit

Permalink
deduplicated more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Sep 2, 2024
1 parent a0e6b25 commit 70681e6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 114 deletions.
4 changes: 3 additions & 1 deletion entries.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"directory": ".",
"filename": "entries.json",
"entries_before_close": [
{
"debits": [
Expand All @@ -13,7 +15,7 @@
"1500"
]
],
"title": "Shareholder investment"
"title": "Opening balances"
},
{
"debits": [
Expand Down
31 changes: 18 additions & 13 deletions playground/db.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
from decimal import Decimal
from enum import Enum
from pathlib import Path
from typing import Optional

from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select

from ui import Chart

from core import T5, Side


class BaseAccount(SQLModel):
name: str = Field(primary_key=True)
title: str | None = None
name: str = Field(primary_key=True)
title: str | None = None

class IncomeSummaryAccount(BaseAccount, table=True):
...

class RetainedEarningsAccount(BaseAccount, table=True):
...
class IncomeSummaryAccount(BaseAccount, table=True): ...


class RetainedEarningsAccount(BaseAccount, table=True): ...


class RegularAccount(BaseAccount, table=True):
t5: T5


class ContraAccount(BaseAccount, table=True):
offsets: str = Field(foreign_key="regularaccount.name")

offsets: str = Field(foreign_key="regularaccount.name")


class Header(SQLModel, table=True):
count_id: Optional[int] = Field(default=None, primary_key=True)
title: str
Expand Down Expand Up @@ -82,7 +84,6 @@ def query(session):
for entry in results:
print(entry)

from ui import Chart

def load_chart(session):
isa = session.exec(select(IncomeSummaryAccount)).one()
Expand All @@ -92,7 +93,8 @@ def load_chart(session):
chart.add(t=account.t5, account_name=account.name, title=account.title)
for contra_account in session.exec(select(ContraAccount)):
chart.offset(contra_account.offsets, contra_account.name, contra_account.title)
return chart
return chart


def save_chart(session, chart):
isa = IncomeSummaryAccount(name=chart.income_summary_account)
Expand All @@ -103,10 +105,13 @@ def save_chart(session, chart):
a = RegularAccount(name=account, t5=t, title=chart.names.get(account))
session.add(a)
for offset in offsets:
c = ContraAccount(name=offset, offsets=account, title=chart.names.get(offset))
c = ContraAccount(
name=offset, offsets=account, title=chart.names.get(offset)
)
session.add(c)
session.commit()


engine = make_engine(wipe=True)
with Session(engine) as session:
populate(session)
Expand Down
24 changes: 12 additions & 12 deletions playground/example_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
book.chart.set_retained_earnings("re")
book.chart.set_income_summary_account("_isa")
book.open()
book.entries.post("Shareholder investment").amount(1500).debit("cash").credit(
book.entry("Shareholder investment").amount(1500).debit("cash").credit(
"equity"
).commit()
book.account_balances.to_file(filename="balances.json")
Expand All @@ -31,19 +31,19 @@
assert book.ledger["cash"].balance == 1500
assert book.ledger["equity"].balance == 1500
# fmt: off
book.entries.post("Bought inventory (no VAT)").amount(1000).debit("inventory").credit("cash").commit()
book.entries.post("Invoice with 20% VAT").debit("ar", 1200).credit("sales", 1000).credit("vat", 200).commit()
book.entries.post("Registered costs").amount(600).debit("cogs").credit("inventory").commit()
book.entries.post("Accepted payment").amount(900).credit("ar").debit("cash").commit()
book.entries.post("Made refund").amount(50).debit("refunds").credit("ar").commit()
book.entries.post("Made cashback").amount(50).debit("cashback").credit("cash").commit()
book.entries.post("Paid salaries").amount(150).credit("cash").debit("salaries").commit()
book.entry("Bought inventory (no VAT)").amount(1000).debit("inventory").credit("cash").commit()
book.entry("Invoice with 20% VAT").debit("ar", 1200).credit("sales", 1000).credit("vat", 200).commit()
book.entry("Registered costs").amount(600).debit("cogs").credit("inventory").commit()
book.entry("Accepted payment").amount(900).credit("ar").debit("cash").commit()
book.entry("Made refund").amount(50).debit("refunds").credit("ar").commit()
book.entry("Provided cashback").amount(50).debit("cashback").credit("cash").commit()
book.entry("Paid salaries").amount(150).credit("cash").debit("salaries").commit()
assert book.income_statement.net_earnings == 150
book.entries.post("Accrue corporate income tax 20%").amount(30).credit("cit_due").debit("cit").commit()
book.entry("Accrue corporate income tax 20%").amount(30).credit("cit_due").debit("cit").commit()
book.close()
book.entries.post("Bought back shares").amount(30).debit("ts").credit("cash").commit()
book.entries.post("Announced dividend").amount(60).debit("re").credit("dividend").commit()
book.entries.post("Pay corporate income tax").amount(30).debit("cit_due").credit("cash").commit()
book.entry("Bought back shares").amount(30).debit("ts").credit("cash").commit()
book.entry("Announced dividend").amount(60).debit("re").credit("dividend").commit()
book.entry("Pay corporate income tax").amount(30).debit("cit_due").credit("cash").commit()
# fmt: on
print((a := book.income_statement).json())
assert a.dict() == {
Expand Down
14 changes: 7 additions & 7 deletions playground/readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
# Open ledger and post entries
# fmt: off
book.open()
book.entries.post("Shareholder investment").amount(300).debit("cash").credit("equity").commit()
book.entries.post("Bought inventory").amount(250).debit("inventory").credit("cash").commit()
book.entries.post("Invoiced customer").debit("ar", 360).credit("sales", 300).credit("vat", 60).commit()
book.entries.post("Shipped goods").amount(200).debit("cogs").credit("inventory").commit()
book.entries.post("Issued partial refund").debit("refunds", 20).credit("ar", 20).commit()
book.entries.post("Accepted payment").amount(180).debit("cash").credit("ar").commit()
book.entry("Shareholder investment").amount(300).debit("cash").credit("equity").commit()
book.entry("Bought inventory").amount(250).debit("inventory").credit("cash").commit()
book.entry("Invoiced customer").debit("ar", 360).credit("sales", 300).credit("vat", 60).commit()
book.entry("Shipped goods").amount(200).debit("cogs").credit("inventory").commit()
book.entry("Issued partial refund").debit("refunds", 20).credit("ar", 20).commit()
book.entry("Accepted payment").amount(180).debit("cash").credit("ar").commit()
# fmt: on

# Close ledger and make post-close entries
book.close()
book.entries.post("Accrue dividend").amount(40).debit("re").credit("dividend").commit()
book.entry("Accrue dividend").amount(40).debit("re").credit("dividend").commit()

# Show reports
print(book.balance_sheet.json())
Expand Down
34 changes: 16 additions & 18 deletions playground/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_book():
book.chart.add_capital("equity")
book.open()
(
book.entries.post("Shareholder investment")
book.entry("Shareholder investment")
.amount(1500)
.debit("cash")
.credit("equity")
Expand All @@ -57,16 +57,16 @@ def simple_book():
book.chart.add_income("sales")
book.chart.add_expense("salary")
book.open()
book.entries.post("Shareholder investment").amount(1500).debit("cash").credit("equity").commit()
book.entries.post("Provided services").amount(800).debit("cash").credit("sales").commit()
book.entries.post("Paid wages").amount(400).debit("salary").credit("cash").commit()
book.entry("Shareholder investment").amount(1500).debit("cash").credit("equity").commit()
book.entry("Provided services").amount(800).debit("cash").credit("sales").commit()
book.entry("Paid wages").amount(400).debit("salary").credit("cash").commit()
return book
# fmt: on


def test_book_is_closed(simple_book):
assert simple_book.is_closed() is False
assert simple_book.close().is_closed() is True
assert simple_book.entries.is_closed() is False
assert simple_book.close().entries.is_closed() is True


def test_income_statement_before_close(simple_book):
Expand Down Expand Up @@ -120,22 +120,22 @@ def test_trial_balance_after_close(simple_book):


def test_entries_store(tmp_path):
entries = (
EntryStore(directory=tmp_path, filename="test.json")
.post("Start business")
.amount(500)
ne = (
NamedEntry(title="Shareholder investment")
.amount(1500)
.debit("cash")
.credit("equity")
.commit()
)
entries = EntryStore(directory=tmp_path, filename="test.json")
entries.append(ne)
_path = entries.to_file()
print(entries)
print(_path)
assert Path(_path).exists()
assert Path(tmp_path / "test.json").exists()
b = EntryStore.from_file(tmp_path, "test.json")
assert b.entries_before_close[0] == double_entry("cash", "equity", 500).add_title(
"Start business"
assert b.entries_before_close[0] == double_entry("cash", "equity", 1500).add_title(
"Shareholder investment"
)


Expand Down Expand Up @@ -169,8 +169,8 @@ def book_after_close():
book.chart.add_asset("cash")
book.chart.add_income("sales", offsets=["refunds"])
book.open()
book.entries.double_entry("Regiter sales", "cash", "sales", 5).commit()
book.entries.double_entry("Issue refund", "refunds", "cash", 3).commit()
book.double_entry("Regiter sales", "cash", "sales", 5).commit()
book.double_entry("Issue refund", "refunds", "cash", 3).commit()
book.close()
return book

Expand All @@ -190,9 +190,7 @@ def test_opening_balances(tmp_path):
_book.chart.add_asset("cash")
_book.chart.add_capital("equity")
_book.open()
_book.entries.double_entry(
"Shareholder investment", "cash", "equity", 1500
).commit()
_book.double_entry("Shareholder investment", "cash", "equity", 1500).commit()
_book.account_balances.to_file(directory=tmp_path)
_book.save(directory=tmp_path)
book = Book("Duffin Mills")
Expand Down
Loading

0 comments on commit 70681e6

Please sign in to comment.