Skip to content

Commit

Permalink
imp:close: infer a default tag value by incrementing current file name [
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Jan 21, 2024
1 parent c2ce1c2 commit f307399
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
39 changes: 27 additions & 12 deletions hledger/Hledger/Cli/Commands/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import System.Console.CmdArgs.Explicit as C

import Hledger
import Hledger.Cli.CliOptions
import Safe (lastDef, readMay)
import Safe (lastDef, readMay, readDef)
import System.FilePath (takeFileName)
import Data.Char (isDigit)

defclosedesc = "closing balances"
defopendesc = "opening balances"
Expand All @@ -31,9 +33,8 @@ defretainacct = "equity:retained earnings"
closemode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Close.txt")
[flagOpt "" ["migrate"] (\s opts -> Right $ setopt "migrate" s opts) "NEW" ("show closing and opening transactions,"
<> " for Asset and Liability accounts by default,"
<> " tagged for easy matching,"
<> " with NEW (eg a new year) as tag value."
<> " for Asset and Liability accounts by default, tagged for easy matching."
<> " The tag's default value can be overridden by providing NEW."
)
,flagOpt "" ["close"] (\s opts -> Right $ setopt "close" s opts) "NEW" "(default) show a closing transaction"
,flagOpt "" ["open"] (\s opts -> Right $ setopt "open" s opts) "NEW" "show an opening transaction"
Expand Down Expand Up @@ -77,15 +78,29 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
closeacct = T.pack $ fromMaybe defcloseacct_ $ maybestringopt "close-acct" rawopts
openacct = maybe closeacct T.pack $ maybestringopt "open-acct" rawopts

-- For easy matching and exclusion, a recognisable tag is added to all generated transactions,
-- with the mode flag's argument if any (NEW, eg a new year number) as its argument.
-- For easy matching and exclusion, a recognisable tag is added to all generated transactions
comment = T.pack $ if
| mode_ == Assert -> "balances:" <> flagval
| mode_ == Retain -> "retain:" <> flagval
| otherwise -> "start:" <> flagval
| mode_ == Assert -> "balances:" <> val
| mode_ == Retain -> "retain:" <> val
| otherwise -> "start:" <> val
where
flagval = fromMaybe "" $ maybestringopt modeflag rawopts
where modeflag = lowercase $ show mode_
val = if null flagval then inferredval else flagval
where
inferredval = newfilename
where
oldfilename = takeFileName $ journalFilePath j
(nonnum, rest) = break isDigit $ reverse oldfilename
(oldnum, rest2) = span isDigit rest
newfilename = case oldnum of
[] -> ""
_ -> reverse rest2 <> newnum <> reverse nonnum
where
newnum = show $ 1 + readDef err (reverse oldnum) -- PARTIAL: should not fail
where err = error' $ "could not read " <> show oldnum <> " as a number in Hledger.Cli.Commands.Close.close"

flagval = fromMaybe "" $ maybestringopt modeflag rawopts
where
modeflag = lowercase $ show mode_

ropts = (_rsReportOpts rspec0){balanceaccum_=Historical, accountlistmode_=ALFlat}
rspec1 = setDefaultConversionOp NoConversionOp rspec0{_rsReportOpts=ropts}
Expand Down Expand Up @@ -236,4 +251,4 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
-- print them
maybe (pure ()) (T.putStr . showTransaction) mclosetxn
maybe (pure ()) (T.putStr . showTransaction) mopentxn


5 changes: 3 additions & 2 deletions hledger/Hledger/Cli/Commands/Close.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ Eg if you want to include equity, you can add `assets liabilities equity` or [`t
Revenues and expenses usually are not migrated to a new file directly; see `--retain` below.

The generated transactions will have a `start:` tag, with its value set to
`--migrate`'s `NEW` argument if any, for easier matching or exclusion.
It's a good idea to provide a `NEW` argument, unique to the new file; the new year number is most often used.
`--migrate`'s `NEW` argument if any, for easier matching or exclusion.
When `NEW` is not specified, it will be inferred if possible by incrementing
a number (eg a year number) within the default journal's main file name.
The other modes behave similarly.

### close --close
Expand Down

0 comments on commit f307399

Please sign in to comment.