135 lines
6.5 KiB
Clojure
135 lines
6.5 KiB
Clojure
(ns auto-ap.datomic.ledger
|
|
(:require [datomic.api :as d]
|
|
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
|
[auto-ap.utils :refer [dollars-0?]]
|
|
[auto-ap.datomic :refer [merge-query apply-sort-3 apply-pagination add-sorter-fields]]
|
|
[auto-ap.datomic :refer [uri]]
|
|
[clj-time.coerce :as c]
|
|
[clj-time.core :as time]))
|
|
|
|
(defn raw-graphql-ids [db args]
|
|
|
|
(let [query (cond-> {:query {:find []
|
|
:in ['$ ]
|
|
:where []}
|
|
:args [db]}
|
|
|
|
(:sort args) (add-sorter-fields {"client" ['[?e :journal-entry/client ?c]
|
|
'[?c :client/name ?sort-client]]
|
|
"date" ['[?e :journal-entry/date ?sort-date]]
|
|
"vendor" ['[?e :journal-entry/vendor ?sort-vendor]]
|
|
"amount" ['[?e :journal-entry/amount ?sort-amount]]}
|
|
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)]})
|
|
|
|
(:vendor-id args)
|
|
(merge-query {:query {:in ['?vendor-id]
|
|
:where ['[?e :journal-entry/vendor ?vendor-id]]}
|
|
:args [(:vendor-id args)]})
|
|
|
|
(:client-code args)
|
|
(merge-query {:query {:in ['?client-code]
|
|
:where ['[?e :journal-entry/client ?client-id]
|
|
'[?client-id :client/code ?client-code]]}
|
|
:args [(:client-code args)]})
|
|
|
|
(:start (:date-range args))
|
|
(merge-query {:query {:in ['?start-date]
|
|
:where ['[?e :journal-entry/date ?date]
|
|
'[(>= ?date ?start-date)]]}
|
|
:args [(c/to-date (:start (:date-range args)))]})
|
|
|
|
(:end (:date-range args))
|
|
(merge-query {:query {:in ['?end-date]
|
|
:where ['[?e :journal-entry/date ?date]
|
|
'[(<= ?date ?end-date)]]}
|
|
:args [(c/to-date (:end (:date-range 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)]})
|
|
|
|
(:bank-account-id args)
|
|
(merge-query {:query {:in ['?a]
|
|
:where ['[?e :journal-entry/line-items ?li]
|
|
'[?li :journal-entry-line/account ?a]]}
|
|
:args [(:bank-account-id args)]})
|
|
|
|
(:account-id args)
|
|
(merge-query {:query {:in ['?a2]
|
|
:where ['[?e :journal-entry/line-items ?li2]
|
|
'[?li2 :journal-entry-line/account ?a2]]}
|
|
:args [(:account-id 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]
|
|
'[?e :journal-entry/amount ?ja2]
|
|
'[(not= 0.0 ?ja2)]]}}))]
|
|
(->> query
|
|
(d/query)
|
|
(apply-sort-3 args)
|
|
(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))
|
|
(filter (fn [je]
|
|
(every?
|
|
(fn [jel]
|
|
(let [include-in-reports (-> jel :journal-entry-line/account :bank-account/include-in-reports)]
|
|
(or (nil? include-in-reports)
|
|
(true? include-in-reports))))
|
|
(:journal-entry/line-items je))))
|
|
(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 {})
|
|
|
|
|
|
|
|
|