diff --git a/src/cljs/auto_ap/views/pages/transactions/bulk_updates.cljs b/src/cljs/auto_ap/views/pages/transactions/bulk_updates.cljs new file mode 100644 index 00000000..73a85594 --- /dev/null +++ b/src/cljs/auto_ap/views/pages/transactions/bulk_updates.cljs @@ -0,0 +1,135 @@ +(ns auto-ap.views.pages.transactions.bulk-updates + (:require + [auto-ap.forms :as forms] + [auto-ap.status :as status] + [auto-ap.subs :as subs] + [auto-ap.views.components.button-radio :refer [button-radio]] + [auto-ap.views.components.expense-accounts-field + :as expense-accounts-field + :refer [expense-accounts-field]] + [auto-ap.views.components.modal :as modal] + [auto-ap.views.components.typeahead :refer [typeahead-v3]] + [auto-ap.views.pages.transactions.common + :refer [data-params->query-params]] + [auto-ap.views.utils :refer [dispatch-event with-user]] + [clojure.string :as str] + [re-frame.core :as re-frame])) + +(re-frame/reg-sub + ::can-submit + :<- [::forms/form ::form] + (fn [{ {:keys []} :data}] + true)) + +(re-frame/reg-event-fx + ::coded + (fn [_ [_ _]] + {:dispatch [::modal/modal-closed]})) + +(re-frame/reg-event-fx + ::code-selected + [with-user (forms/in-form ::form) ] + (fn [{:keys [user db]} [_ checked]] + (let [checked-params (get checked "header") + specific-transactions (map :id (vals (dissoc checked "header"))) + data (:data db)] + {:graphql + {:token user + :owns-state {:single ::form} + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "BulkCodeTransactions"} + :venia/queries [[:bulk-code-transactions + {:filters (some-> checked-params data-params->query-params) + :ids specific-transactions + :vendor (:id (:vendor data)) + :approval-status (:transaction-approval-status data) + :accounts (map + #(-> % + (update :id (fn [i] (if (some-> i (str/starts-with? "new-")) + nil + i))) + (assoc :percentage (/ (get-in % [:amount-percentage]) 100 )) + (assoc :account-id (get-in % [:account :id])) + (select-keys [:percentage :id :location :account-id])) + (:accounts data))} + [:message] + ]]} + :on-success (fn [result] + [::coded + (:message result) + ])}}))) + +(re-frame/reg-event-db + ::changed + (forms/change-handler ::form + (fn [data field value] + (cond (and (= [:vendor] field) + value) + [[:accounts] (expense-accounts-field/default-account (:accounts data) + @(re-frame/subscribe [::subs/vendor-default-account (:id value) (:client data)]) + (:total data) + [])] + :else + [])))) + +(def code-form (forms/vertical-form {:submit-event [::code-selected] + :change-event [::changed] + :can-submit [::can-submit] + :id ::form})) +(defn form [] + (let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form]) + {:keys [form-inline field]} code-form + ] + + (form-inline {} + [:<> + (field "Vendor" + [typeahead-v3 {:entities-by-id @(re-frame/subscribe [::subs/vendors-by-id]) + :entity-index @(re-frame/subscribe [::subs/searchable-vendors-index]) + :entity->text :name + :type "typeahead-v3" + :auto-focus true + :field [:vendor]}]) + + (field "Approval Status" + [button-radio + {:type "button-radio" + :field [:transaction-approval-status] + :options [[:unapproved "Unapproved"] + [:requires-feedback "Client Review"] + [:approved "Approved"] + [:excluded "Excluded from Ledger"]]}]) + + (with-meta + (field nil + [expense-accounts-field {:type "expense-accounts" + :descriptor "account asssignment" + :percentage-only? true + :client (:client data) + :locations (into ["Shared"] @(re-frame/subscribe [::subs/locations-for-client (:id (:client data))])) + :max 100 + :field [:accounts]}]) + {:key (some-> data :vendor :id str)}) + ]))) + +(re-frame/reg-event-fx + ::code-requested + (fn [{:keys [db]} [_ checked params]] + (let [to-delete (if (get checked "header") + [:b.strong.has-text-danger "all visible transactions"] + (str (count checked) " transactions"))] + {:dispatch [::modal/modal-requested {:title "Confirmation" + :body [:div "Please fill in the details on how to code " to-delete ":" + [form ]] + :cancel? true + :confirm {:value "Code" + :class "is-danger" + :status-from [::status/single ::form] + :on-click (dispatch-event [::code-selected checked] )} + :close-event [::status/completed ::code-selected]}] + :db (-> db + (forms/start-form ::form {:accounts [{:id (str "new-" (random-uuid)) + :amount-mode "%" + :amount-percentage 100 + :location nil}] + :client @(re-frame/subscribe [::subs/client (:client-id params)])}))})))