   (:use [wealthpulse.util :only [bigdec=]]))
 
 
-; Filters
+; Filters on entries
 
 (defn account-contains?
   "Returns a function to testing whether an entry account contains a token."

                                :period-end period-end))


(defn filter-zero-accounts
  "Filters a vector of [account, sum] tuples, removing all tuples where sum = 0."
  [account-sums]
  (filter #(not (bigdec= (nth % 1) 0M)) account-sums))



; Calculations

(defn sum-by-account
  "Returns a vector of [account, sum] tuples for all accounts in the account lineage for each entry in entries."
  [entries]
  (let [add-amount-for-accounts (fn [entry] (fn [coll account] (assoc coll account (+ (get coll account 0M) (:amount entry)))))
        for-each-account-in-lineage-add-amount (fn [coll entry] (reduce (add-amount-for-accounts entry) coll (:account-lineage entry)))
        account-sums (reduce for-each-account-in-lineage-add-amount {} entries)]
    (map #(identity [% (account-sums %)]) (keys account-sums))))




    :period-end :: select entries up to and including this date."
  [journal {:keys [accounts-with exclude-accounts-with period-start period-end] :as filters}]
  (let [filtered-entries (filter-entries journal filters)
        account-balances (-> filtered-entries
                             calc-account-balances
                             exclude-zero-accounts
                             exclude-parent-accounts-where-child-has-same-amount)]
    account-balances)
  ;calculate total balance
  ;return amounts & total balance
  )
