Makes integreat run on datomic cloud
This commit is contained in:
@@ -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))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user