refactoring to support grouping simply.

This commit is contained in:
Bryce Covert
2018-05-14 13:33:04 -07:00
parent 431645ad42
commit d9994b988c
7 changed files with 59 additions and 37 deletions

View File

@@ -1,2 +1,2 @@
-- 1526315524 UP add-bank-accounts -- 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"}]}';

View File

@@ -32,7 +32,10 @@
:bank_account :bank_account
{:fields {:id {:type 'Int} {:fields {:id {:type 'Int}
:number {:type 'String} :number {:type 'String}
:check_number {:type 'Int}}} :check_number {:type 'Int}
:name {:type 'String}
:bank_code {:type 'String}
:bank_name {:type 'String}}}
:vendor :vendor
{:fields {:id {:type 'Int} {:fields {:id {:type 'Int}
:name {:type 'String} :name {:type 'String}
@@ -88,6 +91,8 @@
:end {:type 'Int}}} :end {:type 'Int}}}
:check_result {:fields {:invoices {:type '(list :invoice)} :check_result {:fields {:invoices {:type '(list :invoice)}
:pdf_url {:type 'String}}} :pdf_url {:type 'String}}}
} }
@@ -112,9 +117,15 @@
:vendor {:type '(list :vendor) :vendor {:type '(list :vendor)
:resolve :get-vendor}} :resolve :get-vendor}}
:input-objects
{
:invoice_payment {:fields {:invoice_id {:type 'Int}
:amount {:type 'Float}}}
}
:mutations :mutations
{:print_checks {:type :check_result {:print_checks {:type :check_result
:args {:invoice_ids {:type '(list Int)} :args {:invoice_payments {:type '(list :invoice_payment)}
:bank_account_id {:type 'Int} :bank_account_id {:type 'Int}
:company_id {:type 'Int}} :company_id {:type 'Int}}
:resolve :mutation/print-checks}}}) :resolve :mutation/print-checks}}})
@@ -225,7 +236,9 @@
(defn print-checks [context args value] (defn print-checks [context args value]
(->graphql (->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) (:company_id args)
(:bank_account_id args)))) (:bank_account_id args))))

View File

@@ -2,6 +2,7 @@
(:require [auto-ap.db.companies :as companies] (:require [auto-ap.db.companies :as companies]
[auto-ap.db.vendors :as vendors] [auto-ap.db.vendors :as vendors]
[auto-ap.db.invoices :as invoices] [auto-ap.db.invoices :as invoices]
[auto-ap.utils :refer [by]]
[auto-ap.db.checks :as checks] [auto-ap.db.checks :as checks]
[auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.invoices-checks :as invoices-checks]
[auto-ap.db.utils :refer [query]] [auto-ap.db.utils :refer [query]]
@@ -186,12 +187,11 @@
:invoices (map :id invoices)})) :invoices (map :id invoices)}))
(defn print-checks [invoice-ids company-id bank-account-id] (defn print-checks [invoice-payments company-id bank-account-id]
(let [invoices (invoices/get-multi invoice-ids) (let [invoices (invoices/get-multi (map :invoice-id invoice-payments))
company (companies/get-by-id company-id) company (companies/get-by-id company-id)
vendors (into {} vendors (by :id (vendors/get-all))
(map (fn [v] [(:id v) v]) invoice-amounts (by :invoice-id :amount invoice-payments)
(vendors/get-all)))
invoices-grouped-by-vendor (group-by :vendor-id invoices) invoices-grouped-by-vendor (group-by :vendor-id invoices)
checks (-> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))] 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))]) [invoices (checks/insert! (check-for-invoices invoices vendor-id vendors company bank-account-id index))])
@@ -203,7 +203,7 @@
(fn [i] (fn [i]
{:invoice-id (:id i) {:invoice-id (:id i)
:check-id (:id check) :check-id (:id check)
:amount (:total i)}) :amount (invoice-amounts (:id i))})
invoices)) invoices))
checks)) checks))
updated-company (update company :bank-accounts updated-company (update company :bank-accounts
@@ -212,8 +212,7 @@
(if (= bank-account-id (:id ba)) (if (= bank-account-id (:id ba))
(update ba :check-number + (count checks)) (update ba :check-number + (count checks))
ba)) ba))
bas))) bas)))]
]
(make-pdfs (map second checks)) (make-pdfs (map second checks))
(companies/upsert company-id updated-company) (companies/upsert company-id updated-company)

