Vendor sub not needed.
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
(ns auto-ap.events
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[auto-ap.db :as db]
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.routes :as routes]
|
||||
[auto-ap.effects :as effects]
|
||||
|
||||
[auto-ap.utils :refer [by]]
|
||||
[venia.core :as v]
|
||||
[bidi.bidi :as bidi]
|
||||
[cemerick.url :refer [url]]
|
||||
[goog.crypt.base64 :as b64]
|
||||
[clojure.string :as str]))
|
||||
(:require
|
||||
[auto-ap.db :as db]
|
||||
[auto-ap.routes :as routes]
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.views.utils :refer [with-user]]
|
||||
[bidi.bidi :as bidi]
|
||||
[clojure.string :as str]
|
||||
[goog.crypt.base64 :as b64]
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
(defn jwt->data [token]
|
||||
(js->clj (.parse js/JSON (b64/decodeString (second (str/split token #"\." ))))))
|
||||
@@ -275,3 +273,34 @@
|
||||
:query-obj {:venia/queries [[:yodlee-merchants
|
||||
[:name :yodlee-id :id]]]}
|
||||
:on-success [::yodlee-merchants-received]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::vendor-preferences-requested
|
||||
[with-user]
|
||||
(fn [{:keys [user]} [_ {:keys [ client-id vendor-id on-success on-failure owns-state]}]]
|
||||
{:graphql {:token user
|
||||
:query-obj {:venia/queries [[:vendor-by-id
|
||||
{:id vendor-id}
|
||||
[[:automatically-paid-when-due [:id]]
|
||||
[:schedule-payment-dom [[:client [:id]] :dom]]
|
||||
[:default-account [:id]]]]
|
||||
[:account-for-vendor
|
||||
{:vendor-id vendor-id
|
||||
:client-id client-id}
|
||||
[:name :id :numeric-code :location]]]}
|
||||
:owns-state owns-state
|
||||
:on-success (fn [r]
|
||||
(let [schedule-payment-dom (->> r
|
||||
:vendor-by-id
|
||||
:schedule-payment-dom
|
||||
(filter (fn [spd]
|
||||
(= (-> spd :client :id)
|
||||
client-id)))
|
||||
first
|
||||
:dom)
|
||||
automatically-paid-when-due (boolean ((->> r :vendor-by-id :automatically-paid-when-due (map :id) set) client-id))]
|
||||
(conj on-success {:default-account (:account-for-vendor r)
|
||||
:schedule-payment-dom schedule-payment-dom
|
||||
:automatically-paid-when-due automatically-paid-when-due
|
||||
:vendor-autopay? (or automatically-paid-when-due (boolean schedule-payment-dom))})))
|
||||
:on-failure on-failure}}))
|
||||
|
||||
@@ -119,6 +119,7 @@
|
||||
(let [{:keys [client status payments expense-accounts invoice-number date due total outstanding-balance id vendor source-url] :as i} invoice
|
||||
accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client])
|
||||
unautopay-states @(re-frame/subscribe [::status/multi ::unautopay])
|
||||
editing-states @(re-frame/subscribe [::status/multi ::edits])
|
||||
account->name #(:name (accounts-by-id (:id %)))]
|
||||
[grid/row {:class (:class i) :id id :checkable? checkable? :entity invoice}
|
||||
(when-not selected-client
|
||||
@@ -213,7 +214,14 @@
|
||||
(when (and (get actions :edit)
|
||||
(not= ":voided" (:status i)))
|
||||
[buttons/fa-icon {:icon "fa-pencil"
|
||||
:event [::form/editing i]}])
|
||||
:class (status/class-for (get editing-states id))
|
||||
:event
|
||||
[::events/vendor-preferences-requested {:client-id (:id client)
|
||||
:vendor-id (:id vendor)
|
||||
:on-success [::form/editing i]
|
||||
:on-failure []
|
||||
:owns-state {:multi ::edits
|
||||
:which (:id i)}}]}])
|
||||
(when (and (get actions :void)
|
||||
(= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i)))
|
||||
[buttons/sl-icon {:icon "icon-bin-2"
|
||||
|
||||
@@ -90,87 +90,63 @@
|
||||
expense-accounts)}}
|
||||
invoice-read]}]}))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::add-and-print-query
|
||||
(fn [_ [_ bank-account-id type]]
|
||||
(let [{{:keys [invoice-number date location total expense-accounts scheduled-payment vendor client]} :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 (:id vendor)
|
||||
:client-id (:id client)
|
||||
:scheduled-payment scheduled-payment
|
||||
:invoice-number invoice-number
|
||||
:location location
|
||||
:total total
|
||||
:expense-accounts (map (fn [ea]
|
||||
{:id (when-not (str/starts-with? (:id ea) "new-")
|
||||
(:id ea))
|
||||
:account_id (:id (:account ea))
|
||||
:location (:location ea)
|
||||
:amount (:amount ea)})
|
||||
expense-accounts)}
|
||||
:bank-account-id bank-account-id
|
||||
:type type}
|
||||
[:pdf-url [:invoices invoice-read]]]}]})))
|
||||
|
||||
|
||||
|
||||
;; EVENTS
|
||||
|
||||
(re-frame/reg-event-db
|
||||
(re-frame/reg-event-fx
|
||||
::updated
|
||||
(fn [db [_ _ command]]
|
||||
(if (= :create command)
|
||||
(-> db
|
||||
(forms/stop-form ::form )
|
||||
(forms/start-form ::form {:client @(re-frame/subscribe [::subs/client])
|
||||
:status :unpaid
|
||||
:date (date->str (c/now) standard)}))
|
||||
db)))
|
||||
[(re-frame/inject-cofx ::inject/sub [::subs/client])]
|
||||
(fn [{:keys [db] ::subs/keys [client]} [_ _ command]]
|
||||
(when (= :create command)
|
||||
{:db
|
||||
(-> db
|
||||
(forms/stop-form ::form )
|
||||
(forms/start-form ::form {:client client
|
||||
:status :unpaid
|
||||
:date (date->str (c/now) standard)}))})))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
(re-frame/reg-event-fx
|
||||
::adding
|
||||
(fn [db [_ new]]
|
||||
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:client new)])
|
||||
accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id (:client new)])]
|
||||
(-> db (forms/start-form ::form (assoc new :expense-accounts
|
||||
(expense-accounts-field/from-graphql (:expense-accounts new)
|
||||
accounts-by-id
|
||||
0.0
|
||||
locations)))))))
|
||||
(re-frame/reg-event-db
|
||||
[(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
|
||||
[::subs/accounts-by-id (:id (:client which))]))
|
||||
(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
|
||||
[::subs/locations-for-client (:id (:client which))]))]
|
||||
(fn [{:keys [db] ::subs/keys [locations-for-client accounts-by-id]} [_ new]]
|
||||
{:db
|
||||
(-> db (forms/start-form ::form (assoc new :expense-accounts
|
||||
(expense-accounts-field/from-graphql (:expense-accounts new)
|
||||
accounts-by-id
|
||||
0.0
|
||||
locations-for-client))))}))
|
||||
(re-frame/reg-event-fx
|
||||
::editing
|
||||
(fn [db [_ which]]
|
||||
(let [accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id (:client which)])
|
||||
vendor (get @(re-frame/subscribe [::subs/vendors-by-id]) (:id (:vendor which)))
|
||||
edit-invoice (update which :date #(date->str % standard))
|
||||
[(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
|
||||
[::subs/accounts-by-id (:id (:client which))]))
|
||||
(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
|
||||
[::subs/locations-for-client (:id (:client which))]))]
|
||||
(fn [{:keys [db] ::subs/keys [locations-for-client accounts-by-id]} [_ which vendor-preferences]]
|
||||
(let [edit-invoice (update which :date #(date->str % standard))
|
||||
edit-invoice (update edit-invoice :due #(date->str % standard))
|
||||
edit-invoice (update edit-invoice :scheduled-payment #(date->str % standard))
|
||||
edit-invoice (assoc edit-invoice :original edit-invoice)
|
||||
edit-invoice (assoc edit-invoice :vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor)))
|
||||
(:id (:client which)))))
|
||||
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])
|
||||
]
|
||||
(-> db
|
||||
(forms/start-form ::form {:id (:id edit-invoice)
|
||||
:payments (:payments edit-invoice)
|
||||
:status (:status edit-invoice)
|
||||
:date (:date edit-invoice)
|
||||
:due (:due edit-invoice)
|
||||
:vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor)))
|
||||
(:id (:client which))))
|
||||
:scheduled-payment (:scheduled-payment edit-invoice)
|
||||
:invoice-number (:invoice-number edit-invoice)
|
||||
:total (:total edit-invoice)
|
||||
:original edit-invoice
|
||||
:vendor (:vendor edit-invoice)
|
||||
:client (:client edit-invoice)
|
||||
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
|
||||
accounts-by-id
|
||||
(:total which)
|
||||
locations)})))))
|
||||
edit-invoice (assoc edit-invoice :original edit-invoice)]
|
||||
{:db
|
||||
(-> db
|
||||
(forms/start-form ::form {:id (:id edit-invoice)
|
||||
:payments (:payments edit-invoice)
|
||||
:status (:status edit-invoice)
|
||||
:date (:date edit-invoice)
|
||||
:due (:due edit-invoice)
|
||||
:vendor-preferences vendor-preferences
|
||||
:scheduled-payment (:scheduled-payment edit-invoice)
|
||||
:invoice-number (:invoice-number edit-invoice)
|
||||
:total (:total edit-invoice)
|
||||
:original edit-invoice
|
||||
:vendor (:vendor edit-invoice)
|
||||
:client (:client edit-invoice)
|
||||
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
|
||||
accounts-by-id
|
||||
(:total which)
|
||||
locations-for-client)}))})))
|
||||
|
||||
|
||||
|
||||
@@ -179,50 +155,87 @@
|
||||
::changed
|
||||
(forms/change-handler ::form
|
||||
(fn [data field value]
|
||||
(cond (= [:total] field)
|
||||
[[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)]
|
||||
(cond
|
||||
(= [:vendor-preferences] field)
|
||||
(cond-> []
|
||||
(expense-accounts-field/can-replace-with-default? (:expense-accounts data))
|
||||
(into [[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data)
|
||||
(:default-account value)
|
||||
(:total data)
|
||||
(:locations (:client data)))])
|
||||
|
||||
(and (= [:date] field)
|
||||
(:schedule-payment-dom data))
|
||||
[[:scheduled-payment] (date->str (next-dom (str->date value standard) (:schedule-payment-dom data)) standard) ]
|
||||
(:automatically-paid-when-due value)
|
||||
(into [[:scheduled-payment] (:due data)
|
||||
[:schedule-when-due] true])
|
||||
|
||||
(and (= [:schedule-when-due] field) value)
|
||||
[[:scheduled-payment] (:due data)]
|
||||
(:schedule-payment-dom value)
|
||||
(into [[:scheduled-payment] (date->str (next-dom (str->date (:date data) standard) (:schedule-payment-dom value)) standard)]))
|
||||
|
||||
(and (= [:due] field) (:schedule-when-due data))
|
||||
[[:scheduled-payment] value]
|
||||
(= [:total] field)
|
||||
[[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)]
|
||||
|
||||
:else
|
||||
[]))))
|
||||
(and (= [:date] field)
|
||||
(:schedule-payment-dom (:vendor-preferences data)))
|
||||
[[:scheduled-payment] (date->str (next-dom (str->date value standard) (:schedule-payment-dom (:vendor-preferences data))) standard) ]
|
||||
|
||||
(and (= [:schedule-when-due] field) value)
|
||||
[[:scheduled-payment] (:due data)]
|
||||
|
||||
(and (= [:due] field) (:schedule-when-due data))
|
||||
[[:scheduled-payment] value]
|
||||
|
||||
:else
|
||||
[]))))
|
||||
(re-frame/reg-event-db
|
||||
::maybe-change-client
|
||||
[ (forms/in-form ::form)]
|
||||
[ (forms/in-form ::form) ]
|
||||
(fn [{:keys [data] :as f} [_ c]]
|
||||
(if (:id data)
|
||||
f
|
||||
(assoc-in f [:data :client] c))))
|
||||
(if (and (not (:id data))
|
||||
(:id c))
|
||||
(assoc-in f [:data :client] c)
|
||||
f)))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::add-and-print
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user]} [_ bank-account-id type]]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj @(re-frame/subscribe [::add-and-print-query bank-account-id type])
|
||||
:on-success [::added-and-printed]
|
||||
:on-error [::forms/save-error ::form]}}))
|
||||
::add-and-print
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user]
|
||||
{{:keys [invoice-number date location total expense-accounts scheduled-payment vendor client]} :data} :db} [_ bank-account-id type]]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddAndPrintInvoice"}
|
||||
:venia/queries [{:query/data [:add-and-print-invoice
|
||||
{:invoice {:date date
|
||||
:vendor-id (:id vendor)
|
||||
:client-id (:id client)
|
||||
:scheduled-payment scheduled-payment
|
||||
:invoice-number invoice-number
|
||||
:location location
|
||||
:total total
|
||||
:expense-accounts (map (fn [ea]
|
||||
{:id (when-not (str/starts-with? (:id ea) "new-")
|
||||
(:id ea))
|
||||
:account_id (:id (:account ea))
|
||||
:location (:location ea)
|
||||
:amount (:amount ea)})
|
||||
expense-accounts)}
|
||||
:bank-account-id bank-account-id
|
||||
:type type}
|
||||
[:pdf-url [:invoices invoice-read]]]}]}
|
||||
:on-success [::added-and-printed]
|
||||
:on-error [::forms/save-error ::form]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::saving
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} _]
|
||||
[with-user (forms/in-form ::form) (re-frame/inject-cofx ::inject/sub [::edit-query]) (re-frame/inject-cofx ::inject/sub [::create-query])]
|
||||
(fn [{:keys [user] {:keys [data]} :db ::keys [edit-query create-query]} _]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj (if (:id data)
|
||||
@(re-frame/subscribe [::edit-query])
|
||||
@(re-frame/subscribe [::create-query]))
|
||||
edit-query
|
||||
create-query)
|
||||
:on-success (fn [result]
|
||||
[::updated
|
||||
(assoc (if (:id data)
|
||||
@@ -239,12 +252,11 @@
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [db]} [_ fwd-event]]
|
||||
(if (and (:scheduled-payment (:data db))
|
||||
(not (:vendor-autopay? (:data db))))
|
||||
(not (:vendor-autopay? (:vendor-preferences (:data db)))))
|
||||
{:dispatch
|
||||
[::modal/modal-requested {:title "Scheduled payment date"
|
||||
:body [:div "This vendor isn't set up to be automatically paid. On "
|
||||
(:scheduled-payment (:data db))
|
||||
|
||||
" the invoice will be marked as paid, but no payment will be made to the vendor. "
|
||||
"Are you sure you want to continue?"]
|
||||
:confirm {:value "Save"
|
||||
@@ -263,66 +275,18 @@
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::checks-printed
|
||||
(fn [db _]
|
||||
(fn [db [_]]
|
||||
db))
|
||||
|
||||
#_(re-frame/reg-sub
|
||||
::client-accounts
|
||||
:<- [::forms/field ::form [:client]]
|
||||
:<- [::subs/all-accounts]
|
||||
(fn [[client all-accounts]]
|
||||
(subs/accounts-by-id all-accounts client)))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::changed-vendor
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {{:keys [client date due expense-accounts total]} :data} :db} [_ vendor]]
|
||||
(when (:id vendor)
|
||||
{:graphql {:token user
|
||||
:query-obj {:venia/queries [[:vendor-by-id
|
||||
{:id (:id vendor)}
|
||||
[[:automatically-paid-when-due [:id]]
|
||||
[:schedule-payment-dom [[:client [:id]] :dom]]
|
||||
[:default-account [:id]]]]
|
||||
[:account-for-vendor
|
||||
{:vendor-id (:id vendor)
|
||||
:client-id (:id client)}
|
||||
[:name :id :numeric-code :location]]]}
|
||||
:on-success (fn [r]
|
||||
(let [schedule-payment-dom (->> r
|
||||
:vendor-by-id
|
||||
:schedule-payment-dom
|
||||
(filter (fn [spd]
|
||||
(= (-> spd :client :id)
|
||||
(:id client))))
|
||||
first
|
||||
:dom)
|
||||
|
||||
changes (cond-> []
|
||||
(expense-accounts-field/can-replace-with-default? expense-accounts)
|
||||
(into [[:expense-accounts] (expense-accounts-field/default-account expense-accounts
|
||||
(:account-for-vendor r)
|
||||
total
|
||||
(:locations client))])
|
||||
|
||||
(boolean ((->> r :vendor-by-id :automatically-paid-when-due (map :id) set) (:id client)))
|
||||
(into [[:scheduled-payment] due
|
||||
[:schedule-when-due] true
|
||||
[:vendor-autopay? ] true])
|
||||
|
||||
schedule-payment-dom
|
||||
(into [[:scheduled-payment] (date->str (next-dom (str->date date standard) schedule-payment-dom) standard)
|
||||
[:vendor-autopay?] true])
|
||||
|
||||
true
|
||||
(into [[:schedule-payment-dom] schedule-payment-dom]))]
|
||||
(if (seq changes)
|
||||
(into [::changed ] changes)
|
||||
[:ignore]))
|
||||
)
|
||||
:on-failure [:bad]
|
||||
}})))
|
||||
|
||||
[(forms/in-form ::form)]
|
||||
(fn [{{{:keys [client]} :data} :db} [_ vendor]]
|
||||
(when (and (:id client) (:id vendor))
|
||||
{:dispatch [::events/vendor-preferences-requested {:client-id (:id client)
|
||||
:vendor-id (:id vendor)
|
||||
:on-success [::changed [:vendor-preferences]]
|
||||
:on-failure [:hello]}]})))
|
||||
|
||||
|
||||
;; VIEWS
|
||||
@@ -338,7 +302,7 @@
|
||||
{::track/register [{:id ::client
|
||||
:subscription [::subs/client]
|
||||
:event-fn (fn [c]
|
||||
|
||||
(println "changing client?")
|
||||
[::maybe-change-client c])}
|
||||
{:id ::vendor-change
|
||||
:subscription [::forms/field ::form [:vendor]]
|
||||
@@ -357,6 +321,7 @@
|
||||
{:keys [form-inline field raw-field error-notification submit-button ]} invoice-form
|
||||
can-submit? (boolean @(re-frame/subscribe [::can-submit]))
|
||||
status @(re-frame/subscribe [::status/single ::form])
|
||||
active-client @(re-frame/subscribe [::subs/client])
|
||||
exists? (:id data)
|
||||
can-change-amount? (#{:unpaid ":unpaid"} (:status data))
|
||||
min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
|
||||
@@ -383,13 +348,13 @@
|
||||
:else
|
||||
nil)])
|
||||
[:<>
|
||||
(when-not @(re-frame/subscribe [::subs/client])
|
||||
(when-not active-client
|
||||
(field [:span "Client"
|
||||
[:span.has-text-danger " *"]]
|
||||
[typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients])
|
||||
:entity->text :name
|
||||
:type "typeahead-v3"
|
||||
:auto-focus (if @(re-frame/subscribe [::subs/client]) false true)
|
||||
:auto-focus (if active-client false true)
|
||||
:field [:client]
|
||||
:disabled exists?
|
||||
:spec ::invoice/client}]))
|
||||
@@ -401,7 +366,7 @@
|
||||
{:query i}
|
||||
[:name :id]])
|
||||
:type "typeahead-v3"
|
||||
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
|
||||
:auto-focus (if active-client true false)
|
||||
:field [:vendor]}])
|
||||
|
||||
(field [:span "Date"
|
||||
@@ -477,7 +442,7 @@
|
||||
:descriptor "expense account"
|
||||
:locations (:locations (:client data))
|
||||
:max (:total data)
|
||||
:client (or (:client data) @(re-frame/subscribe [::subs/client]))
|
||||
:client (or (:client data) active-client)
|
||||
:field [:expense-accounts]}])
|
||||
{:key (str (:id (:vendor data) "none") "-" (:id (:client data) "none") )})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user