stores checks

This commit is contained in:
Bryce Covert
2018-05-12 10:51:00 -07:00
parent 3f0e15da5d
commit 501a3f0ff1
4 changed files with 104 additions and 28 deletions

View File

@@ -0,0 +1,3 @@
-- 1526142253 DOWN add-checks
DROP TABLE checks;
DROP TABLE invoices_checks;

View File

@@ -0,0 +1,20 @@
-- 1526142253 UP add-checks
CREATE TABLE checks (id serial primary key,
s3_uuid varchar(255),
s3_key varchar(255),
s3_url varchar(255),
vendor_id int references vendors(id),
company_id int references companies(id),
check_number int,
memo varchar(512),
date timestamp with time zone,
amount decimal,
paid_to varchar(255),
data text);
CREATE TABLE invoices_checks (
id serial primary key,
check_id int references checks(id),
invoice_id int references invoices(id),
amount decimal
);

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

View File

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