Files
integreat/src/cljs/auto_ap/views/pages/checks.cljs
2018-08-16 19:36:50 -07:00

190 lines
7.6 KiB
Clojure

(ns auto-ap.views.pages.checks
(:require [re-frame.core :as re-frame]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
[reagent.core :as reagent]
[goog.string :as gstring]
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.events :as events]
[auto-ap.views.utils :refer [dispatch-event date->str ]]
[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]))
(re-frame/reg-sub
::payment-page
(fn [db]
(-> db ::payment-page)))
(re-frame/reg-sub
::params
(fn [db]
(-> db (::params {}))))
(re-frame/reg-event-fx
::params-change
(fn [cofx [_ params]]
{:db (-> (:db cofx)
(assoc-in [:status :loading] true)
(assoc-in [::params] params))
:graphql {:token (-> cofx :db :user)
:query-obj {:venia/queries [[:payment_page
(assoc params :client-id (:id @(re-frame/subscribe [::subs/company])))
[[:payments [:id :status :amount :type :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]
:total
:start
:end]]]}
:on-success [::received]}}))
(re-frame/reg-event-fx
::void-check
(fn [{:keys [db]} [_ payment]]
{:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "VoidPayment"}
:venia/queries [{:query/data [:void-payment
{:payment-id (:id payment)}
[:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]}]}
:on-success [::payment-voided]}}))
(re-frame/reg-event-db
::payment-voided
(fn [db [_ {:keys [void-payment]}]]
(-> db
(update-in [::payment-page :payments] (fn [payments]
(mapv (fn [c]
(if (= (:id c) (:id void-payment))
(assoc void-payment :class "live-removed")
c))
payments))))))
(re-frame/reg-event-db
::received
(fn [db [_ data]]
(-> db
(assoc ::payment-page (first (:payment-page data)))
(assoc-in [:status :loading] false))))
(re-frame/reg-event-fx
::invalidated
(fn [cofx [_ params]]
{:dispatch [::params-change @(re-frame/subscribe [::params])]}))
(defn check-table [{:keys [id payment-page status on-params-change vendors params check-boxes checked on-check-changed expense-event]}]
(let [state (reagent/atom (or @params {}))
selected-company @(re-frame/subscribe [::subs/company])
opc (fn [p]
(swap! state merge p)
(on-params-change p))]
(fn [{:keys [id payment-page status on-params-change vendors checked]}]
(let [{:keys [sort-by asc]} @state
{:keys [payments start end count total]} @payment-page
selected-company @(re-frame/subscribe [::subs/company])
percentage-size (if selected-company "50%" "33%")]
[:div
[paginator {:start start :end end :count count :total total
:on-change (fn [p ]
(on-params-change (swap! state merge p)))}]
"Showing " (inc start) "-" end "/" total
[:table.table.is-fullwidth
[:thead
[:tr
(when-not selected-company
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "client"
:sort-by sort-by
:asc asc}
"Client"])
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "vendor"
:sort-by sort-by
:asc asc}
"Vendor"]
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "check-number"
:sort-by sort-by
:asc asc}
"Check #"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "date"
:sort-by sort-by
:asc asc}
"Date"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "amount"
:sort-by sort-by
:asc asc}
"Amount"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "status"
:sort-by sort-by
:asc asc}
"Status"]
[:th {:style {:width "10em"}} "" ]]]
[:tbody
(if (:loading @status)
[:tr
[:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]]
(for [{:keys [client s3-url payments type check-number date amount id vendor status] :as i} (:payments @payment-page)]
^{:key id}
[:tr {:class (:class i)}
(when-not selected-company
[:td (:name client)])
[:td (:name vendor)]
[:td (cond
(= "cash" type) "Cash"
(= "debit" type) "Debit"
:else check-number)]
[:td (date->str date) ]
[:td (gstring/format "$%.2f" amount )]
[:td status]
[:td
(when (or (= ":pending" status)
(#{":cash" ":debit"} type))
[:button.button.is-warning.is-outlined {:on-click (dispatch-event [::void-check i])} [:span [:span.icon [:i.fa.fa-minus-circle]]]])
(if s3-url
[:a.tag {:href s3-url :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " check-number " (" (gstring/format "$%.2f" amount ) ")")]
[:span.tag [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " check-number " (" (gstring/format "$%.2f" amount ) ")")])
]
]))]]]))))
(def checks-page
(with-meta
(fn []
(let [current-company @(re-frame/subscribe [::subs/company])]
[:div
[:h1.title "Checks"]
[check-table {:id :payments
:params (re-frame/subscribe [::params])
:payment-page (re-frame/subscribe [::payment-page])
:status (re-frame/subscribe [::subs/status])
:on-params-change (fn [params]
(re-frame/dispatch [::params-change params]))}]]))
{:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))