P-LensOpen the app

Accounting

The accounting model

P-Lens keeps a player's pachinko and pachislot results as a double-entry book in hledger's plain-text journal format. This page sets the model out for readers who know double entry but have never been inside a pachinko parlor: the accounts, the units, the prices, and where the book stops.

The book

Plain-text journals as the book of record

The book is a set of hledger journal files on the player's device: main.journal, which includes accounts.journal (commodity and account declarations), prices.journal (exchange rates) and a file of transactions a year. One play at one machine is one transaction. The app runs hledger itself, compiled to WebAssembly, and nothing is kept that hledger has not read first. From the design document's invariants:

A write is kept only if hledger reads it. Every change is offered to hledger, with --strict checks, before it is stored. A refusal leaves the book exactly as it was.

What a reader needs to know of pachinko for the rest of this page: a player rents balls from the parlor for cash, at a rental rate such as 4 yen a ball, and plays them in a machine. The balls left at the end can be exchanged at the parlor's counter for prizes, at the price they were rented at, or left on deposit with the parlor to play another day. Special prizes are sold for cash at a separate exchange shop, which pays less: the exchange rate. Pachislot works the same way with tokens; this page says balls for both. The page on Japan's pachinko parlors describes a visit and the law behind it.

The journal examples are quoted from the app's test fixture, apps/web/fixtures/basic-play, and the code from p-lens at v0.10.63. The parlor names, マレハン東京皇国第8支店 (read Ma-re-han Toh-kyoh Koh-ko-ku Dai-ha-chi Shi-ten, “Marehan, Tokyo Empire, Branch No. 8”) and 723豚の帽子亭店 (read Shi-chi-nissan Buta-no-boh-shi-tei, “723, the Pig's Hat Tavern”), are invented. The account names are Japanese; the English beside them follows the project's README.

Accounts

A small account tree

AccountTypeWhat it holds
資産:現金CashCash. In this book it only ever falls.
資産:貯玉:<parlor>:<rate>AssetBalls on deposit at one parlor, at one rental rate, counted in that parlor's balls.
資産:貯メダル:<parlor>:<rate>AssetThe same for pachislot tokens (medals).
収益:遊技RevenuePlay revenue: the balls or tokens left at the end of a play.
費用:遊技ExpensePlay expense: the balls or tokens played.
費用:貸玉差額, 費用:貸メダル差額ExpenseThe rental spread, in yen: what renting cost above what the balls are worth at the exchange rate.
費用:再プレイ手数料ExpenseReplay fees, in balls or tokens.
純資産:開始残高EquityDeposits a player already had when the book began.
純資産:残高調整EquityA deposit count put right to what the parlor says.
純資産:特殊景品EquityWhat the special prizes of a prize exchange are worth.
純資産:端玉景品等EquityOdd prizes: the small prizes the balls short of a special prize are given as.
純資産:換算調整勘定EquityThe translation adjustment: the part of a yen left when balls are restated in yen.
(統計:期待値)noneTheoretical value, in yen. Virtual postings only.
(統計:時間)nonePlay time, in seconds (秒). Virtual postings only.

hledger cannot tell an account's type from a Japanese name, so the declarations carry it: ; type: A for assets, C for cash, R, X and E for revenue, expenses and equity. The two statistics accounts carry no type, on purpose; the section on them below says why.

There is no liability account, and no account for anything outside play: no travel, no meals. The tree is fixed by the app, and grows only by the deposit accounts of each parlor and a few accounts the app adds the first time an entry needs one. From core/journal/accounts.ts:

/**
 * The account tree every P-Lens book is kept in. Small on purpose; it grows
 * when a book needs a branch it does not have, not before.
 */

Commodities

Balls are not yen

In hledger, an amount is a quantity of a commodity, and a commodity can be any symbol. P-Lens makes each parlor's balls, at each rental rate, a commodity of its own. From core/commodity/commodity.ts:

