you can now change types.

This commit is contained in:
Bryce Covert
2018-05-31 21:57:00 -07:00
parent 4c16c06457
commit 2f744be36c
7 changed files with 162 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
(ns auto-ap.subs
(:require [re-frame.core :as re-frame]
[clojure.string :as str]
[auto-ap.expense-accounts :refer [expense-accounts]]
[goog.crypt.base64 :as base64]))
(re-frame/reg-sub
@@ -72,3 +73,10 @@
::new-invoice-form
(fn [db]
(:new-invoice db)))
(re-frame/reg-sub
::expense-accounts
(fn [db]
(map (fn [[k v]] (assoc v :id k))
expense-accounts)))

View File

@@ -1,7 +1,7 @@
(ns auto-ap.views.components.invoice-table
(:require [re-frame.core :as re-frame]
[auto-ap.subs :as subs]
[auto-ap.views.utils :refer [date->str]]
[auto-ap.views.utils :refer [date->str dispatch-event]]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.views.components.sorter :refer [sorted-column]]
[reagent.core :as reagent]
@@ -22,7 +22,7 @@
:company-id (:id @(re-frame/subscribe [::subs/company])))
[[:invoices [:id :total :outstanding-balance :invoice-number :date
[:vendor [:name :id]]
[:expense_accounts [:amount :id
[:expense_accounts [:amount :id :expense_account_id
[:expense_account [:id :name [:parent [:id :name]]]]]]
[:company [:name :id]]
[:checks [:amount [:check [:amount :s3_url :check_number ]]]]
@@ -33,7 +33,7 @@
(defn invoice-table [{:keys [id invoice-page status on-params-change vendors params check-boxes checked on-check-changed]}]
(defn invoice-table [{:keys [id invoice-page status on-params-change vendors params check-boxes checked on-check-changed expense-event]}]
(let [state (reagent/atom (or @params {}))
opc (fn [p]
(swap! state merge p)
@@ -121,7 +121,7 @@
[:td (gstring/format "$%.2f" outstanding-balance )]
[:td (for [e expense-accounts]
^{:key (:id e)}
[:a.tag (:name (:expense-account e)) " "(gstring/format "$%.2f" (:amount e) ) ])]
[:a.tag {:on-click (dispatch-event (conj expense-event id))} (:name (:expense-account e)) " "(gstring/format "$%.2f" (:amount e) ) ])]
[:td (for [check checks]
^{:key (:id 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

@@ -7,7 +7,7 @@
[auto-ap.entities.invoice :as invoice]
[auto-ap.entities.vendors :as vendor]
[auto-ap.views.utils :refer [dispatch-event bind-field horizontal-field]]
[auto-ap.utils :refer [by]]
[auto-ap.utils :refer [by replace-if]]
[auto-ap.views.pages.check :as check]
[auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
[auto-ap.views.components.modal :refer [modal action-modal]]
@@ -21,11 +21,22 @@
(fn [db]
(-> db ::invoice-page)))
(re-frame/reg-sub
::invoice
(fn [db id]
(println id)
(get (by :id (-> db ::invoice-page :invoices )) id)))
(re-frame/reg-sub
::advanced-print-checks
(fn [db]
(-> db ::advanced-print-checks)))
(re-frame/reg-sub
::change-expense-accounts
(fn [db]
(-> db ::change-expense-accounts)))
(re-frame/reg-sub
::check-results
(fn [db]
@@ -218,6 +229,104 @@
is)))
(dissoc ::new-invoice))}))
(re-frame/reg-event-fx
::change-expense-accounts
(fn [{:keys [db]} [_ id]]
{:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}]
:db (assoc-in db [::change-expense-accounts :invoice] (get (by :id (get-in db [::invoice-page :invoices])) id))}))
(re-frame/reg-event-fx
::change-expense-accounts-saving
(fn [{:keys [db]} [_ id]]
(let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])]
{:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditExpenseAccounts"}
:venia/queries [{:query/data [:edit-expense-accounts
{:invoice-id id
:expense-accounts (map (fn [ea] {:id (:id ea)
:amount (:amount ea)
:expense-account-id (:expense-account-id ea)})
expense-accounts)}
[:id :total :outstanding-balance :invoice-number :date
[:vendor [:name :id]]
[:expense_accounts [:amount :id :expense_account_id
[:expense_account [:id :name [:parent [:id :name]]]]]]
[:company [:name :id]]
[:checks [:amount [:check [:amount :s3_url :check_number ]]]]
]]}]}
:on-success [::expense-accounts-updated]}})))
(re-frame/reg-event-fx
::expense-accounts-updated
(fn [{:keys [db]} [_ data]]
(let [updated (:edit-expense-accounts data)]
{:dispatch [::events/modal-completed ::change-expense-accounts]
:db (-> db
(update-in [::invoice-page :invoices]
(fn [is]
(println "UPDATE" updated is)
(replace-if #(= (:id %1) (:id %2)) updated is)))
(dissoc ::change-expense-accounts))})))
(re-frame/reg-event-db
::add-expense-account-split
(fn [db _]
(update-in db [::change-expense-accounts :invoice :expense-accounts]
conj {:amount 0 :expense-account-id nil})))
(defn change-expense-accounts-modal []
(let [{{:keys [expense-accounts]} :invoice :as data} @(re-frame/subscribe [::change-expense-accounts])
change-event [::events/change-form [::change-expense-accounts]]]
[action-modal {:id ::change-expense-accounts
:title "Change expense accounts"
:action-text "Save"
:save-event [::change-expense-accounts-saving]
#_#_:can-submit? (s/valid? ::invoice/invoice data)}
[:div
[:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]]
[:table.table
[:thead
[:tr
[:th {:style {:width "500px"}} "Expense Account"]
[:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]]
[:tbody
(for [[expense-account index] (map vector expense-accounts (range))]
[:tr
[:td [:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/expense-accounts]))
:type "typeahead"
:field [:invoice :expense-accounts index :expense-account-id]
:event change-event
:spec ::invoice/vendor-id
:subscription data}]]]]
[:td
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [:invoice :expense-accounts index :amount]
:event change-event
:subscription data
:value (doto (get-in data [:invoice :expense-accounts index :amount]) println)
:max (:total data)
:step "0.01"}]]]]]]
[:td [:a.button [:i.fa.fa-times]]]]
)]]]))
(defn print-checks-modal []
(let [{:keys [checked]} @(re-frame/subscribe [::invoice-page])
{:keys [shown? invoices printing?] :as advanced-print-checks} @(re-frame/subscribe [::advanced-print-checks])
@@ -337,6 +446,9 @@
:spec ::invoice/total
:step "0.01"}]]]]]]))
(def unpaid-invoices-page
(with-meta
(fn []
@@ -379,10 +491,12 @@
:check-boxes true
:checked checked
:on-check-changed (fn [which]
(re-frame/dispatch [::toggle-check which]))}]
(re-frame/dispatch [::toggle-check which]))
:expense-event [::change-expense-accounts]}]
[print-checks-modal]
[new-invoice-modal]
[change-expense-accounts-modal]
(when check-results-shown?
[modal
{:title "Your checks are ready!"

View File

@@ -105,6 +105,7 @@
]
(into
[:div.field-body]
(with-keys (map (fn [x] [:div.field x]) controls)))])