Adjusting entries
At January 31, part of the rent belongs to February and some purchased goods remain unsold. Start from the checked journal in Trial balance and checks, defer the February rent, and recognize ending inventory before closing January’s revenue and expenses.
Use exchangealgebra-0.6.1.1 in the project prepared in Add the package to a Stack project. Save the code blocks in their displayed order as Main.hs.
Recover January before adjustments
The program begins with the checked records and trial-balance calculations from Trial balance and checks and the chapters before it.
Code carried over from earlier chapters
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import ExchangeAlgebra hiding (Yen, Dollar, map)
import qualified ExchangeAlgebra.Algebra.Core as Algebra
import ExchangeAlgebra.Journal ((.|))
import qualified ExchangeAlgebra.Journal as Journal
import qualified ExchangeAlgebra.IO.Input.Admission as Admission
import qualified ExchangeAlgebra.Accounting.TrialBalance as TrialBalance
import qualified ExchangeAlgebra.Accounting.Entries as Entries
-- | Currency element retained in the source journal.
data Unit
= Yen -- ^ Japanese yen.
| Dollar -- ^ US dollars.
| Unit -- ^ Inapplicable in an entry; unrestricted in a query.
deriving (Eq, Ord, Show, Generic, Hashable)
instance Element Unit where
wildcard = Unit
instance ExBaseClass (HatBase (AccountTitles, Unit)) where
getAccountTitle (_ :< (account, _)) = account
setAccountTitle (direction :< (_, unit)) account = direction :< (account, unit)
-- | Date and description; the example supplies valid calendar dates.
data EntryNote = EntryNote (Integer, Int, Int) String
deriving (Eq, Ord, Show, Generic, Hashable)
instance Journal.Note EntryNote where
plank = EntryNote (0, 0, 0) ""
-- | One entry with account and currency elements; the alias adds no validation.
type Entry = Alg MoneyDecimal (HatBase (AccountTitles, Unit))
-- | January journal with one distinct note per transaction.
type Book = Journal.Journal EntryNote MoneyDecimal (HatBase (AccountTitles, Unit))
-- | Attach a January date and description after constructing all terms.
record :: Int -> String -> Entry -> Book
record day description algebra = algebra .| EntryNote (2026, 1, day) description
-- | January 1: capital paid into Bank A's savings account.
opening :: Book
opening = record 1 "Opening capital"
(10000 .@ Not :< (Deposits, Yen)
.+ 10000 .@ Not :< (CapitalStock, Yen))
-- | Transactions through January 30, before adjusting entries.
sourceJournal :: Book
sourceJournal = opening
.+ record 10 "Withdrawal from Bank A"
(3000 .@ Not :< (Cash, Yen) .+ 3000 .@ Hat :< (Deposits, Yen))
.+ record 15 "Withdrawal from Bank A"
(2000 .@ Not :< (Cash, Yen) .+ 2000 .@ Hat :< (Deposits, Yen))
.+ record 20 "Purchase of goods"
(500 .@ Not :< (Purchases, Yen) .+ 500 .@ Hat :< (Cash, Yen))
.+ record 25 "Yen-dollar exchange"
(1500 .@ Hat :< (Cash, Yen) .+ 10 .@ Not :< (Cash, Dollar))
.+ record 28 "Cash sale"
(2000 .@ Not :< (Cash, Yen) .+ 2000 .@ Not :< (Sales, Yen))
.+ record 30 "January and February rent"
(1200 .@ Not :< (RentExpense, Yen) .+ 1200 .@ Hat :< (Cash, Yen))
-- | Express dollars in yen at 150 yen per dollar, preserving account and side.
toYen :: MoneyDecimal
-> HatBase (AccountTitles, Unit)
-> (MoneyDecimal, HatBase (AccountTitles, Unit))
toYen amount (direction :< (account, Dollar)) = (amount * 150, direction :< (account, Yen))
toYen amount basis = (amount, basis)
-- | Reporting representation at the January 31 rate, equal to the exchange rate.
yenJournal :: Book
yenJournal = Journal.map (Algebra.mapPosting toYen) sourceJournal
-- | Check the converted journal without changing its notes or term sequences.
acceptedJanuary :: Either String (Admission.Admitted EntryNote (HatBase (AccountTitles, Unit)))
acceptedJanuary = either (Left . show . Admission.failureErrors) Right
(Admission.admit yenJournal)
-- | Print a labeled result, retaining the library's diagnostic constructors.
printNamed :: Show a => String -> a -> IO ()
printNamed label value = putStrLn (label ++ ": " ++ show value)
-- | Stop the example on an unexpected checked failure.
require :: Show e => Either e a -> IO a
require = either (ioError . userError . show) pure
-- | Show gross debit and credit totals in yen, without netting the source.
printTotals :: Entry -> IO ()
printTotals ledger = do
printNamed "Term totals (debit, credit)" (norm (decL ledger), norm (decR ledger))
printNamed "Balanced" (balance ledger)
-- | Accounts used in the shop, ordered for repeatable output including zero balances.
accounts :: [AccountTitles]
accounts = [Cash, Deposits, PrepaidExpenses, MerchandiseInventory,
Purchases, RentExpense, Sales, CapitalStock, RetainedEarnings]
-- | Drop the currency element only after checking every term is in yen.
-- This reporting projection removes the currency element; it does not net terms.
auditLedger :: Entry -> Either String (Alg MoneyDecimal (HatBase AccountTitles))
auditLedger ledger
| Algebra.foldEntries allYen True ledger = Right (Algebra.mapPosting dropUnit ledger)
| otherwise = Left "Audit requires yen on every term"
where
allYen :: Bool -> MoneyDecimal -> HatBase (AccountTitles, Unit) -> Bool
allYen result _ (_ :< (_, unit)) = result && unit == Yen
dropUnit :: MoneyDecimal
-> HatBase (AccountTitles, Unit)
-> (MoneyDecimal, HatBase AccountTitles)
dropUnit amount (direction :< (account, _)) = (amount, direction :< account)
-- | Print account debit and credit totals, net balances, balance totals, and accounting findings.
printTrial :: TrialBalance.TrialBalanceStage -> Entry -> IO ()
printTrial stage ledger = do
reporting <- require (auditLedger ledger)
let balances = TrialBalance.accountBalances reporting
pairs :: [(MoneyDecimal, MoneyDecimal)]
pairs = map (TrialBalance.balancePair . (`TrialBalance.balanceFor` balances)) accounts
totals :: (MoneyDecimal, MoneyDecimal)
totals = foldr TrialBalance.addPair (0, 0) pairs
row :: AccountTitles -> IO ()
row account = do
let selected = projByAccountTitle account ledger
printNamed (show account)
(norm (decL selected), norm (decR selected),
TrialBalance.balanceFor account balances)
putStrLn "Account: (debit turnover, credit turnover, balance)"
mapM_ row accounts
printNamed "Balance totals (debit, credit)" totals
printTotals ledger
printNamed "Findings" $ TrialBalance.trialBalanceFindings
(TrialBalance.TrialBalanceInput reporting stage mempty [] mempty)
Defer rent and count inventory
The rent transfer moves 600 yen from RentExpense to PrepaidExpenses. It is an adjusting entry: debit PrepaidExpenses and credit RentExpense on January 31, leaving 600 yen of rent expense for January. Generate this entry from the unadjusted ledger once.
The shop has no beginning merchandise inventory, purchases goods for 500 yen during January, and counts ending inventory worth 200 yen at cost. Under the periodic three-account method, cost of goods sold is beginning inventory plus purchases less ending inventory: $0+500-200=300$ yen. Debit MerchandiseInventory and credit Purchases for 200 yen; the Purchases balance then represents cost of goods sold.
-- | January 31: defer February's half of the rent, using the rent transfer table.
rentAdjustment :: Entry -> Either String Entry
rentAdjustment ledger = do
table <- either (Left . show) Right $ mkTransferTable
[(RentExpense, (.#)) .-> (PrepaidExpenses, (.#)) |% Portion 1 2]
either (Left . show) Right (transferEntries table ledger)
-- | Keep the currency element when building the periodic inventory adjustment.
yenBase :: Entries.MkBase (HatBase (AccountTitles, Unit))
yenBase direction account = direction :< (account, Yen)
-- | No beginning inventory; the January 31 physical count is worth 200 yen at cost.
inventoryAdjustment :: Entry
inventoryAdjustment = Entries.cogsAdjustmentEntries yenBase 0 200
-- | Add adjustments under distinct notes and check the resulting journal again.
adjustedJanuary :: Book -> Either String Book
adjustedJanuary journal = do
rent <- rentAdjustment (Journal.toAlg journal)
let adjusted :: Book
adjusted = journal
.+ record 31 "Defer February rent" rent
.+ record 31 "Ending inventory" inventoryAdjustment
admitted <- either (Left . show . Admission.failureErrors) Right (Admission.admit adjusted)
pure (Admission.admittedJournal admitted)
Use Entries.cogsAdjustmentEntries with a MkBase that fills in Yen, because the entry builder accepts the existing account-and-currency base while retaining all its elements. Its arguments are the beginning and ending inventory amounts, not the purchases total. A zero beginning inventory produces no zero-valued terms. The builder returns only additional terms, without implicit bar or compress; attach a note and add them to the journal with .+.
Check and inspect the adjusted journal
-- | Run the January procedure and print its checked results.
main :: IO ()
main = do
accepted <- require acceptedJanuary
let journal = Admission.admittedJournal accepted
rent <- require (rentAdjustment (Journal.toAlg journal))
printNamed "Rent adjustment" rent
printNamed "Inventory adjustment" inventoryAdjustment
adjusted <- require (adjustedJanuary journal)
printTrial TrialBalance.BeforeClosing (Journal.toAlg adjusted)
Run stack exec -- ghc -Wall -Werror Main.hs -o adjustment-example, then run ./adjustment-example in your project directory. The program prints:
Rent adjustment: 600:@Hat:<(RentExpense,Yen) .+ 600:@Not:<(PrepaidExpenses,Yen)
Inventory adjustment: 200:@Hat:<(Purchases,Yen) .+ 200:@Not:<(MerchandiseInventory,Yen)
Account: (debit turnover, credit turnover, balance)
Cash: (8500,3200,DebitBalance 5300)
Deposits: (10000,5000,DebitBalance 5000)
PrepaidExpenses: (600,0,DebitBalance 600)
MerchandiseInventory: (200,0,DebitBalance 200)
Purchases: (500,200,DebitBalance 300)
RentExpense: (1200,600,DebitBalance 600)
Sales: (0,2000,CreditBalance 2000)
CapitalStock: (0,10000,CreditBalance 10000)
RetainedEarnings: (0,0,NoBalance)
Balance totals (debit, credit): (12000,12000)
Term totals (debit, credit): (21000,21000)
Balanced: True
Findings: []
The rent and inventory adjustments remain separately selectable under their January 31 notes. Checking the journal again checks their debit-credit equality along with the original entries. The adjusted trial balance has PrepaidExpenses of 600 yen, MerchandiseInventory of 200 yen, Purchases of 300 yen, and RentExpense of 600 yen. Balance totals remain 12,000 yen on each side; gross term totals increase to 21,000 yen because the added entries remain in the record.
Tested with exchangealgebra-0.6.1.1 on 2026-11-20.
Reference
| Type or operation | Inputs and result |
|---|---|
Entries.MkBase b | A function from Hat/Not and account title to the chosen base. |
Entries.cogsAdjustmentEntries mk beginning ending | Return the periodic inventory adjustment; supply non-negative finite amounts in the same unit. |
transferEntries table ledger | Return the additional rent reclassification entry, or a checked transfer error. |