implementing sorting for checks.
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
(ns auto-ap.datomic.checks
|
(ns auto-ap.datomic.checks
|
||||||
(:require [datomic.api :as d]
|
(:require [datomic.api :as d]
|
||||||
[auto-ap.datomic :refer [uri merge-query]]
|
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination]]
|
||||||
[auto-ap.graphql.utils :refer [limited-clients]]
|
[auto-ap.graphql.utils :refer [limited-clients]]
|
||||||
[clojure.set :refer [rename-keys]]
|
[clojure.set :refer [rename-keys]]
|
||||||
[clj-time.coerce :as c]))
|
[clj-time.coerce :as c]))
|
||||||
|
|
||||||
(defn <-datomic [results]
|
(defn <-datomic [result]
|
||||||
(->> results
|
(-> result
|
||||||
(map first)
|
(update :payment/date c/from-date)
|
||||||
(map #(update % :payment/date c/from-date))
|
(update :payment/status :db/ident)
|
||||||
(map #(update % :payment/status :db/ident))
|
(update :payment/type :db/ident)
|
||||||
(map #(update % :payment/type :db/ident))
|
(rename-keys {:invoice-payment/_payment :payment/invoices})))
|
||||||
(map #(rename-keys % {:invoice-payment/_payment :payment/invoices}))))
|
|
||||||
|
|
||||||
(defn add-arg [query name value where & rest]
|
(defn add-arg [query name value where & rest]
|
||||||
(let [query (-> query
|
(let [query (-> query
|
||||||
@@ -20,59 +19,96 @@
|
|||||||
(update-in [:query :where] conj where))]
|
(update-in [:query :where] conj where))]
|
||||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
||||||
|
|
||||||
(def default-read '(pull ?e [*
|
(def default-read '[*
|
||||||
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
|
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
|
||||||
{:payment/client [:client/name :db/id :client/code]}
|
{:payment/client [:client/name :db/id :client/code]}
|
||||||
{:payment/bank-account [*]}
|
{:payment/bank-account [*]}
|
||||||
{:payment/vendor [:vendor/name {:vendor/default-account
|
{:payment/vendor [:vendor/name {:vendor/default-account
|
||||||
[:account/name :account/numeric-code :db/id]} :db/id {:vendor/primary-contact [*]} {:vendor/address [*]}]}
|
[:account/name :account/numeric-code :db/id]} :db/id {:vendor/primary-contact [*]} {:vendor/address [*]}]}
|
||||||
{:payment/status [:db/ident]}
|
{:payment/status [:db/ident]}
|
||||||
{:payment/type [:db/ident]}]))
|
{:payment/type [:db/ident]}])
|
||||||
|
|
||||||
(defn raw-graphql [args]
|
(defn raw-graphql-ids [db args]
|
||||||
(let [check-number-like (try (Long/parseLong (:check-number-like args)) (catch Exception e nil))
|
(let [check-number-like (try (Long/parseLong (:check-number-like args)) (catch Exception e nil))
|
||||||
query (cond-> {:query {:find [default-read]
|
query (cond-> {:query {:find []
|
||||||
:in ['$]
|
:in ['$]
|
||||||
:where ['[?e :payment/client]]}
|
:where []}
|
||||||
:args [(d/db (d/connect uri))]}
|
:args [db]}
|
||||||
|
|
||||||
(limited-clients (:id args)) (add-arg '[?xx ...] (set (map :db/id (limited-clients (:id args))))
|
(limited-clients (:id args))
|
||||||
'[?e :payment/client ?xx])
|
(merge-query {:query {:in ['[?xx ...]]
|
||||||
|
:where ['[?e :payment/client ?xx]]}
|
||||||
|
:args [(set (map :db/id (limited-clients (:id args))))]})
|
||||||
|
|
||||||
(:client-id args) (add-arg '?client-id (:client-id args)
|
(:client-id args)
|
||||||
'[?e :payment/client ?client-id])
|
(merge-query {:query {:in ['?client-id]
|
||||||
(:client-code args) (add-arg '?client-code (:client-code args)
|
:where ['[?e :payment/client ?client-id]]}
|
||||||
'[?e :payment/client ?client-id]
|
:args [(:client-id args)]})
|
||||||
'[?client-id :client/code ?client-code] )
|
(:client-code args)
|
||||||
(:vendor-id args) (add-arg '?vendor-id (:vendor-id args)
|
(merge-query {:query {:in ['?client-code]
|
||||||
'[?e :payment/vendor ?vendor-id])
|
:where ['[?e :payment/client ?client-id]
|
||||||
(:original-id args) (add-arg '?original-id (cond-> (:original-id args) (string? (:original-id args)) Long/parseLong )
|
'[?client-id :client/code ?client-code]]}
|
||||||
'[?e :payment/client ?c]
|
:args [(:client-code args)]})
|
||||||
'[?c :client/original-id ?original-id])
|
|
||||||
(:check-number args) (add-arg '?check-number (:check-number args)
|
|
||||||
'[?e :payment/check-number ?check-number])
|
(:vendor-id args)
|
||||||
(:bank-account-id args) (add-arg '?bank-account-id (:bank-account-id args)
|
(merge-query {:query {:in ['?vendor-id]
|
||||||
'[?e :payment/bank-account ?bank-account-id])
|
:where ['[?e :payment/vendor ?vendor-id]]}
|
||||||
(:amount args) (add-arg '?amount (:amount args)
|
:args [(:vendor-id args)]})
|
||||||
'[?e :payment/amount ?amount])
|
|
||||||
(:status args) (add-arg '?status (:status args)
|
(:original-id args)
|
||||||
'[?e :payment/status ?status])
|
(merge-query {:query {:in ['?original-id]
|
||||||
(:start (:date-range args)) (merge-query {:query {:in '[?start-date]
|
:where ['[?e :payment/client ?c]
|
||||||
:where ['[?e :payment/date ?date]
|
'[?c :client/original-id ?original-id]]}
|
||||||
'[(>= ?date ?start-date)]]}
|
:args [(:original-id args)]})
|
||||||
:args [(c/to-date (:start (:date-range args)))]})
|
|
||||||
|
(:check-number args)
|
||||||
|
(merge-query {:query {:in ['?check-number]
|
||||||
|
:where ['[?e :payment/check-number ?check-number]]}
|
||||||
|
:args [(:check-number args)]})
|
||||||
|
|
||||||
|
(:bank-account-id args)
|
||||||
|
(merge-query {:query {:in ['?bank-account-id]
|
||||||
|
:where ['[?e :payment/bank-account ?bank-account-id]]}
|
||||||
|
:args [(:bank-account-id args)]})
|
||||||
|
|
||||||
|
(:amount args)
|
||||||
|
(merge-query {:query {:in ['?amount]
|
||||||
|
:where ['[?e :payment/amount ?amount]]}
|
||||||
|
:args [(:amount args)]})
|
||||||
|
|
||||||
|
|
||||||
|
(:status args)
|
||||||
|
(merge-query {:query {:in ['?status]
|
||||||
|
:where ['[?e :payment/status ?status]]}
|
||||||
|
:args [(:status args)]})
|
||||||
|
|
||||||
|
(:start (:date-range args))
|
||||||
|
(merge-query {:query {:in '[?start-date]
|
||||||
|
:where ['[?e :payment/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 :payment/date ?date]
|
||||||
|
'[(<= ?date ?end-date)]]}
|
||||||
|
:args [(c/to-date (:end (:date-range args)))]})
|
||||||
|
|
||||||
|
check-number-like
|
||||||
|
(merge-query {:query {:in '[?check-number-like]
|
||||||
|
:where ['[?e :payment/check-number ?check-number-like]]}
|
||||||
|
:args [check-number-like]})
|
||||||
|
|
||||||
|
true
|
||||||
|
(merge-query {:query {:find ['?base-date '?e]
|
||||||
|
:where ['[?e :payment/date ?base-date]]}}))]
|
||||||
|
|
||||||
(:end (:date-range args)) (merge-query {:query {:in '[?end-date]
|
|
||||||
:where ['[?e :payment/date ?date]
|
|
||||||
'[(<= ?date ?end-date)]]}
|
|
||||||
:args [(c/to-date (:end (:date-range args)))]})
|
|
||||||
check-number-like (add-arg '?check-number-like check-number-like
|
|
||||||
'[?e :payment/check-number ?check-number-like]
|
|
||||||
#_'[(.contains ^String ?check-number ?check-number-like)]))]
|
|
||||||
|
|
||||||
(->> (d/query
|
(cond->> query
|
||||||
query)
|
true (d/query)
|
||||||
(<-datomic))))
|
true (apply-sort-2 args)
|
||||||
|
true (apply-pagination args))))
|
||||||
|
|
||||||
(defn sort-fn [args]
|
(defn sort-fn [args]
|
||||||
(cond
|
(cond
|
||||||
@@ -85,13 +121,21 @@
|
|||||||
:else
|
:else
|
||||||
(keyword "payment" (:sort-by args))))
|
(keyword "payment" (:sort-by args))))
|
||||||
|
|
||||||
|
(defn graphql-results [ids db args]
|
||||||
|
(let [results (->> (d/pull-many db default-read ids)
|
||||||
|
(group-by :db/id))
|
||||||
|
payments (->> ids
|
||||||
|
(map results)
|
||||||
|
(map first)
|
||||||
|
(mapv <-datomic))]
|
||||||
|
payments))
|
||||||
|
|
||||||
(defn get-graphql [args]
|
(defn get-graphql [args]
|
||||||
(let [results (raw-graphql args)]
|
(let [db (d/db (d/connect uri))
|
||||||
(cond->> results
|
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
||||||
(:sort-by args) (sort-by (sort-fn args))
|
|
||||||
(= (:asc args) false) (reverse)
|
[(->> (graphql-results ids-to-retrieve db args))
|
||||||
true (drop (:start args 0))
|
matching-count]))
|
||||||
true (take (:count args 100)))))
|
|
||||||
|
|
||||||
(defn count-graphql [args]
|
(defn count-graphql [args]
|
||||||
(->> (raw-graphql args)
|
(->> (raw-graphql args)
|
||||||
@@ -99,10 +143,5 @@
|
|||||||
|
|
||||||
(defn get-by-id [id]
|
(defn get-by-id [id]
|
||||||
(->>
|
(->>
|
||||||
(d/query (-> {:query {:find [default-read]
|
(d/pull (d/db (d/connect uri)) default-read id)
|
||||||
:in ['$]
|
(<-datomic)))
|
||||||
:where []}
|
|
||||||
:args [(d/db (d/connect uri))]}
|
|
||||||
(add-arg '?e id ['?e])))
|
|
||||||
(<-datomic)
|
|
||||||
(first)))
|
|
||||||
|
|||||||
@@ -320,11 +320,8 @@
|
|||||||
|
|
||||||
(defn get-payment-page [context args value]
|
(defn get-payment-page [context args value]
|
||||||
(let [args (assoc args :id (:id context))
|
(let [args (assoc args :id (:id context))
|
||||||
payments (map
|
[payments checks-count] (d-checks/get-graphql (<-graphql args))]
|
||||||
->graphql
|
[{:payments (map ->graphql payments)
|
||||||
(d-checks/get-graphql (<-graphql args)))
|
|
||||||
checks-count (d-checks/count-graphql (<-graphql args))]
|
|
||||||
[{:payments payments
|
|
||||||
:total checks-count
|
:total checks-count
|
||||||
:count (count payments)
|
:count (count payments)
|
||||||
:start (:start args 0)
|
:start (:start args 0)
|
||||||
@@ -332,12 +329,12 @@
|
|||||||
|
|
||||||
(defn get-potential-payments [context args value]
|
(defn get-potential-payments [context args value]
|
||||||
(let [transaction (d-transactions/get-by-id (:transaction_id args))
|
(let [transaction (d-transactions/get-by-id (:transaction_id args))
|
||||||
_ (assert-can-see-client (:id context) (:transaction/client transaction))]
|
_ (assert-can-see-client (:id context) (:transaction/client transaction))
|
||||||
(map ->graphql
|
[payments payments-count] (d-checks/get-graphql {:client-id (:db/id (:transaction/client transaction))
|
||||||
(d-checks/get-graphql {:client-id (:db/id (:transaction/client transaction))
|
:bank-account-id (:db/id (:transaction/bank-account transaction))
|
||||||
:bank-account-id (:db/id (:transaction/bank-account transaction))
|
:amount (- (:transaction/amount transaction))
|
||||||
:amount (- (:transaction/amount transaction))
|
:status :payment-status/pending})]
|
||||||
:status :payment-status/pending}))))
|
(map ->graphql payments)))
|
||||||
|
|
||||||
(defn add-handwritten-check [context args value]
|
(defn add-handwritten-check [context args value]
|
||||||
(let [invoices (d-invoices/get-multi (map :invoice_id (:invoice_payments args)))
|
(let [invoices (d-invoices/get-multi (map :invoice_id (:invoice_payments args)))
|
||||||
|
|||||||
@@ -61,14 +61,11 @@
|
|||||||
|
|
||||||
(defn change-handler [form customize-fn]
|
(defn change-handler [form customize-fn]
|
||||||
(fn [db [_ & path-pairs]]
|
(fn [db [_ & path-pairs]]
|
||||||
(println "CHA" form path-pairs)
|
|
||||||
(reduce
|
(reduce
|
||||||
(fn [db [path value]]
|
(fn [db [path value]]
|
||||||
(println "PATH" path "VALUE" value)
|
|
||||||
(let [updated (assoc-in db (into [::forms form :data] path) value)]
|
(let [updated (assoc-in db (into [::forms form :data] path) value)]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [updated [path value ]]
|
(fn [updated [path value ]]
|
||||||
(println "custom PATH" path "custom VALUE" value)
|
|
||||||
(assoc-in updated (into [::forms form :data] path) value))
|
(assoc-in updated (into [::forms form :data] path) value))
|
||||||
updated
|
updated
|
||||||
(partition 2 (customize-fn (get-in updated [::forms form :data]) path value)))))
|
(partition 2 (customize-fn (get-in updated [::forms form :data]) path value)))))
|
||||||
@@ -118,7 +115,7 @@
|
|||||||
(.stopPropagation e)
|
(.stopPropagation e)
|
||||||
(.preventDefault e))
|
(.preventDefault e))
|
||||||
(when can-submit
|
(when can-submit
|
||||||
(re-frame/dispatch-sync (conj submit-event params))))}
|
(re-frame/dispatch-sync (vec (conj submit-event params)))))}
|
||||||
[:h1.title.is-2 title]
|
[:h1.title.is-2 title]
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -167,9 +167,9 @@
|
|||||||
:create)]
|
:create)]
|
||||||
{:graphql
|
{:graphql
|
||||||
{:token user
|
{:token user
|
||||||
:query-obj @(re-frame/subscribe (if (:id data)
|
:query-obj (if (:id data)
|
||||||
@(re-frame/subscribe [::edit-query])
|
@(re-frame/subscribe [::edit-query])
|
||||||
@(re-frame/subscribe [::create-query])))
|
@(re-frame/subscribe [::create-query]))
|
||||||
:on-success [::succeeded params command]
|
:on-success [::succeeded params command]
|
||||||
:on-error [::forms/save-error ::form]}})))
|
:on-error [::forms/save-error ::form]}})))
|
||||||
|
|
||||||
|
|||||||
@@ -114,6 +114,7 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn potential-payment-matches-box [{:keys [potential-payment-matches] :as params}]
|
(defn potential-payment-matches-box [{:keys [potential-payment-matches] :as params}]
|
||||||
|
(println potential-payment-matches)
|
||||||
[:div.box
|
[:div.box
|
||||||
[:div.columns
|
[:div.columns
|
||||||
[:div.column
|
[:div.column
|
||||||
@@ -154,7 +155,7 @@
|
|||||||
|
|
||||||
(if (and (seq (:potential-payment-matches data))
|
(if (and (seq (:potential-payment-matches data))
|
||||||
(not (:payment data)))
|
(not (:payment data)))
|
||||||
[potential-payment-matches-box {:matches (:potential-payment-matches data)
|
[potential-payment-matches-box {:potential-payment-matches (:potential-payment-matches data)
|
||||||
:edit-completed edit-completed}]
|
:edit-completed edit-completed}]
|
||||||
|
|
||||||
[:div
|
[:div
|
||||||
|
|||||||
Reference in New Issue
Block a user