adds register invoice import
This commit is contained in:
133
src/clj/auto_ap/jobs/register_invoice_import.clj
Normal file
133
src/clj/auto_ap/jobs/register_invoice_import.clj
Normal file
@@ -0,0 +1,133 @@
|
||||
(ns auto-ap.jobs.register-invoice-import
|
||||
(:gen-class)
|
||||
(:require
|
||||
[auto-ap.jobs.core :refer [execute]]
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[clojure.string :as str]
|
||||
[clj-time.coerce :as coerce]
|
||||
[auto-ap.time :as atime]
|
||||
[auto-ap.datomic :refer [conn]]
|
||||
[clojure.data.csv :as csv]
|
||||
[clojure.java.io :as io]
|
||||
[config.core :refer [env]]
|
||||
[clojure.tools.logging :as log]))
|
||||
|
||||
(def bucket (:data-bucket env))
|
||||
|
||||
(defn s3->csv [url]
|
||||
(->> (-> (s3/get-object {:bucket-name bucket
|
||||
:key (str "bulk-import/" url)})
|
||||
:input-stream
|
||||
io/reader
|
||||
csv/read-csv)))
|
||||
|
||||
(defn register-invoice-import [args]
|
||||
(let [{:keys [ledger-url]} args
|
||||
data (s3->csv ledger-url)
|
||||
db (d/db conn)
|
||||
i->invoice-id (fn [i]
|
||||
(try (Long/parseLong i)
|
||||
(catch Exception e
|
||||
(:db/id (d/pull db '[:db/id]
|
||||
[:invoice/original-id (Long/parseLong (first (str/split i #"-")))])))))
|
||||
invoice-totals (->> data
|
||||
(drop 1)
|
||||
(group-by first)
|
||||
(map (fn [[k values]]
|
||||
[(i->invoice-id k)
|
||||
(reduce + 0.0
|
||||
(->> values
|
||||
(map (fn [[_ _ _ _ amount]]
|
||||
(- (Double/parseDouble amount))))))
|
||||
]))
|
||||
(into {}))
|
||||
changes (->>
|
||||
(for [[i
|
||||
invoice-expense-account-id
|
||||
target-account
|
||||
target-date
|
||||
amount
|
||||
_
|
||||
location] (drop 1 data)
|
||||
:let [invoice-id (i->invoice-id i)
|
||||
|
||||
invoice (d/entity db invoice-id)
|
||||
current-total (:invoice/total invoice)
|
||||
target-total (invoice-totals invoice-id) ;; TODO should include expense accounts not visible
|
||||
new-account? (not (boolean (or (some-> invoice-expense-account-id not-empty Long/parseLong)
|
||||
(:db/id (first (:invoice/expense-accounts invoice))))))
|
||||
|
||||
invoice-expense-account-id (or (some-> invoice-expense-account-id not-empty Long/parseLong)
|
||||
(:db/id (first (:invoice/expense-accounts invoice)))
|
||||
(d/tempid :db.part/user))
|
||||
invoice-expense-account (when-not new-account?
|
||||
(or (d/entity db invoice-expense-account-id)
|
||||
(d/entity db [:invoice-expense-account/original-id invoice-expense-account-id])))
|
||||
current-account-id (:db/id (:invoice-expense-account/account invoice-expense-account))
|
||||
target-account-id (Long/parseLong (str/trim target-account))
|
||||
|
||||
target-date (coerce/to-date (atime/parse target-date atime/normal-date))
|
||||
current-date (:invoice/date invoice)
|
||||
|
||||
|
||||
current-expense-account-amount (:invoice-expense-account/amount invoice-expense-account 0.0)
|
||||
target-expense-account-amount (- (Double/parseDouble amount))
|
||||
|
||||
|
||||
current-expense-account-location (:invoice-expense-account/location invoice-expense-account)
|
||||
target-expense-account-location location
|
||||
|
||||
|
||||
[[_ _ invoice-payment]] (vec (d/q
|
||||
'[:find ?p ?a ?ip
|
||||
:in $ ?i
|
||||
:where [?ip :invoice-payment/invoice ?i]
|
||||
[?ip :invoice-payment/amount ?a]
|
||||
[?ip :invoice-payment/payment ?p]
|
||||
]
|
||||
db invoice-id))]
|
||||
:when current-total]
|
||||
|
||||
[
|
||||
(when (not (dollars= current-total target-total))
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total})
|
||||
|
||||
(when new-account?
|
||||
{:db/id invoice-id
|
||||
:invoice/expense-accounts invoice-expense-account-id})
|
||||
|
||||
(when (and target-date (not= current-date target-date))
|
||||
{:db/id invoice-id
|
||||
:invoice/date target-date})
|
||||
|
||||
(when (and
|
||||
(not (dollars= current-total target-total))
|
||||
invoice-payment)
|
||||
[:db/retractEntity invoice-payment])
|
||||
|
||||
(when (or new-account?
|
||||
(not (dollars= current-expense-account-amount target-expense-account-amount)))
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/amount target-expense-account-amount})
|
||||
|
||||
(when (not= current-expense-account-location
|
||||
target-expense-account-location)
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/location target-expense-account-location})
|
||||
|
||||
(when (not= current-account-id target-account-id )
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/account target-account-id})])
|
||||
(mapcat identity)
|
||||
(filter identity)
|
||||
vec)]
|
||||
(doseq [n (partition-all 50 changes)]
|
||||
(log/info "transacting" n)
|
||||
@(d/transact conn n))
|
||||
))
|
||||
|
||||
(defn -main [& _]
|
||||
(execute "register-invoice-import" #(register-invoice-import (:args env))))
|
||||
@@ -2,16 +2,17 @@
|
||||
(:gen-class)
|
||||
(:require
|
||||
[auto-ap.handler :refer [app]]
|
||||
[auto-ap.jobs.bulk-journal-import :as job-bulk-journal-import]
|
||||
[auto-ap.jobs.close-auto-invoices :as job-close-auto-invoices]
|
||||
[auto-ap.jobs.current-balance-cache :as job-current-balance-cache]
|
||||
[auto-ap.jobs.ezcater-upsert :as job-ezcater-upsert]
|
||||
[auto-ap.jobs.import-uploaded-invoices :as job-import-uploaded-invoices]
|
||||
[auto-ap.jobs.intuit :as job-intuit]
|
||||
[auto-ap.jobs.ledger-reconcile :as job-reconcile-ledger]
|
||||
[auto-ap.jobs.plaid :as job-plaid]
|
||||
[auto-ap.jobs.ezcater-upsert :as job-ezcater-upsert]
|
||||
[auto-ap.jobs.register-invoice-import :as job-register-invoice-import]
|
||||
[auto-ap.jobs.square :as job-square]
|
||||
[auto-ap.jobs.square2 :as job-square2]
|
||||
[auto-ap.jobs.bulk-journal-import :as job-bulk-journal-import]
|
||||
[auto-ap.jobs.sysco :as job-sysco]
|
||||
[auto-ap.jobs.vendor-usages :as job-vendor-usages]
|
||||
[auto-ap.jobs.yodlee2 :as job-yodlee2]
|
||||
@@ -125,6 +126,10 @@
|
||||
(= job "ezcater-upsert")
|
||||
(job-ezcater-upsert/-main)
|
||||
|
||||
|
||||
(= job "register-invoice-import")
|
||||
(job-register-invoice-import/-main)
|
||||
|
||||
(= job "bulk-journal-import")
|
||||
(job-bulk-journal-import/-main)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user