Bank balances are visible + import invoices should be open
This commit is contained in:
@@ -5,10 +5,13 @@
|
||||
[clj-time.coerce :as coerce]
|
||||
[config.core :refer [env]]
|
||||
[clojure.string :as str]
|
||||
[unilog.context :as lc]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.api :as d]
|
||||
[clojure.java.io :as io]
|
||||
[amazonica.aws.s3 :as s3])
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[yang.scheduler :as scheduler]
|
||||
[mount.core :as mount])
|
||||
(:import [org.apache.commons.codec.binary Base64]
|
||||
java.util.UUID))
|
||||
|
||||
@@ -122,6 +125,86 @@
|
||||
lms)))
|
||||
->graphql)))
|
||||
|
||||
(defn refresh-bank-account-current-balance [bank-account-id]
|
||||
(let [all-transactions (d/query
|
||||
{:query {:find ['?e '?debit '?credit]
|
||||
:in ['$ '?bank-account-id]
|
||||
:where '[[?e :journal-entry-line/account ?bank-account-id]
|
||||
[(get-else $ ?e :journal-entry-line/debit 0.0) ?debit]
|
||||
[(get-else $ ?e :journal-entry-line/credit 0.0) ?credit]]}
|
||||
:args [(d/db conn) bank-account-id]})
|
||||
debits (->> all-transactions
|
||||
(map (fn [[e debit credit]]
|
||||
debit))
|
||||
(reduce + 0.0))
|
||||
credits (->> all-transactions
|
||||
(map (fn [[e debit credit]]
|
||||
credit))
|
||||
(reduce + 0.0))
|
||||
current-balance (if (= :bank-account-type/check (:bank-account/type (d/entity (d/db conn) bank-account-id)))
|
||||
(- debits credits)
|
||||
(- credits debits))]
|
||||
@(d/transact conn [{:db/id bank-account-id
|
||||
:bank-account/current-balance current-balance}])))
|
||||
|
||||
(defn bank-accounts-needing-refresh []
|
||||
(let [last-refreshed (->> (d/query
|
||||
{:query {:find ['?ba '?tx]
|
||||
:in ['$ ]
|
||||
:where ['[?ba :bank-account/current-balance _ ?tx true]]}
|
||||
:args [(d/history (d/db conn))]})
|
||||
(group-by first)
|
||||
(map (fn [[ba balance-txes]]
|
||||
[ba (->> balance-txes
|
||||
(map second)
|
||||
(sort-by #(- %))
|
||||
first)])))
|
||||
has-newer-transaction (->> (d/query
|
||||
{:query {:find ['?ba]
|
||||
:in '[$ [[?ba ?last-refreshed] ...] ]
|
||||
:where ['[_ :journal-entry-line/account ?ba ?tx]
|
||||
'[(>= ?tx ?last-refreshed)]]}
|
||||
:args [(d/history (d/db conn))
|
||||
last-refreshed]})
|
||||
(map first)
|
||||
(set))
|
||||
|
||||
no-current-balance (->> (d/query {:query {:find ['?ba]
|
||||
:in '[$]
|
||||
:where ['[?ba :bank-account/code]
|
||||
'(not [?ba :bank-account/current-balance])
|
||||
]}
|
||||
:args [(d/db conn)]})
|
||||
(map first))]
|
||||
(into has-newer-transaction no-current-balance)))
|
||||
|
||||
(defn build-current-balance [bank-accounts]
|
||||
(doseq [bank-account bank-accounts]
|
||||
(log/info "Refreshing bank account" (-> (d/db conn) (d/entity bank-account) :bank-account/code))
|
||||
(try
|
||||
(refresh-bank-account-current-balance bank-account)
|
||||
(catch Exception e
|
||||
(log/error "Can't refresh current balance" e)))))
|
||||
|
||||
(defn refresh-all-current-balance []
|
||||
(lc/with-context {:source "current-balance-cache"}
|
||||
(build-current-balance (->> (d/query {:query {:find ['?ba]
|
||||
:in '[$]
|
||||
:where ['[?ba :bank-account/code]]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)))))
|
||||
|
||||
(defn refresh-current-balance []
|
||||
(lc/with-context {:source "current-balance-cache"}
|
||||
(try
|
||||
(log/info "Refreshing running balance cache")
|
||||
(build-current-balance (bank-accounts-needing-refresh))
|
||||
(catch Exception e
|
||||
(log/error e)))))
|
||||
|
||||
(mount/defstate current-balance-worker
|
||||
:start (scheduler/every (* 17 60 1000) refresh-current-balance)
|
||||
:stop (scheduler/stop current-balance-worker))
|
||||
|
||||
(defn get-client [context args value]
|
||||
(->graphql
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(ns auto-ap.graphql.invoices
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client assert-admin enum->keyword]]
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client assert-admin assert-power-user enum->keyword]]
|
||||
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
@@ -38,13 +38,17 @@
|
||||
:count Integer/MAX_VALUE)))))
|
||||
|
||||
(defn reject-invoices [context {:keys [invoices] :as in} value]
|
||||
(assert-admin (:id context))
|
||||
(assert-power-user (:id context))
|
||||
(doseq [i invoices]
|
||||
(assert-can-see-client (:id context) (:db/id (:invoice/client (d/entity (d/db conn) i)))))
|
||||
(let [transactions (map (fn [i] [:db/retractEntity i ]) invoices)
|
||||
transaction-result (audit-transact transactions (:id context))]
|
||||
invoices))
|
||||
|
||||
(defn approve-invoices [context {:keys [invoices] :as in} value]
|
||||
(assert-admin (:id context))
|
||||
(assert-power-user (:id context))
|
||||
(doseq [i invoices]
|
||||
(assert-can-see-client (:id context) (:db/id (:invoice/client (d/entity (d/db conn) i)))))
|
||||
(let [transactions (map (fn [i] {:db/id i :invoice/import-status :import-status/imported}) invoices)
|
||||
transaction-result (audit-transact transactions (:id context))]
|
||||
invoices))
|
||||
|
||||
Reference in New Issue
Block a user