From fd2164e8baccf0d4277eb076c9dd3d5373d12322 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 12 May 2018 15:57:42 -0700 Subject: [PATCH] checks are now stored --- src/clj/auto_ap/db/checks.clj | 10 ++ src/clj/auto_ap/db/invoices_checks.clj | 32 +++++++ src/clj/auto_ap/graphql.clj | 28 ++++++ src/clj/auto_ap/routes/checks.clj | 91 +++++++++++-------- .../views/components/invoice_table.cljs | 22 +++-- 5 files changed, 134 insertions(+), 49 deletions(-) create mode 100644 src/clj/auto_ap/db/invoices_checks.clj diff --git a/src/clj/auto_ap/db/checks.clj b/src/clj/auto_ap/db/checks.clj index c3fd0766..b341a6de 100644 --- a/src/clj/auto_ap/db/checks.clj +++ b/src/clj/auto_ap/db/checks.clj @@ -31,6 +31,16 @@ (dissoc :pdf-data) (dissoc :invoices))) +(defn insert! [row] + (-> (j/insert! (get-conn) + :checks + (-> row + fields->data + clj->db)) + first + db->clj + data->fields)) + (defn insert-multi! [rows] (->> (j/insert-multi! (get-conn) :checks diff --git a/src/clj/auto_ap/db/invoices_checks.clj b/src/clj/auto_ap/db/invoices_checks.clj new file mode 100644 index 00000000..18b36e26 --- /dev/null +++ b/src/clj/auto_ap/db/invoices_checks.clj @@ -0,0 +1,32 @@ +(ns auto-ap.db.invoices-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 :invoices-checks)) + +(defn query [q] + (map data->fields (u/query q))) + +(defn get-all [] + (query base-query)) + +(defn get-for-invoice-id [id] + (query (-> base-query + (helpers/merge-where [:= :invoice-id id])))) + +(defn get-by-id [id] + (first (query (-> base-query + (helpers/merge-where [:= :id id]))))) + +(defn insert-multi! [rows] + (->> (j/insert-multi! (get-conn) + :invoices_checks + (->> rows + (map clj->db))) + (map db->clj) + )) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 18546cf4..cdfc7ea7 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -8,7 +8,9 @@ [auto-ap.db.invoices :as invoices] [auto-ap.db.vendors :as vendors] [auto-ap.db.companies :as companies] + [auto-ap.db.checks :as checks] [auto-ap.db.reminders :as reminders] + [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.utils :as utils] [clojure.walk :as walk] [clojure.string :as str]) @@ -37,18 +39,34 @@ :vendor {:type :vendor :resolve :get-vendor-for-invoice}} } + + :check {:fields {:id {:type 'Int} + :amount {:type 'String} + :s3_url {:type 'String} + :check_number {:type 'Int}}} + + :invoice_check + {:fields {:id {:type 'Int} + :amount {:type 'String} + :check_id {:type 'Int} + :check {:type :check + :resolve :get-check-by-id}}} + :invoice {:fields {:id {:type 'Int} :total {:type 'String} :invoice_number {:type 'String} :date {:type 'String} :company_id {:type 'Int} + :checks {:type '(list :invoice_check) + :resolve :get-invoices-checks} :vendor {:type :vendor :resolve :get-vendor-for-invoice} :company {:type :company :resolve :get-company-for-invoice}}} + :invoice_page {:fields {:invoices {:type '(list :invoice)} :count {:type 'Int} :total {:type 'Int} @@ -162,6 +180,14 @@ (vendor-cache (:vendor_id value)) (vendors/get-by-id (:vendor_id value))))) +(defn get-check-by-id [context args value] + (->graphql + (checks/get-by-id (:check_id value)))) + +(defn get-invoices-checks [context args value] + (->graphql + (invoices-checks/get-for-invoice-id (:id value)))) + (defn get-company-for-invoice [context args value] (->graphql (if-let [company-cache (:company-cache context)] @@ -182,6 +208,8 @@ :get-reminder-page get-reminder-page :get-vendor-for-invoice get-vendor-for-invoice :get-company-for-invoice get-company-for-invoice + :get-invoices-checks get-invoices-checks + :get-check-by-id get-check-by-id :get-company get-company :get-vendor get-vendor}) schema/compile)) diff --git a/src/clj/auto_ap/routes/checks.clj b/src/clj/auto_ap/routes/checks.clj index a8c1a495..b59b19dc 100644 --- a/src/clj/auto_ap/routes/checks.clj +++ b/src/clj/auto_ap/routes/checks.clj @@ -3,6 +3,7 @@ [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] [auto-ap.db.checks :as checks] + [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.utils :refer [query]] [auto-ap.parse :as parse] [amazonica.aws.s3 :as s3] @@ -24,7 +25,8 @@ [{: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) "--")] + _ (println amount (class amount)) + amount (str "--" (.format df amount) "--")] [:table {:num-cols 12 :border false :leading 11} [(let [{:keys [name address1 city state zip bank]} company] @@ -149,6 +151,39 @@ :content-type "application/pdf"}) (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/merged-checks/" uuid ".pdf"))) +(defn check-for-invoices [invoices vendor-id vendors companies] + (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") + :check-number 1234 + :amount (reduce + 0 (map :total invoices)) + :memo memo + :vendor-id (:id vendor) + :company-id (:id company) + :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)})) + (defroutes routes (wrap-routes @@ -162,48 +197,24 @@ (map (fn [v] [(:id v) v]) (vendors/get-all))) invoices-grouped-by-vendor (group-by :vendor-id invoices) - 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) ] + checks (-> (for [[vendor-id invoices] invoices-grouped-by-vendor] + [invoices (checks/insert! (check-for-invoices invoices vendor-id vendors companies))]) + doall) + invoice-checks (invoices-checks/insert-multi! + (mapcat + (fn [[invoices check]] + (map + (fn [i] + {:invoice-id (:id i) + :check-id (:id check) + :amount (:total i)}) + invoices)) + checks)) ] - (make-pdfs checks) + (make-pdfs (map second checks)) {:status 200 - :body (pr-str {:url (merge-pdfs (map :s3-key checks)) + :body (pr-str {:url (merge-pdfs (map (comp :s3-key second) checks)) :checks checks :invoice-checks invoice-checks}) :headers {"Content-Type" "application/edn"}}))) diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index 06eacd57..ac10b137 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -17,7 +17,7 @@ {:venia/queries [[:invoice_page (assoc params :company-id (:id @(re-frame/subscribe [::subs/company]))) - [[:invoices [:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]] + [[:invoices [:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]] [:checks [:amount [:check [:amount :s3_url :check_number ]]]]]] :total :start :end]]]}) @@ -44,41 +44,42 @@ (when check-boxes [:th]) [sorted-column {:on-sort opc - :style {:width "25%" :cursor "pointer"} + :style {:width "23%" :cursor "pointer"} :sort-key "vendor" :sort-by sort-by :asc asc} "Vendor"] [sorted-column {:on-sort opc - :style {:width "25%" :cursor "pointer"} + :style {:width "23%" :cursor "pointer"} :sort-key "company" :sort-by sort-by :asc asc} "Company"] [sorted-column {:on-sort opc - :style {:width "18%" :cursor "pointer"} + :style {:width "16%" :cursor "pointer"} :sort-key "invoice-number" :sort-by sort-by :asc asc} "Invoice #"] [sorted-column {:on-sort opc - :style {:width "16%" :cursor "pointer"} + :style {:width "14%" :cursor "pointer"} :sort-key "date" :sort-by sort-by :asc asc} "Date"] [sorted-column {:on-sort opc - :style {:width "16%" :cursor "pointer"} + :style {:width "14%" :cursor "pointer"} :sort-key "total" :sort-by sort-by :asc asc} - "Amount"]]] + "Amount"] + [:th {:style {:width "10%"}} "" ]]] [:tbody (if (:loading @status) [:tr [:td {:col-span 5} [:i.fa.fa-spin.fa-spinner]]] - (for [{:keys [company invoice-number date total id vendor] :as i} (:invoices @invoice-page)] + (for [{:keys [company checks invoice-number date total id vendor] :as i} (:invoices @invoice-page)] ^{:key id} [:tr (when check-boxes @@ -93,4 +94,7 @@ [:td invoice-number] [:td (date->str date) ] - [:td (gstring/format "$%.2f" total )]]))]]])))) + [:td (gstring/format "$%.2f" total )] + [:td (for [check checks] + ^{:key (:id check)} + [:a.tag {:href (:s3-url (:check check)) :target "_new"} (str "check " (:check-number (:check check)))])]]))]]]))))