From 4069f731e98bb04ae3cffdc68769cbf8cc945984 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 27 Jul 2018 16:39:44 -0700 Subject: [PATCH] you can now create cash payments. --- .../1531519712-UP-add-real-cbc-banks.sql | 2 +- .../1532704782-UP-fix-iguanas-locations.sql | 2 +- .../1532733305-DOWN-add-check-type.sql | 2 + .../1532733305-UP-add-check-type.sql | 2 + src/clj/auto_ap/db/checks.clj | 3 +- src/clj/auto_ap/graphql.clj | 2 + src/clj/auto_ap/routes/checks.clj | 52 ++++++++++++++----- src/cljs/auto_ap/events.cljs | 4 +- src/cljs/auto_ap/views/pages/checks.cljs | 9 ++-- .../auto_ap/views/pages/unpaid_invoices.cljs | 29 +++++++---- 10 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 migrator/migrations/1532733305-DOWN-add-check-type.sql create mode 100644 migrator/migrations/1532733305-UP-add-check-type.sql diff --git a/migrator/migrations/1531519712-UP-add-real-cbc-banks.sql b/migrator/migrations/1531519712-UP-add-real-cbc-banks.sql index b9d79689..0e6eabc6 100644 --- a/migrator/migrations/1531519712-UP-add-real-cbc-banks.sql +++ b/migrator/migrations/1531519712-UP-add-real-cbc-banks.sql @@ -1,6 +1,6 @@ -- 1531519712 UP add-real-cbc-banks -update companies set data ='{:locations ["CB"] :bank-accounts [{:number "03103062" :id 1 :check-number 10000 :bank-name "United Security Bank" :bank-code "90-4149/1211" :routing "121141495" :name "A1123 USB Operating 3062" :yodlee-account-id 0} {:number "03103070" :id 2 :check-number 10000 :bank-name "United Security Bank" :bank-code "90-4149/1211" :routing "121141495" :name "A1125 USB Operating 3070" :yodlee-account-id 0} {:number "930545652" :id 3 :check-number 10000 :bank-name "Chase Bank" :bank-code "90-3913/1222" :routing "322271627" :name "A1126 Rent Impound Chase Acct" :yodlee-account-id 0} {:number "1812" :id 4 :check-number 10000 :bank-name "Chase Credit Card" :bank-code "" :routing "" :name "A1127 Chase Card 1812" :yodlee-account-id 0}]}' where name = 'Campbell Brewing Co'; +update companies set data ='{:locations ["CB"] :bank-accounts [{:id 0 :type "cash" :name "Cash"} {:number "03103062" :id 1 :check-number 10000 :bank-name "United Security Bank" :bank-code "90-4149/1211" :routing "121141495" :name "A1123 USB Operating 3062" :yodlee-account-id 0} {:number "03103070" :id 2 :check-number 10000 :bank-name "United Security Bank" :bank-code "90-4149/1211" :routing "121141495" :name "A1125 USB Operating 3070" :yodlee-account-id 0} {:number "930545652" :id 3 :check-number 10000 :bank-name "Chase Bank" :bank-code "90-3913/1222" :routing "322271627" :name "A1126 Rent Impound Chase Acct" :yodlee-account-id 0} {:number "1812" :id 4 :check-number 10000 :bank-name "Chase Credit Card" :bank-code "" :routing "" :name "A1127 Chase Card 1812" :yodlee-account-id 0}]}' where name = 'Campbell Brewing Co'; delete from transactions; diff --git a/migrator/migrations/1532704782-UP-fix-iguanas-locations.sql b/migrator/migrations/1532704782-UP-fix-iguanas-locations.sql index 02d6b19c..0048b373 100644 --- a/migrator/migrations/1532704782-UP-fix-iguanas-locations.sql +++ b/migrator/migrations/1532704782-UP-fix-iguanas-locations.sql @@ -1,2 +1,2 @@ -- 1532704782 UP fix-iguanas-locations -update companies set data = '{:locations ["DT" "EG" "SC" "SG" "CB" "BH"] :bank-accounts [{:number "000158443280" :id 1 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1120 BofA Exp and Main - 3280" :yodlee-account-id 16279663} {:number "" :id 2 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1122 BofA Squirrel - 5255" :yodlee-account-id 16279666} {:number "000158743279" :id 3 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1121 BofA Tax & SLO - 3279" :yodlee-account-id 16279664} {:number "" :id 4 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1123 BofA Paychecks - 5250" :yodlee-account-id 16279665} {:number "" :id 5 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1125 BofA EG Exp - 5598" :yodlee-account-id 16279667} {:number "" :id 6 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1126 BofA SC Exp - 5318" :yodlee-account-id 16279668} {:number "" :id 7 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1127 BofA SG Exp - 8407" :yodlee-account-id 16279669} {:number "" :id 8 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1124 BofA DT Exp - 8279" :yodlee-account-id 16279670} {:number "" :id 9 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1128 BofA CB Exp - 2495" :yodlee-account-id 16279671} {:number "" :id 10 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1129 BofA BH Exp - 1665" :yodlee-account-id 16279672}]}' where id = 39; +update companies set data = '{:locations ["DT" "EG" "SC" "SG" "CB" "BH"] :bank-accounts [{:id 0 :type "cash" :name "Cash"} {:number "000158443280" :id 1 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1120 BofA Exp and Main - 3280" :yodlee-account-id 16279663} {:number "" :id 2 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1122 BofA Squirrel - 5255" :yodlee-account-id 16279666} {:number "000158743279" :id 3 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1121 BofA Tax & SLO - 3279" :yodlee-account-id 16279664} {:number "" :id 4 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1123 BofA Paychecks - 5250" :yodlee-account-id 16279665} {:number "" :id 5 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1125 BofA EG Exp - 5598" :yodlee-account-id 16279667} {:number "" :id 6 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1126 BofA SC Exp - 5318" :yodlee-account-id 16279668} {:number "" :id 7 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1127 BofA SG Exp - 8407" :yodlee-account-id 16279669} {:number "" :id 8 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1124 BofA DT Exp - 8279" :yodlee-account-id 16279670} {:number "" :id 9 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1128 BofA CB Exp - 2495" :yodlee-account-id 16279671} {:number "" :id 10 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1129 BofA BH Exp - 1665" :yodlee-account-id 16279672}]}' where id = 39; diff --git a/migrator/migrations/1532733305-DOWN-add-check-type.sql b/migrator/migrations/1532733305-DOWN-add-check-type.sql new file mode 100644 index 00000000..ee36c46d --- /dev/null +++ b/migrator/migrations/1532733305-DOWN-add-check-type.sql @@ -0,0 +1,2 @@ +-- 1532733305 DOWN add-check-type +alter table checks drop column type; diff --git a/migrator/migrations/1532733305-UP-add-check-type.sql b/migrator/migrations/1532733305-UP-add-check-type.sql new file mode 100644 index 00000000..ff6db511 --- /dev/null +++ b/migrator/migrations/1532733305-UP-add-check-type.sql @@ -0,0 +1,2 @@ +-- 1532733305 UP add-check-type +alter table checks add column type varchar(255); diff --git a/src/clj/auto_ap/db/checks.clj b/src/clj/auto_ap/db/checks.clj index 0855573e..a2e612c2 100644 --- a/src/clj/auto_ap/db/checks.clj +++ b/src/clj/auto_ap/db/checks.clj @@ -50,10 +50,9 @@ (map db->clj) (map data->fields))) -(def all-keys #{:company-id :vendor-id :id :status :date :s3-uuid :s3-key :s3-url :check-number :memo :amount :paid-to :bank-account-id}) +(def all-keys #{:company-id :vendor-id :id :status :date :s3-uuid :s3-key :s3-url :check-number :memo :amount :paid-to :bank-account-id :type}) (defn add-sort-by [q sort-by asc] - (println sort-by) (let [sort-by-key (keyword sort-by)] (cond (nil? sort-by) (helpers/merge-order-by q [:date]) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 404ed84e..5004fa4b 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -41,6 +41,7 @@ :bank_account {:fields {:id {:type 'Int} + :type {:type 'String} :number {:type 'String} :check_number {:type 'Int} :name {:type 'String} @@ -84,6 +85,7 @@ :check {:fields {:id {:type 'Int} + :type {:type 'String} :amount {:type 'String} :vendor {:type :vendor :resolve :get-vendor-for-check} diff --git a/src/clj/auto_ap/routes/checks.clj b/src/clj/auto_ap/routes/checks.clj index cc7ff885..7af4ac0f 100644 --- a/src/clj/auto_ap/routes/checks.clj +++ b/src/clj/auto_ap/routes/checks.clj @@ -175,10 +175,15 @@ :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 company bank-account-id index invoice-amounts] + + +(defmulti check-for-invoices (fn [invoices vendor-id vendors company bank-account index invoice-amounts] + (:type bank-account))) + +(defmethod check-for-invoices "check" [invoices vendor-id vendors company bank-account index invoice-amounts] (let [uuid (str (UUID/randomUUID)) vendor (vendors vendor-id) - bank-account (first (filter #(= (:id %) bank-account-id) (:bank-accounts company))) + memo (str "Invoice #'s: " (str/join ", " @@ -189,7 +194,8 @@ :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 (+ index (:check-number bank-account)) - :bank-account-id bank-account-id + :type "check" + :bank-account-id (:id bank-account) :amount (reduce + 0 (map (comp invoice-amounts :id) invoices)) :memo memo :vendor-id (:id vendor) @@ -208,8 +214,20 @@ :acct (:bank-code bank-account) :routing (:routing bank-account) :acct-number (:number bank-account)}}} + :invoices (map :id invoices)})) +(defmethod check-for-invoices "cash" [invoices vendor-id vendors company bank-account index invoice-amounts] + (let [vendor (vendors vendor-id)] + {:type "cash" + :bank-account-id (:id bank-account) + :amount (reduce + 0 (map (comp invoice-amounts :id) invoices)) + :status "cleared" + :memo "Cash" + :vendor-id (:id vendor) + :company-id (:id company) + :date (time/now) + :invoices (map :id invoices)})) (defn print-checks [invoice-payments company-id bank-account-id] (let [invoices (invoices/get-multi (map :invoice-id invoice-payments)) @@ -217,8 +235,9 @@ vendors (by :id (vendors/get-all)) invoice-amounts (by :invoice-id :amount invoice-payments) invoices-grouped-by-vendor (group-by :vendor-id invoices) + bank-account (first (filter #(= (:id %) bank-account-id) (:bank-accounts company))) checks (-> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))] - [invoices (checks/insert! (check-for-invoices invoices vendor-id vendors company bank-account-id index invoice-amounts))]) + [invoices (checks/insert! (check-for-invoices invoices vendor-id vendors company bank-account index invoice-amounts))]) doall) invoice-checks (invoices-checks/insert-multi! (mapcat @@ -230,20 +249,25 @@ :amount (invoice-amounts (:id i))}) invoices)) checks)) - updated-company (update company :bank-accounts - (fn [bas] - (map (fn [ba] - (if (= bank-account-id (:id ba)) - (update ba :check-number + (count checks)) - ba)) - bas)))] + updated-company (if (= (:type bank-account) "cash" ) + company + (update company :bank-accounts + (fn [bas] + (map (fn [ba] + (if (= bank-account-id (:id ba)) + (update ba :check-number + (count checks)) + ba)) + bas))))] - (make-pdfs (map second checks)) - (companies/upsert company-id updated-company) + (when (not= (:type bank-account) "cash") + (make-pdfs (map second checks)) + (companies/upsert company-id updated-company)) (doseq [{:keys [invoice-id amount]} invoice-payments] (invoices/apply-payment invoice-id amount)) {:invoices (invoices/get-multi (map :id (mapcat first checks))) - :pdf-url (merge-pdfs (map (comp :s3-key second) checks))})) + :pdf-url (if (= (:type bank-account) "cash") + nil + (merge-pdfs (map (comp :s3-key second) checks)))})) (defroutes routes diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index d12d4812..12f79b78 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -37,7 +37,7 @@ :graphql {:token token :query-obj {:venia/queries [[:company - [:id :name :locations [:bank-accounts [:id :number :check-number :name] ]]] + [:id :name :locations [:bank-accounts [:id :number :check-number :name :type] ]]] [:vendor [:id :name :default-expense-account :primary-contact :primary-email :primary-phone :secondary-contact :secondary-email :secondary-phone :print-as :invoice-reminder-schedule :code]]]} @@ -53,7 +53,7 @@ (fn [{:keys [db]} [_ token user]] {:graphql {:token token :query-obj {:venia/queries [[:company - [:id :name [:bank-accounts [:id :number :check-number :name]]]] + [:id :name [:bank-accounts [:id :number :check-number :name :type]]]] [:vendor [:id :name :default-expense-account]]]} diff --git a/src/cljs/auto_ap/views/pages/checks.cljs b/src/cljs/auto_ap/views/pages/checks.cljs index 75214e30..0067f71e 100644 --- a/src/cljs/auto_ap/views/pages/checks.cljs +++ b/src/cljs/auto_ap/views/pages/checks.cljs @@ -36,7 +36,7 @@ :graphql {:token (-> cofx :db :user) :query-obj {:venia/queries [[:check_page (assoc params :company-id (:id @(re-frame/subscribe [::subs/company]))) - [[:checks [:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]] + [[:checks [:id :status :amount :type :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]] :total :start :end]]]} @@ -107,6 +107,7 @@ :sort-by sort-by :asc asc} "Company"]) + [sorted-column {:on-sort opc :style {:width percentage-size :cursor "pointer"} :sort-key "vendor" @@ -146,14 +147,16 @@ [:tr [:td {:col-span 5} [:i.fa.fa-spin.fa-spinner]]] - (for [{:keys [company s3-url checks check-number date amount id vendor status] :as i} (:checks @check-page)] + (for [{:keys [company s3-url checks type check-number date amount id vendor status] :as i} (:checks @check-page)] ^{:key id} [:tr {:class (:class i)} (when-not selected-company [:td (:name company)]) [:td (:name vendor)] - [:td check-number] + [:td (if (= "cash" type) + "Cash" + check-number)] [:td (date->str date) ] [:td (gstring/format "$%.2f" amount )] [:td status] diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 096210fc..f76a5d2f 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -230,7 +230,8 @@ (fn [{:keys [db]} _] {:dispatch [::events/modal-status ::new-invoice {:visible? true}] :db (assoc-in db [::new-invoice] {:company-id (:id @(re-frame/subscribe [::subs/company])) - :date (date->str (c/now) standard)})})) + :date (date->str (c/now) standard) + :location (first (:locations @(re-frame/subscribe [::subs/company])))})})) (re-frame/reg-event-fx ::edit-invoice @@ -778,8 +779,11 @@ [:div.dropdown-menu {:role "menu"} [:div.dropdown-content (list - (for [{:keys [id number name]} (:bank-accounts current-company)] - ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} "Print checks from " name]) + (for [{:keys [id number name type]} (:bank-accounts current-company)] + (do (println type) + (if (= "cash" type) + ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} "With cash"] + ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} "Print checks from " name]))) ^{:key "advanced-divider"} [:hr.dropdown-divider] (when (= 1 (count checked)) @@ -808,11 +812,18 @@ [handwrite-checks-modal] [change-expense-accounts-modal] (when check-results-shown? - [modal - {:title "Your checks are ready!" - :hide-event [::close-check-results]} - [:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."] - [:div [:em "Remember to turn off all scaling and margins."]] - ]) + (if pdf-url + [modal + {:title "Your checks are ready!" + :hide-event [::close-check-results]} + [:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."] + [:div [:em "Remember to turn off all scaling and margins."]] + ] + [modal + {:title "Payment created!" + :hide-event [::close-check-results]} + [:div [:em "Your payment was created."]] + ])) + ])) {:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))