2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
2008-10-15 10:00:10 +04:00
|
|
|
A 'TimeLog' is a parsed timelog file (see timeclock.el or the command-line
|
|
|
|
version) containing zero or more 'TimeLogEntry's. It can be converted to a
|
2008-10-03 17:41:27 +04:00
|
|
|
'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 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-11-27 03:35:00 +03:00
|
|
|
import Ledger.Dates
|
2008-10-13 01:52:48 +04:00
|
|
|
import Ledger.Commodity
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.Amount
|
2009-04-03 14:58:05 +04:00
|
|
|
import Ledger.LedgerTransaction
|
2007-03-12 10:40:33 +03:00
|
|
|
|
|
|
|
instance Show TimeLogEntry where
|
2008-11-11 15:34:05 +03:00
|
|
|
show t = printf "%s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlcomment t)
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
-- | Convert time log entries to ledger transactions. When there is no
|
2009-01-26 23:22:42 +03:00
|
|
|
-- clockout, add one with the provided current time. Sessions crossing
|
2009-03-15 15:42:03 +03:00
|
|
|
-- midnight are split into days to give accurate per-day totals.
|
2009-04-03 14:58:05 +04:00
|
|
|
entriesFromTimeLogEntries :: LocalTime -> [TimeLogEntry] -> [LedgerTransaction]
|
2009-01-25 10:06:59 +03:00
|
|
|
entriesFromTimeLogEntries _ [] = []
|
2009-04-01 09:00:08 +04:00
|
|
|
entriesFromTimeLogEntries now [i]
|
|
|
|
| odate > idate = [entryFromTimeLogInOut i o'] ++ entriesFromTimeLogEntries now [i',o]
|
2009-03-15 15:42:03 +03:00
|
|
|
| otherwise = [entryFromTimeLogInOut i o]
|
|
|
|
where
|
2009-04-01 09:00:08 +04:00
|
|
|
o = TimeLogEntry 'o' end ""
|
|
|
|
end = if itime > now then itime else now
|
2009-03-15 15:42:03 +03:00
|
|
|
(itime,otime) = (tldatetime i,tldatetime o)
|
|
|
|
(idate,odate) = (localDay itime,localDay otime)
|
|
|
|
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
|
|
|
|
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
|
2009-04-01 09:00:08 +04:00
|
|
|
entriesFromTimeLogEntries now (i:o:rest)
|
|
|
|
| odate > idate = [entryFromTimeLogInOut i o'] ++ entriesFromTimeLogEntries now (i':o:rest)
|
|
|
|
| otherwise = [entryFromTimeLogInOut i o] ++ entriesFromTimeLogEntries now rest
|
2009-01-26 23:22:42 +03:00
|
|
|
where
|
|
|
|
(itime,otime) = (tldatetime i,tldatetime o)
|
|
|
|
(idate,odate) = (localDay itime,localDay otime)
|
|
|
|
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
|
|
|
|
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
|
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
|
2008-10-15 23:11:06 +04:00
|
|
|
-- entry, representing the time expenditure. Note this entry is not balanced,
|
|
|
|
-- since we omit the \"assets:time\" transaction for simpler output.
|
2009-04-03 14:58:05 +04:00
|
|
|
entryFromTimeLogInOut :: TimeLogEntry -> TimeLogEntry -> LedgerTransaction
|
2008-12-11 04:35:07 +03:00
|
|
|
entryFromTimeLogInOut i o
|
2009-04-03 14:58:05 +04:00
|
|
|
| otime >= itime = t
|
2008-12-11 04:35:07 +03:00
|
|
|
| otherwise =
|
2009-04-03 14:58:05 +04:00
|
|
|
error $ "clock-out time less than clock-in time in:\n" ++ showLedgerTransaction t
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2009-04-03 14:58:05 +04:00
|
|
|
t = LedgerTransaction {
|
|
|
|
ltdate = idate,
|
|
|
|
ltstatus = True,
|
|
|
|
ltcode = "",
|
|
|
|
ltdescription = showtime itod ++ "-" ++ showtime otod,
|
|
|
|
ltcomment = "",
|
|
|
|
ltpostings = ps,
|
|
|
|
ltpreceding_comment_lines=""
|
2008-12-11 04:35:07 +03:00
|
|
|
}
|
2009-01-25 10:06:59 +03:00
|
|
|
showtime = take 5 . show
|
2008-10-09 01:08:42 +04:00
|
|
|
acctname = tlcomment i
|
2009-01-25 10:06:59 +03:00
|
|
|
itime = tldatetime i
|
|
|
|
otime = tldatetime o
|
|
|
|
itod = localTimeOfDay itime
|
|
|
|
otod = localTimeOfDay otime
|
|
|
|
idate = localDay itime
|
|
|
|
odate = localDay otime
|
|
|
|
hrs = elapsedSeconds (toutc otime) (toutc itime) / 3600 where toutc = localTimeToUTC utc
|
|
|
|
amount = Mixed [hours hrs]
|
2009-04-03 14:58:05 +04:00
|
|
|
ps = [Posting False acctname amount "" RegularPosting
|
|
|
|
--,Posting "assets:time" (-amount) "" RegularPosting
|
2008-10-15 23:11:06 +04:00
|
|
|
]
|