Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Rename journal to ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Sep 14, 2015
1 parent e8a7fde commit 1d6fc91
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ understand Gratipay, you should start by understanding our schema.

There are three main parts to our schema:

- The ``journal``. Gratipay implements a full-fledged double-entry accounting
system, and the ``journal`` table is at the heart of it.
- The ``ledger``. Gratipay implements a full-fledged double-entry accounting
system, and the ``ledger`` table is at the heart of it.

- **~user**-related tables. The primary table for users is ``participants``.
A number of other tables record additional information related to users,
Expand Down
16 changes: 8 additions & 8 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Payday(object):
transfer_takes
process_draws
settle_card_holds
make_journal_entries
make_ledger_entries
take_over_balances
update_stats
end
Expand Down Expand Up @@ -148,17 +148,17 @@ def payin(self):
self.transfer_takes(cursor, self.ts_start)
self.process_draws(cursor)
entries = cursor.all("""
SELECT * FROM journal WHERE "timestamp" > %s
SELECT * FROM ledger WHERE "timestamp" > %s
""", (self.ts_start,))
try:
self.settle_card_holds(cursor, holds)
self.make_journal_entries(cursor)
self.make_ledger_entries(cursor)
check_db(cursor)
except:
# Dump payments for debugging
import csv
from time import time
with open('%s_journal.csv' % time(), 'wb') as f:
with open('%s_ledger.csv' % time(), 'wb') as f:
csv.writer(f).writerows(entries)
raise
self.take_over_balances()
Expand Down Expand Up @@ -312,12 +312,12 @@ def capture(p):


@staticmethod
def make_journal_entries(cursor):
log("Making journal entries.")
def make_ledger_entries(cursor):
log("Making ledger entries.")
nentries = len(cursor.all("""
INSERT INTO journal
INSERT INTO ledger
(ts, amount, debit, credit, payday)
(SELECT * FROM payday_journal)
(SELECT * FROM payday_ledger)
RETURNING id;
"""))
log("Journal entries recorded: %i." % nentries)
Expand Down
14 changes: 7 additions & 7 deletions sql/branch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ BEGIN;
FOR EACH ROW EXECUTE PROCEDURE create_account_for_team();


-- The Journal
-- The General Ledger

