(ns auto-ap.views.pages.payments.table (:require [auto-ap.subs :as subs] [auto-ap.routes :as routes] [cemerick.url :as url] [bidi.bidi :as bidi] [auto-ap.views.components.paginator :refer [paginator]] [auto-ap.views.components.sorter :refer [sorted-column]] [auto-ap.views.components.sort-by-list :refer [sort-by-list]] [auto-ap.views.utils :refer [date->str dispatch-event nf]] [auto-ap.views.components.dropdown :refer [drop-down drop-down-contents]] [auto-ap.events :as events] [goog.string :as gstring] [re-frame.core :as re-frame] [auto-ap.views.components.grid :as grid] [auto-ap.views.components.buttons :as buttons] [auto-ap.status :as status] [auto-ap.views.pages.data-page :as data-page])) (re-frame/reg-event-fx ::void-check (fn [{:keys [db]} [_ payment]] {:graphql {:token (-> db :user) :owns-state {:multi ::void :which (:id payment)} :query-obj {:venia/operation {:operation/type :mutation :operation/name "VoidPayment"} :venia/queries [{:query/data [:void-payment {:payment-id (:id payment)} [:id :status [:bank-account [:name]] :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]] [:invoices [:invoice-id]]]]}]} :on-success [::payment-voided]}})) (re-frame/reg-event-db ::payment-voided (fn [db [_ {:keys [void-payment]}]] db)) (defn row [{check :check selected-client :selected-client states :states }] (let [{:keys [client s3-url bank-account payments type check-number date amount id vendor status invoices] :as check} check] [grid/row {:class (:class check) :id id} (when-not selected-client [grid/cell {} (:name client)]) [grid/cell {} (:name vendor)] [grid/cell {} (:name bank-account)] [grid/cell {} (cond (= :cash type) "Cash" (= :debit type) "Debit" :else (if s3-url [:a {:href s3-url :target "_new"} [:a.button [:span.icon [:i.fa.fa-share-square-o]] [:span (str " " check-number )]]] check-number))] [grid/cell {} (date->str date) ] [grid/cell {:class "has-text-right"} (nf amount )] [grid/cell {} status] [grid/button-cell {} [:div.buttons (when (and (seq invoices) (not= :voided status)) [drop-down {:id [::invoices id] :header [:button.button.badge {:data-badge (str (clojure.core/count invoices)) :aria-haspopup true? :tab-index "0" :on-click (dispatch-event [::events/toggle-menu [::invoices id]]) } "Invoices"]} [:div {:style {:max-width "250px" :text-overflow "ellipsis" :white-space "nowrap" :overflow "hidden"}} (for [invoice invoices] ^{:key (:invoice-number (:invoice invoice))} [:a.dropdown-item {:href (str (bidi/path-for routes/routes :invoices ) "?" (url/map->query {:invoice-number-like (str "\"" (:invoice-number (:invoice invoice)) "\"")})) :target "_new"} (str " " (:invoice-number (:invoice invoice)))])]]) [:span {:style {:margin-left "1em"}}] (when (or (= :pending status) (and (#{":cash" :cash ":debit" :debit} type) (not= :voided status))) [buttons/sl-icon {:event [::void-check check] :icon :icon-bin-2 :class (status/class-for (get states (:id check)))}])]]])) (defn table [{:keys [data-page]}] (let [selected-client @(re-frame/subscribe [::subs/client]) {:keys [data status]} @(re-frame/subscribe [::data-page/page data-page]) states @(re-frame/subscribe [::status/multi ::void])] [grid/grid {:data-page data-page :column-count (if selected-client 7 8)} [grid/controls data] [grid/table {:fullwidth true} [grid/header {} [grid/row {} (when-not selected-client [grid/sortable-header-cell {:sort-key "client" :sort-name "Client"} "Client"]) [grid/sortable-header-cell {:sort-key "vendor" :sort-name "Vendor"} "Vendor"] [grid/sortable-header-cell {:sort-key "bank-account" :sort-name "Bank Account"} "Bank Account"] [grid/sortable-header-cell {:sort-key "check-number" :sort-name "Check Number"} "Check Number"] [grid/sortable-header-cell {:sort-key "date" :sort-name "Date" :style {:width "8em"}} "Date"] [grid/sortable-header-cell {:sort-key "amount" :sort-name "Amount" :class "has-text-right" :style {:width "8em"}} "Amount"] [grid/sortable-header-cell {:sort-key "status" :sort-name "Status" :style {:width "7em"}} "Status"] [grid/header-cell {:style {:width "12em"}}]]] [grid/body (for [check (:data data)] ^{:key (:id check)} [row {:check check :selected-client selected-client :states states}])]]]))