Skip to content

Commit f70eac2

Browse files
committed
imp:close: add --assertion-type option
1 parent 60a1adc commit f70eac2

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

hledger/Hledger/Cli/Commands/Close.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Hledger.Cli.CliOptions
2222
import Safe (lastDef, readMay, readDef)
2323
import System.FilePath (takeFileName)
2424
import Data.Char (isDigit)
25+
import Hledger.Read.RulesReader (parseBalanceAssertionType)
2526

2627
defclosedesc = "closing balances"
2728
defopendesc = "opening balances"
@@ -44,6 +45,7 @@ closemode = hledgerCommandMode
4445
,flagNone ["explicit","x"] (setboolopt "explicit") "show all amounts explicitly"
4546
,flagNone ["show-costs"] (setboolopt "show-costs") "show amounts with different costs separately"
4647
,flagNone ["interleaved"] (setboolopt "interleaved") "show source and destination postings together"
48+
,flagReq ["assertion-type"] (\s opts -> Right $ setopt "assertion-type" s opts) "TYPE" "=, ==, =* or ==*"
4749
,flagReq ["close-desc"] (\s opts -> Right $ setopt "close-desc" s opts) "DESC" "set closing transaction's description"
4850
,flagReq ["close-acct"] (\s opts -> Right $ setopt "close-acct" s opts) "ACCT" "set closing transaction's destination account"
4951
,flagReq ["open-desc"] (\s opts -> Right $ setopt "open-desc" s opts) "DESC" "set opening transaction's description"
@@ -135,6 +137,12 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
135137
-- interleave equity postings next to the corresponding closing posting, or put them all at the end ?
136138
interleaved = boolopt "interleaved" rawopts
137139

140+
-- a balance assertion template of the right type
141+
assertion =
142+
case maybestringopt "assertion-type" rawopts >>= parseBalanceAssertionType of
143+
Nothing -> nullassertion
144+
Just (total, inclusive) -> nullassertion{batotal=total, bainclusive=inclusive}
145+
138146
-- the closing (balance-asserting or balance-zeroing) transaction
139147
mclosetxn
140148
| mode_ `notElem` [Migrate, Close, Assert, Retain] = Nothing
@@ -158,7 +166,7 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
158166
-- balance assertion amounts are unpriced (#824)
159167
,pbalanceassertion =
160168
if islast
161-
then Just nullassertion{baamount=precise b}
169+
then Just assertion{baamount=precise b}
162170
else Nothing
163171
}
164172
| -- get the balances for each commodity and transaction price
@@ -178,7 +186,7 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
178186
-- balance assertion amounts are unpriced (#824)
179187
,pbalanceassertion =
180188
if islast
181-
then Just nullassertion{baamount=precise b{aquantity=0, acost=Nothing}}
189+
then Just assertion{baamount=precise b{aquantity=0, acost=Nothing}}
182190
else Nothing
183191
}
184192

@@ -210,7 +218,7 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
210218
| mode_ == Assign =
211219
[ posting{paccount = a
212220
,pamount = missingmixedamt
213-
,pbalanceassertion = Just nullassertion{baamount=b}
221+
,pbalanceassertion = Just assertion{baamount=b}
214222
-- case mcommoditysum of
215223
-- Just s -> Just nullassertion{baamount=precise s}
216224
-- Nothing -> Nothing
@@ -232,7 +240,7 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
232240
,pamount = mixedAmount $ precise b
233241
,pbalanceassertion =
234242
case mcommoditysum of
235-
Just s -> Just nullassertion{baamount=precise s{acost=Nothing}}
243+
Just s -> Just assertion{baamount=precise s{acost=Nothing}}
236244
Nothing -> Nothing
237245
}
238246
: [posting{paccount=openacct, pamount=mixedAmount . precise $ negate b} | interleaved]

hledger/Hledger/Cli/Commands/Close.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ or restored to their previous balances in an opening transaction.
115115
These provide useful error checking, but you can ignore them temporarily with `-I`,
116116
or remove them if you prefer.
117117

118+
Single-commodity, subaccount-exclusive balance assertions (`=`) are generated by default.
119+
This can be changed with `--assertion-type='==*'` (eg).
120+
118121
When running `close` you should probably avoid using `-C`, `-R`, `status:` (filtering by status or realness)
119122
or `--auto` (generating postings), since the generated balance assertions would then require these.
120123

0 commit comments

Comments
 (0)