/**
 * The commodity a parlor's tokens are at one rental rate, e.g.
 * `玉(4円・マレハン東京皇国第8支店)`.
 *
 * One commodity per parlor, kind and rental rate, on purpose. A ball at one
 * parlor cannot be exchanged at another, nor a 1-yen ball as a 4-yen one, so
 * economically they are different things, and hledger keeps them apart only if
 * their symbols differ. The parlor and rate are in the symbol so the journal
 * stays readable without any lookup table.
 */
export const tokenCommodity = (kind: TokenKind, parlorName: string, tier: string): string => `${TOKEN_NAMES[kind]}(${tier}・${parlorName})`

The design document lists it among the invariants:

Each parlor's tokens at each rental rate are their own commodity. 玉(4円・マレハン東京皇国第8支店), 玉(1円・マレハン東京皇国第8支店) and 玉(4円・723豚の帽子亭店) cannot be exchanged for each other, so they are different commodities, held as real assets.

Converting balls to yen on entry would be simpler to add up, and would make two decisions at the wrong time: which price to convert at (the rental price or the exchange price, which differ), and what to do when the parlor changes its rate later. Kept as a commodity, a ball is counted as a ball, and its value in yen is asked for when it is wanted. The same pattern fits anything one issuer controls: store credit, loyalty points, airline miles.

Prices

Exchange rates are price directives

What a parlor's ball is worth in yen is that parlor's exchange rate: what the exchange shop pays, in the end, for the special prizes a ball gets at the counter. In the book it is an hledger P directive. The fixture's prices.journal:

P 2026-09-01 "玉(4円・マレハン東京皇国第8支店)" 3.57 JPY
P 2026-09-01 "枚(20円・マレハン東京皇国第8支店)" 17.86 JPY
P 2026-09-01 "玉(4円・723豚の帽子亭店)" 3.33 JPY
P 2026-09-01 "枚(20円・723豚の帽子亭店)" 18.20 JPY

P 2026-09-25 "玉(4円・マレハン東京皇国第8支店)" 3.50 JPY

At マレハン東京皇国第8支店, a ball rented for 4 yen goes for 4 yen of prizes at the counter, and comes back as 3.57 yen at the exchange shop (28 balls to 100 yen). So the book values the ball at 3.57 from the moment it is rented, and the 0.43 yen a ball is the rental spread below. On September 25 the parlor changed its rate to 3.50, and the book has a second line for it. The first line stays. From the invariants:

Exchange rates are only ever added. A changed rate is a new P directive on a later day; past days keep the rate they had.

Valuation is hledger's --value, and the app asks two different questions with it, which it keeps apart (invariant 7):

  • A play's result values each posting at the rate of its own day, --value=then. A play in September stays at September's rate.
  • Deposits on a given day are valued at the rate in force on that day, --value=end with the period ending there. The fixture's expected figures (expected.json, which the app's tests read the book against) value the same 4,800 balls at マレハン東京皇国第8支店 at 17,136 yen on September 20, at 3.57, and at 16,800 yen on October 3, at 3.50.

The 336 yen between those two figures belongs to no play. It is a revaluation (評価損益), and the book never writes it (ARCHITECTURE.md): "Nothing is entered for it and no account holds it (invariant 1: it follows from the P lines and the savings a book already has, so it is asked, not stored; a rate set right afterwards sets it right with no entry to undo, and two devices cannot both write it)." The app shows it apart from results, and never adds it into them.

A play

One play, one entry

The first play in the fixture's transactions/2026.journal:

2026-09-05 マレハン東京皇国第8支店 | 流星 123番台  ; play-id: made-up-0905, parlor-id: malehan-tokyokoukoku, game-kind: pachinko, machine: 流星, cabinet-number: 123
    資産:現金                        -12000 JPY
    費用:貸玉差額                    1290 JPY
    費用:遊技                        3000 "玉(4円・マレハン東京皇国第8支店)" @ 3.57 JPY
    資産:貯玉:マレハン東京皇国第8支店:4円         4800 "玉(4円・マレハン東京皇国第8支店)"
    収益:遊技                        -4800 "玉(4円・マレハン東京皇国第8支店)"
    (統計:期待値)                    5500 JPY
    (統計:時間)                      10800 秒

