adds register invoice import

This commit is contained in:
2022-12-04 11:09:51 -08:00
parent 58fad1b4f1
commit 3e512a52ac
7 changed files with 394 additions and 19 deletions

View 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))))

View File

@@ -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)

View File

@@ -5,6 +5,7 @@
[auto-ap.ledger :as l :refer [transact-with-ledger]]
[auto-ap.server]
[auto-ap.square.core :as square]
[auto-ap.square.core2 :as square2]
[auto-ap.time :as atime]
[auto-ap.utils :refer [by]]
[clj-time.coerce :as c]
@@ -466,11 +467,43 @@
(square/upsert-settlements client square-location)))))
(defn historical-load-sales2 [client-code days]
(let [client (d/pull (d/db auto-ap.datomic/conn)
square/square-read
[:client/code client-code])]
(doseq [square-location (:client/square-locations client)
:when (:square-location/client-location square-location)]
(println "orders")
(lc/with-context {:source "Historical loading data"}
(doseq [d (per/periodic-seq (t/plus (t/today) (t/days (- days)))
(t/today)
(t/days 1))]
(println d)
(square2/upsert client square-location (c/to-date-time d) (c/to-date-time (t/plus d (t/days 1))))))
(println "refunds")
(square2/upsert-refunds client square-location)
(println "settlements")
(with-redefs [square2/lookup-dates (fn lookup-dates []
(->> (per/periodic-seq (t/plus (t/today) (t/days (- days)))
(t/today)
(t/days 2))
(map (fn [d]
[(atime/unparse (t/plus d (t/days 1)) atime/iso-date)
(atime/unparse (t/plus d (t/days 2)) atime/iso-date)]))))]
(square2/upsert-settlements client square-location)))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn load-sales-for-day [date]
(doseq [client (d/q [:find [(list 'pull '?e square/square-read ) '...]
:where ['?e :client/square-locations ]]
(d/db auto-ap.datomic/conn))
:where ['?e :client/square-locations ]]
(d/db auto-ap.datomic/conn))
square-location (:client/square-locations client)
:when (:square-location/client-location square-location)]
(println client)

View File

@@ -17,7 +17,7 @@
(def default-read [:name :start-date :end-date :status])
(def job-types [:yodlee2 :yodlee2-accounts :intuit :plaid :bulk-journal-import :ezcater-upsert])
(def job-types [:yodlee2 :yodlee2-accounts :intuit :plaid :bulk-journal-import :register-invoice-import :ezcater-upsert])
(re-frame/reg-event-fx
::params-change
@@ -64,7 +64,7 @@
:venia/queries [{:query/data
[:request-job
{:which (name which)
:args (when (= which :bulk-journal-import)
:args (when (#{:bulk-journal-import :register-invoice-import} which)
(str/escape (pr-str (:data form)) {\" "\\\""}))}
[:message]]}]}
:on-success [::success]}}))
@@ -128,6 +128,22 @@
[form-builder/submit-button {:class "is-small"} "Ledger Import"]
]]])
(defn register-invoice-import-button []
[form-builder/builder {:submit-event [::request :register-invoice-import]
:id ::register-invoice-import-form}
[:p.field.has-addons
[:p.control
[:a.button.is-static.is-small
"s3://data.prod.app.integreatconsult.com/bulk-import/"]]
[:p.control
[form-builder/raw-field-v2 {:field :ledger-url}
[:input.input.is-small {:placeholder "invoices.csv"}]]]
[:p.control
[form-builder/submit-button {:class "is-small"} "Register Invoice Import"]
]]])
;; VIEWS
(def jobs-content
(with-meta
@@ -147,7 +163,8 @@
[job-button {:which :intuit} "Start Intuit"]
[job-button {:which :plaid} "Start Plaid"]
[job-button {:which :ezcater-upsert} "EZCater Sync"]
[bulk-journal-import-button]]]
[bulk-journal-import-button]
[register-invoice-import-button]]]
[table/table {:id :jobs
:data-page ::page}]])]))
{:component-did-mount (dispatch-event [::mounted ])