diff --git a/resources/datomic/ion-config.edn b/resources/datomic/ion-config.edn index 9b2b70e5..42edc407 100644 --- a/resources/datomic/ion-config.edn +++ b/resources/datomic/ion-config.edn @@ -1,4 +1,5 @@ -{ :allow [iol-ion.tx/upsert-entity - iol-ion.tx/reset-scalars - iol-ion.tx/reset-rels] +{ :allow [iol-ion.tx/upsert-entity + iol-ion.tx/reset-scalars + iol-ion.tx/reset-rels + iol-ion.tx/propose-invoice] :app-name "iol-cloud"} diff --git a/src/clj/auto_ap/datomic/invoices.clj b/src/clj/auto_ap/datomic/invoices.clj index 550efab8..ded584c9 100644 --- a/src/clj/auto_ap/datomic/invoices.clj +++ b/src/clj/auto_ap/datomic/invoices.clj @@ -331,18 +331,4 @@ 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? - [] - [(remove-nils invoice)]))) + diff --git a/src/clj/auto_ap/jobs/ntg.clj b/src/clj/auto_ap/jobs/ntg.clj index 8a176575..2a62db1f 100644 --- a/src/clj/auto_ap/jobs/ntg.clj +++ b/src/clj/auto_ap/jobs/ntg.clj @@ -3,6 +3,7 @@ [amazonica.aws.s3 :as s3] [auto-ap.datomic.clients :as d-clients] [auto-ap.jobs.core :refer [execute]] + [iol-ion.tx :refer [propose-invoice]] [auto-ap.logging :as log] [auto-ap.parse :as parse] [auto-ap.time :as atime] @@ -83,7 +84,7 @@ :destination-key invoice-key}) (->> (extract-invoice-details (read-csv k) clients) (map (fn [i] - [:propose-invoice (assoc i :invoice/source-url invoice-url)])) + [`(propose-invoice ~(assoc i :invoice/source-url invoice-url))])) )) (catch Exception e (log/error ::cant-load-file diff --git a/src/clj/auto_ap/jobs/sysco.clj b/src/clj/auto_ap/jobs/sysco.clj index 3f2ca360..cd9cffed 100644 --- a/src/clj/auto_ap/jobs/sysco.clj +++ b/src/clj/auto_ap/jobs/sysco.clj @@ -5,6 +5,7 @@ [auto-ap.jobs.core :refer [execute]] [auto-ap.datomic.clients :as d-clients] [auto-ap.datomic.invoices :refer [code-invoice propose-invoice]] + [iol-ion.tx :refer [propose-invoice]] [auto-ap.ledger :refer [transact-with-ledger]] [auto-ap.parse :as parse] [auto-ap.time :as t] diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 50fb7c55..7d599c9a 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -2,6 +2,7 @@ (:require [amazonica.aws.s3 :as s3] [auto-ap.datomic :refer [conn remove-nils uri]] + [iol-ion.tx :refer [propose-invoice]] [auto-ap.datomic.accounts :as a] [auto-ap.datomic.clients :as d-clients] [auto-ap.datomic.invoices :as d-invoices] @@ -253,7 +254,7 @@ :transaction-account/location "A" :transaction-account/amount (Math/abs (:invoice/total invoice))}]})) ] - [`(d-invoices/propose-invoice ~(d-invoices/code-invoice (validate-invoice (remove-nils invoice) + [`(propose-invoice ~(d-invoices/code-invoice (validate-invoice (remove-nils invoice) user))) (some-> payment remove-nils) transaction]))) @@ -274,7 +275,7 @@ (map #(validate-invoice % user)) admin-only-if-multiple-clients (mapv d-invoices/code-invoice) - (mapv (fn [i] `(d-invoices/propose-invoice ~i))))] + (mapv (fn [i] `(propose-invoice ~i))))] (log/info "creating invoice" potential-invoices) (let [tx (transact-with-ledger potential-invoices user)] diff --git a/src/clj/iol_ion/tx.clj b/src/clj/iol_ion/tx.clj index d075f8b5..8f5525a2 100644 --- a/src/clj/iol_ion/tx.clj +++ b/src/clj/iol_ion/tx.clj @@ -5,6 +5,19 @@ (defn random-tempid [] (str (UUID/randomUUID))) +(defn remove-nils [m] + (let [result (reduce-kv + (fn [m k v] + (if (not (nil? v)) + (assoc m k v) + m + )) + {} + m)] + (if (seq result) + result + nil))) + (defn by ([f xs] (by f identity xs)) @@ -111,3 +124,20 @@ []))] ops)) + + +(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? + [] + [(remove-nils invoice)])))