View File

@@ -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)))

View File

@@ -21,7 +21,7 @@
:user token) :user token)
:graphql {:token token :graphql {:token token
:query-obj {:venia/queries [[:company :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]}})))) :on-success [::received-companies]}}))))

View File

@@ -13,7 +13,6 @@
;; TODO partial payments ;; TODO partial payments
;; TODO invoice status = paid when complete ;; TODO invoice status = paid when complete
;; TODO performance ;; TODO performance
;; TODO psql transactions
;; TODO refactor graphql ;; TODO refactor graphql
@@ -102,4 +101,4 @@
[:td (gstring/format "$%.2f" total )] [:td (gstring/format "$%.2f" total )]
[:td (for [check checks] [:td (for [check checks]
^{:key (:id check)} ^{: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) ) ")")])]]))]]]))))

View File

@@ -4,6 +4,7 @@
[auto-ap.entities.vendors :as vendor] [auto-ap.entities.vendors :as vendor]
[auto-ap.events :as events] [auto-ap.events :as events]
[auto-ap.views.utils :refer [dispatch-event]] [auto-ap.views.utils :refer [dispatch-event]]
[auto-ap.utils :refer [by]]
[auto-ap.views.pages.check :as check] [auto-ap.views.pages.check :as check]
[auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
@@ -54,32 +55,32 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::print-checks ::print-checks
(fn [{:keys [db]} [_ bank-account-id]] (fn [{:keys [db]} [_ bank-account-id]]
{ (let [invoice-amounts (by :id :total (get-in db [::invoice-page :invoices]))]
:db (assoc-in db [::invoice-page :print-checks-shown?] false ) {
:graphql :db (assoc-in db [::invoice-page :print-checks-shown?] false )
{:token (-> db :user) :graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"} :query-obj {:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"}
:venia/queries [[:print-checks
{:invoice_ids (vec (get-in db [::invoice-page :checked])) :venia/queries [[:print-checks
:bank_account_id bank-account-id {:invoice_payments (map (fn [id]
:company_id (:company db)} {:invoice-id id
[[:invoices [:id [:checks [:amount [:check [:amount :s3_url :check_number ]]]]]] :amount (invoice-amounts id)})
:pdf_url]]]} (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 (re-frame/reg-event-fx
::checks-created ::checks-created
(fn [{:keys [db]} [_ data]] (fn [{:keys [db]} [_ data]]
(let [{{:keys [pdf-url invoices]} :print-checks} data (let [{{:keys [pdf-url invoices]} :print-checks} data
invoices-by-id (reduce invoices-by-id (by :id invoices) ]
(fn [x a]
(assoc x (:id a) a))
{}
invoices)]
{:new-window pdf-url {:new-window pdf-url
:db (-> db :db (-> db
(update-in [::invoice-page :invoices] (update-in [::invoice-page :invoices]
@@ -116,8 +117,8 @@
[:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]] [:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]]
[:div.dropdown-menu {:role "menu"} [:div.dropdown-menu {:role "menu"}
[:div.dropdown-content [:div.dropdown-content
(for [{:keys [id number]} (:bank-accounts current-company)] (for [{:keys [id number name]} (:bank-accounts current-company)]
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} number])]]])] ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id])} name])]]])]
[invoice-table {:id :unpaid [invoice-table {:id :unpaid
:params (re-frame/subscribe [::params]) :params (re-frame/subscribe [::params])