lots of changes to make ledger actually visible.
This commit is contained in:
28
src/clj/auto_ap/datomic/accounts.clj
Normal file
28
src/clj/auto_ap/datomic/accounts.clj
Normal file
@@ -0,0 +1,28 @@
|
||||
(ns auto-ap.datomic.accounts
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.graphql.utils :refer [->graphql]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]))
|
||||
|
||||
(defn get-accounts [args]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]}])]
|
||||
:in ['$]
|
||||
:where [['?e :account/name]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(:account-set args) (merge-query {:query {:in ['?account-set]
|
||||
:where [['?e :account/account-set '?account-set]]}
|
||||
:args [(:account-set args)]}))]
|
||||
(->>
|
||||
(d/query query)
|
||||
(map first))))
|
||||
|
||||
(defn get-account-by-numeric-code-and-sets [numeric-code sets]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]}])]
|
||||
:in ['$ '?numeric-code]
|
||||
:where ['[?e :account/numeric-code ?numeric-code]]}
|
||||
:args [(d/db (d/connect uri)) numeric-code]})]
|
||||
(->>
|
||||
(d/query query)
|
||||
(map first)
|
||||
(first))))
|
||||
|
||||
#_(get-account-by-numeric-code-and-sets 5110 nil)
|
||||
@@ -1,15 +1,105 @@
|
||||
(ns auto-ap.datomic.ledger
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]))
|
||||
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
||||
[auto-ap.datomic :refer [merge-query ]]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[clj-time.coerce :as c]))
|
||||
|
||||
(defn sort-fn [sort-by]
|
||||
(cond
|
||||
(= "client" sort-by)
|
||||
#(-> % :transaction/client :client/name)
|
||||
|
||||
(= "account" sort-by)
|
||||
#(vector (-> % :transaction/account :account/name) (-> % :db/id))
|
||||
|
||||
:else
|
||||
(keyword "transaction" sort-by)))
|
||||
|
||||
(defn add-sorter-field [q sort-map args]
|
||||
(merge-query q
|
||||
{:query {:find ['?sorter]
|
||||
:where (sort-map
|
||||
(:sort-by args)
|
||||
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
|
||||
|
||||
(defn apply-sort [args sort-fn results ]
|
||||
(cond->> results
|
||||
sort-fn (sort-by sort-fn)
|
||||
(= (:asc args) false) (reverse)))
|
||||
|
||||
(defn apply-sort-2 [args results ]
|
||||
(let [comparator (if (= (:asc args) false)
|
||||
(fn [x y] (compare y x))
|
||||
(fn [x y] (compare x y)))]
|
||||
(sort comparator results )))
|
||||
|
||||
(defn apply-pagination [args results]
|
||||
|
||||
{:ids (->> results
|
||||
(drop (:start args 0))
|
||||
(take (:count args 100))
|
||||
(map last))
|
||||
:count (count results)})
|
||||
|
||||
(defn raw-graphql-ids [db args]
|
||||
(let [query (cond-> {:query {:find []
|
||||
:in ['$ ]
|
||||
:where []}
|
||||
:args [db]}
|
||||
|
||||
(:sort-by args) (add-sorter-field {"client" ['[?e :journal-entry/client ?c]
|
||||
'[?c :client/name ?sorter]]
|
||||
"date" ['[?e :journal-entry/date ?sorter]]
|
||||
"amount" ['[?e :journal-entry/amount ?sorter]]}
|
||||
args)
|
||||
|
||||
(limited-clients (:id args))
|
||||
(merge-query {:query {:in ['[?xx ...]]
|
||||
:where ['[?e :journal-entry/client ?xx]]}
|
||||
:args [(set (map :db/id (limited-clients (:id args))))]})
|
||||
|
||||
#_(:bank-account-id args)
|
||||
#_(merge-query {:query {:in ['?bank-account-id]
|
||||
:where ['[?e :transaction/bank-account ?bank-account-id]]}
|
||||
:args [(:bank-account-id args)]})
|
||||
|
||||
(:client-id args)
|
||||
(merge-query {:query {:in ['?client-id]
|
||||
:where ['[?e :journal-entry/client ?client-id]]}
|
||||
:args [(:client-id args)]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?e] :where ['[?e :journal-entry/original-entity]]}}))]
|
||||
(cond->> query
|
||||
true (d/query)
|
||||
(:sort-by args) (apply-sort-2 args)
|
||||
true (apply-pagination args))))
|
||||
|
||||
(defn graphql-results [ids db args]
|
||||
(->> (d/pull-many db '[* {:journal-entry/client [:client/name :client/code :db/id]
|
||||
:journal-entry/vendor [:vendor/name :db/id]
|
||||
:journal-entry/line-items [* {:journal-entry-line/account [*
|
||||
{:account/type [*]}]}]
|
||||
|
||||
}]
|
||||
ids)
|
||||
(map #(update % :journal-entry/date c/from-date))
|
||||
(apply-sort args (some-> (:sort-by args) sort-fn))))
|
||||
|
||||
(defn get-graphql [args]
|
||||
(->> (d/q '[:find (pull ?e [* {:journal-entry/client [:client/name :client/code :db/id]
|
||||
:journal-entry/vendor [:vendor/name :db/id]
|
||||
:journal-entry/line-items [* {:journal-entry-line/account [*
|
||||
{:account/type [*]}]}]
|
||||
|
||||
}])
|
||||
:where [?e :journal-entry/original-entity]]
|
||||
(d/db (d/connect uri)))
|
||||
(map first)))
|
||||
(let [db (d/db (d/connect uri))
|
||||
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
||||
|
||||
[(->> (graphql-results ids-to-retrieve db args))
|
||||
matching-count]))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#_(get-graphql {})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user