Uploading invoices now attaches the file.

This commit is contained in:
2022-01-12 20:53:12 -08:00
parent 4d6b3b1e2e
commit 30bbf51011
7 changed files with 260 additions and 198 deletions

View File

@@ -1,20 +1,15 @@
(ns auto-ap.routes.invoices
(:require [auto-ap.datomic :refer [remove-nils uri]]
[auto-ap.datomic.accounts :as d-accounts]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :as d-invoices]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.graphql.utils :refer [assert-admin assert-can-see-client]]
[auto-ap.import.manual :as manual]
[auto-ap.import.manual.common :as c]
[auto-ap.logging :refer [info-event]]
[auto-ap.parse :as parse]
[auto-ap.parse.util :as parse-u]
[auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.time-utils :refer [next-dom]]
[auto-ap.utils :refer [by]]
[clj-time.coerce :as coerce :refer [to-date]]
[clj-time.core :as time]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.string :as str]
@@ -22,7 +17,9 @@
[compojure.core :refer [context defroutes POST wrap-routes]]
[datomic.api :as d]
[ring.middleware.json :refer [wrap-json-response]]
[unilog.context :as lc]))
[unilog.context :as lc]
[amazonica.aws.s3 :as s3]
[config.core :refer [env]]))
(defn reset-id [i]
(update i :invoice-number
@@ -97,51 +94,7 @@
rows))
(defn invoice-rows->transaction [rows]
(->> rows
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor automatically-paid-when-due schedule-payment-dom]}]
(let [invoice (cond->
#:invoice {:db/id (.toString (java.util.UUID/randomUUID))
:vendor vendor-id
:client client-id
:default-location default-location
:import-status :import-status/imported
:automatically-paid-when-due automatically-paid-when-due
#_#_:default-expense-account default-expense-account
:total total
:outstanding-balance (if (= "Cash" check)
0.0
total)
:status (if (= "Cash" check)
:invoice-status/paid
:invoice-status/unpaid)
:invoice-number invoice-number
:date (to-date date)
:expense-accounts [#:invoice-expense-account {:account (or account-id
(:db/id (d-vendors/account-for-client-id vendor client-id)))
:location default-location
:amount total}]}
(:vendor/terms vendor) (assoc :invoice/due (coerce/to-date
(time/plus date (time/days (d-vendors/terms-for-client-id vendor client-id)))))
automatically-paid-when-due (assoc :invoice/scheduled-payment (coerce/to-date
(time/plus date (time/days (d-vendors/terms-for-client-id vendor client-id)))))
schedule-payment-dom (assoc :invoice/scheduled-payment (coerce/to-date
(next-dom date schedule-payment-dom)
)))
payment (if (= :invoice-status/paid (:invoice/status invoice))
#:invoice-payment {:invoice (:db/id invoice)
:amount (:invoice/total invoice)
:payment (remove-nils #:payment {:db/id (.toString (java.util.UUID/randomUUID))
:vendor (:invoice/vendor invoice)
:client (:invoice/client invoice)
:type :payment-type/cash
:amount (:invoice/total invoice)
:status :payment-status/cleared
:date (:invoice/date invoice)})}
)]
[[:propose-invoice (remove-nils invoice)]
(some-> payment remove-nils)])))
(filter identity)))
(defn match-vendor [vendor-code forced-vendor]
@@ -170,99 +123,125 @@
(throw (ex-info (str "No vendor with the name " vendor-code " was found.")
{:vendor-code vendor-code})))))
(defn import-uploaded-invoice [user client forced-location forced-vendor imports]
(defn import->invoice [{:keys [invoice-number source-url customer-identifier account-number total date vendor-code text full-text client-override vendor-override location-override]} clients]
(let [matching-client (or (and account-number
(parse/best-match clients account-number 0.0))
(and customer-identifier
(parse/best-match clients customer-identifier))
(when client-override
(first (filter (fn [c]
(= (:db/id c) (Long/parseLong client-override)))
clients))))
matching-vendor (match-vendor vendor-code vendor-override)
matching-location (or (when-not (str/blank? location-override)
location-override)
(parse/best-location-match matching-client text full-text))]
(remove-nils #:invoice {:invoice/client (:db/id matching-client)
:invoice/client-identifier customer-identifier
:invoice/vendor (:db/id matching-vendor)
:invoice/source-url source-url
:invoice/invoice-number invoice-number
:invoice/total (Double/parseDouble total)
:invoice/date (to-date date)
:invoice/location matching-location
:invoice/import-status :import-status/pending
:invoice/outstanding-balance (Double/parseDouble total)
:invoice/status :invoice-status/unpaid})))
(defn validate-invoice [invoice user]
(when-not (:invoice/client invoice)
(throw (ex-info (str "Searched clients for '" (:invoice/client-identifier invoice) "'. No client found in file. Select a client first.")
{:invoice-number (:invoice/invoice-number invoice)
:customer-identifier (:invoice/client-identifier invoice)})))
(assert-can-see-client user (:invoice/client invoice))
(doseq [k [:invoice/invoice-number :invoice/client :invoice/vendor :invoice/total :invoice/outstanding-balance :invoice/date]]
(when (not (get invoice k))
(throw (ex-info (str (name k) "not found on invoice " invoice)
invoice))))
invoice)
(defn extant-invoice? [{:invoice/keys [invoice-number vendor client]}]
(try
(->> (d/query
(cond-> {:query {:find ['?e '?outstanding-balance '?status '?import-status2]
:in ['$ '?invoice-number '?vendor '?client]
:where '[[?e :invoice/invoice-number ?invoice-number]
[?e :invoice/vendor ?vendor]
[?e :invoice/client ?client]
[?e :invoice/outstanding-balance ?outstanding-balance]
[?e :invoice/status ?status]
[?e :invoice/import-status ?import-status]
[?import-status :db/ident ?import-status2]]}
:args [(d/db (d/connect uri)) invoice-number vendor client]}))
first
boolean)
(catch Exception e
(throw (ex-info (str "Failed to find potential matching invoice with"
" invoice " invoice-number
" vendor " vendor
" client " client
". "
(.toString e))
{:invoice-number invoice-number})))))
(defn invoice-rows->transaction [rows user]
(->> rows
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor automatically-paid-when-due schedule-payment-dom]}]
(let [invoice #:invoice {:db/id (.toString (java.util.UUID/randomUUID))
:vendor vendor-id
:client client-id
:default-location default-location
:location default-location
:import-status :import-status/imported
:automatically-paid-when-due automatically-paid-when-due
:total total
:outstanding-balance (if (= "Cash" check)
0.0
total)
:status (if (= "Cash" check)
:invoice-status/paid
:invoice-status/unpaid)
:invoice-number invoice-number
:date (to-date date)}
payment (if (= :invoice-status/paid (:invoice/status invoice))
#:invoice-payment {:invoice (:db/id invoice)
:amount (:invoice/total invoice)
:payment (remove-nils #:payment {:db/id (.toString (java.util.UUID/randomUUID))
:vendor (:invoice/vendor invoice)
:client (:invoice/client invoice)
:type :payment-type/cash
:amount (:invoice/total invoice)
:status :payment-status/cleared
:date (:invoice/date invoice)})}
)]
[[:propose-invoice (d-invoices/code-invoice (validate-invoice (remove-nils invoice)
user))]
(some-> payment remove-nils)])))
(filter identity)))
(defn import-uploaded-invoice [user imports]
(lc/with-context {:area "upload-invoice"}
(log/info "Number of invoices to import is" (count imports) "sample: " (first imports))
(let [clients (d-clients/get-all)
transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text]}]
(let [
matching-client (or (and account-number
(parse/best-match clients account-number 0.0))
(and customer-identifier
(parse/best-match clients customer-identifier))
(when client
(first (filter (fn [c]
(= (:db/id c) (Long/parseLong client)))
clients))))
_ (assert-can-see-client user (:db/id matching-client))
_ (when-not matching-client
(throw (ex-info (str "Searched clients for '" customer-identifier "'. No client found in file. Select a client first.")
{:invoice-number invoice-number
:customer-identifier customer-identifier
:vendor-code vendor-code})))
matching-vendor (match-vendor vendor-code forced-vendor)
_ (info-event "Found match for invoice" {:invoice-number invoice-number
:vendor (select-keys matching-vendor [:vendor/name :db/id])
:client (select-keys matching-client [:client/name :db/id])})
matching-location (or (when-not (str/blank? forced-location)
forced-location)
(parse/best-location-match matching-client text full-text))
[existing-id existing-outstanding-balance existing-status import-status] (when (and matching-client matching-location)
(try
(->> (d/query
(cond-> {:query {:find ['?e '?outstanding-balance '?status '?import-status2]
:in ['$ '?invoice-number '?vendor '?client]
:where '[[?e :invoice/invoice-number ?invoice-number]
[?e :invoice/vendor ?vendor]
[?e :invoice/client ?client]
[?e :invoice/outstanding-balance ?outstanding-balance]
[?e :invoice/status ?status]
[?e :invoice/import-status ?import-status]
[?import-status :db/ident ?import-status2]]}
:args [(d/db (d/connect uri)) invoice-number (:db/id matching-vendor) (:db/id matching-client)]}))
first)
(catch Exception e
(throw (ex-info (str "Failed to find potential matching invoice with"
" invoice " invoice-number
" vendor " matching-vendor
" client " (:client/name matching-client)
". "
(.toString e))
{:args [ invoice-number matching-vendor (:db/id matching-client)]})))
))
automatically-paid-for (set (map :db/id (:vendor/automatically-paid-when-due matching-vendor)))
schedule-payment-dom (get (by (comp :db/id :vendor-schedule-payment-dom/client) :vendor-schedule-payment-dom/dom (:vendor/schedule-payment-dom matching-vendor))
(:db/id matching-client))]
(cond
(not (and matching-location matching-client))
result
(= :import-status/imported import-status)
result
:else
(conj result [:propose-invoice (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
:invoice/client-identifier customer-identifier
:invoice/vendor (:db/id matching-vendor)
:invoice/invoice-number invoice-number
:invoice/total (Double/parseDouble total)
:invoice/date (to-date date)
:invoice/import-status :import-status/pending
:invoice/outstanding-balance (or existing-outstanding-balance (Double/parseDouble total))
:invoice/status (or existing-status :invoice-status/unpaid)
:invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (d-vendors/account-for-client-id matching-vendor (:db/id matching-client))
:location matching-location
:amount (Double/parseDouble total)}])
:db/id existing-id
})
(:vendor/terms matching-vendor) (assoc :invoice/due (coerce/to-date
(time/plus date (time/days (d-vendors/terms-for-client-id matching-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 matching-vendor (:db/id matching-client))))))
schedule-payment-dom (assoc :invoice/scheduled-payment (to-date
(next-dom date schedule-payment-dom))))]))
))
[]
imports)]
(when-not (seq transactions)
(let [clients (d-clients/get-all)
potential-invoices (->> imports
(mapv #(import->invoice % clients))
(mapv #(validate-invoice % user))
(filter #(not (extant-invoice? %)))
(mapv d-invoices/code-invoice)
(mapv (fn [i] [:propose-invoice i])))]
(when-not (seq potential-invoices)
(throw (ex-info "No new invoices found."
{:imports (str imports)})))
(log/info "creating invoice" transactions)
@(d/transact (d/connect uri) (vec (set transactions))))))
{:imports (str (vec imports))})))
(log/info "creating invoice" potential-invoices)
@(d/transact (d/connect uri) potential-invoices))))
(defn validate-account-rows [rows code->existing-account]
@@ -273,8 +252,7 @@
))))]
(throw (ex-info (str "You are adding accounts without a valid type" )
{:rows bad-types})))
(when-let [duplicate-rows (seq (->> rows
(when-let [duplicate-rows (seq (->> rows
(filter (fn [[account]]
(not-empty account)))
(group-by (fn [[account]]
@@ -426,7 +404,19 @@
{:keys [filename tempfile]} files]
(lc/with-context {:parsing-file filename}
(try
(import-uploaded-invoice user client location vendor (parse/parse-file (.getPath tempfile) filename))
(let [extension (last (str/split (.getName (io/file filename)) #"\." ))
s3-location (str "invoice-files/" (str (UUID/randomUUID)) "." extension)
_ (s3/put-object :bucket-name (:data-bucket env)
:key s3-location
:input-stream (io/input-stream tempfile)
:metadata {:content-type "application/pdf"})
imports (->> (parse/parse-file (.getPath tempfile) filename)
(map #(assoc %
:client-override client
:location-override location
:vendor-override vendor
:source-url (str "https://" (:data-bucket env) "/" s3-location))))]
(import-uploaded-invoice user imports))
{:status 200
:body (pr-str {})
:headers {"Content-Type" "application/edn"}}
@@ -459,7 +449,8 @@
(not= "Cash" (:check %))))
(map :vendor-name)
set)
inserted-rows @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows)))]
inserted-rows @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows)
user))]
{:status 200
:body (pr-str {:imported (count (:new grouped-rows))
:already-imported (count (:exists grouped-rows))