{-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} module Hcompta.Journal where import Data.Map.Strict (Map) import qualified Data.Map.Strict as Data.Map import Hcompta.Date (Date) import Hcompta.Lib.Consable (Consable(..)) class Transaction t where transaction_date :: t -> Date newtype Journal t = Journal (Map Date [t]) deriving (Eq, Show) instance Foldable Journal where foldMap f (Journal t) = foldMap (foldMap f) t instance Transaction t => Monoid (Journal t) where mempty = Journal mempty mappend (Journal x) (Journal y) = Journal $ Data.Map.unionWith mappend x y instance Transaction t => Consable (Journal) t where mcons t (Journal !ts) = Journal $ Data.Map.insertWith mappend (transaction_date t) [t] ts journal :: Transaction t => t -> Journal t -> Journal t journal = mcons transactions :: Transaction t => Journal t -> Map Date [t] transactions (Journal ts) = ts