Vendor sub not needed.

This commit is contained in:
2022-04-11 07:52:38 -07:00
parent b4d5b7fde6
commit 919f8a773d
3 changed files with 178 additions and 176 deletions

View File

@@ -1,16 +1,14 @@
(ns auto-ap.events (ns auto-ap.events
(:require [re-frame.core :as re-frame] (:require
[auto-ap.db :as db] [auto-ap.db :as db]
[auto-ap.subs :as subs] [auto-ap.routes :as routes]
[auto-ap.routes :as routes] [auto-ap.subs :as subs]
[auto-ap.effects :as effects] [auto-ap.utils :refer [by]]
[auto-ap.views.utils :refer [with-user]]
[auto-ap.utils :refer [by]] [bidi.bidi :as bidi]
[venia.core :as v] [clojure.string :as str]
[bidi.bidi :as bidi] [goog.crypt.base64 :as b64]
[cemerick.url :refer [url]] [re-frame.core :as re-frame]))
[goog.crypt.base64 :as b64]
[clojure.string :as str]))
(defn jwt->data [token] (defn jwt->data [token]
(js->clj (.parse js/JSON (b64/decodeString (second (str/split token #"\." )))))) (js->clj (.parse js/JSON (b64/decodeString (second (str/split token #"\." ))))))
@@ -275,3 +273,34 @@
:query-obj {:venia/queries [[:yodlee-merchants :query-obj {:venia/queries [[:yodlee-merchants
[:name :yodlee-id :id]]]} [:name :yodlee-id :id]]]}
:on-success [::yodlee-merchants-received]}})) :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}}))

View File