CREATE TABLE journal
CREATE TABLE ledger
( id bigserial PRIMARY KEY
, ts timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
, amount numeric(35, 2) NOT NULL
Expand All @@ -99,12 +99,12 @@ BEGIN;
to_credit = (SELECT participant FROM accounts WHERE id=NEW.credit);

IF (to_debit IS NULL) AND (to_credit IS NULL) THEN
-- No participants involved in this journal entry.
-- No participants involved in this ledger entry.
RETURN NULL;
END IF;

IF (to_debit IS NOT NULL) AND (to_credit IS NOT NULL) THEN
-- Two participants involved in this journal entry!
-- Two participants involved in this ledger entry!
-- This is a bug: we don't allow direct transfers from one ~user to another.
RAISE USING MESSAGE =
'Both ' || to_debit || ' and ' || to_credit || ' are participants.';
Expand All @@ -126,13 +126,13 @@ BEGIN;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_balance AFTER INSERT ON journal
CREATE TRIGGER update_balance AFTER INSERT ON ledger
FOR EACH ROW EXECUTE PROCEDURE update_balance();


-- Journal Notes
-- Ledger Notes

CREATE TABLE journal_notes
CREATE TABLE ledger_notes
( id bigserial PRIMARY KEY
, body text NOT NULL
, author text NOT NULL REFERENCES participants
Expand Down
18 changes: 9 additions & 9 deletions sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ CREATE TRIGGER protect_team_balances AFTER UPDATE ON payday_teams

-- Subscriptions

DROP TABLE IF EXISTS payday_journal_so_far;
CREATE TABLE payday_journal_so_far AS
SELECT * FROM journal WHERE payday = (SELECT id FROM current_payday());
DROP TABLE IF EXISTS payday_ledger_so_far;
CREATE TABLE payday_ledger_so_far AS
SELECT * FROM ledger WHERE payday = (SELECT id FROM current_payday());

DROP TABLE IF EXISTS payday_payment_instructions;
CREATE TABLE payday_payment_instructions AS
Expand All @@ -94,7 +94,7 @@ CREATE TABLE payday_payment_instructions AS
JOIN payday_participants p ON p.username = s.participant
WHERE s.amount > 0
AND ( SELECT id
FROM payday_journal_so_far so_far
FROM payday_ledger_so_far so_far
WHERE so_far.debit = (SELECT id FROM accounts WHERE team=s.team)
AND so_far.credit = (SELECT id FROM accounts WHERE participant=s.participant)
) IS NULL
Expand Down Expand Up @@ -125,8 +125,8 @@ CREATE TABLE payday_takes

-- Journal

DROP TABLE IF EXISTS payday_journal;
CREATE TABLE payday_journal
DROP TABLE IF EXISTS payday_ledger;
CREATE TABLE payday_ledger
( ts timestamptz DEFAULT now()
, amount numeric(35,2) NOT NULL
, debit bigint NOT NULL
Expand Down Expand Up @@ -175,11 +175,11 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER payday_update_balance AFTER INSERT ON payday_journal
CREATE TRIGGER payday_update_balance AFTER INSERT ON payday_ledger
FOR EACH ROW EXECUTE PROCEDURE payday_update_balance();


-- Prepare a statement that makes a journal entry
-- Prepare a statement that makes a ledger entry

CREATE OR REPLACE FUNCTION pay(text, text, numeric, payment_direction)
RETURNS void AS $$
Expand Down Expand Up @@ -214,7 +214,7 @@ RETURNS void AS $$
to_credit := participant_account;
END IF;

INSERT INTO payday_journal
INSERT INTO payday_ledger
(amount, debit, credit)
VALUES ($3, to_debit, to_credit);
END;
Expand Down
28 changes: 14 additions & 14 deletions tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def test_payin_doesnt_make_null_payments(self):
assert not payments


def test_payday_journal_updates_participant_and_team_balances_for_payroll(self):
def test_payday_ledger_updates_participant_and_team_balances_for_payroll(self):
self.make_team(is_approved=True)
assert Participant.from_username('picard').balance == 0

Expand All @@ -420,7 +420,7 @@ def test_payday_journal_updates_participant_and_team_balances_for_payroll(self):

cursor.run("UPDATE payday_teams SET balance=20 WHERE slug='TheEnterprise'")
cursor.run("""
INSERT INTO payday_journal
INSERT INTO payday_ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE team='TheEnterprise')
Expand All @@ -434,7 +434,7 @@ def test_payday_journal_updates_participant_and_team_balances_for_payroll(self):
assert self.db.one("SELECT balance FROM participants "
"WHERE username='picard'") == 0

def test_payday_journal_updates_participant_and_team_balances_for_subscriptions(self):
def test_payday_ledger_updates_participant_and_team_balances_for_subscriptions(self):
self.make_team(is_approved=True)
self.make_participant('alice', claimed_time='now', balance=20)

Expand All @@ -443,7 +443,7 @@ def test_payday_journal_updates_participant_and_team_balances_for_subscriptions(
payday.prepare(cursor)

cursor.run("""
INSERT INTO payday_journal
INSERT INTO payday_ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE participant='alice')
Expand All @@ -455,7 +455,7 @@ def test_payday_journal_updates_participant_and_team_balances_for_subscriptions(
assert cursor.one("SELECT new_balance FROM payday_participants "
"WHERE username='alice'") == D('9.23')

def test_payday_journal_disallows_negative_payday_team_balance(self):
def test_payday_ledger_disallows_negative_payday_team_balance(self):
self.make_team(is_approved=True)

payday = Payday.start()
Expand All @@ -464,15 +464,15 @@ def test_payday_journal_disallows_negative_payday_team_balance(self):
cursor.run("UPDATE payday_teams SET balance=10 WHERE slug='TheEnterprise'")
with pytest.raises(IntegrityError):
cursor.run("""
INSERT INTO payday_journal
INSERT INTO payday_ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE team='TheEnterprise')
, (SELECT id FROM accounts WHERE participant='picard')
)
""")

def test_payday_journal_disallows_negative_payday_participant_balance(self):
def test_payday_ledger_disallows_negative_payday_participant_balance(self):
self.make_team()
self.make_participant('alice', claimed_time='now', balance=10)

Expand All @@ -481,7 +481,7 @@ def test_payday_journal_disallows_negative_payday_participant_balance(self):
payday.prepare(cursor)
with pytest.raises(IntegrityError):
cursor.run("""
INSERT INTO payday_journal
INSERT INTO payday_ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE participant='alice')
Expand All @@ -505,13 +505,13 @@ def test_process_payment_instructions(self):
payday.process_payment_instructions(cursor)
assert cursor.one("select balance from payday_teams where slug='TheEnterprise'") == D('0.51')
assert cursor.one("select balance from payday_teams where slug='TheTrident'") == 0
payday.make_journal_entries(cursor)
payday.make_ledger_entries(cursor)

assert Participant.from_username('alice').balance == D('0.49')
assert Participant.from_username('picard').balance == 0
assert Participant.from_username('shelby').balance == 0

entries = self.db.one("SELECT * FROM journal")
entries = self.db.one("SELECT * FROM ledger")
assert entries.amount == D('0.51')
assert entries.debit == self.db.one("SELECT id FROM accounts WHERE participant='alice'")
assert entries.credit == self.db.one("SELECT id FROM accounts WHERE team='TheEnterprise'")
Expand All @@ -535,7 +535,7 @@ def test_transfer_takes(self):
with self.db.get_cursor() as cursor:
payday.prepare(cursor)
payday.transfer_takes(cursor, payday.ts_start)
payday.make_journal_entries(cursor)
payday.make_ledger_entries(cursor)

participants = self.db.all("SELECT username, balance FROM participants")

Expand Down Expand Up @@ -564,12 +564,12 @@ def test_process_draws(self):
assert cursor.one("select new_balance from payday_participants "
"where username='picard'") == D('0.51')
assert cursor.one("select balance from payday_teams where slug='TheEnterprise'") == 0
assert payday.make_journal_entries(cursor) == 2
assert payday.make_ledger_entries(cursor) == 2

assert Participant.from_id(alice.id).balance == D('0.49')
assert Participant.from_username('picard').balance == D('0.51')

entry = self.db.one("SELECT * FROM journal "
entry = self.db.one("SELECT * FROM ledger "
"WHERE credit=(SELECT id FROM accounts WHERE participant='hannibal')")
assert entry.amount == D('0.51')

Expand Down Expand Up @@ -603,7 +603,7 @@ def test_take_over_during_payin(self):
bruce.delete_elsewhere('twitter', str(bob.id))
billy = self.make_participant('billy', claimed_time='now')
billy.take_over(('github', str(bruce.id)), have_confirmation=True)
payday.make_journal_entries(cursor)
payday.make_ledger_entries(cursor)
payday.take_over_balances()
assert Participant.from_id(bob.id).balance == 0
assert Participant.from_id(bruce.id).balance == 0
Expand Down
26 changes: 13 additions & 13 deletions tests/py/test_journal.py → tests/py/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TestJournal(Harness):

def test_journal_has_system_accounts(self):
def test_ledger_has_system_accounts(self):
system_accounts = self.db.all('SELECT type, system FROM accounts')
assert system_accounts == [ ('asset', 'cash')
, ('asset', 'accounts receivable')
Expand All @@ -21,19 +21,19 @@ def test_journal_has_system_accounts(self):
, ('expense', 'chargeback expenses')
]

def test_journal_creates_accounts_automatically_for_participants(self):
def test_ledger_creates_accounts_automatically_for_participants(self):
self.make_participant('alice')
account = self.db.one("SELECT * FROM accounts WHERE participant IS NOT NULL")
assert account.participant == 'alice'
assert account.type == 'liability'

def test_journal_creates_accounts_automatically_for_teams(self):
def test_ledger_creates_accounts_automatically_for_teams(self):
self.make_team()
account = self.db.one("SELECT * FROM accounts WHERE team IS NOT NULL")
assert account.team == 'TheATeam'
assert account.type == 'liability'

def test_journal_is_okay_with_teams_and_participants_with_same_name(self):
def test_ledger_is_okay_with_teams_and_participants_with_same_name(self):
self.make_participant('alice')
account = self.db.one("SELECT * FROM accounts WHERE participant IS NOT NULL")
assert account.participant == 'alice'
Expand All @@ -42,27 +42,27 @@ def test_journal_is_okay_with_teams_and_participants_with_same_name(self):
account = self.db.one("SELECT * FROM accounts WHERE team IS NOT NULL")
assert account.team == 'alice'

def test_journal_catches_system_account_collision(self):
def test_ledger_catches_system_account_collision(self):
with raises(IntegrityError):
self.db.one("INSERT INTO accounts (type, system) VALUES ('asset', 'cash')")

def test_journal_catches_participant_account_collision(self):
def test_ledger_catches_participant_account_collision(self):
self.make_participant('alice')
with raises(IntegrityError):
self.db.one("INSERT INTO accounts (type, participant) VALUES ('liability', 'alice')")

def test_journal_catches_team_account_collision(self):
def test_ledger_catches_team_account_collision(self):
self.make_team()
with raises(IntegrityError):
self.db.one("INSERT INTO accounts (type, team) VALUES ('liability', 'TheATeam')")

def test_journal_increments_participant_balance(self):
def test_ledger_increments_participant_balance(self):
self.make_team()
alice = self.make_participant('alice')
assert alice.balance == 0

self.db.run("""
INSERT INTO journal
INSERT INTO ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE team='TheATeam')
Expand All @@ -72,13 +72,13 @@ def test_journal_increments_participant_balance(self):

assert Participant.from_username('alice').balance == D('10.77')

def test_journal_decrements_participant_balance(self):
def test_ledger_decrements_participant_balance(self):
self.make_team()
alice = self.make_participant('alice', balance=20)
assert alice.balance == D('20.00')

self.db.run("""
INSERT INTO journal
INSERT INTO ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE participant='alice')
Expand All @@ -88,13 +88,13 @@ def test_journal_decrements_participant_balance(self):

assert Participant.from_username('alice').balance == D('9.23')

def test_journal_allows_negative_balance(self):
def test_ledger_allows_negative_balance(self):
self.make_team()
alice = self.make_participant('alice', balance=10)
assert alice.balance == D('10.00')

self.db.run("""
INSERT INTO journal
INSERT INTO ledger
(amount, debit, credit)
VALUES ( 10.77
, (SELECT id FROM accounts WHERE participant='alice')
Expand Down

0 comments on commit 1d6fc91

Please sign in to comment.