Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle bucket directive
  • Loading branch information
jneubrand committed Apr 21, 2020
1 parent 058807c commit 9de2c32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions hledger-lib/Hledger/Data/Journal.hs
Expand Up @@ -177,6 +177,7 @@ instance Semigroup Journal where
,jparsedefaultcommodity = jparsedefaultcommodity j2
,jparseparentaccounts = jparseparentaccounts j2
,jparsealiases = jparsealiases j2
,jparsedefaultaccount = jparsedefaultaccount j2
-- ,jparsetransactioncount = jparsetransactioncount j1 + jparsetransactioncount j2
,jparsetimeclockentries = jparsetimeclockentries j1 <> jparsetimeclockentries j2
,jincludefilestack = jincludefilestack j2
Expand All @@ -202,6 +203,7 @@ nulljournal = Journal {
,jparsedefaultcommodity = Nothing
,jparseparentaccounts = []
,jparsealiases = []
,jparsedefaultaccount = Nothing
-- ,jparsetransactioncount = 0
,jparsetimeclockentries = []
,jincludefilestack = []
Expand Down
1 change: 1 addition & 0 deletions hledger-lib/Hledger/Data/Types.hs
Expand Up @@ -463,6 +463,7 @@ data Journal = Journal {
,jparsedefaultcommodity :: Maybe (CommoditySymbol,AmountStyle) -- ^ the current default commodity and its format, specified by the most recent D directive
,jparseparentaccounts :: [AccountName] -- ^ the current stack of parent account names, specified by apply account directives
,jparsealiases :: [AccountAlias] -- ^ the current account name aliases in effect, specified by alias directives (& options ?)
,jparsedefaultaccount :: Maybe AccountName
-- ,jparsetransactioncount :: Integer -- ^ the current count of transactions parsed so far (only journal format txns, currently)
,jparsetimeclockentries :: [TimeclockEntry] -- ^ timeclock sessions which have not been clocked out
,jincludefilestack :: [FilePath]
Expand Down
8 changes: 8 additions & 0 deletions hledger-lib/Hledger/Read/Common.hs
Expand Up @@ -48,6 +48,8 @@ module Hledger.Read.Common (
finaliseJournal,
setYear,
getYear,
setDefaultAccount,
getDefaultAccount,
setDefaultCommodityAndStyle,
getDefaultCommodityAndStyle,
getDefaultAmountStyle,
Expand Down Expand Up @@ -373,6 +375,12 @@ getParentAccount = fmap (concatAccountNames . reverse . jparseparentaccounts) ge
addAccountAlias :: MonadState Journal m => AccountAlias -> m ()
addAccountAlias a = modify' (\(j@Journal{..}) -> j{jparsealiases=a:jparsealiases})

setDefaultAccount :: MonadState Journal m => Maybe AccountName -> m ()
setDefaultAccount a = modify' (\(j@Journal{..}) -> j{jparsedefaultaccount=a})

getDefaultAccount :: JournalParser m (Maybe AccountName)
getDefaultAccount = jparsedefaultaccount `fmap` get

getAccountAliases :: MonadState Journal m => m [AccountAlias]
getAccountAliases = fmap jparsealiases get

Expand Down
22 changes: 21 additions & 1 deletion hledger-lib/Hledger/Read/JournalReader.hs
Expand Up @@ -237,6 +237,7 @@ directivep = (do
,defaultcommoditydirectivep
,commodityconversiondirectivep
,ignoredpricecommoditydirectivep
,bucketdirectivep
]
) <?> "directive"

Expand Down Expand Up @@ -593,6 +594,13 @@ commodityconversiondirectivep = do
lift restofline
return ()

bucketdirectivep :: JournalParser m ()
bucketdirectivep = do
keywordp "bucket" <?> "bucket directive"
lift (skipMany spacenonewline)
acct <- modifiedaccountnamep
setDefaultAccount (Just acct)

--- *** transactions

-- | Parse a transaction modifier (auto postings) rule.
Expand Down Expand Up @@ -687,8 +695,20 @@ transactionp = do
let year = first3 $ toGregorian date
postings <- postingsp (Just year)
endpos <- getSourcePos
acc <- getDefaultAccount
let postingsBucketed = (
postings ++ [posting { pdate=Nothing
, pdate2=Nothing
, pstatus=Unmarked
, paccount=fromJust acc
, pamount=missingmixedamt
, pcomment=""
, ptype=RegularPosting
, ptags=[]
, pbalanceassertion=Nothing
} | all hasAmount postings && isJust acc])
let sourcepos = journalSourcePos startpos endpos
return $ txnTieKnot $ Transaction 0 "" sourcepos date edate status code description comment tags postings
return $ txnTieKnot $ Transaction 0 "" sourcepos date edate status code description comment tags postingsBucketed

--- *** postings

Expand Down

0 comments on commit 9de2c32

Please sign in to comment.