2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
2008-10-03 17:41:27 +04:00
|
|
|
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.
|
2008-10-03 06:04:15 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2008-10-03 04:40:06 +04:00
|
|
|
module Ledger.TimeLog
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2008-10-03 17:41:27 +04:00
|
|
|
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.Currency
|
|
|
|
import Ledger.Amount
|
|
|
|
import Ledger.RawTransaction
|
2008-10-03 06:37:19 +04:00
|
|
|
import Ledger.Entry
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.RawLedger
|
2007-03-12 10:40:33 +03:00
|
|
|
|
|
|
|
instance Show TimeLogEntry where
|
2007-07-04 16:05:54 +04:00
|
|
|
show t = printf "%s %s %s" (show $ tlcode t) (tldatetime t) (tlcomment t)
|
2007-03-12 10:40:33 +03:00
|
|
|
|
|
|
|
instance Show TimeLog where
|
|
|
|
show tl = printf "TimeLog with %d entries" $ length $ timelog_entries tl
|
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Convert a time log to a ledger.
|
2008-10-03 02:17:04 +04:00
|
|
|
ledgerFromTimeLog :: TimeLog -> RawLedger
|
2008-10-09 01:08:42 +04:00
|
|
|
ledgerFromTimeLog tl = RawLedger [] [] (entriesFromTimeLogEntries $ timelog_entries tl) ""
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | Convert time log entries to ledger entries.
|
2008-10-03 06:37:19 +04:00
|
|
|
entriesFromTimeLogEntries :: [TimeLogEntry] -> [Entry]
|
2008-10-09 01:08:42 +04:00
|
|
|
entriesFromTimeLogEntries [] = []
|
|
|
|
entriesFromTimeLogEntries [i] = entriesFromTimeLogEntries [i, clockoutFor i]
|
|
|
|
entriesFromTimeLogEntries (i:o:rest) = [entryFromTimeLogInOut i o] ++ entriesFromTimeLogEntries rest
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-10-08 21:00:22 +04:00
|
|
|
-- | When there is a trailing clockin entry, provide the missing clockout.
|
2008-10-09 01:08:42 +04:00
|
|
|
-- "Now" would be preferable but requires IO, for now use the clockin time.
|
|
|
|
clockoutFor (TimeLogEntry _ t _) = TimeLogEntry 'o' t ""
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-10-09 01:08:42 +04:00
|
|
|
-- | 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 = "",
|
2008-10-09 11:08:06 +04:00
|
|
|
edescription = "",
|
2008-10-09 01:08:42 +04:00
|
|
|
ecomment = "",
|
|
|
|
etransactions = txns,
|
|
|
|
epreceding_comment_lines=""
|
|
|
|
}
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2008-10-09 01:08:42 +04:00
|
|
|
acctname = tlcomment i
|
|
|
|
indate = showDateFrom intime
|
|
|
|
outdate = showDateFrom outtime
|
|
|
|
intime = parsedatetime $ tldatetime i
|
|
|
|
outtime = parsedatetime $ tldatetime o
|
2008-10-09 11:07:48 +04:00
|
|
|
hours = realToFrac (diffUTCTime outtime intime) / 3600
|
2008-10-09 01:08:42 +04:00
|
|
|
amount = Amount (getcurrency "h") hours 1
|
|
|
|
txns = [RawTransaction acctname amount "", RawTransaction "assets:TIME" (-amount) ""]
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2008-10-03 17:41:27 +04:00
|
|
|
showDateFrom :: UTCTime -> String
|
|
|
|
showDateFrom = formatTime defaultTimeLocale "%Y/%m/%d"
|