more standardization

This commit is contained in:
Bryce Covert
2020-08-17 10:58:05 -07:00
parent 85a110d878
commit 8036d34a4e
12 changed files with 252 additions and 255 deletions

View File

@@ -15,7 +15,6 @@
[ring/ring-defaults "0.2.1"] [ring/ring-defaults "0.2.1"]
[mount "0.1.16"] [mount "0.1.16"]
[tolitius/yang "0.1.10"] [tolitius/yang "0.1.10"]
[day8.re-frame/forward-events-fx "0.0.6"]
[ring "1.6.3" :exclusions [commons-codec [ring "1.6.3" :exclusions [commons-codec
commons-io commons-io
clj-time clj-time

View File

@@ -291,6 +291,7 @@
(when-not (seq transactions) (when-not (seq transactions)
(throw (ex-info "No invoices found." (throw (ex-info "No invoices found."
{:imports (str imports)}))) {:imports (str imports)})))
(log/info "creating invoice" transactions)
@(d/transact (d/connect uri) (vec (set transactions)))))) @(d/transact (d/connect uri) (vec (set transactions))))))

View File

@@ -31,6 +31,15 @@
replaced replaced
(into [x] replaced)))) (into [x] replaced))))
(defn remove-by [xs f x]
(into []
(filter
(fn [t]
(if (= (f t) (f x))
false
true))
xs)))
(defn merge-by [xs f x] (defn merge-by [xs f x]
(let [found? (atom false) (let [found? (atom false)
replaced (mapv replaced (mapv

View File

@@ -1,51 +1,28 @@
(ns auto-ap.views.components.expense-accounts-dialog (ns auto-ap.views.components.expense-accounts-dialog
(:require [auto-ap.entities.invoices-expense-accounts (:require [auto-ap.forms :as forms]
:as [auto-ap.status :as status]
invoices-expense-accounts]
[auto-ap.events :as events]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.views.components.modal :refer [action-modal]] [auto-ap.utils :refer [by]]
[auto-ap.views.components.typeahead :refer [typeahead]] [auto-ap.views.components.modal :as modal]
[auto-ap.views.utils :refer [bind-field dispatch-event]] [auto-ap.views.components.typeahead :refer [typeahead-entity]]
[clojure.spec.alpha :as s] [auto-ap.views.pages.invoices.common :refer [invoice-read]]
[auto-ap.views.utils :refer [dispatch-event with-user]]
[clojure.string :as str] [clojure.string :as str]
[goog.string :as gstring] [goog.string :as gstring]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
(re-frame/reg-sub (re-frame/reg-sub
::change-expense-accounts ::can-submit
(fn [db] (fn [db]
(-> db ::change-expense-accounts))) true))
(re-frame/reg-sub
::expense-account
:<- [::change-expense-accounts]
(fn [{{:keys [expense-accounts] :or {expense-accounts [] }} :invoice} [_ invoice-expense-account-id]]
(first (filter #(= invoice-expense-account-id (:id %)) expense-accounts))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::change-expense-accounts ::try-save
(fn [{:keys [db]} [_ i]] [(forms/in-form ::form)]
{:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}] (fn [{:keys [db]} [_ id ]]
:db (assoc-in db [::change-expense-accounts :invoice] i)})) (let [{{:keys [ total]} :invoice
:keys [expense-accounts]} (:data db)
expense-accounts (vals expense-accounts)
(re-frame/reg-event-db
::change
(fn [db [_ id f v]]
(update-in db [::change-expense-accounts :invoice :expense-accounts]
(fn [expense-accounts]
(mapv
(fn [expense-account]
(if (= id (:id expense-account))
(assoc-in expense-account f v)
expense-account))
expense-accounts)))))
(re-frame/reg-event-fx
::change-expense-accounts-saving
(fn [{:keys [db]} [_ on-success id ]]
(let [{{:keys [expense-accounts total]} :invoice} @(re-frame/subscribe [::change-expense-accounts])
expense-accounts-total (->> expense-accounts expense-accounts-total (->> expense-accounts
(map :new-amount) (map :new-amount)
(map js/parseFloat) (map js/parseFloat)
@@ -56,65 +33,71 @@
(reduce + 0)) (reduce + 0))
does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)] does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)]
(if (and does-add-up? (if (and does-add-up?
(every? :new-amount expense-accounts) (every? :new-amount expense-accounts))
(s/valid? (s/* ::invoices-expense-accounts/invoices-expense-account) expense-accounts))
{:dispatch [::change-expense-accounts-submit on-success id]} {:dispatch [::save]}
{:dispatch [::events/modal-status ::change-expense-accounts {:saving? false :error-message "Expense accounts do not add up."}]})))) {:dispatch [::status/error ::form [{:message "Expense accounts do not add up."}]]}))))
(re-frame/reg-event-db
::saving-error
(fn [db [_ message]]
(assoc-in db [::change-expense-accounts :error] message)))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::change-expense-accounts-submit ::save
(fn [{:keys [db] } [_ on-success id]] [with-user (forms/in-form ::form)]
(let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])] (fn [{:keys [db user] } [_ id]]
(let [{{:keys [id]} :invoice
:keys [expense-accounts]} (:data db)
expense-accounts (vals expense-accounts)]
{:graphql {:graphql
{:token (-> db :user) {:token user
:owns-state {:single ::form}
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditExpenseAccounts"} :operation/name "EditExpenseAccounts"}
:venia/queries [{:query/data [:edit-expense-accounts :venia/queries [{:query/data [:edit-expense-accounts
{:invoice-id id {:invoice-id id
:expense-accounts (map (fn [ea] {:id (if (clojure.string/includes? (:id ea) "new-") :expense-accounts (map (fn [ea] {:id (if (clojure.string/includes? (str (:id ea)) "new-")
nil nil
(:id ea) (:id ea))
)
:amount (:new-amount ea) :amount (:new-amount ea)
:location (:location ea) :location (:location ea)
:account-id (:id (:account ea))}) :account-id (:id (:account ea))})
expense-accounts)} expense-accounts)}
[:id :total :outstanding-balance :invoice-number :date :status invoice-read]}]}
[:vendor [:name :id]] :on-success (fn [result]
[:expense_accounts [:amount :id :location [::updated (:edit-expense-accounts result)])}})))
[:account [:id :name :numeric-code :location]]]]
[:client [:name :id :locations]] (re-frame/reg-event-fx
[:payments [:amount :id [:payment [:amount :s3_url :check_number ]]]]]]}]} ::updated
:on-success on-success}}))) (fn [_ _]
{:dispatch [::modal/modal-closed]}))
(re-frame/reg-event-db (re-frame/reg-event-db
::add-expense-account-split ::add-split
[(forms/in-form ::form)]
(fn [db _] (fn [db _]
(let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-expense-accounts])] (update-in db [:data :expense-accounts]
(update-in db [::change-expense-accounts :invoice :expense-accounts] assoc (str "new-" (random-uuid)) {:amount "0.0" :account nil :location (-> db :data :invoice :client :locations first) })))
conj {:amount "0.0" :id (str "new-" (random-uuid)) :account {} :location (first locations)}))))
(re-frame/reg-event-db (re-frame/reg-event-db
::remove-expense-account-split ::remove-expense-account-split
[(forms/in-form ::form)]
(fn [db [_ x]] (fn [db [_ x]]
(update-in db [::change-expense-accounts :invoice :expense-accounts] (update-in db [:data :expense-accounts] dissoc x)))
(fn [expense-accounts]
(vec (filter #(not= x (:id %)) expense-accounts) )))))
(def change-expense-accounts-form (forms/vertical-form {:submit-event [::try-save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn change-expense-accounts-modal [{:keys [updated-event]}] (defn form []
(let [{{:keys [expense-accounts total] :or {expense-accounts [] total 0} {:keys [locations]} :client} :invoice error :error :as data} @(re-frame/subscribe [::change-expense-accounts]) (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
expense-accounts (:expense-accounts data)
{:keys [total] :or {total 0} {:keys [locations] :as client} :client} (:invoice data)
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} change-expense-accounts-form
multi-location? (> (count locations) 1) multi-location? (> (count locations) 1)
change-event [::change] chooseable-expense-accounts @(re-frame/subscribe [::subs/accounts client])
chooseable-expense-accounts @(re-frame/subscribe [::subs/accounts (:client (:invoice data))])
expense-accounts-total (->> expense-accounts expense-accounts-total (->> expense-accounts
vals
(map :new-amount) (map :new-amount)
(map js/parseFloat) (map js/parseFloat)
(map #(or % 0.0)) (map #(or % 0.0))
@@ -122,85 +105,91 @@
0.0 0.0
%)) %))
(reduce + 0))] (reduce + 0))]
[action-modal {:id ::change-expense-accounts [:div
:title "Change expense accounts"
:action-text "Save"
:save-event [::change-expense-accounts-saving updated-event]
:can-submit? true}
(when error
[:div error] )
[:div [:div
[:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]] [:a.button.is-primary {:on-click (dispatch-event [::add-split])} "Add split"]]
[:table.table (form-inline {}
[:thead [:table.table
[:tr [:thead
[:th {:style {:width "500px"}} "Expense Account"] [:tr
(when multi-location? [:th {:style {:width "500px"}} "Expense Account"]
[:th {:style {:width "200px"}} "Location"]) (when multi-location?
[:th {:style {:width "200px"}} "Original Amount"] [:th {:style {:width "200px"}} "Location"])
[:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "200px"}} "Original Amount"]
[:th {:style {:width "5em"}}]]] [:th {:style {:width "300px"}} "Amount"]
[:tbody [:th {:style {:width "5em"}}]]]
(doall (for [{:keys [id] :as expense-account} expense-accounts [:tbody
:let [sub @(re-frame/subscribe [::expense-account (:id expense-account)])]] (doall (for [[id expense-account] expense-accounts]
^{:key id} ^{:key id}
[:tr [:tr
[:td.expandable [:div.control [:td.expandable [:div.control
[bind-field (raw-field
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) chooseable-expense-accounts) [typeahead-entity {:matches chooseable-expense-accounts
:type "typeahead" :type "typeahead-entity"
:field [:account :id] :match->text (fn [x] (str (:numeric-code x) " - " (:name x)))
:event [::change id] :field [:expense-accounts id :account]}])]]
:spec ::invoices-expense-accounts/account-id
:subscription sub}]]]]
(when multi-location? (when multi-location?
[:td [:td
(if-let [forced-location (:location @(re-frame/subscribe [::subs/account (:client (:invoice data)) (-> sub :account :id )]))] (if-let [forced-location (:location @(re-frame/subscribe [::subs/account client (get-in expense-accounts [id :account :id])]))]
[:div.select [:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select [:div.select
[bind-field (raw-field
[:select {:type "select" [:select {:type "select"
:field [:location] :field [:expense-accounts id :location]
:spec (set locations) :spec (set locations)}
:event [::change id] (map (fn [l] ^{:key l} [:option {:value l} l]) locations)])])])
:subscription sub}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) [:td
(str "$" (get-in expense-accounts [id :amount]))]
[:td [:td
(str "$" (get-in sub [:amount]))] [:div.control
[:td [:div.field.has-addons.is-extended
[:div.control [:p.control [:a.button.is-static "$"]]
[:div.field.has-addons.is-extended [:p.control
[:p.control [:a.button.is-static "$"]] (raw-field
[:p.control [:input.input {:type "number"
[bind-field :field [:expense-accounts id :new-amount-temp]
[:input.input {:type "number" :style {:text-align "right"}
:field [:new-amount-temp] :on-blur (dispatch-event [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
:style {:text-align "right"} :on-key-down (fn [e ]
:on-blur (dispatch-event [::change id [:new-amount] (:new-amount-temp sub)]) (if (= 13 (.-keyCode e))
:on-key-down (fn [e ] (do
(if (= 13 (.-keyCode e)) (re-frame/dispatch [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])])
(do true)
false))
(re-frame/dispatch [::change id [:new-amount] (:new-amount-temp sub)]) :max (:total data)
true) :step "0.01"}])]]]]
false)) [:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split id])} [:i.fa.fa-times]]]]))
:event [::change id] [:tr
:subscription sub [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "]
:value (get-in data [:new-amount-temp]) [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
[:tr
:max (:total data) [:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "]
:step "0.01"}]]]]]] [:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]])) [:tr
[:tr [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "]
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "] [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]])]))
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
[:tr (re-frame/reg-event-fx
[:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "] ::show
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]] (fn [{:keys [db]} [_ i]]
[:tr (let [accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id])]
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "] {:dispatch [::modal/modal-requested {:title "Change expense accounts"
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]])) :body [form]
:confirm {:value "Save"
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::try-save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/start-form ::form
{:expense-accounts (by :id
(mapv
(fn [ea]
(assoc ea :account (accounts-by-id (:id (:account ea)))))
(:expense-accounts i)))
:invoice i}))})))

View File

@@ -170,7 +170,9 @@
(let [children (r/children (r/current-component))] (let [children (r/children (r/current-component))]
[:> Consumer {} [:> Consumer {}
(fn [consume] (fn [consume]
(let [{:strs [column-count status]} (js->clj consume)] (let [{:strs [column-count status check-boxes?]} (js->clj consume)
column-count (cond-> column-count
check-boxes? inc)]
(r/as-element (r/as-element
(cond (= :loading (:state status)) (cond (= :loading (:state status))
^{:key "loading-body"} ^{:key "loading-body"}

View File

@@ -15,7 +15,8 @@
[cljs-time.core :as t] [cljs-time.core :as t]
[clojure.string :as str] [clojure.string :as str]
[goog.string :as gstring] [goog.string :as gstring]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]
[auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog]))
(defn query [params] (defn query [params]
{:venia/queries [[:invoice_page {:venia/queries [[:invoice_page
@@ -84,15 +85,14 @@
{:invoice-id id} {:invoice-id id}
invoice-read]}]} invoice-read]}]}
:on-success (fn [result] :on-success (fn [result]
[::invoice-updated (assoc (:unvoid-invoice result) [::invoice-updated (:unvoid-invoice result)])}}))
:class "live-added")])}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::invoice-updated ::invoice-updated
(fn [{:keys [db]} [_ invoice]] (fn [{:keys [db]} [_ invoice]]
{:db db})) {:db db}))
(defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event ]}] (defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event actions]}]
(let [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor checkable?] :as i} invoice (let [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor checkable?] :as i} invoice
accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client]) accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client])
account->name #(:name (accounts-by-id (:id %)))] account->name #(:name (accounts-by-id (:id %)))]
@@ -134,10 +134,12 @@
^{:key (:id e)} ^{:key (:id e)}
[:span.dropdown-item (account->name (:account e)) " " (gstring/format "$%.2f" (:amount e) ) ]) [:span.dropdown-item (account->name (:account e)) " " (gstring/format "$%.2f" (:amount e) ) ])
[:hr.dropdown-divider] (when (get actions :expense-accounts)
[:<>
(when expense-event [:hr.dropdown-divider]
[:a.dropdown-item.is-primary {:on-click (dispatch-event (conj expense-event i))} "Change"])]]])
[:a.dropdown-item.is-primary {:on-click (dispatch-event [::expense-accounts-dialog/show i])} "Change"]])]]])
[:span {:style {:margin-left "1em"}}] [:span {:style {:margin-left "1em"}}]
(when (seq payments) (when (seq payments)
[:<> [:<>
@@ -165,17 +167,20 @@
(when (= :cleared (:status (:payment payment))) (when (= :cleared (:status (:payment payment)))
(str " - " (:post-date (:transaction (:payment payment))))))]))]] (str " - " (:post-date (:transaction (:payment payment))))))]))]]
[:span {:style {:margin-right "1em"}}]]) [:span {:style {:margin-right "1em"}}]])
(when (not= ":voided" (:status i)) (when (and (get actions :edit)
(not= ":voided" (:status i)))
[buttons/fa-icon {:icon "fa-pencil" [buttons/fa-icon {:icon "fa-pencil"
:event [::form/editing i]}]) :event [::form/editing i]}])
(when (and (= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i))) (when (and (get actions :void)
(= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i)))
[buttons/sl-icon {:icon "icon-bin-2" [buttons/sl-icon {:icon "icon-bin-2"
:event [::void-invoice i]}]) :event [::void-invoice i]}])
(when (= ":voided" (:status i)) (when (and (get actions :void)
(= ":voided" (:status i)))
[buttons/fa-icon {:icon "fa-undo" [buttons/fa-icon {:icon "fa-undo"
:event [::unvoid-invoice i]}])]]])) :event [::unvoid-invoice i]}])]]]))
(defn invoice-table [{:keys [id data checked status vendors check-boxes on-check-changed expense-event overrides]}] (defn invoice-table [{:keys [id data checked status check-boxes on-check-changed overrides actions]}]
(let [selected-client @(re-frame/subscribe [::subs/client]) (let [selected-client @(re-frame/subscribe [::subs/client])
{:keys [sort]} @(re-frame/subscribe [::table-params]) {:keys [sort]} @(re-frame/subscribe [::table-params])
selected-client @(re-frame/subscribe [::subs/client]) selected-client @(re-frame/subscribe [::subs/client])
@@ -204,7 +209,7 @@
:checked checked :checked checked
:status status :status status
:check-boxes? check-boxes :check-boxes? check-boxes
:column-count (if selected-client 7 8)} :column-count (if selected-client 8 9)}
[grid/controls data [grid/controls data
[:div.level-item [:div.level-item
"Outstanding " (nf (:outstanding data))]] "Outstanding " (nf (:outstanding data))]]
@@ -235,5 +240,5 @@
^{:key id} ^{:key id}
[row {:invoice i [row {:invoice i
:selected-client selected-client :selected-client selected-client
:overrides overrides :actions actions
:expense-event expense-event}])]])])) :overrides overrides}])]])]))

View File

@@ -11,11 +11,11 @@
[auto-ap.views.utils :refer [dispatch-event with-user]] [auto-ap.views.utils :refer [dispatch-event with-user]]
[vimsical.re-frame.cofx.inject :as inject] [vimsical.re-frame.cofx.inject :as inject]
[vimsical.re-frame.fx.track :as track] [vimsical.re-frame.fx.track :as track]
[day8.re-frame.forward-events-fx]
[auto-ap.events :as events] [auto-ap.events :as events]
[auto-ap.utils :refer [replace-by merge-by]] [auto-ap.utils :refer [replace-by merge-by]]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[auto-ap.status :as status])) [auto-ap.status :as status]
[auto-ap.effects.forward :as forward]))
;; SUBS ;; SUBS
@@ -91,16 +91,17 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::deleted-transaction-rule ::deleted-transaction-rule
[(re-frame/path [::page :transaction-rules])] [(re-frame/path [::page :transaction-rules])]
(fn [transaction-rules [_ [_ {id :delete-transaction-rule}]]] (fn [transaction-rules [_ {id :delete-transaction-rule}]]
(merge-by transaction-rules :id {:id id :class "live-removed"}))) (merge-by transaction-rules :id {:id id :class "live-removed"})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::mounted ::mounted
(fn [{:keys [db]}] (fn [{:keys [db]}]
{:dispatch-n [[::events/yodlee-merchants-needed]] {:dispatch-n [[::events/yodlee-merchants-needed]]
:forward-events {:register ::page ::forward/register {:id ::page
:events #{::table/deleted-transaction-rule} :events #{::table/deleted-transaction-rule}
:dispatch-to [::deleted-transaction-rule]} :event-fn (fn [[_ result]]
[::deleted-transaction-rule result])}
::track/register {:id ::params ::track/register {:id ::params
:subscription [::params] :subscription [::params]
:event-fn (fn [params] [::params-change params])}})) :event-fn (fn [params] [::params-change params])}}))
@@ -108,7 +109,7 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted ::unmounted
(fn [{:keys [db]}] (fn [{:keys [db]}]
{:forward-events {:unregister ::page} {::forward/dispose {:id ::page}
::track/dispose {:id ::params}})) ::track/dispose {:id ::params}}))
;; VIEWS ;; VIEWS

View File

@@ -17,7 +17,8 @@
[auto-ap.routes :as routes] [auto-ap.routes :as routes]
[bidi.bidi :as bidi] [bidi.bidi :as bidi]
[auto-ap.status :as status] [auto-ap.status :as status]
[auto-ap.views.pages.admin.users.form :as form])) [auto-ap.views.pages.admin.users.form :as form]
[auto-ap.effects.forward :as forward]))
@@ -56,19 +57,20 @@
:role :role
[:clients [:id :name]]]]]} [:clients [:id :name]]]]]}
:on-success [::received]} :on-success [::received]}
:forward-events {:register ::edited-user ::forward/register {:id ::edited-user
:events #{::form/saved} :events #{::form/saved}
:dispatch-to [::saved]}})) :event-fn (fn [[_ query-result]]
[::saved query-result])}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted ::unmounted
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{:forward-events {:unregister ::edited-user}})) {::forward/dispose {:id ::edited-user}}))
(re-frame/reg-event-db (re-frame/reg-event-db
::saved ::saved
(fn [db [_ [_ query-result]]] (fn [db [_ query-result]]
(-> db (-> db
(update ::users (update ::users
replace-by replace-by

View File

@@ -22,12 +22,18 @@
(let [checked (or checked #{})] (let [checked (or checked #{})]
(disj checked to-remove)))))) (disj checked to-remove))))))
(re-frame/reg-event-db
::reset-checked
(fn [db [_ id]]
(update db ::checked dissoc id)))
(re-frame/reg-event-db (re-frame/reg-event-db
::updated-entity ::updated-entity
(fn [db [_ id entity]] (fn [db [_ id entity]]
(update-in db (update-in db
[::data id] [::data id :data]
replace-by :id (assoc entity :class "live-added")))) replace-by :id (update entity :class #(or % "live-added")))))
(re-frame/reg-event-db (re-frame/reg-event-db
::received ::received
@@ -62,13 +68,3 @@
(defn in-page-entities [which] (defn in-page-entities [which]
(re-frame/path [::data which :data ] )) (re-frame/path [::data which :data ] ))
(re-frame/reg-event-db
::entity-updated
(fn [db [_ which [_ entity] :as g]]
(println g)
(update-in db [::data which :data]
(fn [entities]
(let [by-id (by :id entities )]
(if (by-id (:id entity))
(merge-by entities :id entity)
(into [entity] entities)))))))

View File

@@ -108,11 +108,13 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::updated ::updated
(fn [db [_ invoice command]] (fn [db [_ invoice command]]
(-> db (if (= :create command)
(forms/stop-form ::form ) (-> db
(forms/start-form ::form {:client @(re-frame/subscribe [::subs/client]) (forms/stop-form ::form )
:status :unpaid (forms/start-form ::form {:client @(re-frame/subscribe [::subs/client])
:date (date->str (c/now) standard)})))) :status :unpaid
:date (date->str (c/now) standard)}))
db)))
(re-frame/reg-event-db (re-frame/reg-event-db
::adding ::adding
@@ -188,27 +190,28 @@
::saving ::saving
[with-user (forms/in-form ::form)] [with-user (forms/in-form ::form)]
(fn [{:keys [user] {:keys [data]} :db} _] (fn [{:keys [user] {:keys [data]} :db} _]
(let [command (if (:id data) {:graphql
:edit {:token user
:create)] :owns-state {:single ::form}
{:graphql :query-obj (if (:id data)
{:token user @(re-frame/subscribe [::edit-query])
:owns-state {:single ::form} @(re-frame/subscribe [::create-query]))
:query-obj (if (:id data) :on-success (fn [result]
@(re-frame/subscribe [::edit-query]) [::updated
@(re-frame/subscribe [::create-query])) (assoc (if (:id data)
:on-success (fn [result] (:edit-invoice result)
[::updated (assoc (if (:id data) (:add-invoice result))
(:edit-invoice result) :class "live-added")
(:add-invoice result)) (if (:id data)
:class "live-added")]) :edit
:on-error [::forms/save-error ::form]}}))) :create)])
:on-error [::forms/save-error ::form]}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::added-and-printed ::added-and-printed
(fn [{:keys [db]} [_ result]] (fn [{:keys [db]} [_ result]]
(let [invoice (first (:invoices (:add-and-print-invoice result)))] (let [invoice (first (:invoices (:add-and-print-invoice result)))]
{:dispatch-n [[::updated (assoc invoice :class "live-added")] {:dispatch-n [[::updated (assoc invoice :class "live-added") :create]
[::checks-printed [invoice] (:pdf-url (:add-and-print-invoice result))]]}))) [::checks-printed [invoice] (:pdf-url (:add-and-print-invoice result))]]})))
(re-frame/reg-event-db (re-frame/reg-event-db

View File

@@ -20,7 +20,8 @@
[auto-ap.views.pages.check :as check] [auto-ap.views.pages.check :as check]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.status :as status] [auto-ap.status :as status]
[vimsical.re-frame.fx.track :as track])) [vimsical.re-frame.fx.track :as track]
[auto-ap.effects.forward :as forward]))
@@ -72,7 +73,7 @@
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{:db (dissoc db ::last-params ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::payment-page) {:db (dissoc db ::last-params ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::payment-page)
::track/dispose {:id ::params} ::track/dispose {:id ::params}
:forward-events {:unregister ::page}})) ::forward/dispose {:id ::page}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::mounted ::mounted
@@ -80,14 +81,15 @@
{::track/register {:id ::params {::track/register {:id ::params
:subscription [::params] :subscription [::params]
:event-fn (fn [params] [::params-change params])} :event-fn (fn [params] [::params-change params])}
:forward-events {:register ::page ::forward/register {:id ::page
:events #{::table/payment-voided} :events #{::table/payment-voided}
:dispatch-to [::payment-voided]}})) :event-fn (fn [[_ result]]
[::payment-voided result])}}))
(re-frame/reg-event-db (re-frame/reg-event-db
::payment-voided ::payment-voided
[(re-frame/path [::payment-page :payments])] [(re-frame/path [::payment-page :payments])]
(fn [db [_ [_ {:keys [void-payment]}]]] (fn [db [_ {:keys [void-payment]}]]
(mapv (fn [c] (mapv (fn [c]
(if (= (:id c) (:id void-payment)) (if (= (:id c) (:id void-payment))
(assoc void-payment :class "live-removed") (assoc void-payment :class "live-removed")

View File

@@ -6,11 +6,10 @@
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.utils :refer [by merge-by replace-if]] [auto-ap.utils :refer [by merge-by replace-if]]
[auto-ap.views.components.dropdown :refer [drop-down]] [auto-ap.views.components.dropdown :refer [drop-down]]
[auto-ap.effects.forward :as forward]
[auto-ap.views.components.expense-accounts-dialog [auto-ap.views.components.expense-accounts-dialog
:as :as
expense-accounts-dialog expense-accounts-dialog]
:refer
[change-expense-accounts-modal]]
[auto-ap.views.components.invoice-table :as table] [auto-ap.views.components.invoice-table :as table]
[auto-ap.views.components.invoices.side-bar [auto-ap.views.components.invoices.side-bar
:as :as
@@ -36,10 +35,8 @@
[reagent.core :as r] [reagent.core :as r]
[vimsical.re-frame.fx.track :as track])) [vimsical.re-frame.fx.track :as track]))
(re-frame/reg-sub ;; TODO: Sort out approaches for which buttons to show
::change-expense-accounts ;; TODO: make it so filters are basically free
(fn [db]
(-> db ::change-expense-accounts)))
(re-frame/reg-sub (re-frame/reg-sub
::params ::params
@@ -77,8 +74,8 @@
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{:db (dissoc db ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::last-params) {:db (dissoc db ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::last-params)
:dispatch [::data-page/dispose :invoices] :dispatch [::data-page/dispose :invoices]
:forward-events [{:unregister ::updated} ::forward/dispose [{:id ::updated}
{:unregister ::checks-printed}] {:id ::checks-printed}]
::track/dispose [{:id ::params}]})) ::track/dispose [{:id ::params}]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -88,12 +85,14 @@
:subscription [::params] :subscription [::params]
:event-fn (fn [params] :event-fn (fn [params]
[::params-change params])}] [::params-change params])}]
:forward-events [{:register ::updated ::forward/register [{:id ::updated
:events #{::table/invoice-updated ::form/updated} :events #{::table/invoice-updated ::form/updated ::expense-accounts-dialog/updated}
:dispatch-to [::data-page/entity-updated :invoices]} :event-fn (fn [[_ invoice]]
{:register ::checks-printed [::data-page/updated-entity :invoices invoice])}
:events #{::form/checks-printed ::advanced-print-checks/checks-printed ::handwritten-checks/succeeded} {:id ::checks-printed
:dispatch-to [::checks-printed]}]})) :events #{::form/checks-printed ::advanced-print-checks/checks-printed ::handwritten-checks/succeeded}
:event-fn (fn [[_ invoices pdf-url]]
[::checks-printed invoices pdf-url])}]}))
(re-frame/reg-sub (re-frame/reg-sub
@@ -104,7 +103,6 @@
(filter (comp #(get checked %) :id) (:data data)))) (filter (comp #(get checked %) :id) (:data data))))
(defn print-checks-query [invoice-payments bank-account-id type client-id] (defn print-checks-query [invoice-payments bank-account-id type client-id]
{:venia/operation {:operation/type :mutation {:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"} :operation/name "PrintChecks"}
@@ -132,18 +130,19 @@
(:client db)) (:client db))
:on-success (fn [result] :on-success (fn [result]
[::checks-printed [::checks-printed
[nil (:invoices (:print-checks result))
(:invoices (:print-checks result)) (:pdf-url (:print-checks result))])}}))
(:pdf-url (:print-checks result))]])}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::checks-printed ::checks-printed
(fn [{:keys [db]} [_ [_ invoices pdf-url]]] (fn [{:keys [db]} [_ invoices pdf-url]]
{:dispatch-n (cond->> (mapv {:dispatch-n (cond->> [[::data-page/reset-checked :invoices]]
(fn [i] true (into (mapv
[::data-page/entity-updated :invoices [nil (assoc i :class "live-added")]]) (fn [i]
invoices) [::data-page/updated-entity :invoices i])
invoices))
pdf-url (into [[::modal/modal-requested {:title "Your checks are ready!" pdf-url (into [[::modal/modal-requested {:title "Your checks are ready!"
:body [:div :body [:div
[:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."] [:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."]
@@ -158,15 +157,6 @@
#_#_:date (date->str (c/now) standard) #_#_:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))}]})) :location (first (:locations @(re-frame/subscribe [::subs/client])))}]}))
(re-frame/reg-event-fx
::expense-accounts-updated
(fn [{:keys [db]} [_ data]]
(let [updated (:edit-expense-accounts data)]
{:dispatch-n [[::events/modal-completed ::expense-accounts-dialog/change-expense-accounts]
[::data-page/entity-updated :invoices [nil updated]]]
:db (-> db
(dissoc ::change-expense-accounts))})))
(defn pay-button [] (defn pay-button []
(let [current-client @(re-frame/subscribe [::subs/client]) (let [current-client @(re-frame/subscribe [::subs/client])
@@ -232,7 +222,7 @@
:check-boxes (= status :unpaid) :check-boxes (= status :unpaid)
:on-check-changed (fn [new] :on-check-changed (fn [new]
(re-frame/dispatch [::data-page/toggle-check :invoices new ])) (re-frame/dispatch [::data-page/toggle-check :invoices new ]))
:expense-event [::expense-accounts-dialog/change-expense-accounts]}]])) :actions #{:edit :void :expense-accounts}}]]))
(defn unpaid-invoices-page [params] (defn unpaid-invoices-page [params]
(r/create-class (r/create-class
@@ -245,6 +235,4 @@
params @(re-frame/subscribe [::params])] params @(re-frame/subscribe [::params])]
[side-bar-layout {:side-bar [invoices-side-bar {}] [side-bar-layout {:side-bar [invoices-side-bar {}]
:main [unpaid-invoices-content params] :main [unpaid-invoices-content params]
:bottom [:div
[change-expense-accounts-modal {:updated-event [::expense-accounts-updated]}]]
:right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))})) :right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))}))