From 52de2690b31c26648bf5ce6c8233a0f3d62a075e Mon Sep 17 00:00:00 2001 From: Bryce Date: Mon, 21 Aug 2023 20:59:49 -0700 Subject: [PATCH] adds export of account lookup --- src/clj/auto_ap/routes/exports.clj | 94 +++++++++++++++++++++++++++++- src/clj/user.clj | 12 ++-- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/src/clj/auto_ap/routes/exports.clj b/src/clj/auto_ap/routes/exports.clj index 61f8bd19..fd285f83 100644 --- a/src/clj/auto_ap/routes/exports.clj +++ b/src/clj/auto_ap/routes/exports.clj @@ -22,12 +22,12 @@ [datomic.api :as dc] [ring.middleware.json :refer [wrap-json-response]] [venia.core :as venia] - [auto-ap.logging :as alog])) + [auto-ap.logging :as alog] + [com.brunobonacci.mulog :as mu])) (defn wrap-csv-response [handler] (fn [request] (let [response (handler request)] - (println "RESPONSE IS" response) (update response :body #(with-open [w (java.io.StringWriter.)] (csv/write-csv w %) (.toString w)))))) @@ -462,6 +462,92 @@ {:body (into (list) (apply dc/q (read-string (get query-params "query" )) (into [(dc/db conn)] (read-string (get query-params "args" "[]")))))})) +(defn my-compare [^String a ^String b] + (.compareTo a b)) + +(defn find-account-snapshot [^java.util.ArrayList account-lookup-keys ^java.util.ArrayList account-snapshot-values v] + (let [best-index (java.util.Collections/binarySearch account-lookup-keys v compare) + best-index (if (neg? best-index) + (dec (Math/abs best-index)) ;; fill forward + best-index) + best-value (nth account-snapshot-values best-index)] + best-value)) + +(defn export-ntg-account-snapshot [_] + (alog/info ::starting-account-snapshot) + (let [clients (pull-many (dc/db conn) '[:db/id :client/code :client/locations] [#_[:client/code "NGAK"] [:client/code "NGOP"] [:client/code "NGRV"] [:client/code "NGE1"]]) + account->numeric-code (into {} (seq (dc/q '[:find ?i ?n + :in $ [?a ...] + :where [?i ?a ?n]] + (dc/db conn) [:account/numeric-code :bank-account/numeric-code]))) + account->name (let [lookup (into {} (seq (dc/q '[:find ?i ?n + :in $ + :where [?i :account/name ?n]] + (dc/db conn))))] + (fn account->name [x] + (or (lookup x) "Bank Account"))) + account->location (into {} (seq (dc/q '[:find ?i ?l + :in $ + :where [?i :account/location ?l]] + (dc/db conn)))) + used-accounts (->> (for [c clients + d (dc/index-range (dc/db conn) + :journal-entry-line/client+account+location+date + [(:db/id c)] + [(inc (:db/id c))]) + :let [[_ account] (:v d)]] + account) + (into #{}) + (sort-by account->numeric-code)) + dates (->> (range -365 0) + (map (fn [delta] + (atime/unparse-local (time/plus (time/floor (time/plus (atime/local-now) (time/days 1)) time/day) (time/days delta)) + atime/iso-date))))] + (alog/info ::continue-account-snapshot + :used-accounts (count used-accounts) + :date-count (count dates)) + {:status 200 + :body + (for [client clients + :let [all-entries (->> (dc/index-pull (dc/db conn) + {:index :avet + :selector [:db/id :journal-entry-line/running-balance :journal-entry-line/client+account+location+date] + :start [:journal-entry-line/client+account+location+date [(:db/id client)]] + :reverse false + :limit 1}) + (take-while (fn matches-client [curr] + (= (first (:journal-entry-line/client+account+location+date curr)) + (:db/id client))))) + account-lookup (->> all-entries + (reduce + (fn build-account-lookup [acc curr] + (let [[client account location date] (:journal-entry-line/client+account+location+date curr) + numeric-code (account->numeric-code account)] + (assoc acc (format "%d-%d-%s-%s" client numeric-code location (atime/unparse-local (clj-time.coerce/to-date-time date) atime/iso-date)) (:journal-entry-line/running-balance curr)))) + (sorted-map))) + account-lookup-keys (java.util.ArrayList. (keys account-lookup)) + account-lookup-values (java.util.ArrayList. (map second account-lookup)) + _ (alog/info ::account-lookup-size + :size (count account-lookup))] + a (sort-by account->numeric-code used-accounts) + :let [numeric-code (account->numeric-code a)] + l (or (account->location a) (:client/locations client)) + :when (and numeric-code (>= numeric-code 40000)) + date-str dates] + [(:client/code client) l numeric-code (account->name a) date-str + (with-precision 2 + (double (.setScale (bigdec (or + (find-account-snapshot account-lookup-keys account-lookup-values (format "%d-%d-%s-%s" (:db/id client) numeric-code l date-str)) + 0.0)) + 2 java.math.RoundingMode/HALF_UP)))] + #_(conj [(:client/code client) l numeric-code (account->name a) date-str]))})) + +(defn wrap-predetermined-api-key [handler key] + (fn [request] + (if (= key (get (:headers request) "authorization")) + (handler request) + {:status 401}))) + (def routes2 {"api/" {"sales/" {"aggregated/" {#"export/?" {:get :aggregated-sales-export}} #"export/?" {:get :export-sales}} @@ -472,7 +558,8 @@ "vendors/" {#"export/?" {:get :export-vendors} "company/" {#"export" {:get :export-company-vendors}}} "ledger/" {#"export/?" {:get :export-ledger} - #"trial-balance/export/?" {:get :export-trial-balance}} + #"trial-balance/export/?" {:get :export-trial-balance} + #"account-snapshot/export" {:get :export-ntg-account-snapshot}} "accounts/" {#"export/?" {:get :export-accounts}} "transactions/" {#"export/?" {:get :export-transactions} #"export2/?" {:get :export-transactions2}} @@ -487,6 +574,7 @@ :export-vendors (-> export-vendors wrap-json-response wrap-secure) :export-company-vendors (-> export-company-vendors wrap-csv-response wrap-secure) :export-ledger (-> export-ledger wrap-json-response wrap-secure) + :export-ntg-account-snapshot (-> export-ntg-account-snapshot wrap-csv-response (wrap-predetermined-api-key "fd07755a-ed4c-4c9a-ad85-fbdd8af37206")) :export-trial-balance (-> export-trial-balance wrap-csv-response wrap-secure) :export-accounts (-> export-accounts wrap-json-response wrap-secure) :export-transactions (-> export-transactions wrap-json-response wrap-secure) diff --git a/src/clj/user.clj b/src/clj/user.clj index 12b2c5e4..54cdbf8a 100644 --- a/src/clj/user.clj +++ b/src/clj/user.clj @@ -32,15 +32,17 @@ (org.apache.commons.io.input BOMInputStream))) (defn println-event [item] - (printf "%s - %s:%s %s\n" (:mulog/namespace item) (:mulog/event-name item) + (printf "%s: %s - %s:%s %s\n" + (str (c/to-date-time (:mulog/timestamp item))) + (:mulog/namespace item) (:mulog/event-name item) (if (:mulog/duration item) (str " " (int (/ (:mulog/duration item) 1000000)) "ms") "") (pr-str (reduce - (fn [acc [k v]] - (assoc acc k v)) - {} - item)))) + (fn [acc [k v]] + (assoc acc k v)) + {} + item)))) (deftype DevPublisher [config buffer transform]