Read it in three parts.

  • Rental. 12,000 yen leaves cash. It rented 3,000 balls, which go straight into the machine as play expense at the day's exchange rate, 3.57 yen: 10,710 yen. The other 1,290 yen is the rental spread, an expense in yen.
  • What was left. 4,800 balls were left at the end. They are play revenue (negative, since hledger writes a credit negative) and go into the deposit at that parlor.
  • Statistics. The play's theoretical value, 5,500 yen, and its length, 10,800 seconds, as virtual postings that balance nothing. A section below is about them.

The result is the income statement of the entry, each posting valued at that day's rate:

Play revenue4,800 balls × 3.5717,136 yen
Play expense3,000 balls × 3.57−10,710 yen
Rental spread−1,290 yen
Result+5,136 yen

The fixture's expected.json gives the same: "actual": "5136" and "theoretical": "5500" for this play.

Renting balls is spending yen

The code that writes the rental, from core/tally/compose.ts:

/**
 * Renting tokens and shooting them: cash spent on the play, split at the
 * moment of renting into what the tokens are worth at that day's exchange
 * rate and the spread lost on top of it. Rented tokens are never savings —
 * they go from the counter into the cabinet — so no savings account is
 * touched.
 *
 * ¥12,000 for 3,000 balls exchanged at ¥3.57 is ¥10,710 of balls shot and a
 * ¥1,290 loss, settled that day. A later change of rate moves what balls
 * are worth and never this loss.
 */
const rentalPostings = (rental: Rental, rate: Decimal, holding: Holding): readonly Posting[] => {
  const spread = subtract(rental.outlay, multiply(rental.tokens, rate))
  return unlessZero(rental.tokens, [
    real(ACCOUNTS.cash, negate(rental.outlay), YEN),
    ...unlessZero(spread, [real(rentalSpreadAccount(holding.kind), spread, YEN)]),
    { ...real(ACCOUNTS.playExpense, rental.tokens, holding.commodity), cost: { rate, commodity: YEN } },
  ])
}

Rented balls are not deposited and then withdrawn; they are spent the moment they are rented (ARCHITECTURE.md). The spread is settled on the day and never revalued: a test writes the same rental on September 26, after the rate fell to 3.50, and gets a spread of 1,500 yen instead (tally.test.ts). Cash appears once in the entry, with a minus sign.

Deposits

Deposits are an asset, in balls

The deposit account holds balls, not yen. A balance report shows a deposit in balls, and in yen only when asked to value it. A play from deposited balls touches no cash at all; this one, from the same file, took 3,000 balls out of the deposit at 723豚の帽子亭店 and put back the 2,400 left:

2026-10-03 723豚の帽子亭店 | 森物語 210番台  ; play-id: made-up-1003, parlor-id: 723-butanoboushitei, game-kind: pachinko, machine: 森物語, cabinet-number: 210
    資産:貯玉:723豚の帽子亭店:4円              -3000 "玉(4円・723豚の帽子亭店)"
    費用:遊技                        3000 "玉(4円・723豚の帽子亭店)"
    資産:貯玉:723豚の帽子亭店:4円              2400 "玉(4円・723豚の帽子亭店)"
    収益:遊技                        -2400 "玉(4円・723豚の帽子亭店)"
    (統計:期待値)                    900 JPY

Its result is 2,400 − 3,000 = −600 balls, at that parlor's 3.33 yen: −1,998 yen, which is the fixture's whole result for October. The deposit's balance is never stored. It is the sum of the account's postings: 7,200 balls left on September 20, less 3,000, plus 2,400, is the 6,600 the expected figures give.

Fees

Fees in balls, spreads in yen