@@ -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 (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]) accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client])
unautopay-states @(re-frame/subscribe [::status/multi ::unautopay]) unautopay-states @(re-frame/subscribe [::status/multi ::unautopay])
editing-states @(re-frame/subscribe [::status/multi ::edits])
account->name #(:name (accounts-by-id (:id %)))] account->name #(:name (accounts-by-id (:id %)))]
[grid/row {:class (:class i) :id id :checkable? checkable? :entity invoice} [grid/row {:class (:class i) :id id :checkable? checkable? :entity invoice}
(when-not selected-client (when-not selected-client
@@ -213,7 +214,14 @@
(when (and (get actions :edit) (when (and (get actions :edit)
(not= ":voided" (:status i))) (not= ":voided" (:status i)))
[buttons/fa-icon {:icon "fa-pencil" [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) (when (and (get actions :void)
(= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i))) (= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i)))
[buttons/sl-icon {:icon "icon-bin-2" [buttons/sl-icon {:icon "icon-bin-2"

View File

@@ -90,87 +90,63 @@
expense-accounts)}} expense-accounts)}}
invoice-read]}]})) 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 ;; EVENTS
(re-frame/reg-event-db (re-frame/reg-event-fx
::updated ::updated
(fn [db [_ _ command]] [(re-frame/inject-cofx ::inject/sub [::subs/client])]
(if (= :create command) (fn [{:keys [db] ::subs/keys [client]} [_ _ command]]
(-> db (when (= :create command)
(forms/stop-form ::form ) {:db
(forms/start-form ::form {:client @(re-frame/subscribe [::subs/client]) (-> db
:status :unpaid (forms/stop-form ::form )
:date (date->str (c/now) standard)})) (forms/start-form ::form {:client client
db))) :status :unpaid
:date (date->str (c/now) standard)}))})))
(re-frame/reg-event-db (re-frame/reg-event-fx
::adding ::adding
(fn [db [_ new]] [(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:client new)]) [::subs/accounts-by-id (:id (:client which))]))
accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id (:client new)])] (re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
(-> db (forms/start-form ::form (assoc new :expense-accounts [::subs/locations-for-client (:id (:client which))]))]
(expense-accounts-field/from-graphql (:expense-accounts new) (fn [{:keys [db] ::subs/keys [locations-for-client accounts-by-id]} [_ new]]
accounts-by-id {:db
0.0 (-> db (forms/start-form ::form (assoc new :expense-accounts
locations))))))) (expense-accounts-field/from-graphql (:expense-accounts new)
(re-frame/reg-event-db accounts-by-id
0.0
locations-for-client))))}))
(re-frame/reg-event-fx
::editing ::editing
(fn [db [_ which]] [(re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
(let [accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id (:client which)]) [::subs/accounts-by-id (:id (:client which))]))
vendor (get @(re-frame/subscribe [::subs/vendors-by-id]) (:id (:vendor which))) (re-frame/inject-cofx ::inject/sub (fn [[_ which _]]
edit-invoice (update which :date #(date->str % standard)) [::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 :due #(date->str % standard))
edit-invoice (update edit-invoice :scheduled-payment #(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 :original edit-invoice)]
edit-invoice (assoc edit-invoice :vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor))) {:db
(:id (:client which))))) (-> db
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))]) (forms/start-form ::form {:id (:id edit-invoice)
] :payments (:payments edit-invoice)
(-> db :status (:status edit-invoice)
(forms/start-form ::form {:id (:id edit-invoice) :date (:date edit-invoice)
:payments (:payments edit-invoice) :due (:due edit-invoice)
:status (:status edit-invoice) :vendor-preferences vendor-preferences
:date (:date edit-invoice) :scheduled-payment (:scheduled-payment edit-invoice)
:due (:due edit-invoice) :invoice-number (:invoice-number edit-invoice)
:vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor))) :total (:total edit-invoice)
(:id (:client which)))) :original edit-invoice
:scheduled-payment (:scheduled-payment edit-invoice) :vendor (:vendor edit-invoice)
:invoice-number (:invoice-number edit-invoice) :client (:client edit-invoice)
:total (:total edit-invoice) :expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
:original edit-invoice accounts-by-id
:vendor (:vendor edit-invoice) (:total which)
:client (:client edit-invoice) locations-for-client)}))})))
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
accounts-by-id
(:total which)
locations)})))))
@@ -179,50 +155,87 @@
::changed ::changed
(forms/change-handler ::form (forms/change-handler ::form
(fn [data field value] (fn [data field value]
(cond (= [:total] field) (cond
[[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)] (= [: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) (:automatically-paid-when-due value)
(:schedule-payment-dom data)) (into [[:scheduled-payment] (:due data)
[[:scheduled-payment] (date->str (next-dom (str->date value standard) (:schedule-payment-dom data)) standard) ] [:schedule-when-due] true])
(and (= [:schedule-when-due] field) value) (:schedule-payment-dom value)
[[:scheduled-payment] (:due data)] (into [[:scheduled-payment] (date->str (next-dom (str->date (:date data) standard) (:schedule-payment-dom value)) standard)]))
(and (= [:due] field) (:schedule-when-due data)) (= [:total] field)
[[:scheduled-payment] value] [[: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 (re-frame/reg-event-db
::maybe-change-client ::maybe-change-client
[ (forms/in-form ::form)] [ (forms/in-form ::form) ]
(fn [{:keys [data] :as f} [_ c]] (fn [{:keys [data] :as f} [_ c]]
(if (:id data) (if (and (not (:id data))
f (:id c))
(assoc-in f [:data :client] c)))) (assoc-in f [:data :client] c)
f)))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::add-and-print ::add-and-print
[with-user (forms/in-form ::form)] [with-user (forms/in-form ::form)]
(fn [{:keys [user]} [_ bank-account-id type]] (fn [{:keys [user]
{:graphql {{:keys [invoice-number date location total expense-accounts scheduled-payment vendor client]} :data} :db} [_ bank-account-id type]]
{:token user {:graphql
:owns-state {:single ::form} {:token user
:query-obj @(re-frame/subscribe [::add-and-print-query bank-account-id type]) :owns-state {:single ::form}
:on-success [::added-and-printed] :query-obj {:venia/operation {:operation/type :mutation
:on-error [::forms/save-error ::form]}})) :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 (re-frame/reg-event-fx
::saving ::saving
[with-user (forms/in-form ::form)] [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} _] (fn [{:keys [user] {:keys [data]} :db ::keys [edit-query create-query]} _]
{:graphql {:graphql
{:token user {:token user
:owns-state {:single ::form} :owns-state {:single ::form}
:query-obj (if (:id data) :query-obj (if (:id data)
@(re-frame/subscribe [::edit-query]) edit-query
@(re-frame/subscribe [::create-query])) create-query)
:on-success (fn [result] :on-success (fn [result]
[::updated [::updated
(assoc (if (:id data) (assoc (if (:id data)
@@ -239,12 +252,11 @@
[with-user (forms/in-form ::form)] [with-user (forms/in-form ::form)]
(fn [{:keys [db]} [_ fwd-event]] (fn [{:keys [db]} [_ fwd-event]]
(if (and (:scheduled-payment (:data db)) (if (and (:scheduled-payment (:data db))
(not (:vendor-autopay? (:data db)))) (not (:vendor-autopay? (:vendor-preferences (:data db)))))
{:dispatch {:dispatch
[::modal/modal-requested {:title "Scheduled payment date" [::modal/modal-requested {:title "Scheduled payment date"
:body [:div "This vendor isn't set up to be automatically paid. On " :body [:div "This vendor isn't set up to be automatically paid. On "
(:scheduled-payment (:data db)) (:scheduled-payment (:data db))
" the invoice will be marked as paid, but no payment will be made to the vendor. " " the invoice will be marked as paid, but no payment will be made to the vendor. "
"Are you sure you want to continue?"] "Are you sure you want to continue?"]
:confirm {:value "Save" :confirm {:value "Save"
@@ -263,66 +275,18 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::checks-printed ::checks-printed
(fn [db _] (fn [db [_]]
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 (re-frame/reg-event-fx
::changed-vendor ::changed-vendor
[with-user (forms/in-form ::form)] [(forms/in-form ::form)]
(fn [{:keys [user] {{:keys [client date due expense-accounts total]} :data} :db} [_ vendor]] (fn [{{{:keys [client]} :data} :db} [_ vendor]]
(when (:id vendor) (when (and (:id client) (:id vendor))
{:graphql {:token user {:dispatch [::events/vendor-preferences-requested {:client-id (:id client)
:query-obj {:venia/queries [[:vendor-by-id :vendor-id (:id vendor)
{:id (:id vendor)} :on-success [::changed [:vendor-preferences]]
[[:automatically-paid-when-due [:id]] :on-failure [:hello]}]})))
[: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]
}})))
;; VIEWS ;; VIEWS
@@ -338,7 +302,7 @@
{::track/register [{:id ::client {::track/register [{:id ::client
:subscription [::subs/client] :subscription [::subs/client]
:event-fn (fn [c] :event-fn (fn [c]
(println "changing client?")
[::maybe-change-client c])} [::maybe-change-client c])}
{:id ::vendor-change {:id ::vendor-change
:subscription [::forms/field ::form [:vendor]] :subscription [::forms/field ::form [:vendor]]
@@ -357,6 +321,7 @@
{:keys [form-inline field raw-field error-notification submit-button ]} invoice-form {:keys [form-inline field raw-field error-notification submit-button ]} invoice-form
can-submit? (boolean @(re-frame/subscribe [::can-submit])) can-submit? (boolean @(re-frame/subscribe [::can-submit]))
status @(re-frame/subscribe [::status/single ::form]) status @(re-frame/subscribe [::status/single ::form])
active-client @(re-frame/subscribe [::subs/client])
exists? (:id data) exists? (:id data)
can-change-amount? (#{:unpaid ":unpaid"} (:status data)) can-change-amount? (#{:unpaid ":unpaid"} (:status data))
min-total (if (= (:total (:original data)) (:outstanding-balance (:original data))) min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
@@ -383,13 +348,13 @@
:else :else
nil)]) nil)])
[:<> [:<>
(when-not @(re-frame/subscribe [::subs/client]) (when-not active-client
(field [:span "Client" (field [:span "Client"
[:span.has-text-danger " *"]] [:span.has-text-danger " *"]]
[typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients]) [typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients])
:entity->text :name :entity->text :name
:type "typeahead-v3" :type "typeahead-v3"
:auto-focus (if @(re-frame/subscribe [::subs/client]) false true) :auto-focus (if active-client false true)
:field [:client] :field [:client]
:disabled exists? :disabled exists?
:spec ::invoice/client}])) :spec ::invoice/client}]))
@@ -401,7 +366,7 @@
{:query i} {:query i}
[:name :id]]) [:name :id]])
:type "typeahead-v3" :type "typeahead-v3"
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false) :auto-focus (if active-client true false)
:field [:vendor]}]) :field [:vendor]}])
(field [:span "Date" (field [:span "Date"
@@ -477,7 +442,7 @@
:descriptor "expense account" :descriptor "expense account"
:locations (:locations (:client data)) :locations (:locations (:client data))
:max (:total data) :max (:total data)
:client (or (:client data) @(re-frame/subscribe [::subs/client])) :client (or (:client data) active-client)
:field [:expense-accounts]}]) :field [:expense-accounts]}])
{:key (str (:id (:vendor data) "none") "-" (:id (:client data) "none") )}) {:key (str (:id (:vendor data) "none") "-" (:id (:client data) "none") )})