Uploading invoices now attaches the file.
This commit is contained in:
@@ -3,23 +3,23 @@
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[auto-ap.datomic :refer [conn]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.invoices :refer [code-invoice]]
|
||||
[auto-ap.parse :as parse]
|
||||
[auto-ap.time :as t]
|
||||
[auto-ap.time-utils :refer [next-dom]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.data.csv :as csv]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.string :as str]
|
||||
[clojure.tools.logging :as log]
|
||||
[com.unbounce.dogstatsd.core :as statsd]
|
||||
[config.core :refer [env]]
|
||||
[datomic.api :as d]
|
||||
[unilog.context :as lc]
|
||||
[clj-time.core :as time]
|
||||
[mount.core :as mount]
|
||||
[yang.scheduler :as scheduler]
|
||||
[com.unbounce.dogstatsd.core :as statsd]))
|
||||
[unilog.context :as lc]
|
||||
[yang.scheduler :as scheduler])
|
||||
(:import
|
||||
(java.util UUID)))
|
||||
|
||||
(def bucket-name (:data-bucket env))
|
||||
|
||||
@@ -76,37 +76,25 @@
|
||||
tax (Double/parseDouble (summary-row "TotalTaxAmount"))
|
||||
date (t/parse
|
||||
(header-row "InvoiceDate")
|
||||
"yyMMdd")
|
||||
automatically-paid-for (set (map :db/id (:vendor/automatically-paid-when-due sysco-vendor)))
|
||||
schedule-payment-dom (get (by (comp :db/id :vendor-schedule-payment-dom/client) :vendor-schedule-payment-dom/dom (:vendor/schedule-payment-dom sysco-vendor))
|
||||
(:db/id matching-client))]
|
||||
"yyMMdd")]
|
||||
(log/infof "Importing %s for %s" (header-row "InvoiceNumber") (header-row "CustomerName"))
|
||||
|
||||
[:propose-invoice (cond-> #:invoice {:invoice-number (header-row "InvoiceNumber")
|
||||
:total (+ total tax)
|
||||
:outstanding-balance (+ total tax)
|
||||
: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
|
||||
:expense-accounts [#:invoice-expense-account {:account (:db/id (:vendor/default-account sysco-vendor))
|
||||
:amount (+ total tax)
|
||||
:location (parse/best-location-match matching-client location-hint location-hint )}]}
|
||||
(:vendor/terms sysco-vendor) (assoc :invoice/due (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id sysco-vendor (:db/id matching-client))))))
|
||||
|
||||
(boolean (automatically-paid-for (:db/id matching-client))) (assoc :invoice/scheduled-payment (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id sysco-vendor (:db/id matching-client))))))
|
||||
schedule-payment-dom (assoc :invoice/scheduled-payment (coerce/to-date
|
||||
(next-dom date schedule-payment-dom))))]))
|
||||
(code-invoice #:invoice {:invoice-number (header-row "InvoiceNumber")
|
||||
:total (+ total tax)
|
||||
:outstanding-balance (+ total tax)
|
||||
:location (parse/best-location-match 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})))
|
||||
|
||||
(defn mark-key [k]
|
||||
(s3/copy-object {:source-bucket-name bucket-name
|
||||
(s3/copy-object {:source-bucket-name bucket-name
|
||||
:destination-bucket-name bucket-name
|
||||
:destination-key (str/replace-first k "pending" "imported")
|
||||
:source-key k})
|
||||
:destination-key (str/replace-first k "pending" "imported")
|
||||
:source-key k})
|
||||
(s3/delete-object {:bucket-name bucket-name
|
||||
:key k}))
|
||||
|
||||
@@ -127,6 +115,7 @@
|
||||
:prefix "sysco/pending"})
|
||||
:object-summaries
|
||||
(map :key))]
|
||||
|
||||
|
||||
(statsd/event {:title "Sysco import started"
|
||||
:text (format "Found %d sysco invoice to import: %s" (count keys) (pr-str keys))
|
||||
@@ -134,20 +123,36 @@
|
||||
nil)
|
||||
(log/infof "Found %d sysco invoice to import: %s" (count keys) (pr-str keys))
|
||||
|
||||
(let [result @(d/transact conn (mapv (fn [k]
|
||||
(try
|
||||
(-> k
|
||||
read-sysco-csv
|
||||
(extract-invoice-details clients sysco-vendor))
|
||||
(catch Exception e
|
||||
(log/error e)
|
||||
[])))
|
||||
keys))]
|
||||
(let [transaction (->> keys
|
||||
(mapcat (fn [k]
|
||||
(try
|
||||
(let [invoice-key (str "invoice-files/" (UUID/randomUUID) ".csv")
|
||||
invoice-url (str "https://" (:data-bucket env) "/" invoice-key)]
|
||||
(s3/copy-object {:source-bucket-name (:data-bucket env)
|
||||
:destination-bucket-name (:data-bucket env)
|
||||
:source-key k
|
||||
:destination-key invoice-key})
|
||||
[[:propose-invoice
|
||||
(-> k
|
||||
read-sysco-csv
|
||||
(extract-invoice-details clients sysco-vendor)
|
||||
(assoc :invoice/source-url invoice-url))]])
|
||||
(catch Exception e
|
||||
(log/error (str "Cannot load file " k) e)
|
||||
(log/info
|
||||
(s3/copy-object {:source-bucket-name (:data-bucket env)
|
||||
:destination-bucket-name (:data-bucket env)
|
||||
:source-key k
|
||||
:destination-key (doto (str "sysco/error/"
|
||||
(.getName (io/file k)))
|
||||
println)}))
|
||||
[])))))
|
||||
result @(d/transact conn transaction)]
|
||||
(log/infof "Imported %d invoices" (/ (count (:tempids result)) 2)))
|
||||
(doseq [k keys]
|
||||
(mark-key k))
|
||||
(statsd/event {:title "Sysco import ended"
|
||||
:text "Sysco completed"
|
||||
(statsd/event {:title "Sysco import ended"
|
||||
:text "Sysco completed"
|
||||
:priority :low} nil))))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user