diff --git a/migrator/migrations/1526315524-UP-add-bank-accounts.sql b/migrator/migrations/1526315524-UP-add-bank-accounts.sql index f428202b..7e302947 100644 --- a/migrator/migrations/1526315524-UP-add-bank-accounts.sql +++ b/migrator/migrations/1526315524-UP-add-bank-accounts.sql @@ -1,2 +1,2 @@ -- 1526315524 UP add-bank-accounts -update companies set data = '{:bank-accounts [{:number "123456789" :id 1 :check-number 6789} {:number "987654321" :id 2 :check-number 1234}]}'; +update companies set data = '{:bank-accounts [{:number "123456789" :id 1 :check-number 6789 :bank-name "Bank of America" :bank-code "90-4149/1211" :routing "12345678" :name "BOA-6789"} {:number "987654321" :id 2 :check-number 1234 :bank-name "Bank of America" :bank-code "90-4149/1211" :routing "123456" :name "BOA-4321"}]}'; diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 4a5c5e1f..a052e6d2 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -32,7 +32,10 @@ :bank_account {:fields {:id {:type 'Int} :number {:type 'String} - :check_number {:type 'Int}}} + :check_number {:type 'Int} + :name {:type 'String} + :bank_code {:type 'String} + :bank_name {:type 'String}}} :vendor {:fields {:id {:type 'Int} :name {:type 'String} @@ -88,6 +91,8 @@ :end {:type 'Int}}} :check_result {:fields {:invoices {:type '(list :invoice)} :pdf_url {:type 'String}}} + + } @@ -112,9 +117,15 @@ :vendor {:type '(list :vendor) :resolve :get-vendor}} + :input-objects + { + :invoice_payment {:fields {:invoice_id {:type 'Int} + :amount {:type 'Float}}} + } + :mutations {:print_checks {:type :check_result - :args {:invoice_ids {:type '(list Int)} + :args {:invoice_payments {:type '(list :invoice_payment)} :bank_account_id {:type 'Int} :company_id {:type 'Int}} :resolve :mutation/print-checks}}}) @@ -225,7 +236,9 @@ (defn print-checks [context args value] (->graphql - (rchecks/print-checks (:invoice_ids args) + (rchecks/print-checks (map (fn [i] {:invoice-id (:invoice_id i) + :amount (:amount i)}) + (:invoice_payments args)) (:company_id args) (:bank_account_id args)))) diff --git a/src/clj/auto_ap/routes/checks.clj b/src/clj/auto_ap/routes/checks.clj index 1c82f0f1..e1463c2b 100644 --- a/src/clj/auto_ap/routes/checks.clj +++ b/src/clj/auto_ap/routes/checks.clj @@ -2,6 +2,7 @@ (:require [auto-ap.db.companies :as companies] [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] + [auto-ap.utils :refer [by]] [auto-ap.db.checks :as checks] [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.utils :refer [query]] @@ -186,12 +187,11 @@ :invoices (map :id invoices)})) -(defn print-checks [invoice-ids company-id bank-account-id] - (let [invoices (invoices/get-multi invoice-ids) +(defn print-checks [invoice-payments company-id bank-account-id] + (let [invoices (invoices/get-multi (map :invoice-id invoice-payments)) company (companies/get-by-id company-id) - vendors (into {} - (map (fn [v] [(:id v) v]) - (vendors/get-all))) + vendors (by :id (vendors/get-all)) + invoice-amounts (by :invoice-id :amount invoice-payments) invoices-grouped-by-vendor (group-by :vendor-id invoices) 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))]) @@ -203,7 +203,7 @@ (fn [i] {:invoice-id (:id i) :check-id (:id check) - :amount (:total i)}) + :amount (invoice-amounts (:id i))}) invoices)) checks)) updated-company (update company :bank-accounts @@ -212,8 +212,7 @@ (if (= bank-account-id (:id ba)) (update ba :check-number + (count checks)) ba)) - bas))) - ] + bas)))] (make-pdfs (map second checks)) (companies/upsert company-id updated-company) diff --git a/src/cljc/auto_ap/utils.cljc b/src/cljc/auto_ap/utils.cljc new file mode 100644 index 00000000..87a3d9a9 --- /dev/null +++ b/src/cljc/auto_ap/utils.cljc @@ -0,0 +1,10 @@ +(ns auto-ap.utils) + +(defn by + ([f xs] + (by f identity xs)) + ([f fv xs] + (reduce + #(assoc %1 (f %2) (fv %2)) + {} + xs))) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 88656219..feda9fe7 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -21,7 +21,7 @@ :user token) :graphql {:token token :query-obj {:venia/queries [[:company - [:id :name [:bank-accounts [:id :number :check-number]]]]]} + [:id :name [:bank-accounts [:id :number :check-number :name]]]]]} :on-success [::received-companies]}})))) diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index 66918ebb..2c3ed813 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -13,7 +13,6 @@ ;; TODO partial payments ;; TODO invoice status = paid when complete ;; TODO performance -;; TODO psql transactions ;; TODO refactor graphql @@ -102,4 +101,4 @@ [:td (gstring/format "$%.2f" total )] [:td (for [check checks] ^{:key (:id check)} - [:a.tag {:href (:s3-url (:check check)) :target "_new"} [:i.fa.fa-money-check] (str " " (:check-number (:check check)) " (" (gstring/format "$%.2f" (:amount check) ) ")")])]]))]]])))) + [:a.tag {:href (:s3-url (:check check)) :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " (:check-number (:check check)) " (" (gstring/format "$%.2f" (:amount check) ) ")")])]]))]]])))) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 6563a5cc..0bc9b24d 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -4,6 +4,7 @@ [auto-ap.entities.vendors :as vendor] [auto-ap.events :as events] [auto-ap.views.utils :refer [dispatch-event]] + [auto-ap.utils :refer [by]] [auto-ap.views.pages.check :as check] [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [auto-ap.subs :as subs] @@ -54,32 +55,32 @@ (re-frame/reg-event-fx ::print-checks (fn [{:keys [db]} [_ bank-account-id]] - { - :db (assoc-in db [::invoice-page :print-checks-shown?] false ) - :graphql - {:token (-> db :user) - - :query-obj {:venia/operation {:operation/type :mutation - :operation/name "PrintChecks"} - - :venia/queries [[:print-checks - {:invoice_ids (vec (get-in db [::invoice-page :checked])) - :bank_account_id bank-account-id - :company_id (:company db)} - [[:invoices [:id [:checks [:amount [:check [:amount :s3_url :check_number ]]]]]] - :pdf_url]]]} + (let [invoice-amounts (by :id :total (get-in db [::invoice-page :invoices]))] + { + :db (assoc-in db [::invoice-page :print-checks-shown?] false ) + :graphql + {:token (-> db :user) + + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "PrintChecks"} + + :venia/queries [[:print-checks + {:invoice_payments (map (fn [id] + {:invoice-id id + :amount (invoice-amounts id)}) + (get-in db [::invoice-page :checked])) + :bank_account_id bank-account-id + :company_id (:company db)} + [[:invoices [:id [:checks [:amount [:check [:amount :s3_url :check_number ]]]]]] + :pdf_url]]]} - :on-success [::checks-created]}})) + :on-success [::checks-created]}}))) (re-frame/reg-event-fx ::checks-created (fn [{:keys [db]} [_ data]] (let [{{:keys [pdf-url invoices]} :print-checks} data - invoices-by-id (reduce - (fn [x a] - (assoc x (:id a) a)) - {} - invoices)] + invoices-by-id (by :id invoices) ] {:new-window pdf-url :db (-> db (update-in [::invoice-page :invoices] @@ -116,8 +117,8 @@ [:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]] [:div.dropdown-menu {:role "menu"} [:div.dropdown-content - (for [{:keys [id number]} (:bank-accounts current-company)] - ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} number])]]])] + (for [{:keys [id number name]} (:bank-accounts current-company)] + ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} name])]]])] [invoice-table {:id :unpaid :params (re-frame/subscribe [::params])