diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 8d92cbf1..1ef909f9 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -27,46 +27,55 @@ (or (not (:id data)) (< (.abs js/Math (- (js/parseFloat (:total data)) (reduce + 0 (map (fn [ea] (js/parseFloat (:amount ea))) (:expense-accounts data))))) 0.001)))))) +(defmulti submit-query (fn [_ [_ command]] + command)) + +(defmethod submit-query :create [db] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + {:venia/operation {:operation/type :mutation + :operation/name "AddInvoice"} + :venia/queries [{:query/data [:add-invoice + {:invoice {:date date + :vendor-id vendor-id + :client-id client-id + :invoice-number invoice-number + :location location + :total total}} + invoice-read]}]})) + +(defmethod submit-query :edit [db] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + {:venia/operation {:operation/type :mutation + :operation/name "EditInvoice"} + :venia/queries [{:query/data [:edit-invoice + {:invoice {:id id + :invoice-number invoice-number + :date date + :total total + :expense-accounts (map (fn [ea] + {:id (:id ea) + :amount (:amount ea)}) + expense-accounts)}} + invoice-read]}]})) + +(defmethod submit-query :add-and-print [db [_ _ bank-account-id type]] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + {:venia/operation {:operation/type :mutation + :operation/name "AddAndPrintInvoice"} + :venia/queries [{:query/data [:add-and-print-invoice + {:invoice {:date date + :vendor-id vendor-id + :client-id client-id + :invoice-number invoice-number + :location location + :total total} + :bank-account-id bank-account-id + :type type} + [:pdf-url [:invoices invoice-read]]]}]})) + (re-frame/reg-sub ::submit-query - (fn [db [_ command bank-account-id type]] - (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] - (condp = command - :create {:venia/operation {:operation/type :mutation - :operation/name "AddInvoice"} - :venia/queries [{:query/data [:add-invoice - {:invoice {:date date - :vendor-id vendor-id - :client-id client-id - :invoice-number invoice-number - :location location - :total total - }} - invoice-read]}]} - :edit {:venia/operation {:operation/type :mutation - :operation/name "EditInvoice"} - :venia/queries [{:query/data [:edit-invoice - {:invoice {:id id - :invoice-number invoice-number - :date date - :total total - :expense-accounts (map (fn [ea] - {:id (:id ea) - :amount (:amount ea)}) - expense-accounts)}} - invoice-read]}]} - :add-and-print {:venia/operation {:operation/type :mutation - :operation/name "AddAndPrintInvoice"} - :venia/queries [{:query/data [:add-and-print-invoice - {:invoice {:date date - :vendor-id vendor-id - :client-id client-id - :invoice-number invoice-number - :location location - :total total} - :bank-account-id bank-account-id - :type type} - [:pdf-url [:invoices invoice-read]]]}]})))) + submit-query) ;; EVENTS (re-frame/reg-event-db @@ -105,44 +114,42 @@ (re-frame/reg-event-fx ::submitted - (fn [{:keys [db]} [_ invoice-created invoice-printed command bank-account-id type]] + (fn [{:keys [db]} [_ params command bank-account-id type]] (when @(re-frame/subscribe [::can-submit-edit-invoice]) (let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])] {:db (forms/loading db ::form) :graphql {:token (-> db :user) :query-obj @(re-frame/subscribe [::submit-query command bank-account-id type]) - :on-success [::succeeded command invoice-created invoice-printed] + :on-success [::succeeded params command] :on-error [::forms/save-error ::form]}})))) (re-frame/reg-event-fx ::succeeded - (fn [{:keys [db]} [_ command invoice-created invoice-printed result]] + (fn [{:keys [db]} [_ {:keys [invoice-created invoice-printed]} command result]] (let [invoice (condp = command :edit (:edit-invoice result) :create (:add-invoice result) - :add-and-print (first (:invoices (:add-and-print-invoice result)))) - db (cond-> db - true (forms/stop-form ::form) + :add-and-print (first (:invoices (:add-and-print-invoice result))))] - (#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client])) - :status :unpaid - :date (date->str (c/now) standard) - :location (first (:locations @(re-frame/subscribe [::subs/client])))})) - events (cond-> [(conj invoice-created invoice)] - (= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))] + {:db (cond-> db + true (forms/stop-form ::form) - {:db db - :dispatch-n events}))) + (#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client])) + :status :unpaid + :date (date->str (c/now) standard) + :location (first (:locations @(re-frame/subscribe [::subs/client])))})) + :dispatch-n (cond-> [(conj invoice-created invoice)] + (= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))}))) ;; VIEWS -(defn form [{:keys [can-change-amount? invoice-created invoice-printed]}] +(defn form [{:keys [can-change-amount?] :as params}] [forms/side-bar-form {:form ::form } (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form]) exists? (:id data) @@ -164,8 +171,8 @@ (.stopPropagation e) (.preventDefault e)) (if exists? - (re-frame/dispatch-sync [::submitted invoice-created nil :edit]) - (re-frame/dispatch-sync [::submitted invoice-created nil :create])))} + (re-frame/dispatch-sync [::submitted params :edit]) + (re-frame/dispatch-sync [::submitted params :create])))} [:h1.title.is-2 "New Invoice"] (when-not @(re-frame/subscribe [::subs/client]) [:div.field @@ -303,10 +310,10 @@ (list (for [{:keys [id number name type]} (->> (:bank-accounts current-client) (filter :visible) (sort-by :sort-order))] (if (= :cash type) - ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-invoice invoice-created invoice-printed id :cash])} "With cash"] + ^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::submitted params id :cash])} "With cash"] (list - ^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :check])} "Print checks from " name] - ^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :debit])} "Debit from " name]))))]]]) + ^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::submitted params :add-and-print id :check])} "Print checks from " name] + ^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::submitted params :add-and-print id :debit])} "Debit from " name]))))]]]) [:div.column [:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice]) "" diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 216bff55..538d9601 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -703,7 +703,7 @@ :component-will-mount #(re-frame/dispatch-sync [::params-change {:status status}]) })) (defn unpaid-invoices-page [{:keys [status]}] - (let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/new-invoice])] + (let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/form])] [side-bar-layout {:side-bar [invoices-side-bar {} ^{:key "extra-filter"} [:div