@@ -5,8 +5,9 @@ module Hledger.Flow.Reports
5
5
) where
6
6
7
7
import Turtle
8
- import Prelude hiding (FilePath , putStrLn )
9
- import Hledger.Flow.Types
8
+ import Prelude hiding (FilePath , putStrLn , writeFile )
9
+ import qualified Data.Text as T
10
+ import Hledger.Flow.Types (LogMessage , FullTimedOutput )
10
11
import Hledger.Flow.Report.Types
11
12
import Hledger.Flow.Common
12
13
import Control.Concurrent.STM
@@ -16,6 +17,7 @@ generateReports opts = sh (
16
17
do
17
18
ch <- liftIO newTChanIO
18
19
logHandle <- fork $ consoleChannelLoop ch
20
+ liftIO $ if (showOptions opts) then channelOut ch (repr opts) else return ()
19
21
(reports, diff) <- time $ liftIO $ generateReports' opts ch
20
22
liftIO $ channelOut ch $ format (" Generated " % d% " reports in " % s) (length reports) $ repr diff
21
23
liftIO $ terminateChannelLoop ch
@@ -26,4 +28,45 @@ generateReports' :: ReportOptions -> TChan LogMessage -> IO [FilePath]
26
28
generateReports' opts ch = do
27
29
logVerbose opts ch " Something will be here Real Soon Now (tm)"
28
30
channelOut ch " Report generation has not been implemented. Yet. https://github.com/apauley/hledger-flow/pull/4"
29
- return []
31
+ ownerReports opts ch " everyone"
32
+
33
+ ownerReports :: ReportOptions -> TChan LogMessage -> Text -> IO [FilePath ]
34
+ ownerReports opts ch owner = do
35
+ let journal = (baseDir opts) </> " all-years" <.> " journal"
36
+ let reportsDir = (baseDir opts) </> " reports" </> fromText owner
37
+ let actions = map (\ r -> r opts ch journal reportsDir) [accountList, incomeStatement]
38
+ results <- if (sequential opts) then sequence actions else single $ shellToList $ parallel actions
39
+ return $ map fst results
40
+
41
+ incomeStatement :: ReportOptions -> TChan LogMessage -> FilePath -> FilePath -> IO (FilePath , FullTimedOutput )
42
+ incomeStatement opts ch journal reportsDir = do
43
+ mktree reportsDir
44
+ let outputFile = reportsDir </> " income-expenses" <.> " txt"
45
+ let sharedOptions = [" --depth" , " 2" , " --pretty-tables" , " not:equity" ]
46
+ let reportArgs = [" incomestatement" ] ++ sharedOptions ++ [" --average" , " --yearly" ]
47
+ generateReport' opts ch journal outputFile reportArgs
48
+
49
+ accountList :: ReportOptions -> TChan LogMessage -> FilePath -> FilePath -> IO (FilePath , FullTimedOutput )
50
+ accountList opts ch journal reportsDir = do
51
+ let outputFile = reportsDir </> " accounts" <.> " txt"
52
+ let reportArgs = [" accounts" ]
53
+ generateReport' opts ch journal outputFile reportArgs
54
+
55
+ generateReport' :: ReportOptions -> TChan LogMessage -> FilePath -> FilePath -> [Text ] -> IO (FilePath , FullTimedOutput )
56
+ generateReport' opts ch journal outputFile args = do
57
+ let reportsDir = directory outputFile
58
+ mktree reportsDir
59
+ let relativeJournal = relativeToBase opts journal
60
+ let reportArgs = [" --file" , format fp journal] ++ args
61
+ let reportDisplayArgs = [" --file" , format fp relativeJournal] ++ args
62
+ let action = procStrictWithErr " hledger" reportArgs empty
63
+ let cmd = format (" hledger " % s) $ showCmdArgs reportDisplayArgs
64
+ result@ ((exitCode, stdOut, stdErr), _) <- logVerboseTime opts ch cmd action
65
+ if not (T. null stdOut) then do
66
+ writeTextFile outputFile (cmd <> " \n\n " <> stdOut)
67
+ channelOut ch $ format (" Wrote " % fp) $ relativeToBase opts outputFile
68
+ else channelErr ch $ format (" No report output for '" % s% " ' " % s) cmd (repr exitCode)
69
+ if not (T. null stdErr)
70
+ then channelErr ch $ stdErr
71
+ else return ()
72
+ return (outputFile, result)
0 commit comments