Some parlors take a fee for playing deposited balls again (再プレイ, sai-pu-ray, replay: a rebuy from deposited chips, in casino terms): to pay out 1,000 balls, they take 1,100 off the deposit. The book writes that fee in balls, because balls are what was taken. From core/tally/compose.ts:

/**
 * A replay: tokens out of savings and spent, with the replay's fee taken
 * off savings beside them. Counted in tokens only; what that comes to in
 * yen is hledger's to say, at the rate of the day it is asked about.
 */
const replayPostings = (replay: Replay, holding: Holding): readonly Posting[] => {
  const fee = subtract(replay.deducted, replay.paidOut)
  return [
    ...unlessZero(replay.deducted, [real(holding.account, negate(replay.deducted), holding.commodity)]),
    ...unlessZero(replay.paidOut, [real(ACCOUNTS.playExpense, replay.paidOut, holding.commodity)]),
    ...unlessZero(fee, [real(ACCOUNTS.replayFee, fee, holding.commodity)]),
  ]
}

The app's tests compose such a replay (tally.test.ts). Written out by the app's own code, it is this entry:

2026-09-05 マレハン東京皇国第8支店 | 流星 123番台  ; play-id: made-up-0905, parlor-id: malehan-tokyokoukoku, game-kind: pachinko, machine: 流星, cabinet-number: 123
    資産:貯玉:マレハン東京皇国第8支店:4円  -1100 "玉(4円・マレハン東京皇国第8支店)"
    費用:遊技                           1000 "玉(4円・マレハン東京皇国第8支店)"
    費用:再プレイ手数料                 100 "玉(4円・マレハン東京皇国第8支店)"
Rental spreadReplay fee
WhenRenting balls for cashPlaying deposited balls again
What leaves the playerYenBalls
Unit in the bookYenThat parlor's balls
If the rate changes laterUnchangedValued at the rate of the day asked about
Posted by the parlorNo; it follows from two posted ratesYes, as a pair of counts

The spread is not called a fee in the book, since no parlor posts it as one; it is the gap between the rental price and what the exchange shop pays. A parlor that takes no replay fee pays out what it takes off the deposit, and the entry then has no fee line at all.

Statistics

Play time and theoretical value

Two figures that belong to a play are not money. The book keeps both beside the money as unbalanced virtual postings. From the invariants:

A theoretical figure is never real. The theoretical value (期待値, ki-tai-chi) is an unbalanced virtual posting, (統計:期待値), on an account with no type: hledger sums it when asked and leaves it out of every balanced report. Play time is kept the same way, (統計:時間) in 秒: a statistic hledger adds up, never money.

Play time, in seconds

From core/tally/play-time.ts:

/** A play's time, in seconds, as a posting of statistics beside its tally: virtual, so it balances nothing. */
export const playTimePosting = (seconds: Decimal): Posting => ({
  account: ACCOUNTS.playTime,
  amount: { quantity: seconds, commodity: SECONDS },
  kind: "virtual",
})

Seconds are a commodity like any other (commodity 1. 秒), so hledger adds up play time by month, machine or parlor with the same queries it uses for money.

Theoretical value

