1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE NamedFieldPuns #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
7 module Hcompta.CLI.Format.Ledger where
9 import Control.Exception (tryJust)
10 import Control.Monad (Monad(..), guard)
11 import Data.Either (Either(..))
12 import qualified Data.List
13 import qualified Data.Text.Lazy as TL
14 import Prelude (($), (.), FilePath, IO)
15 import System.Environment as Env (getEnv)
16 import System.IO.Error (isDoesNotExistError)
17 import Text.Show (Show(..))
19 import qualified Hcompta.Amount as Amount
20 import qualified Hcompta.Balance as Balance
21 import qualified Hcompta.CLI.Context as Context
22 import Hcompta.CLI.Lang (Lang)
23 import qualified Hcompta.CLI.Lang as Lang
24 import qualified Hcompta.CLI.Write as Write
25 import qualified Hcompta.Format.Ledger.Read as Ledger.Read
26 import qualified Hcompta.Format.Ledger.Write as Ledger.Write
27 import Hcompta.Lib.Leijen (ToDoc(..))
28 import qualified Hcompta.Lib.Leijen as W
30 -- | Return 'FilePath's containing 'Hcompta.Format.Ledger.Journal's:
32 -- * either those given if any,
33 -- * or the one in LEDGER_FILE environment variable if any,
34 -- * or the one in LEDGER environment variable if any.
35 paths :: Context.Context -> [FilePath] -> IO [FilePath]
37 tryJust (guard . isDoesNotExistError) $ Env.getEnv "LEDGER_FILE"
39 Right ok -> return [ok]
41 tryJust (guard . isDoesNotExistError) $ Env.getEnv "LEDGER"
43 Right ok -> return [ok]
45 let lang = Context.lang context
47 toDoc lang $ Lang.Message_no_ledger_file_given
48 paths _context ps = return ps
50 instance ToDoc Lang Ledger.Read.Error where
53 Ledger.Read.Error_date date -> toDoc lang date
54 Ledger.Read.Error_transaction_not_equilibrated tr unit_sums ->
55 i18n_transaction_not_equilibrated tr unit_sums
56 Lang.Message_the_following_transaction_is_not_equilibrated_because
57 Ledger.Read.Error_virtual_transaction_not_equilibrated tr unit_sums ->
58 i18n_transaction_not_equilibrated tr unit_sums
59 Lang.Message_the_following_virtual_transaction_is_not_equilibrated_because
60 Ledger.Read.Error_reading_file file_path exn ->
62 [ toDoc lang $ Lang.Message_failed_to_read_file file_path
63 , W.text $ TL.pack $ show exn
65 Ledger.Read.Error_including_file file_path errs ->
67 [ toDoc lang $ Lang.Message_failed_to_include_file file_path
71 i18n_transaction_not_equilibrated tr unit_sums msg =
74 , W.vsep $ Data.List.map
75 (\Balance.Unit_Sum{Balance.unit_sum_amount} ->
76 let amt = Amount.sum_balance unit_sum_amount in
78 Lang.Message_unit_sums_up_to_the_non_null_amount
82 , Ledger.Write.transaction tr