imp:close: infer a default tag value by incrementing current file name [#2151]
This commit is contained in:
parent
c2ce1c2037
commit
f3073990cc
@ -19,7 +19,9 @@ import System.Console.CmdArgs.Explicit as C
|
|||||||
|
|
||||||
import Hledger
|
import Hledger
|
||||||
import Hledger.Cli.CliOptions
|
import Hledger.Cli.CliOptions
|
||||||
import Safe (lastDef, readMay)
|
import Safe (lastDef, readMay, readDef)
|
||||||
|
import System.FilePath (takeFileName)
|
||||||
|
import Data.Char (isDigit)
|
||||||
|
|
||||||
defclosedesc = "closing balances"
|
defclosedesc = "closing balances"
|
||||||
defopendesc = "opening balances"
|
defopendesc = "opening balances"
|
||||||
@ -31,9 +33,8 @@ defretainacct = "equity:retained earnings"
|
|||||||
closemode = hledgerCommandMode
|
closemode = hledgerCommandMode
|
||||||
$(embedFileRelative "Hledger/Cli/Commands/Close.txt")
|
$(embedFileRelative "Hledger/Cli/Commands/Close.txt")
|
||||||
[flagOpt "" ["migrate"] (\s opts -> Right $ setopt "migrate" s opts) "NEW" ("show closing and opening transactions,"
|
[flagOpt "" ["migrate"] (\s opts -> Right $ setopt "migrate" s opts) "NEW" ("show closing and opening transactions,"
|
||||||
<> " for Asset and Liability accounts by default,"
|
<> " for Asset and Liability accounts by default, tagged for easy matching."
|
||||||
<> " tagged for easy matching,"
|
<> " The tag's default value can be overridden by providing NEW."
|
||||||
<> " with NEW (eg a new year) as tag value."
|
|
||||||
)
|
)
|
||||||
,flagOpt "" ["close"] (\s opts -> Right $ setopt "close" s opts) "NEW" "(default) show a closing transaction"
|
,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"
|
,flagOpt "" ["open"] (\s opts -> Right $ setopt "open" s opts) "NEW" "show an opening transaction"
|
||||||
@ -77,15 +78,29 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
|
|||||||
closeacct = T.pack $ fromMaybe defcloseacct_ $ maybestringopt "close-acct" rawopts
|
closeacct = T.pack $ fromMaybe defcloseacct_ $ maybestringopt "close-acct" rawopts
|
||||||
openacct = maybe closeacct T.pack $ maybestringopt "open-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,
|
-- 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.
|
|
||||||
comment = T.pack $ if
|
comment = T.pack $ if
|
||||||
| mode_ == Assert -> "balances:" <> flagval
|
| mode_ == Assert -> "balances:" <> val
|
||||||
| mode_ == Retain -> "retain:" <> flagval
|
| mode_ == Retain -> "retain:" <> val
|
||||||
| otherwise -> "start:" <> flagval
|
| otherwise -> "start:" <> val
|
||||||
where
|
where
|
||||||
flagval = fromMaybe "" $ maybestringopt modeflag rawopts
|
val = if null flagval then inferredval else flagval
|
||||||
where modeflag = lowercase $ show mode_
|
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}
|
ropts = (_rsReportOpts rspec0){balanceaccum_=Historical, accountlistmode_=ALFlat}
|
||||||
rspec1 = setDefaultConversionOp NoConversionOp rspec0{_rsReportOpts=ropts}
|
rspec1 = setDefaultConversionOp NoConversionOp rspec0{_rsReportOpts=ropts}
|
||||||
@ -236,4 +251,4 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do
|
|||||||
-- print them
|
-- print them
|
||||||
maybe (pure ()) (T.putStr . showTransaction) mclosetxn
|
maybe (pure ()) (T.putStr . showTransaction) mclosetxn
|
||||||
maybe (pure ()) (T.putStr . showTransaction) mopentxn
|
maybe (pure ()) (T.putStr . showTransaction) mopentxn
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
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
|
The generated transactions will have a `start:` tag, with its value set to
|
||||||
`--migrate`'s `NEW` argument if any, for easier matching or exclusion.
|
`--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.
|
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.
|
The other modes behave similarly.
|
||||||
|
|
||||||
### close --close
|
### close --close
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user