The theoretical value is what players call 仕事量 (shi-go-to-ryoh): for the plays actually done, the cumulative theoretical gain or loss that occurred under their calculation conditions (算出条件: the machine's figures and what the player measured). Each play's entry carries its own, and over plays they add up. It is not the result. The result is what actually moved, and it lands on either side of the theoretical value.

FixtureResultTheoretical value
September 2026+41,142 yen+10,500 yen
October 2026−1,998 yen+2,700 yen
Both months+39,144 yen+13,200 yen

The account is named 統計:期待値, 期待値 meaning expected value, and the app's screens use that word both for this figure and for the figures its calculators show for a machine under given conditions, apart from any recorded play. In the book, the account holds only the theoretical value of plays already made.

Opened in desktop hledger, the book's balance reports include the virtual postings unless they are left out with -R (--real). The income statement and balance sheet leave them out anyway, since they pick accounts by type and these two have none. The article Beyond money: hledger as an aggregation engine goes into the pattern, and its limits.

The end

Where the book ends

In Japan, you cannot cash out at the pachinko parlor.

The parlor exchanges balls for prizes and does not pay out cash. Special prizes (特殊景品, to-ku-shu kay-hin) are bought for cash by a separate business, outside the parlor, in what is known as the three-shop system (三店方式, sahn-ten hoh-shi-ki). The parlor page explains it, with the law's text. The book records what happens inside the parlor, and its last entry is the exchange at the counter. From core/savings/prize-exchange.ts:

/**
 * Savings exchanged for prizes at a parlor's counter: so many tokens out of
 * one pool on a day, and what the special prizes (特殊景品) handed over for
 * them are worth in yen. A parlor gives prizes, never money, and the book
 * says what the parlor did: what becomes of a prize afterwards is outside
 * it. It is no tally and no tally's result — the tokens were won, or lost,
 * when they were played for.
 */

The fixture holds no exchange, but the app's tests record one on top of it (savings.test.ts): 4,000 of the 4,800 deposited balls at マレハン東京皇国第8支店, exchanged on October 4 for special prizes the exchange shop pays 13,800 yen for. Through prizeExchangeEntry and the fixture's prices, it is this entry:

2026-10-04 マレハン東京皇国第8支店 | 景品交換  ; parlor-id: malehan-tokyokoukoku, savings: prize-exchange
    資産:貯玉:マレハン東京皇国第8支店:4円  -4000 "玉(4円・マレハン東京皇国第8支店)" @ 3.5 JPY
    純資産:特殊景品                     13800 JPY
    純資産:端玉景品等                   228 JPY
    純資産:換算調整勘定
  • 4,000 balls leave the deposit at the rate in force that day, 3.50 yen: 14,000 yen.
  • The special prizes are entered at the value the player gives, 13,800 yen: what the exchange shop pays for them.
  • The balls too few for another special prize are given as small prizes, odd prizes (端玉景品等), which the book counts at the rental price: 57 balls at 4 yen, 228 yen.
  • hledger balances the remainder, −28 yen, into the translation adjustment, which is written with no amount.

All three accounts are equity, not assets. The README puts it among the three things that make the model (README.md):

Equity is the terminal account. The booking ends with the prize exchange: the prizes received are entered not as assets but in 純資産:特殊景品 (special prizes) and 純資産:端玉景品等 (odd prizes), and the part of a yen left when balls or medals are restated in yen in 純資産:換算調整勘定 (the translation adjustment). The keeping, consumption, transfer or any other transaction of a prize after it is received is not booked.

Cash never rises. No entry the app writes puts a positive amount to 資産:現金. A loss is recorded as cash spent, and a gain as prizes taken home, in equity. There is no entry that turns special prizes into cash.

A prize booked as an asset would need a later entry for what became of it, and the next thing that becomes of a special prize happens outside the parlor. Equity has no later entry. A test builds every kind of entry the app composes and checks that none raises cash (cash-never-rises.test.ts). The same holds for a play kept in yen alone, as money in and money back: a gain there goes to special prizes, not to cash (core/tally/sides.ts).

The exchange is not a result either. The balls were counted as revenue on the day they were left, at that day's rate, so no income statement counts the exchange; the tests check that the totals are the same before and after it. The article Law shapes the ledger: modeling Japanese pachinko follows the model from the law to the code.

Invariant

No stored balances

The first of the design document's invariants:

The journal text is the only source of truth. IndexedDB holds journal files; it is a container, never a second record. Nothing hledger can answer — a balance, a total, a count — is stored.

and the third:

What hledger can express is not reimplemented. Periods are query dates, valuation is --value, sums are balance reports. No figure a book comes to is worked out here.

A deposit's balance is the sum of its postings, a month's result is an income statement over that month, and a revaluation is asked of the prices and the deposits. None of them is written anywhere, so none can disagree with the entries. Correct an entry or a rate, and every figure that depends on it is correct the next time it is asked for.