 (defn- transform-account
   [account depth]
   (concat [{:caption (:name account)
-            :value (+ (:balance account) (:children-balance account))
+            :value (+ (:value account) (:children-value account))
             :style :data
             :depth depth}]
           (mapcat #(transform-account % (+ 1 depth))

(defn- set-balance-delta
  [storage-spec account start end]
  (let [children (set-balance-deltas storage-spec start end (:children account))
        new-children-balance (reduce #(+ %1
                                         (:balance %2)
                                         (:children-balance %2))
                                     0
                                     children)
        new-balance (transactions/balance-delta storage-spec
                                                (:id account)
                                                start
                                                end)]
    (assoc account :children children
                   :children-balance new-children-balance
                   :balance new-balance)))

; TODO consider removing this function
(defn- set-balance-deltas

  [account-group calc-fn]
  (let [updated (update-in account-group [:accounts] calc-fn)]
    (assoc updated :value (reduce #(+ %1
                                      (:value %2)
                                      (:children-value %2))
                                  (bigdec 0)
                                  (:accounts updated)))))

