stores checks
This commit is contained in:
41
src/clj/auto_ap/db/checks.clj
Normal file
41
src/clj/auto_ap/db/checks.clj
Normal file
@@ -0,0 +1,41 @@
|
||||
(ns auto-ap.db.checks
|
||||
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn execute!] :as u]
|
||||
[auto-ap.entities.companies :as entity]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.java.jdbc :as j]
|
||||
[honeysql.core :as sql]
|
||||
[honeysql.helpers :as helpers]))
|
||||
|
||||
(def base-query (sql/build :select :*
|
||||
:from :checks))
|
||||
|
||||
(defn data->fields [x]
|
||||
(-> x
|
||||
(merge (:data x))
|
||||
(dissoc :data)))
|
||||
|
||||
(defn query [q]
|
||||
(map data->fields (u/query q)))
|
||||
|
||||
(defn get-all []
|
||||
(query base-query))
|
||||
|
||||
(defn get-by-id [id]
|
||||
(first (query (-> base-query
|
||||
(helpers/merge-where [:= :id id])))))
|
||||
|
||||
(defn fields->data [x]
|
||||
(-> x
|
||||
(assoc-in [:data :pdf-data] (:pdf-data x))
|
||||
(assoc-in [:data :invoices] (:invoices x))
|
||||
(dissoc :pdf-data)
|
||||
(dissoc :invoices)))
|
||||
|
||||
(defn insert-multi! [rows]
|
||||
(->> (j/insert-multi! (get-conn)
|
||||
:checks
|
||||
(->> rows
|
||||
(map fields->data)
|
||||
(map clj->db)))
|
||||
(map db->clj)
|
||||
(map data->fields)))
|
||||
@@ -2,6 +2,7 @@
|
||||
(:require [auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.db.checks :as checks]
|
||||
[auto-ap.db.utils :refer [query]]
|
||||
[auto-ap.parse :as parse]
|
||||
[amazonica.aws.s3 :as s3]
|
||||
@@ -161,38 +162,49 @@
|
||||
(map (fn [v] [(:id v) v])
|
||||
(vendors/get-all)))
|
||||
invoices-grouped-by-vendor (group-by :vendor-id invoices)
|
||||
uuid (str (UUID/randomUUID))
|
||||
checks (for [[vendor-id invoices] invoices-grouped-by-vendor
|
||||
:let [uuid (str (UUID/randomUUID))
|
||||
vendor (vendors vendor-id)
|
||||
company (companies (:company-id (first invoices)))
|
||||
memo (str "Invoice #'s: "
|
||||
(str/join ", "
|
||||
(map (fn [i]
|
||||
(str (:invoice-number i) "(" (:total i) ")"))
|
||||
invoices)))]]
|
||||
{:s3-uuid uuid
|
||||
:s3-key (str "checks/" uuid ".pdf")
|
||||
:s3-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/checks/" uuid ".pdf")
|
||||
:pdf-data {:vendor vendor
|
||||
:paid-to (:name vendor)
|
||||
:amount (reduce + 0 (map :total invoices))
|
||||
:check "1234"
|
||||
:memo memo
|
||||
:date "5/10/2018"
|
||||
:company {:name (:name company)
|
||||
:address1 "123 main st"
|
||||
:city "Campbell"
|
||||
:state "CA"
|
||||
:zip "95008"
|
||||
:bank {:name "Bank of America, NA"
|
||||
:acct "11-35/2010"
|
||||
:acct-number "123456789"}}}})]
|
||||
checks (-> (for [[vendor-id invoices] invoices-grouped-by-vendor
|
||||
:let [uuid (str (UUID/randomUUID))
|
||||
vendor (vendors vendor-id)
|
||||
company (companies (:company-id (first invoices)))
|
||||
memo (str "Invoice #'s: "
|
||||
(str/join ", "
|
||||
(map (fn [i]
|
||||
(str (:invoice-number i) "(" (:total i) ")"))
|
||||
invoices)))]]
|
||||
{:s3-uuid uuid
|
||||
:s3-key (str "checks/" uuid ".pdf")
|
||||
:s3-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/checks/" uuid ".pdf")
|
||||
:pdf-data {:vendor vendor
|
||||
:paid-to (:name vendor)
|
||||
:amount (reduce + 0 (map :total invoices))
|
||||
:check "1234"
|
||||
:memo memo
|
||||
:date "5/10/2018"
|
||||
:company {:name (:name company)
|
||||
:address1 "123 main st"
|
||||
:city "Campbell"
|
||||
:state "CA"
|
||||
:zip "95008"
|
||||
:bank {:name "Bank of America, NA"
|
||||
:acct "11-35/2010"
|
||||
:acct-number "123456789"}}}
|
||||
:invoices (map :id invoices)})
|
||||
(checks/insert-multi!))
|
||||
invoice-checks (mapcat
|
||||
(fn [c]
|
||||
(map
|
||||
(fn [i]
|
||||
{:invoice-id i
|
||||
:check-id (:id c)
|
||||
:amount (:total i)})
|
||||
(:invoices c)))
|
||||
checks) ]
|
||||
|
||||
(make-pdfs checks)
|
||||
|
||||
{:status 200
|
||||
:body (pr-str {:url (merge-pdfs (map :s3-key checks))
|
||||
:checks checks})
|
||||
:checks checks
|
||||
:invoice-checks invoice-checks})
|
||||
:headers {"Content-Type" "application/edn"}})))
|
||||
wrap-secure))
|
||||
|
||||
Reference in New Issue
Block a user