112 lines
4.8 KiB
Clojure
112 lines
4.8 KiB
Clojure
(ns auto-ap.datomic.ledger
|
|
(:require [datomic.api :as d]
|
|
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
|
[auto-ap.datomic :refer [merge-query apply-sort-2 apply-sort apply-pagination add-sorter-field]]
|
|
[auto-ap.datomic :refer [uri]]
|
|
[clj-time.coerce :as c]))
|
|
|
|
(defn sort-fn [sort-by]
|
|
(cond
|
|
(= "client" sort-by)
|
|
#(-> % :journal-entry/client :client/name)
|
|
|
|
(= "vendor" sort-by)
|
|
#(-> % :journal-entry/vendor :vendor/name)
|
|
|
|
:else
|
|
(keyword "journal-entry" sort-by)))
|
|
|
|
(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]]
|
|
"vendor" ['[?e :journal-entry/vendor ?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))))]})
|
|
|
|
(:client-id args)
|
|
(merge-query {:query {:in ['?client-id]
|
|
:where ['[?e :journal-entry/client ?client-id]]}
|
|
:args [(:client-id args)]})
|
|
|
|
(:to-date args)
|
|
(merge-query {:query {:in ['?to-date]
|
|
:where ['[?e :journal-entry/date ?d]
|
|
'[(<= ?d ?to-date)]]}
|
|
:args [(c/to-date (:to-date args))]})
|
|
|
|
(:from-date args)
|
|
(merge-query {:query {:in ['?from-date]
|
|
:where ['[?e :journal-entry/date ?d]
|
|
'[(>= ?d ?from-date)]]}
|
|
:args [(c/to-date (:from-date args))]})
|
|
|
|
(:from-numeric-code args)
|
|
(merge-query {:query {:in ['?from-numeric-code]
|
|
:where ['[?e :journal-entry/line-items ?li]
|
|
'[?li :journal-entry-line/account ?a]
|
|
'[?a :account/numeric-code ?c]
|
|
'[(>= ?c ?from-numeric-code)]]}
|
|
:args [(:from-numeric-code args)]})
|
|
|
|
(:to-numeric-code args)
|
|
(merge-query {:query {:in ['?to-numeric-code]
|
|
:where ['[?e :journal-entry/line-items ?li]
|
|
'[?li :journal-entry-line/account ?a]
|
|
'[?a :account/numeric-code ?c]
|
|
'[(<= ?c ?to-numeric-code)]]}
|
|
:args [(:to-numeric-code args)]})
|
|
|
|
(:location args)
|
|
(merge-query {:query {:in ['?location]
|
|
:where ['[?e :journal-entry/line-items ?li]
|
|
'[?li :journal-entry-line/location ?location]]}
|
|
:args [(:location args)]})
|
|
|
|
true
|
|
(merge-query {:query {:find ['?base-date '?e] :where ['[?e :journal-entry/date ?base-date]]}}))]
|
|
(->> query
|
|
(d/query)
|
|
(apply-sort-2 args [:desc :asc])
|
|
(apply-pagination args))))
|
|
|
|
(defn graphql-results [ids db args]
|
|
(let [results (->> (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 [*]}
|
|
{:bank-account/type [*]}]}]}]
|
|
ids)
|
|
(map #(update % :journal-entry/date c/from-date))
|
|
(group-by :db/id))]
|
|
(->> ids
|
|
(map results)
|
|
(map first))))
|
|
|
|
(defn get-graphql [args]
|
|
(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 {})
|
|
|
|
|
|
|
|
|