checks are generated in a pretty nice way
This commit is contained in:
@@ -11,112 +11,143 @@
|
||||
[compojure.core :refer [GET POST context defroutes
|
||||
wrap-routes]]
|
||||
[clojure.string :as str]
|
||||
[clj-pdf.core :as pdf])
|
||||
[clj-pdf.core :as pdf]
|
||||
[clojure.java.io :as io])
|
||||
(:import [java.text DecimalFormat]
|
||||
[java.util UUID]))
|
||||
[java.util UUID]
|
||||
[java.io ByteArrayOutputStream]))
|
||||
|
||||
(defn check-page [checks uuid]
|
||||
(pdf/pdf
|
||||
[{: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 [df (DecimalFormat. "#,###.00")
|
||||
amount (str "--" (.format df amount) "--")]]
|
||||
[:table {:num-cols 12 :border false :leading 11}
|
||||
[(let [{:keys [name address1 city state zip bank]} company]
|
||||
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" address1 "\n" (str city ", " state zip)] ])
|
||||
(let [{{:keys [name acct]} :bank} company]
|
||||
[:cell {:colspan 6 :align :center} [:paragraph {:style :bold} name] [:paragraph acct]])
|
||||
[:cell {:colspan 2 :size 13}
|
||||
check]]
|
||||
(defn make-check-pdf [check]
|
||||
(let [output-stream (ByteArrayOutputStream.)]
|
||||
(pdf/pdf
|
||||
[{:left-margin 10 :right-margin 0 :top-margin 0 :bottom-margin 0}
|
||||
(let [{:keys [paid-to company check date amount memo] {vendor-name :name :as vendor} :vendor} check
|
||||
df (DecimalFormat. "#,###.00")
|
||||
amount (str "--" (.format df amount) "--")]
|
||||
|
||||
[:table {:num-cols 12 :border false :leading 11}
|
||||
[(let [{:keys [name address1 city state zip bank]} company]
|
||||
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" address1 "\n" (str city ", " state zip)] ])
|
||||
(let [{{:keys [name acct]} :bank} company]
|
||||
[:cell {:colspan 6 :align :center} [:paragraph {:style :bold} name] [:paragraph acct]])
|
||||
[:cell {:colspan 2 :size 13}
|
||||
check]]
|
||||
|
||||
[[:cell {:colspan 9}]
|
||||
[:cell {:colspan 3} date]]
|
||||
[[:cell {:colspan 9}]
|
||||
[:cell {:colspan 3} date]]
|
||||
|
||||
[[:cell {:size 16 :leading 16} "PAY"]
|
||||
[:cell {:size 6 :leading 6 } "TO THE ORDER OF"]
|
||||
[:cell {:colspan 7} vendor-name]
|
||||
[:cell {:colspan 3} amount]]
|
||||
[[:cell {:size 16 :leading 16} "PAY"]
|
||||
[:cell {:size 6 :leading 6 } "TO THE ORDER OF"]
|
||||
[:cell {:colspan 7} vendor-name]
|
||||
[:cell {:colspan 3} amount]]
|
||||
|
||||
[[:cell {}]
|
||||
[:cell {:colspan 8} "One thousand two hundred thirty four and fifty six cents" [:line]]
|
||||
[:cell {:colspan 3}]]
|
||||
[[:cell {}]
|
||||
[:cell {:colspan 8} "One thousand two hundred thirty four and fifty six cents" [:line]]
|
||||
[:cell {:colspan 3}]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
|
||||
[[:cell "MEMO"]
|
||||
[:cell {:colspan 7} memo [:line]]
|
||||
[:cell {:colspan 4}]]
|
||||
[[:cell "MEMO"]
|
||||
[:cell {:colspan 7} memo [:line]]
|
||||
[:cell {:colspan 4}]]
|
||||
|
||||
[[:cell {:colspan 2}]
|
||||
[:cell {:colspan 10 :leading 30}
|
||||
[:phrase {:size 18 :ttf-name "public/micrenc.ttf"} (str "c" check "c a" (:acct-number (:bank company)) "a 10302c")]]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell]
|
||||
(into
|
||||
[:cell {:colspan 9}]
|
||||
(let [{:keys [name address1 city state zip bank]} company]
|
||||
(list
|
||||
[:paragraph name]
|
||||
[:paragraph address1]
|
||||
[:paragraph city ", " state zip]
|
||||
)))
|
||||
[:cell {:colspan 2 :size 13}
|
||||
check]]
|
||||
[[:cell {:colspan 2}]
|
||||
[:cell {:colspan 10 :leading 30}
|
||||
[:phrase {:size 18 :ttf-name "public/micrenc.ttf"} (str "c" check "c a" (:acct-number (:bank company)) "a 10302c")]]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell]
|
||||
(into
|
||||
[:cell {:colspan 9}]
|
||||
(let [{:keys [name address1 city state zip bank]} company]
|
||||
(list
|
||||
[:paragraph name]
|
||||
[:paragraph address1]
|
||||
[:paragraph city ", " state zip]
|
||||
)))
|
||||
[:cell {:colspan 2 :size 13}
|
||||
check]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
|
||||
[[:cell]
|
||||
[:cell {:colspan 5} [:paragraph
|
||||
vendor-name "\n"
|
||||
(:address1 vendor) "\n"
|
||||
(:city vendor) ", " (:state vendor) " " (:zip vendor)]]
|
||||
[:cell {:align :right}
|
||||
"Paid to:\n"
|
||||
"Amount:\n"
|
||||
"Date:\n"]
|
||||
[[:cell]
|
||||
[:cell {:colspan 5} [:paragraph
|
||||
vendor-name "\n"
|
||||
(:address1 vendor) "\n"
|
||||
(:city vendor) ", " (:state vendor) " " (:zip vendor)]]
|
||||
[:cell {:align :right}
|
||||
"Paid to:\n"
|
||||
"Amount:\n"
|
||||
"Date:\n"]
|
||||
|
||||
[:cell {:colspan 5}
|
||||
[:paragraph paid-to]
|
||||
[:paragraph amount]
|
||||
[:paragraph date]]]
|
||||
[:cell {:colspan 5}
|
||||
[:paragraph paid-to]
|
||||
[:paragraph amount]
|
||||
[:paragraph date]]]
|
||||
|
||||
[[:cell {:colspan 3} "Memo:"]
|
||||
[:cell {:colspan 9} memo]]
|
||||
[[:cell {:colspan 3} "Memo:"]
|
||||
[:cell {:colspan 9} memo]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
|
||||
[[:cell {:colspan 5}]
|
||||
[:cell {:align :right :colspan 2}
|
||||
"Check:\n"
|
||||
"Vendor:\n"
|
||||
"Bank Account:\n"
|
||||
"Paid To:\n"
|
||||
"Amount:\n"
|
||||
"Date:\n"]
|
||||
[[:cell {:colspan 5}]
|
||||
[:cell {:align :right :colspan 2}
|
||||
"Check:\n"
|
||||
"Vendor:\n"
|
||||
"Bank Account:\n"
|
||||
"Paid To:\n"
|
||||
"Amount:\n"
|
||||
"Date:\n"]
|
||||
|
||||
[:cell {:colspan 5}
|
||||
[:paragraph check]
|
||||
[:paragraph vendor-name]
|
||||
[:paragraph (:name (:bank company))]
|
||||
[:paragraph paid-to]
|
||||
[:paragraph amount]
|
||||
[:paragraph date]]]
|
||||
[[:cell {:colspan 3} "Memo:"]
|
||||
[:cell {:colspan 9} memo]]
|
||||
])]
|
||||
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")))
|
||||
|
||||
[:cell {:colspan 5}
|
||||
[:paragraph check]
|
||||
[:paragraph vendor-name]
|
||||
[:paragraph (:name (:bank company))]
|
||||
[:paragraph paid-to]
|
||||
[:paragraph amount]
|
||||
[:paragraph date]]]
|
||||
[[:cell {:colspan 3} "Memo:"]
|
||||
[:cell {:colspan 9} memo]]
|
||||
])]
|
||||
(doto (str "/tmp/" uuid ".pdf") println)))
|
||||
|
||||
(defroutes routes
|
||||
(wrap-routes
|
||||
@@ -130,39 +161,38 @@
|
||||
(map (fn [v] [(:id v) v])
|
||||
(vendors/get-all)))
|
||||
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))
|
||||
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"}}}})]
|
||||
|
||||
|
||||
(check-page (for [[vendor-id invoices] invoices-grouped-by-vendor
|
||||
:let [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)
|
||||
))]]
|
||||
{: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"}}})
|
||||
uuid)
|
||||
|
||||
(s3/put-object :bucket-name (:data-bucket env)
|
||||
:key (str "checks/" uuid ".pdf")
|
||||
:file (str "/tmp/" uuid ".pdf"))
|
||||
(make-pdfs checks)
|
||||
|
||||
{: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"}})))
|
||||
wrap-secure))
|
||||
|
||||
Reference in New Issue
Block a user