Makes integreat run on datomic cloud
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
(ns auto-ap.datomic.invoices
|
||||
(:require
|
||||
[auto-ap.datomic
|
||||
:refer [add-sorter-fields apply-pagination apply-sort-3 conn merge-query uri]]
|
||||
:refer [add-sorter-fields
|
||||
apply-pagination
|
||||
apply-sort-3
|
||||
conn
|
||||
merge-query
|
||||
pull-many
|
||||
remove-nils]]
|
||||
[auto-ap.datomic.accounts :as d-accounts]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.utils :refer [limited-clients]]
|
||||
@@ -9,7 +15,7 @@
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
[clojure.set :refer [rename-keys]]
|
||||
[datomic.api :as d]))
|
||||
[datomic.client.api :as dc]))
|
||||
|
||||
(def default-read '[*
|
||||
{:invoice/client [:client/name :db/id :client/locations :client/code]}
|
||||
@@ -37,7 +43,7 @@
|
||||
|
||||
(defn raw-graphql-ids
|
||||
([args]
|
||||
(raw-graphql-ids (d/db conn) args))
|
||||
(raw-graphql-ids (dc/db conn) args))
|
||||
([db args]
|
||||
(->> (cond-> {:query {:find []
|
||||
:in ['$]
|
||||
@@ -164,13 +170,13 @@
|
||||
(merge-query {:query {:find ['?sort-default '?e ]
|
||||
:where ['[?e :invoice/client]
|
||||
'[?e :invoice/date ?sort-default]]}}) )
|
||||
(d/query)
|
||||
(dc/q)
|
||||
(apply-sort-3 args)
|
||||
(apply-pagination args))))
|
||||
|
||||
|
||||
(defn graphql-results [ids db _]
|
||||
(let [results (->> (d/pull-many db default-read ids)
|
||||
(let [results (->> (pull-many db default-read ids)
|
||||
(group-by :db/id))
|
||||
|
||||
invoices (->> ids
|
||||
@@ -182,11 +188,11 @@
|
||||
(defn sum-outstanding [ids]
|
||||
|
||||
(->>
|
||||
(d/query {:query {:find ['?id '?o]
|
||||
(dc/q {:query {:find ['?id '?o]
|
||||
:in ['$ '[?id ...]]
|
||||
:where ['[?id :invoice/outstanding-balance ?o]]
|
||||
}
|
||||
:args [(d/db conn)
|
||||
:args [(dc/db conn)
|
||||
ids]})
|
||||
(map last)
|
||||
(reduce
|
||||
@@ -196,11 +202,11 @@
|
||||
(defn sum-total-amount [ids]
|
||||
|
||||
(->>
|
||||
(d/query {:query {:find ['?id '?o]
|
||||
(dc/q {:query {:find ['?id '?o]
|
||||
:in ['$ '[?id ...]]
|
||||
:where ['[?id :invoice/total ?o]]
|
||||
}
|
||||
:args [(d/db conn)
|
||||
:args [(dc/db conn)
|
||||
ids]})
|
||||
(map last)
|
||||
(reduce
|
||||
@@ -209,7 +215,7 @@
|
||||
|
||||
(defn get-graphql [args]
|
||||
|
||||
(let [db (d/db (d/connect uri))
|
||||
(let [db (dc/db conn)
|
||||
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)
|
||||
outstanding (sum-outstanding ids-to-retrieve)
|
||||
total-amount (sum-total-amount ids-to-retrieve)]
|
||||
@@ -221,34 +227,34 @@
|
||||
total-amount]))
|
||||
|
||||
(defn get-by-id [id]
|
||||
(-> (d/db (d/connect uri))
|
||||
(d/pull default-read id)
|
||||
(-> (dc/db conn)
|
||||
(dc/pull default-read id)
|
||||
(<-datomic)))
|
||||
|
||||
(defn get-multi [ids]
|
||||
(map <-datomic
|
||||
(-> (d/db (d/connect uri))
|
||||
(d/pull-many default-read ids))))
|
||||
(pull-many (dc/db conn) default-read ids )))
|
||||
|
||||
|
||||
|
||||
(defn find-conflicting [{:keys [:invoice/invoice-number :invoice/vendor :invoice/client :db/id]}]
|
||||
|
||||
(->> (d/query
|
||||
(cond-> {:query {:find [(list 'pull '?e default-read)]
|
||||
:in ['$ '?invoice-number '?vendor '?client '?invoice-id]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
[(not= ?e ?invoice-id)]]}
|
||||
:args [(d/db (d/connect uri)) invoice-number vendor client (or id 0)]}))
|
||||
|
||||
(->> (dc/q
|
||||
(cond-> {:query {:find [(list 'pull '?e default-read)]
|
||||
:in ['$ '?invoice-number '?vendor '?client '?invoice-id]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
[(not= ?e ?invoice-id)]]}
|
||||
|
||||
:args [(dc/db conn) invoice-number vendor client (or id 0)]}))
|
||||
(map first)
|
||||
(map <-datomic)))
|
||||
|
||||
|
||||
(defn get-existing-set []
|
||||
(let [vendored-results (set (d/query {:query {:find ['?vendor '?client '?invoice-number]
|
||||
(let [vendored-results (set (dc/q {:query {:find ['?vendor '?client '?invoice-number]
|
||||
:in ['$]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
@@ -256,8 +262,8 @@
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
|
||||
:args [(d/db (d/connect uri))]}))
|
||||
vendorless-results (->> (d/query {:query {:find ['?client '?invoice-number]
|
||||
:args [(dc/db conn)]}))
|
||||
vendorless-results (->> (dc/q {:query {:find ['?client '?invoice-number]
|
||||
:in ['$]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
(not [?e :invoice/vendor])
|
||||
@@ -265,7 +271,7 @@
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
|
||||
:args [(d/db (d/connect uri))]})
|
||||
:args [(dc/db conn)]})
|
||||
(mapv (fn [[client invoice-number]]
|
||||
[nil client invoice-number]) )
|
||||
set)]
|
||||
@@ -276,37 +282,37 @@
|
||||
(->> {:query {:find ['?e]
|
||||
:in ['$ '[?e ...]]
|
||||
:where ['[?e :invoice/date]]}
|
||||
:args [(d/db conn) ids]}
|
||||
(d/query)
|
||||
:args [(dc/db conn) ids]}
|
||||
(dc/q)
|
||||
(map first)
|
||||
vec)
|
||||
[]))
|
||||
|
||||
(defn code-invoice [invoice]
|
||||
(let [db (d/db auto-ap.datomic/conn)
|
||||
(let [db (dc/db auto-ap.datomic/conn)
|
||||
client-id (:invoice/client invoice)
|
||||
vendor-id (:invoice/vendor invoice)
|
||||
date (:invoice/date invoice)
|
||||
vendor (d/pull db '[*] vendor-id)
|
||||
vendor (dc/pull db '[*] vendor-id)
|
||||
due (when (:vendor/terms vendor)
|
||||
(-> date
|
||||
(coerce/to-date-time)
|
||||
(time/plus (time/days (d-vendors/terms-for-client-id vendor client-id)))
|
||||
coerce/to-date))
|
||||
automatically-paid? (boolean (seq (d/q '[:find [?c ...]
|
||||
:in $ ?v ?c
|
||||
:where [?v :vendor/automatically-paid-when-due ?c]]
|
||||
automatically-paid? (boolean (seq (map first (dc/q '[:find ?c
|
||||
:in $ ?v ?c
|
||||
:where [?v :vendor/automatically-paid-when-due ?c]]
|
||||
db
|
||||
vendor-id
|
||||
client-id))))
|
||||
[schedule-payment-dom] (map first (dc/q '[:find ?dom
|
||||
:in $ ?v ?c
|
||||
:where [?v :vendor/schedule-payment-dom ?sp ]
|
||||
[?sp :vendor-schedule-payment-dom/client ?c]
|
||||
[?sp :vendor-schedule-payment-dom/dom ?dom]]
|
||||
db
|
||||
vendor-id
|
||||
client-id)))
|
||||
[schedule-payment-dom] (d/q '[:find [?dom ...]
|
||||
:in $ ?v ?c
|
||||
:where [?v :vendor/schedule-payment-dom ?sp ]
|
||||
[?sp :vendor-schedule-payment-dom/client ?c]
|
||||
[?sp :vendor-schedule-payment-dom/dom ?dom]]
|
||||
db
|
||||
vendor-id
|
||||
client-id)
|
||||
client-id))
|
||||
|
||||
scheduled-payment (cond automatically-paid?
|
||||
due
|
||||
@@ -324,3 +330,19 @@
|
||||
true (assoc :invoice/expense-accounts [default-expense-account])
|
||||
due (assoc :invoice/due due)
|
||||
scheduled-payment (assoc :invoice/scheduled-payment scheduled-payment))))
|
||||
|
||||
(defn propose-invoice [db invoice]
|
||||
(let [existing? (boolean (seq (dc/q '[:find ?i
|
||||
:in $ ?invoice-number ?client ?vendor
|
||||
:where
|
||||
[?i :invoice/invoice-number ?invoice-number]
|
||||
[?i :invoice/client ?client]
|
||||
[?i :invoice/vendor ?vendor]
|
||||
(not [?i :invoice/status :invoice-status/voided])]
|
||||
db
|
||||
(:invoice/invoice-number invoice)
|
||||
(:invoice/client invoice)
|
||||
(:invoice/vendor invoice))))]
|
||||
(if existing?
|
||||
[]
|
||||
[(doto (remove-nils invoice) println)])))
|
||||
|
||||
Reference in New Issue
Block a user