       (if (validation/has-error? validated)
         validated
         (if (delay-balances? (:entity-id transaction))
-            (create-transaction-without-balances s validated)
-            (create-transaction-and-adjust-balances s validated))))))
+          (create-transaction-without-balances s validated)
+          (create-transaction-and-adjust-balances s validated))))))
 
 (defn find-by-id
   "Returns the specified transaction"

                                    s
                                    (before-save-item item)))]
      (> records-affected 0))))

(defn- calculate-item-index-and-balance
  "Accepts a transaction item and a context containing
    :index   - a transaction item index
    :balance - a transaction item balance
    :storage - service used to communicate with the data store

  Calculates the new index and balance for the item and updates
  them in the data store

  Returns a context containing
    :index   - the new index
    :balance - the new balance
    :storage - unchanged

  This is done in the context of updating existing transaction
  items that are affected by a new or updated transaction"
  [{:keys [index balance storage]} item]
  (let [new-index (+ 1 index)
        account (accounts/find-by-id storage (:account-id item)) ; TODO: remove redundant calls to find the account
        polarized-amount (accounts/polarize-amount item account)
        new-balance (+ balance polarized-amount)
        value-changed (update-item-index-and-balance storage (assoc item
                                                                    :index new-index
                                                                    :balance new-balance))
        result {:index new-index
                :balance new-balance
                :storage storage}]
    (if value-changed
      result
      (-> result
          (assoc ::skip-account-update true)
          reduced))))

(defn- update-affected-balances
  "Updates transaction items and corresponding accounts that
  succeed the specified items"
  [storage-spec items transaction-date]
  (doseq [[account-id items] (group-by :account-id items)]
    (let [last-item (last items) ; these should already be sorted
          account (accounts/find-by-id storage-spec account-id)
          subsequent-items (subsequent-items storage-spec last-item)
          final (reduce calculate-item-index-and-balance
                        (assoc last-item :storage storage-spec)
                        subsequent-items)]
      (when-not (::skip-account-update final)
        (accounts/update storage-spec (assoc account :balance (:balance final)))))))

(declare reload)
(defn- no-reconciled-items-changed?

     ~@body

     ; Recalculate balances for affected accounts
     (->> (get-in @ambient-settings [~entity-id :delayed-account-ids])
          (map #(hash-map :account-id % :index -1 :balance 0M))
          (map #(recalculate-account-items ~storage-spec %))
          doall)

     ; clean up the ambient settings as if we were never here
     (swap! ambient-settings dissoc ~entity-id)))
