checks are generated in a pretty nice way
This commit is contained in:
@@ -11,16 +11,20 @@
|
|||||||
[compojure.core :refer [GET POST context defroutes
|
[compojure.core :refer [GET POST context defroutes
|
||||||
wrap-routes]]
|
wrap-routes]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clj-pdf.core :as pdf])
|
[clj-pdf.core :as pdf]
|
||||||
|
[clojure.java.io :as io])
|
||||||
(:import [java.text DecimalFormat]
|
(:import [java.text DecimalFormat]
|
||||||
[java.util UUID]))
|
[java.util UUID]
|
||||||
|
[java.io ByteArrayOutputStream]))
|
||||||
|
|
||||||
(defn check-page [checks uuid]
|
(defn make-check-pdf [check]
|
||||||
|
(let [output-stream (ByteArrayOutputStream.)]
|
||||||
(pdf/pdf
|
(pdf/pdf
|
||||||
[{:left-margin 10 :right-margin 0 :top-margin 0 :bottom-margin 0}
|
[{:left-margin 10 :right-margin 0 :top-margin 0 :bottom-margin 0}
|
||||||
(for [{:keys [paid-to company check date amount memo] {vendor-name :name :as vendor} :vendor} checks
|
(let [{:keys [paid-to company check date amount memo] {vendor-name :name :as vendor} :vendor} check
|
||||||
:let [df (DecimalFormat. "#,###.00")
|
df (DecimalFormat. "#,###.00")
|
||||||
amount (str "--" (.format df amount) "--")]]
|
amount (str "--" (.format df amount) "--")]
|
||||||
|
|
||||||
[:table {:num-cols 12 :border false :leading 11}
|
[:table {:num-cols 12 :border false :leading 11}
|
||||||
[(let [{:keys [name address1 city state zip bank]} company]
|
[(let [{:keys [name address1 city state zip bank]} company]
|
||||||
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" address1 "\n" (str city ", " state zip)] ])
|
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" address1 "\n" (str city ", " state zip)] ])
|
||||||
@@ -116,7 +120,34 @@
|
|||||||
[[:cell {:colspan 3} "Memo:"]
|
[[:cell {:colspan 3} "Memo:"]
|
||||||
[:cell {:colspan 9} memo]]
|
[:cell {:colspan 9} memo]]
|
||||||
])]
|
])]
|
||||||
(doto (str "/tmp/" uuid ".pdf") println)))
|
output-stream)
|
||||||
|
(.toByteArray output-stream)))
|
||||||
|
|
||||||
|
(defn make-pdfs [checks]
|
||||||
|
(loop [[check & checks] checks]
|
||||||
|
(when check
|
||||||
|
(s3/put-object :bucket-name (:data-bucket env)
|
||||||
|
:key (:s3-key check)
|
||||||
|
:input-stream (-> check
|
||||||
|
:pdf-data
|
||||||
|
make-check-pdf
|
||||||
|
(io/make-input-stream {}))
|
||||||
|
:metadata {:content-type "application/pdf"})
|
||||||
|
(recur checks))))
|
||||||
|
|
||||||
|
(defn merge-pdfs [keys]
|
||||||
|
(let [merged-pdf-stream (java.io.ByteArrayOutputStream.)
|
||||||
|
uuid (str (UUID/randomUUID))]
|
||||||
|
(apply pdf/collate (concat [merged-pdf-stream] (->> keys
|
||||||
|
(map #(s3/get-object (:data-bucket env) %))
|
||||||
|
(map :input-stream))))
|
||||||
|
(s3/put-object :bucket-name (:data-bucket env)
|
||||||
|
:key (str "merged-checks/" uuid ".pdf")
|
||||||
|
:input-stream (io/make-input-stream (.toByteArray merged-pdf-stream) {})
|
||||||
|
:metadata {:content-length (count (.toByteArray merged-pdf-stream))
|
||||||
|
:content-type "application/pdf"})
|
||||||
|
(str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/merged-checks/" uuid ".pdf")))
|
||||||
|
|
||||||
|
|
||||||
(defroutes routes
|
(defroutes routes
|
||||||
(wrap-routes
|
(wrap-routes
|
||||||
@@ -130,19 +161,20 @@
|
|||||||
(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))]
|
uuid (str (UUID/randomUUID))
|
||||||
|
checks (for [[vendor-id invoices] invoices-grouped-by-vendor
|
||||||
|
:let [uuid (str (UUID/randomUUID))
|
||||||
(check-page (for [[vendor-id invoices] invoices-grouped-by-vendor
|
vendor (vendors vendor-id)
|
||||||
:let [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
|
||||||
{:vendor vendor
|
: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)
|
:paid-to (:name vendor)
|
||||||
:amount (reduce + 0 (map :total invoices))
|
:amount (reduce + 0 (map :total invoices))
|
||||||
:check "1234"
|
:check "1234"
|
||||||
@@ -155,14 +187,12 @@
|
|||||||
: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"}}}})]
|
||||||
uuid)
|
|
||||||
|
|
||||||
(s3/put-object :bucket-name (:data-bucket env)
|
(make-pdfs checks)
|
||||||
:key (str "checks/" uuid ".pdf")
|
|
||||||
:file (str "/tmp/" uuid ".pdf"))
|
|
||||||
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (pr-str {:url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/checks/" uuid ".pdf")})
|
:body (pr-str {:url (merge-pdfs (map :s3-key checks))
|
||||||
|
:checks checks})
|
||||||
:headers {"Content-Type" "application/edn"}})))
|
:headers {"Content-Type" "application/edn"}})))
|
||||||
wrap-secure))
|
wrap-secure))
|
||||||
|
|||||||
Reference in New Issue
Block a user