added ability to customize accounts.

This commit is contained in:
Bryce Covert
2020-05-11 06:57:37 -07:00
parent 7af6873cfc
commit 8f66aac170

View File

@@ -185,3 +185,43 @@
:in ['$]
:where ['[?e :account/numeric-code ?z]]}
:args [(d/db (d/connect uri))]}))))
(defn customize-accounts [customer filename]
(let [conn (d/connect uri)
[header & rows] (-> filename (io/resource) io/input-stream (BOMInputStream.) (io/reader) csv/read-csv)
[client-id] (first (d/query (-> {:query {:find ['?e]
:in ['$ '?z]
:where [['?e :client/code '?z]]}
:args [(d/db (d/connect uri)) customer]})))
_ (println client-id)
headers (map read-string header)
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
:db/id])]
:in ['$]
:where ['[?e :account/name]]}
:args [(d/db conn)]})))
txes (transduce
(comp
(filter (fn [[_ account _ override-name]]
(and
(not (str/blank? override-name))
(not (str/blank? account)))))
(map (fn parse-map [[_ account account-name override-name _ type]]
(let [code (Integer/parseInt account)
existing-id (:db/id (code->existing-account code))]
(cond-> {:account/client-overrides [{:account-client-override/client client-id
:account-client-override/name override-name}]}
existing-id (assoc :db/id existing-id)
(not existing-id) (assoc :account/applicability :account-applicability/customized
:account/name account-name
:account/account-set "default"
:account/numeric-code code
:account/code (str code)
:account/type (keyword "account-type" (str/lower-case type))))))))
conj
[]
rows)]
@(d/transact conn txes)))