Makes integreat run on datomic cloud

This commit is contained in:
2022-08-23 12:13:12 -07:00
parent 58b9dcf272
commit d02fba2b44
58 changed files with 2163 additions and 1257 deletions

View File

@@ -6,24 +6,23 @@
[auto-ap.time :as time]
[clj-time.coerce :as coerce]
[clojure.tools.logging :as log]
[datomic.api :as d]))
[datomic.client.api :as dc]))
(defn close-auto-invoices []
(let [invoices-to-close (d/query {:query {:find ['?e]
(let [invoices-to-close (dc/q {:query {:find ['?e]
:in ['$ '?today]
:where ['[?e :invoice/scheduled-payment ?d]
'[?e :invoice/status :invoice-status/unpaid]
'[(<= ?d ?today)]]}
:args [(d/db conn) (coerce/to-date (time/local-now))]})]
:args [(dc/db conn) (coerce/to-date (time/local-now))]})]
(log/info "Closing " (count invoices-to-close) "scheduled invoices")
(some->> invoices-to-close
seq
(dc/transact conn {:tx-data (some->> invoices-to-close
seq
(mapv (fn [[i]] {:db/id i
:invoice/outstanding-balance 0.0
:invoice/status :invoice-status/paid}))
(d/transact conn)
deref)
(mapv (fn [[i]] {:db/id i
:invoice/outstanding-balance 0.0
:invoice/status :invoice-status/paid}))
)})
(log/info "Closed " (count invoices-to-close) "scheduled invoices")))

View File

@@ -2,9 +2,9 @@
(:require
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [conn]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :refer [code-invoice]]
[auto-ap.jobs.core :refer [execute]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :refer [code-invoice propose-invoice]]
[auto-ap.ledger :refer [transact-with-ledger]]
[auto-ap.parse :as parse]
[auto-ap.time :as t]
@@ -15,7 +15,8 @@
[clojure.tools.logging :as log]
[com.unbounce.dogstatsd.core :as statsd]
[config.core :refer [env]]
[datomic.api :as d])
[datomic.client.api :as dc]
[auto-ap.datomic.vendors :as d-vendors])
(:import
(java.util UUID)))
@@ -26,16 +27,15 @@
(def summary-keys ["TranCode" "GroupID" "Company" "CustomerNumber" "InvoiceNumber" "RecordType" "Item" "InvoiceDocument" "TotalLines" "TotalQtyInvoice" "TotalQty" "TotalQtySplit" "TotalQtyPounds" "TotalExtendedPrice" "TotalTaxAmount" "TotalInvoiceAmount" "AccountDate"])
(defn get-sysco-vendor []
(let [db (d/db conn)]
(d/entity
db
(->
(d/q '[:find ?v
:in $
:where [?v :vendor/name "Sysco"]]
db)
(let [db (dc/db conn)]
(->
(dc/q '[:find (pull ?v r)
:in $ r
:where [?v :vendor/name "Sysco"]]
db
d-vendors/default-read)
first
first))))
first)))
(defn read-sysco-csv [k]
@@ -45,7 +45,7 @@
io/reader
csv/read-csv))
(defn extract-invoice-details [csv-rows clients sysco-vendor]
(defn extract-invoice-details [csv-rows sysco-vendor]
(let [[header-row & csv-rows] csv-rows
header-row (into {} (map vector header-keys header-row))
summary-row (->> csv-rows
@@ -65,8 +65,8 @@
(header-row "City2")])
account-number (some-> account-number Long/parseLong str)
[matching-client similarity] (and account-number
(parse/best-match clients account-number 0.0))
matching-client (and account-number
(d-clients/exact-match account-number))
_ (when-not matching-client
(throw (ex-info "cannot find matching client"
{:account-number account-number
@@ -81,14 +81,19 @@
(cond-> #:invoice {:invoice-number (header-row "InvoiceNumber")
:total (+ total tax)
:outstanding-balance (+ total tax)
:location (parse/best-location-match matching-client location-hint location-hint )
:location (parse/best-location-match (dc/pull (dc/db conn)
[{:client/location-matches [:location-match/location :location-match/matches]}
:client/default-location
:client/locations]
matching-client)
location-hint
location-hint )
:date (coerce/to-date date)
:vendor (:db/id sysco-vendor )
:client (:db/id matching-client)
:import-status :import-status/completed
:status :invoice-status/unpaid
:client-identifier customer-identifier}
similarity (assoc :invoice/similarity (- 1.0 (double similarity)))
true (code-invoice))))
(defn mark-key [k]
@@ -111,7 +116,6 @@
(defn import-sysco []
(let [sysco-vendor (get-sysco-vendor)
clients (d-clients/get-all)
keys (->> (s3/list-objects-v2 {:bucket-name bucket-name
:prefix "sysco/pending"})
:object-summaries
@@ -129,11 +133,11 @@
:destination-bucket-name (:data-bucket env)
:source-key k
:destination-key invoice-key})
[[:propose-invoice
(-> k
[`(propose-invoice
~(-> k
read-sysco-csv
(extract-invoice-details clients sysco-vendor)
(assoc :invoice/source-url invoice-url))]])
(extract-invoice-details sysco-vendor)
(assoc :invoice/source-url invoice-url)))])
(catch Exception e
(log/error (str "Cannot load file " k) e)
(log/info
@@ -144,8 +148,7 @@
(.getName (io/file k)))
println)}))
[])))))
result (transact-with-ledger transaction {:user/name "sysco importer" :user/role "admin"})]
#_(log/infof "Imported %d invoices" (/ (count (:tempids result)) 2)))
result (transact-with-ledger transaction {:user/name "sysco importer" :user/role "admin"})])
(doseq [k keys]
(mark-key k))))

View File

@@ -3,31 +3,29 @@
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.jobs.core :refer [execute]]
[datomic.api :as d]))
[datomic.client.api :as dc]))
(defn refresh-vendor-usages []
(->> {:query {:find ['?v '?c '(count ?e)]
:in ['$]
:where ['[?v :vendor/name]
'(or-join [?v ?c ?e]
(and
[?e :invoice/vendor ?v]
[?e :invoice/client ?c])
(and
[?e :transaction/vendor ?v]
[?e :transaction/client ?c])
(and
[?e :journal-entry/vendor ?v]
[?e :journal-entry/client ?c]))]}
:args [(d/db conn)]}
(d/query)
(map (fn [[v c cnt]]
#:vendor-usage {:vendor v
:client c
:key (str v "-" c)
:count cnt}))
(d/transact conn)
deref))
(dc/transact conn {:tx-data (->> (dc/q '[:find ?v ?c (count ?e)
:in $
:where
[?v :vendor/name]
(or-join [?v ?c ?e]
(and
[?e :invoice/vendor ?v]
[?e :invoice/client ?c])
(and
[?e :transaction/vendor ?v]
[?e :transaction/client ?c])
(and
[?e :journal-entry/vendor ?v]
[?e :journal-entry/client ?c]))]
(dc/db conn))
(map (fn [[v c cnt]]
#:vendor-usage {:vendor v
:client c
:key (str v "-" c)
:count cnt})))}))
(defn -main [& _]
(execute "vendor-usages" refresh-vendor-usages))