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