{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Close (
closemode
,close
)
where
import Control.Monad (when)
import Data.Function (on)
import Data.List (groupBy)
import Data.Maybe
import Data.Time.Calendar
import System.Console.CmdArgs.Explicit as C
import Hledger
import Hledger.Cli.CliOptions
closemode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Close.txt")
[flagNone ["opening"] (setboolopt "opening") "show just opening transaction"
,flagNone ["closing"] (setboolopt "closing") "show just closing transaction"
]
[generalflagsgroup1]
hiddenflags
([], Just $ argsFlag "[QUERY]")
close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
today <- getCurrentDay
let
(opening, closing) =
case (boolopt "opening" rawopts, boolopt "closing" rawopts) of
(False, False) -> (True, True)
(o, c) -> (o, c)
ropts_ = ropts{balancetype_=HistoricalBalance, accountlistmode_=ALFlat}
q = queryFromOpts today ropts_
openingdate = fromMaybe today $ queryEndDate False q
closingdate = addDays (-1) openingdate
(acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j
balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmount b) acctbals
setprec = setNaturalPrecision
closingps = [posting{paccount = a
,pamount = mixed [setprec $ negate b]
,pbalanceassertion = if islast then Just assertion{baamount=setprec b{aquantity=0, aprice=Nothing}} else Nothing
}
| (a,_,_,mb) <- acctbals
, let bs = amounts $ normaliseMixedAmount mb
, let bs' = concat [reverse $ zip (reverse bs) (True : repeat False)
| bs <- groupBy ((==) `on` acommodity) bs]
, (b, islast) <- bs'
]
++
[posting{paccount = "equity:closing balances"
,pamount = negate balancingamt
}
]
openingps = [posting{paccount = a
,pamount = mixed [setprec b]
,pbalanceassertion = case mcommoditysum of
Just s -> Just assertion{baamount=setprec s{aprice=Nothing}}
Nothing -> Nothing
}
| (a,_,_,mb) <- acctbals
, let bs = amounts $ normaliseMixedAmount mb
, let bs' = concat [reverse $ zip (reverse bs) (Just commoditysum : repeat Nothing)
| bs <- groupBy ((==) `on` acommodity) bs
, let commoditysum = (sum bs)]
, (b, mcommoditysum) <- bs'
]
++
[posting{paccount = "equity:opening balances"
,pamount = balancingamt
}
]
showtxn = showTransactionUnelided
when closing $ putStr $ showtxn (nulltransaction{tdate=closingdate, tdescription="closing balances", tpostings=closingps})
when opening $ putStr $ showtxn (nulltransaction{tdate=openingdate, tdescription="opening balances", tpostings=openingps})