stores checks
This commit is contained in:
3
migrator/migrations/1526142253-DOWN-add-checks.sql
Normal file
3
migrator/migrations/1526142253-DOWN-add-checks.sql
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
-- 1526142253 DOWN add-checks
|
||||||
|
DROP TABLE checks;
|
||||||
|
DROP TABLE invoices_checks;
|
||||||
20
migrator/migrations/1526142253-UP-add-checks.sql
Normal file
20
migrator/migrations/1526142253-UP-add-checks.sql
Normal 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
|
||||||
|
);
|
||||||
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]
|
(:require [auto-ap.db.companies :as companies]
|
||||||
[auto-ap.db.vendors :as vendors]
|
[auto-ap.db.vendors :as vendors]
|
||||||
[auto-ap.db.invoices :as invoices]
|
[auto-ap.db.invoices :as invoices]
|
||||||
|
[auto-ap.db.checks :as checks]
|
||||||
[auto-ap.db.utils :refer [query]]
|
[auto-ap.db.utils :refer [query]]
|
||||||
[auto-ap.parse :as parse]
|
[auto-ap.parse :as parse]
|
||||||
[amazonica.aws.s3 :as s3]
|
[amazonica.aws.s3 :as s3]
|
||||||
@@ -161,38 +162,49 @@
|
|||||||
(map (fn [v] [(:id v) v])
|
(map (fn [v] [(:id v) v])
|
||||||
(vendors/get-all)))
|
(vendors/get-all)))
|
||||||
invoices-grouped-by-vendor (group-by :vendor-id invoices)
|
invoices-grouped-by-vendor (group-by :vendor-id invoices)
|
||||||
uuid (str (UUID/randomUUID))
|
checks (-> (for [[vendor-id invoices] invoices-grouped-by-vendor
|
||||||
checks (for [[vendor-id invoices] invoices-grouped-by-vendor
|
:let [uuid (str (UUID/randomUUID))
|
||||||
:let [uuid (str (UUID/randomUUID))
|
vendor (vendors vendor-id)
|
||||||
vendor (vendors vendor-id)
|
company (companies (:company-id (first invoices)))
|
||||||
company (companies (:company-id (first invoices)))
|
memo (str "Invoice #'s: "
|
||||||
memo (str "Invoice #'s: "
|
(str/join ", "
|
||||||
(str/join ", "
|
(map (fn [i]
|
||||||
(map (fn [i]
|
(str (:invoice-number i) "(" (:total i) ")"))
|
||||||
(str (:invoice-number i) "(" (:total i) ")"))
|
invoices)))]]
|
||||||
invoices)))]]
|
{:s3-uuid uuid
|
||||||
{:s3-uuid uuid
|
:s3-key (str "checks/" uuid ".pdf")
|
||||||
:s3-key (str "checks/" uuid ".pdf")
|
:s3-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/checks/" uuid ".pdf")
|
||||||
:s3-url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/checks/" uuid ".pdf")
|
:pdf-data {:vendor vendor
|
||||||
:pdf-data {:vendor vendor
|
:paid-to (:name vendor)
|
||||||
:paid-to (:name vendor)
|
:amount (reduce + 0 (map :total invoices))
|
||||||
:amount (reduce + 0 (map :total invoices))
|
:check "1234"
|
||||||
:check "1234"
|
:memo memo
|
||||||
:memo memo
|
:date "5/10/2018"
|
||||||
:date "5/10/2018"
|
:company {:name (:name company)
|
||||||
:company {:name (:name company)
|
:address1 "123 main st"
|
||||||
:address1 "123 main st"
|
:city "Campbell"
|
||||||
:city "Campbell"
|
:state "CA"
|
||||||
:state "CA"
|
:zip "95008"
|
||||||
:zip "95008"
|
:bank {:name "Bank of America, NA"
|
||||||
:bank {:name "Bank of America, NA"
|
:acct "11-35/2010"
|
||||||
:acct "11-35/2010"
|
:acct-number "123456789"}}}
|
||||||
: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)
|
(make-pdfs checks)
|
||||||
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (pr-str {:url (merge-pdfs (map :s3-key checks))
|
:body (pr-str {:url (merge-pdfs (map :s3-key checks))
|
||||||
:checks checks})
|
:checks checks
|
||||||
|
:invoice-checks invoice-checks})
|
||||||
:headers {"Content-Type" "application/edn"}})))
|
:headers {"Content-Type" "application/edn"}})))
|
||||||
wrap-secure))
|
wrap-secure))
|
||||||
|
|||||||
Reference in New Issue
Block a user