lib: fix generation of periodic transactions with days/months/... repeat
This commit is contained in:
parent
2bb6e25390
commit
cf4029a5ed
@ -60,7 +60,6 @@ module Hledger.Data.Dates (
|
|||||||
spansSpan,
|
spansSpan,
|
||||||
spanIntersect,
|
spanIntersect,
|
||||||
spansIntersect,
|
spansIntersect,
|
||||||
spanIntervalIntersect,
|
|
||||||
spanDefaultsFrom,
|
spanDefaultsFrom,
|
||||||
spanUnion,
|
spanUnion,
|
||||||
spansUnion,
|
spansUnion,
|
||||||
@ -263,27 +262,6 @@ spanIntersect (DateSpan b1 e1) (DateSpan b2 e2) = DateSpan b e
|
|||||||
b = latest b1 b2
|
b = latest b1 b2
|
||||||
e = earliest e1 e2
|
e = earliest e1 e2
|
||||||
|
|
||||||
-- | Calculate the intersection of two DateSpans, adjusting the start date so
|
|
||||||
-- the interval is preserved.
|
|
||||||
--
|
|
||||||
-- >>> let intervalIntersect = spanIntervalIntersect (Days 3)
|
|
||||||
-- >>> mkdatespan "2018-01-01" "2018-01-03" `intervalIntersect` mkdatespan "2018-01-01" "2018-01-05"
|
|
||||||
-- DateSpan 2018/01/01-2018/01/02
|
|
||||||
-- >>> mkdatespan "2018-01-01" "2018-01-05" `intervalIntersect` mkdatespan "2018-01-02" "2018-01-05"
|
|
||||||
-- DateSpan 2018/01/04
|
|
||||||
-- >>> mkdatespan "2018-01-01" "2018-01-05" `intervalIntersect` mkdatespan "2018-01-03" "2018-01-05"
|
|
||||||
-- DateSpan 2018/01/04
|
|
||||||
-- >>> mkdatespan "2018-01-01" "2018-01-05" `intervalIntersect` mkdatespan "2018-01-04" "2018-01-05"
|
|
||||||
-- DateSpan 2018/01/04
|
|
||||||
-- >>> mkdatespan "2018-01-01" "2018-01-05" `intervalIntersect` mkdatespan "2017-12-01" "2018-01-05"
|
|
||||||
-- DateSpan 2018/01/01-2018/01/04
|
|
||||||
spanIntervalIntersect :: Interval -> DateSpan -> DateSpan -> DateSpan
|
|
||||||
spanIntervalIntersect (Days n) (DateSpan (Just b1) e1) sp2@(DateSpan (Just b2) _) =
|
|
||||||
DateSpan (Just b) e1 `spanIntersect` sp2
|
|
||||||
where
|
|
||||||
b = if b1 < b2 then addDays (diffDays b1 b2 `mod` toInteger n) b2 else b1
|
|
||||||
spanIntervalIntersect _ sp1 sp2 = sp1 `spanIntersect` sp2
|
|
||||||
|
|
||||||
-- | Fill any unspecified dates in the first span with the dates from
|
-- | Fill any unspecified dates in the first span with the dates from
|
||||||
-- the second one. Sort of a one-way spanIntersect.
|
-- the second one. Sort of a one-way spanIntersect.
|
||||||
spanDefaultsFrom (DateSpan a1 b1) (DateSpan a2 b2) = DateSpan a b
|
spanDefaultsFrom (DateSpan a1 b1) (DateSpan a2 b2) = DateSpan a b
|
||||||
|
|||||||
@ -24,7 +24,7 @@ import Hledger.Data.Amount
|
|||||||
import Hledger.Data.Posting (post, commentAddTagNextLine)
|
import Hledger.Data.Posting (post, commentAddTagNextLine)
|
||||||
import Hledger.Data.Transaction
|
import Hledger.Data.Transaction
|
||||||
import Hledger.Utils.UTF8IOCompat (error')
|
import Hledger.Utils.UTF8IOCompat (error')
|
||||||
-- import Hledger.Utils.Debug
|
import Hledger.Utils.Debug
|
||||||
|
|
||||||
-- $setup
|
-- $setup
|
||||||
-- >>> :set -XOverloadedStrings
|
-- >>> :set -XOverloadedStrings
|
||||||
@ -201,9 +201,8 @@ instance Show PeriodicTransaction where
|
|||||||
--
|
--
|
||||||
runPeriodicTransaction :: PeriodicTransaction -> DateSpan -> [Transaction]
|
runPeriodicTransaction :: PeriodicTransaction -> DateSpan -> [Transaction]
|
||||||
runPeriodicTransaction PeriodicTransaction{..} requestedspan =
|
runPeriodicTransaction PeriodicTransaction{..} requestedspan =
|
||||||
[ t{tdate=d} | (DateSpan (Just d) _) <- ptinterval `splitSpan` spantofill ]
|
[ t{tdate=d} | (DateSpan (Just d) _) <- alltxnspans, spanContainsDate requestedspan d ]
|
||||||
where
|
where
|
||||||
spantofill = spanIntervalIntersect ptinterval ptspan requestedspan
|
|
||||||
t = nulltransaction{
|
t = nulltransaction{
|
||||||
tstatus = ptstatus
|
tstatus = ptstatus
|
||||||
,tcode = ptcode
|
,tcode = ptcode
|
||||||
@ -216,6 +215,10 @@ runPeriodicTransaction PeriodicTransaction{..} requestedspan =
|
|||||||
,tpostings = ptpostings
|
,tpostings = ptpostings
|
||||||
}
|
}
|
||||||
period = "~ " <> ptperiodexpr
|
period = "~ " <> ptperiodexpr
|
||||||
|
-- All spans described by this periodic transaction, where spanStart is event date.
|
||||||
|
-- If transaction does not have start/end date, we set them to start/end of requested span,
|
||||||
|
-- to avoid generating (infinitely) many events.
|
||||||
|
alltxnspans = dbg3 "alltxnspans" $ ptinterval `splitSpan` (spanDefaultsFrom ptspan requestedspan)
|
||||||
|
|
||||||
-- | Check that this date span begins at a boundary of this interval,
|
-- | Check that this date span begins at a boundary of this interval,
|
||||||
-- or return an explanatory error message including the provided period expression
|
-- or return an explanatory error message including the provided period expression
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user