mirror of
https://github.com/simonmichael/hledger.git
synced 2024-11-08 07:09:28 +03:00
65 lines
2.2 KiB
Haskell
65 lines
2.2 KiB
Haskell
{-|
|
|
|
|
A 'TimeLog' is a parsed timelog file (generated by timeclock.el),
|
|
containing zero or more 'TimeLogEntry's. It can be converted to a
|
|
'RawLedger' for querying.
|
|
|
|
-}
|
|
|
|
module Ledger.TimeLog
|
|
where
|
|
|
|
import Ledger.Utils
|
|
import Ledger.Types
|
|
import Ledger.Currency
|
|
import Ledger.Amount
|
|
import Ledger.RawTransaction
|
|
import Ledger.Entry
|
|
import Ledger.RawLedger
|
|
|
|
instance Show TimeLogEntry where
|
|
show t = printf "%s %s %s" (show $ tlcode t) (tldatetime t) (tlcomment t)
|
|
|
|
instance Show TimeLog where
|
|
show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl
|
|
|
|
-- | Convert a time log to a ledger.
|
|
ledgerFromTimeLog :: TimeLog -> RawLedger
|
|
ledgerFromTimeLog tl = RawLedger [] [] (entriesFromTimeLogEntries $ timelog_entries tl) ""
|
|
|
|
-- | Convert time log entries to ledger entries.
|
|
entriesFromTimeLogEntries :: [TimeLogEntry] -> [Entry]
|
|
entriesFromTimeLogEntries [] = []
|
|
entriesFromTimeLogEntries [i] = entriesFromTimeLogEntries [i, clockoutFor i]
|
|
entriesFromTimeLogEntries (i:o:rest) = [entryFromTimeLogInOut i o] ++ entriesFromTimeLogEntries rest
|
|
|
|
-- | When there is a trailing clockin entry, provide the missing clockout.
|
|
-- "Now" would be preferable but requires IO, for now use the clockin time.
|
|
clockoutFor (TimeLogEntry _ t _) = TimeLogEntry 'o' t ""
|
|
|
|
-- | Convert a timelog clockin and clockout entry to an equivalent ledger
|
|
-- entry, representing the time expenditure.
|
|
entryFromTimeLogInOut :: TimeLogEntry -> TimeLogEntry -> Entry
|
|
entryFromTimeLogInOut i o =
|
|
Entry {
|
|
edate = indate, -- ledger uses outdate
|
|
estatus = True,
|
|
ecode = "",
|
|
edescription = acctname,
|
|
ecomment = "",
|
|
etransactions = txns,
|
|
epreceding_comment_lines=""
|
|
}
|
|
where
|
|
acctname = tlcomment i
|
|
indate = showDateFrom intime
|
|
outdate = showDateFrom outtime
|
|
intime = parsedatetime $ tldatetime i
|
|
outtime = parsedatetime $ tldatetime o
|
|
hours = realToFrac (diffUTCTime outtime intime) / 3600
|
|
amount = Amount (getcurrency "h") hours 1
|
|
txns = [RawTransaction acctname amount "", RawTransaction "assets:TIME" (-amount) ""]
|
|
|
|
showDateFrom :: UTCTime -> String
|
|
showDateFrom = formatTime defaultTimeLocale "%Y/%m/%d"
|