;run: --forecast with changed reporting interval fix (+test) [#2345]

This commit is contained in:
Dmitry Astapov 2025-03-08 16:34:45 +00:00 committed by Simon Michael
parent 542d1802ee
commit c0b3c932ea
2 changed files with 34 additions and 4 deletions

View File

@ -43,6 +43,7 @@ import Hledger.Cli.DocFiles (runTldrForPage, runInfoForTopic, runManForTopic)
import Hledger.Cli.Utils (journalTransform)
import Text.Printf (printf)
import System.Process (system)
import Data.Maybe (isJust)
-- | Command line options for this command.
runmode = hledgerCommandMode
@ -240,10 +241,9 @@ withJournalCached defaultJournalOverride cliopts cmd = do
either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal
where
-- InputOptions contain reportspan_ that is used to calculare forecast period,
-- that is used by journalFinalise to insert forecast transactions.addHeaderBorders
-- For the purposes of caching, we want to ignore this, as it is only used for forecast
-- and it is sufficient to include forecast_ in the InputOptions that we use as a key.
ioptsWithoutReportSpan = iopts { reportspan_ = emptydatespan }
-- that is used by journalFinalise to insert forecast transactions.
-- For the purposes of caching, we want to ignore this when --forecast is not given.
ioptsWithoutReportSpan = if isJust (forecast_ iopts) then iopts else iopts { reportspan_ = emptydatespan }
-- Read stdin, or if we read it alread, use a cache
-- readStdin :: InputOpts -> ExceptT String IO Journal
readStdin = do

View File

@ -164,3 +164,33 @@ readAndCacheJournalFile reading and caching -:
readAndCacheJournalFile reading and caching sample.journal:
readAndCacheJournalFile using cache for -:
readAndCacheJournalFile using cache for sample.journal:
# ** 12. Run commands with forecast properly honor reporting interval
<
2025-01-01 Buy beans
expenses:food $2
assets -$2
~ every 14 days from 2025-01-01 Buy soylent green
expenses:food:not people $1
assets -$1
$ hledger run -f- -- echo "Till Feb" -- register --forecast -b 2025-01 -e 2025-02 -- echo "Till Mar" -- register --forecast -b 2025-01 -e 2025-03
>
Till Feb
2025-01-01 Buy beans expenses:food $2 $2
assets $-2 0
2025-01-15 Buy soylent green ex:food:not people $1 $1
assets $-1 0
2025-01-29 Buy soylent green ex:food:not people $1 $1
assets $-1 0
Till Mar
2025-01-01 Buy beans expenses:food $2 $2
assets $-2 0
2025-01-15 Buy soylent green ex:food:not people $1 $1
assets $-1 0
2025-01-29 Buy soylent green ex:food:not people $1 $1
assets $-1 0
2025-02-12 Buy soylent green ex:food:not people $1 $1
assets $-1 0
2025-02-26 Buy soylent green ex:food:not people $1 $1
assets $-1 0