                      :items []
                      :storage storage}
                     sorted-items))))
-
-(defn- calculate-balances-and-indexes
-  "Updates transaction item and account balances resulting from the
-  specified transaction.
-  
-  The balance for each transaction item is the sum of the balance of
-  the previous transaction item and the polarized transaction amount.
-  
-  The polarized transaction amount is the positive or negative change
-  on the balance of the associated account and is dependant on the action
-  (debit or credit) and the account type (asset, liability, equity,
-  income, or expense)"
-  [storage items]
-  (->> items
-       (group-by :account-id)
-       (mapcat #(add-item-balance-and-index storage %))))
 
 (defn- subsequent-items
   "Returns items in the specified account on or after the specified

  ([item] (after-item-read item nil))
  ([item account]
   (if (map? item)
     (cond-> item
       true (update-in [:action] keyword)
       true (assoc :reconciled? (= "completed" (:reconciliation-status item)))
       account (polarize-item-amount account))
     item)))

(defn- item-value-sum

(defn- create-transaction-and-adjust-balances
  [storage transaction]
  (let [created (link-lots storage (create-transaction* storage transaction))]
    (doall (->> (:items transaction)

                ; create database records
                (map #(assoc % :transaction-id (:id created)))
                (map #(create-transaction-item* storage %))

                ; process indexes and balances, update affected accounts
                (group-by :account-id)
                (map second)
                (map #(sort-by :index %))
                (map first)
                (map #(get-previous-item storage %))
                (map #(recalculate-account-items storage %))
                (map #(recalculate-account-items storage %))))
    (reload storage created)))

(defn create
