revamped logging!
This commit is contained in:
@@ -19,8 +19,6 @@
|
||||
|
||||
(let [client (when (:id edit_client) (d-clients/get-by-id (:id edit_client)))
|
||||
id (or (:db/id client) "new-client")
|
||||
_ (println id)
|
||||
_ (println edit_client)
|
||||
_ (when client
|
||||
@(d/transact (d/connect uri)
|
||||
(into
|
||||
@@ -79,7 +77,6 @@
|
||||
)
|
||||
(:forecasted_transactions edit_client))]]
|
||||
result @(d/transact (d/connect uri) transactions)]
|
||||
(println result "ID" id)
|
||||
(-> result :tempids (get id) (or id) d-clients/get-by-id
|
||||
(update :client/location-matches
|
||||
(fn [lms]
|
||||
|
||||
@@ -58,7 +58,6 @@
|
||||
:location location}))
|
||||
|
||||
(defn add-invoice-transaction [{:keys [total invoice_number location automatically_paid_when_due client_id vendor_id vendor_name date due expense_accounts] :as in}]
|
||||
(println date)
|
||||
(let [vendor (d-vendors/get-by-id vendor_id)
|
||||
account (:vendor/default-account vendor)
|
||||
_ (when-not (:db/id account)
|
||||
@@ -119,11 +118,10 @@
|
||||
|
||||
(defn edit-invoice [context {{:keys [id due invoice_number total vendor_id date client_id expense_accounts automatically_paid_when_due] :as in} :invoice} value]
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
_ (when (seq (doto (d-invoices/find-conflicting {:db/id id
|
||||
:invoice/invoice-number invoice_number
|
||||
:invoice/vendor (:db/id (:invoice/vendor invoice))
|
||||
:invoice/client (:db/id (:invoice/client invoice))})
|
||||
println))
|
||||
_ (when (seq (d-invoices/find-conflicting {:db/id id
|
||||
:invoice/invoice-number invoice_number
|
||||
:invoice/vendor (:db/id (:invoice/vendor invoice))
|
||||
:invoice/client (:db/id (:invoice/client invoice))}))
|
||||
(throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number})))
|
||||
|
||||
expense-account-total (reduce + 0 (map (fn [x] (Double/parseDouble (:amount x))) expense_accounts))
|
||||
@@ -151,7 +149,6 @@
|
||||
|
||||
(defn void-invoice [context {id :invoice_id} value]
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
_ (println invoice)
|
||||
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
|
||||
updated-invoice (d-invoices/update {:db/id id
|
||||
:invoice/total 0.0
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
[auto-ap.parse.util :as parse]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.parse.templates :as t]
|
||||
[auto-ap.datomic.clients :as d-clients]))
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[clojure.tools.logging :as log]
|
||||
[unilog.context :as lc]))
|
||||
|
||||
(defn get-ledger-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
@@ -106,15 +108,12 @@
|
||||
vals
|
||||
(mapcat (fn [a]
|
||||
(map (fn [o]
|
||||
#_(println "override" (:db/id a) (:db/id (:account-client-override/client o)))
|
||||
#_(print [[(:db/id a) (:db/id (:account-client-override/client o))]])
|
||||
[[(:db/id a) (:db/id (:account-client-override/client o))]
|
||||
(:account-client-override/name o)])
|
||||
(:account/client-overrides a))
|
||||
) )
|
||||
(into {} ))]
|
||||
(fn [a]
|
||||
#_(println a client-id (keys overrides-by-client))
|
||||
{:name (or (:bank-account/name (bank-accounts a))
|
||||
(overrides-by-client [a client-id])
|
||||
(:account/name (accounts a)))
|
||||
@@ -158,7 +157,6 @@
|
||||
(defn get-profit-and-loss [context args value]
|
||||
(let [client-id (:client_id args)
|
||||
_ (assert-can-see-client (:id context) client-id)
|
||||
_ (println args)
|
||||
all-ledger-entries (full-ledger-for-client client-id)
|
||||
lookup-account (build-account-lookup client-id)]
|
||||
|
||||
@@ -179,108 +177,113 @@
|
||||
|
||||
(defn import-ledger [context args value]
|
||||
(assert-admin (:id context))
|
||||
(let [all-vendors (by :vendor/name (d-vendors/get-graphql {}))
|
||||
all-clients (by :client/code (d-clients/get-all ))
|
||||
all-client-bank-accounts (reduce
|
||||
(fn [acc client]
|
||||
(assoc acc (:client/code client)
|
||||
(set (->> (:client/bank-accounts client)
|
||||
(map :bank-account/code)
|
||||
))))
|
||||
{}
|
||||
(d-clients/get-all))
|
||||
all-client-locations (reduce
|
||||
(fn [acc client]
|
||||
(assoc acc (:client/code client)
|
||||
(-> (set (:client/locations client))
|
||||
(conj "HQ")
|
||||
(conj "A"))))
|
||||
(lc/with-context {:area "import ledger"}
|
||||
(let [all-vendors (by :vendor/name (d-vendors/get-graphql {}))
|
||||
all-clients (by :client/code (d-clients/get-all ))
|
||||
all-client-bank-accounts (reduce
|
||||
(fn [acc client]
|
||||
(assoc acc (:client/code client)
|
||||
(set (->> (:client/bank-accounts client)
|
||||
(map :bank-account/code)
|
||||
))))
|
||||
{}
|
||||
(d-clients/get-all))
|
||||
all-client-locations (reduce
|
||||
(fn [acc client]
|
||||
(assoc acc (:client/code client)
|
||||
(-> (set (:client/locations client))
|
||||
(conj "HQ")
|
||||
(conj "A"))))
|
||||
{}
|
||||
(d-clients/get-all))
|
||||
new-hidden-vendors (reduce
|
||||
(fn [new-vendors {:keys [vendor_name line_items]}]
|
||||
(if (or (all-vendors vendor_name)
|
||||
(new-vendors vendor_name))
|
||||
new-vendors
|
||||
(assoc new-vendors vendor_name
|
||||
{:vendor/name vendor_name
|
||||
:vendor/hidden true
|
||||
:db/id vendor_name})))
|
||||
{}
|
||||
(d-clients/get-all))
|
||||
new-hidden-vendors (reduce
|
||||
(fn [new-vendors {:keys [vendor_name line_items]}]
|
||||
(if (or (all-vendors vendor_name)
|
||||
(new-vendors vendor_name))
|
||||
new-vendors
|
||||
(assoc new-vendors vendor_name
|
||||
{:vendor/name vendor_name
|
||||
:vendor/hidden true
|
||||
:db/id vendor_name})))
|
||||
{}
|
||||
(:entries args))
|
||||
all-vendors (into all-vendors new-hidden-vendors)
|
||||
all-accounts (transduce (map (comp str :account/numeric-code)) conj #{} (a/get-accounts))
|
||||
transaction (doall (map
|
||||
(assoc-error (fn [entry]
|
||||
(let [entry (-> entry
|
||||
(update :amount #(Double/parseDouble %))
|
||||
(update :line_items
|
||||
(fn [lis]
|
||||
(mapv
|
||||
(fn [li ]
|
||||
(-> li
|
||||
(update :debit #(Double/parseDouble (if (str/blank? %) "0" %)))
|
||||
(update :credit #(Double/parseDouble (if (str/blank? %) "0" %)))))
|
||||
lis))))]
|
||||
(let [vendor (all-vendors (:vendor_name entry))]
|
||||
(when-not (all-clients (:client_code entry))
|
||||
(throw (Exception. (str "Client '" (:client_code entry )"' not found.")) ))
|
||||
(when-not vendor
|
||||
(throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found."))))
|
||||
(when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry))
|
||||
(throw (Exception. (str "Date must be MM/dd/yyyy"))))
|
||||
(when-not (dollars= (doto (reduce + 0.0 (map :debit (:line_items entry))))
|
||||
(reduce + 0.0 (map :credit (:line_items entry))))
|
||||
(throw (Exception. (str "Debits '"
|
||||
(reduce + 0 (map :debit (:line_items entry)))
|
||||
"' and credits '"
|
||||
(reduce + 0 (map :credit (:line_items entry)))
|
||||
"' do not add up."))))
|
||||
(remove-nils
|
||||
{:journal-entry/source (:source entry)
|
||||
:journal-entry/client [:client/code (:client_code entry)]
|
||||
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
|
||||
:journal-entry/external-id (:external_id entry)
|
||||
:journal-entry/vendor (all-vendors (:vendor_name entry))
|
||||
:journal-entry/amount (:amount entry)
|
||||
:journal-entry/note (:note entry)
|
||||
:journal-entry/cleared-against (:cleared_against entry)
|
||||
(:entries args))
|
||||
all-vendors (into all-vendors new-hidden-vendors)
|
||||
all-accounts (transduce (map (comp str :account/numeric-code)) conj #{} (a/get-accounts))
|
||||
transaction (doall (map
|
||||
(assoc-error (fn [entry]
|
||||
(let [entry (-> entry
|
||||
(update :amount #(Double/parseDouble %))
|
||||
(update :line_items
|
||||
(fn [lis]
|
||||
(mapv
|
||||
(fn [li ]
|
||||
(-> li
|
||||
(update :debit #(Double/parseDouble (if (str/blank? %) "0" %)))
|
||||
(update :credit #(Double/parseDouble (if (str/blank? %) "0" %)))))
|
||||
lis))))]
|
||||
(let [vendor (all-vendors (:vendor_name entry))]
|
||||
(when-not (all-clients (:client_code entry))
|
||||
(throw (Exception. (str "Client '" (:client_code entry )"' not found.")) ))
|
||||
(when-not vendor
|
||||
(throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found."))))
|
||||
(when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry))
|
||||
(throw (Exception. (str "Date must be MM/dd/yyyy"))))
|
||||
(when-not (dollars= (doto (reduce + 0.0 (map :debit (:line_items entry))))
|
||||
(reduce + 0.0 (map :credit (:line_items entry))))
|
||||
(throw (Exception. (str "Debits '"
|
||||
(reduce + 0 (map :debit (:line_items entry)))
|
||||
"' and credits '"
|
||||
(reduce + 0 (map :credit (:line_items entry)))
|
||||
"' do not add up."))))
|
||||
(remove-nils
|
||||
{:journal-entry/source (:source entry)
|
||||
:journal-entry/client [:client/code (:client_code entry)]
|
||||
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
|
||||
:journal-entry/external-id (:external_id entry)
|
||||
:journal-entry/vendor (all-vendors (:vendor_name entry))
|
||||
:journal-entry/amount (:amount entry)
|
||||
:journal-entry/note (:note entry)
|
||||
:journal-entry/cleared-against (:cleared_against entry)
|
||||
|
||||
:journal-entry/line-items
|
||||
(mapv (fn [ea]
|
||||
(when-not (get
|
||||
(get all-client-locations (:client_code entry))
|
||||
(:location ea))
|
||||
(throw (Exception. (str "Location '" (:location ea) "' not found."))))
|
||||
(when (< (or (:debit ea) (:credit ea)) 0.0)
|
||||
(throw (Exception. (str (or (:debit ea) (:credit ea)) "must be greater than 0."))))
|
||||
(when (and (not (all-accounts (:account_identifier ea)))
|
||||
(not (get
|
||||
(get all-client-bank-accounts (:client_code entry))
|
||||
(:account_identifier ea))))
|
||||
(throw (Exception. (str "Account '" (:account_identifier ea) "' not found."))))
|
||||
(remove-nils {:journal-entry-line/account
|
||||
(if (re-matches #"^[0-9]+$" (:account_identifier ea))
|
||||
(:db/id (a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))
|
||||
[:bank-account/code (:account_identifier ea)])
|
||||
:journal-entry-line/location (:location ea)
|
||||
:journal-entry-line/debit (when (> (:debit ea) 0)
|
||||
(:debit ea))
|
||||
:journal-entry-line/credit (when (> (:credit ea) 0)
|
||||
(:credit ea))}))
|
||||
(:line_items entry))
|
||||
|
||||
:journal-entry/cleared true})))))
|
||||
(:entries args)))
|
||||
errors (filter :error transaction)
|
||||
success (filter (comp not :error) transaction)
|
||||
retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:journal-entry/external-id x)]])
|
||||
success)]
|
||||
(println (take 4 success))
|
||||
(run! (fn [batch] (println "transacting retraction batch") @(d/transact (d/connect uri) batch)) (partition-all 100 retraction))
|
||||
(run! (fn [batch] (println "transacting success batch") @(d/transact (d/connect uri) batch)) (partition-all 100 success))
|
||||
{:successful (map (fn [x] {:external_id (:journal-entry/external-id x)}) success)
|
||||
:existing []
|
||||
:errors (map (fn [x] {:external_id (:external_id x)
|
||||
:error (:error x)}) errors)}))
|
||||
:journal-entry/line-items
|
||||
(mapv (fn [ea]
|
||||
(when-not (get
|
||||
(get all-client-locations (:client_code entry))
|
||||
(:location ea))
|
||||
(throw (Exception. (str "Location '" (:location ea) "' not found."))))
|
||||
(when (< (or (:debit ea) (:credit ea)) 0.0)
|
||||
(throw (Exception. (str (or (:debit ea) (:credit ea)) "must be greater than 0."))))
|
||||
(when (and (not (all-accounts (:account_identifier ea)))
|
||||
(not (get
|
||||
(get all-client-bank-accounts (:client_code entry))
|
||||
(:account_identifier ea))))
|
||||
(throw (Exception. (str "Account '" (:account_identifier ea) "' not found."))))
|
||||
(remove-nils {:journal-entry-line/account
|
||||
(if (re-matches #"^[0-9]+$" (:account_identifier ea))
|
||||
(:db/id (a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))
|
||||
[:bank-account/code (:account_identifier ea)])
|
||||
:journal-entry-line/location (:location ea)
|
||||
:journal-entry-line/debit (when (> (:debit ea) 0)
|
||||
(:debit ea))
|
||||
:journal-entry-line/credit (when (> (:credit ea) 0)
|
||||
(:credit ea))}))
|
||||
(:line_items entry))
|
||||
|
||||
:journal-entry/cleared true})))))
|
||||
(:entries args)))
|
||||
errors (filter :error transaction)
|
||||
success (filter (comp not :error) transaction)
|
||||
retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:journal-entry/external-id x)]])
|
||||
success)]
|
||||
(log/info "manual ledger import has " (count success) " new rows")
|
||||
(run! (fn [batch]
|
||||
(log/info "transacting retraction batch")
|
||||
@(d/transact (d/connect uri) batch)) (partition-all 100 retraction))
|
||||
(run! (fn [batch]
|
||||
(log/info "transacting success batch")
|
||||
@(d/transact (d/connect uri) batch)) (partition-all 100 success))
|
||||
{:successful (map (fn [x] {:external_id (:journal-entry/external-id x)}) success)
|
||||
:existing []
|
||||
:errors (map (fn [x] {:external_id (:external_id x)
|
||||
:error (:error x)}) errors)})))
|
||||
|
||||
|
||||
@@ -18,10 +18,6 @@
|
||||
[auto-ap.datomic.transaction-rules :as tr]
|
||||
[auto-ap.rule-matching :as rm]
|
||||
[clj-time.coerce :as coerce]))
|
||||
(defn prn-each [xs]
|
||||
(doseq [x xs]
|
||||
(println x))
|
||||
xs)
|
||||
|
||||
(def approval-status->graphql (ident->enum-f :transaction/approval-status))
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
":user" :user-role/user})
|
||||
|
||||
(defn edit-user [context {:keys [edit_user] :as args} value]
|
||||
(println args edit_user)
|
||||
(assert-admin (:id context))
|
||||
(let [user (d-users/get-by-id (:id edit_user))
|
||||
new-clients (set (map #(Long/parseLong %) (:clients edit_user)))
|
||||
@@ -21,11 +20,10 @@
|
||||
|
||||
@(d/transact (d/connect uri)
|
||||
|
||||
(doto (-> [{:db/id (:db/id user)
|
||||
:user/role (role->datomic-role (:role edit_user))
|
||||
:user/clients new-clients}]
|
||||
(into (map (fn [c] [:db/retract (:db/id user) :user/clients c]) clients-to-remove)))
|
||||
clojure.pprint/pprint))
|
||||
(-> [{:db/id (:db/id user)
|
||||
:user/role (role->datomic-role (:role edit_user))
|
||||
:user/clients new-clients}]
|
||||
(into (map (fn [c] [:db/retract (:db/id user) :user/clients c]) clients-to-remove))))
|
||||
(->graphql
|
||||
(d-users/get-by-id (:id edit_user)))))
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
(ns auto-ap.graphql.utils
|
||||
(:require [clojure.string :as str]
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[clojure.walk :as walk]))
|
||||
[clojure.walk :as walk]
|
||||
[clojure.tools.logging :as log]))
|
||||
|
||||
|
||||
(defn snake->kebab [s]
|
||||
@@ -43,24 +44,23 @@
|
||||
m))
|
||||
|
||||
(defn is-admin? [id]
|
||||
(println "role" id)
|
||||
(= "admin" (:user/role id)))
|
||||
|
||||
(defn assert-admin [id]
|
||||
(println "role" id)
|
||||
(when-not (= "admin" (:user/role id))
|
||||
(log/warn "user " id " not an admin!")
|
||||
(throw-unauthorized)))
|
||||
|
||||
(defn can-see-client? [identity client]
|
||||
(when (not client)
|
||||
(println "WARNING - permission checking for null client"))
|
||||
(log/warn "WARNING - permission checking for null client"))
|
||||
(or (= "admin" (:user/role identity))
|
||||
((set (map :db/id (:user/clients identity))) (:db/id client))
|
||||
((set (map :db/id (:user/clients identity))) client)))
|
||||
|
||||
(defn assert-can-see-client [identity client]
|
||||
(when-not (can-see-client? identity client)
|
||||
(println "IDENTITY " identity " can not see company " client)
|
||||
(log/warn "IDENTITY " identity " can not see company " client)
|
||||
(throw-unauthorized)))
|
||||
|
||||
(defn limited-clients [id]
|
||||
|
||||
@@ -72,19 +72,15 @@
|
||||
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/account-overrides account-overrides])
|
||||
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides])
|
||||
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/automatically-paid-when-due
|
||||
(doto (mapv
|
||||
(fn [apwd]
|
||||
{:db/id apwd})
|
||||
(:automatically_paid_when_due in))
|
||||
println)]))
|
||||
(mapv
|
||||
(fn [apwd]
|
||||
{:db/id apwd})
|
||||
(:automatically_paid_when_due in))]))
|
||||
|
||||
_ (println transaction)
|
||||
transaction-result @(d/transact (d/connect uri) transaction)]
|
||||
|
||||
(-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor"))
|
||||
id))
|
||||
|
||||
(doto println)
|
||||
(->graphql))))
|
||||
|
||||
(defn merge-vendors [context {:keys [from to]} value]
|
||||
|
||||
Reference in New Issue
Block a user