you can add overrides.

This commit is contained in:
Bryce Covert
2020-04-19 14:38:55 -07:00
parent 5579fc95b2
commit a48ee8dd52
6 changed files with 179 additions and 103 deletions

View File

@@ -147,7 +147,34 @@
:auto-ap/add-due {:txes [[{:db/ident :invoice/due
:db/doc "When you gotta pay"
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one}]]}}]
:db/cardinality :db.cardinality/one}]]}
:auto-ap/add-vendor-overrides {:txes [[{:db/ident :vendor-account-override/account
:db/doc "the account for invoices"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :vendor-account-override/client
:db/doc "How many days till you pay"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :vendor-terms-override/terms
:db/doc "How many days till you pay"
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}
{:db/ident :vendor-terms-override/client
:db/doc "How many days till you pay"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :vendor/terms-overrides
:db/doc "Overrides per-client"
:db/isComponent true
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
{:db/ident :vendor/account-overrides
:db/isComponent true
:db/doc "Overrides per-client"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}]]}
}]
(println "Conforming database...")
(c/ensure-conforms conn norms-map)
(when (not (seq args))

View File

@@ -3,31 +3,23 @@
[auto-ap.datomic :refer [uri]]))
(defn get-graphql [args]
(prn (->> (d/q '[:find (pull ?e [*])
(->> (d/q '[:find (pull ?e [* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}])
:where [?e :vendor/name]]
(d/db (d/connect uri)))
(map first)
(take 5)))
(->> (d/q '[:find (pull ?e [*])
:where [?e :vendor/name]]
(d/db (d/connect uri)))
(map first)
#_(map (fn [c]
(update c :client/bank-accounts
(fn [bas]
(map (fn [ba]
(update ba :bank-account/type :db/ident ))
bas)))))))
(map first)))
(defn get-by-id [id]
(->> (d/q '[:find (pull ?e [* {:default-account [:account/name :db/id :account/location]}])
(->> (d/q '[:find (pull ?e [*
{:default-account [:account/name :db/id :account/location]
:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]}])
:in $ ?e
:where [?e]]
(d/db (d/connect uri))
id)
(map first)
(first)
))
(first)))

View File

@@ -110,12 +110,26 @@
:city {:type 'String}
:state {:type 'String}
:zip {:type 'String}}}
:terms_override
{:fields {:id {:type :id}
:client {:type :client}
:terms {:type 'Int}
}}
:account_override
{:fields {:id {:type :id}
:client {:type :client}
:account {:type :account}}}
:vendor
{:fields {:id {:type :id}
:name {:type 'String}
:code {:type 'String}
:terms {:type 'Int}
:hidden {:type 'Boolean}
:terms_overrides {:type '(list :terms_override)}
:account_overrides {:type '(list :account_override)}
:print_as {:type 'String}
:primary_contact {:type :contact}
@@ -516,10 +530,22 @@
:state {:type 'String}
:zip {:type 'String}}}
:add_terms_override
{:fields {:id {:type :id}
:client_id {:type :id}
:terms {:type 'Int}
}}
:add_account_override
{:fields {:id {:type :id}
:client_id {:type :id}
:account_id {:type :id}}}
:add_vendor
{:fields {:id {:type :id}
:name {:type 'String}
:terms {:type 'Int}
:terms_overrides {:type '(list :add_terms_override)}
:code {:type 'String}
:hidden {:type 'Boolean}
@@ -529,6 +555,7 @@
:address {:type :add_address}
:default_account_id {:type :id}
:account_overrides {:type '(list :add_account_override)}
:invoice_reminder_schedule {:type 'String}}}
:edit_expense_account

View File

@@ -8,7 +8,7 @@
[clojure.set :as set]))
(defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule] :as in} :vendor} value]
(defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule terms_overrides account_overrides] :as in} :vendor} value]
(let [_ (println default_account_id)
transaction [(remove-nils #:vendor {:db/id (if id
id
@@ -20,6 +20,20 @@
:print-as print_as
:default-account default_account_id
:invoice-reminder-schedule (keyword invoice_reminder_schedule)
:terms-overrides (mapv
(fn [to]
(cond->
#:vendor-terms-override {:client (:client_id to)
:terms (:terms to)}
(:id to) (assoc :db/id (:id to))))
terms_overrides)
:account-overrides (mapv
(fn [ao]
(cond->
#:vendor-account-override {:client (:client_id ao)
:account (:account_id ao)}
(:id ao) (assoc :db/id (:id ao))))
account_overrides)
:address (when address
(remove-nils #:address {:db/id (if (:id address)
(:id address)

View File

@@ -53,6 +53,8 @@
:on-success [::received-initial]}}))))
(def vendor-query
[:id :name :hidden :terms [:default-account [:name :id :location]]
[:account-overrides [[:client [:id :name]] :id [:account [:id :numeric-code :name]]]]
[:terms-overrides [[:client [:id :name]] :id :terms]]
[:primary-contact [:name :phone :email :id]]
[:secondary-contact [:id :name :phone :email]]
:print-as :invoice-reminder-schedule :code

View File

@@ -33,26 +33,26 @@
(map first)
first))))
(re-frame/reg-sub
::selected-override
(fn [db]
(::selected-override db)))
(re-frame/reg-event-db
::override-selected
(fn [db [_ which]]
(assoc db ::selected-override which)))
(re-frame/reg-event-db
::override-added
(fn [db [_ z]]
(println "selected" z)
(assoc db ::selected-override "101")))
(re-frame/reg-event-fx
::started
(fn [{:keys [db]} [_ vendor]]
(println vendor)
{:db (-> db (forms/start-form ::vendor-form (-> vendor
(update :account-overrides #(mapv
(fn [ao]
{:id (:id ao)
:client (:client ao)
:override (:account ao)})
%))
(update :terms-overrides #(mapv
(fn [to]
{:id (:id to)
:client (:client to)
:override (:terms to)})
%))
(update :hidden #(if (nil? %)
false
%))
@@ -72,23 +72,67 @@
(re-frame/reg-event-fx
::save
[with-user (forms/triggers-loading ::vendor-form) (forms/in-form ::vendor-form)]
(fn [{:keys [user] {:keys [data]} :db} _]
(fn [{:keys [user] {{:keys [name hidden print-as terms invoice-reminder-schedule primary-contact secondary-contact address default-account terms-overrides account-overrides id] :as data} :data} :db} _]
(when (s/valid? ::entity/vendor data)
{ :graphql
{:token user
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "UpsertVendor"}
:venia/queries [{:query/data [:upsert-vendor
{:vendor (-> data
(assoc :default-account-id (:id (:default-account data)))
(dissoc :default-account))}
{:vendor {:id id
:name name
:hidden hidden
:print-as print-as
:terms terms
:terms-overrides (mapv
(fn [{:keys [client override id]}]
{:id id
:client-id (:id client)
:terms override})
terms-overrides)
:default-account-id (:id default-account)
:account-overrides (mapv
(fn [{:keys [client override id]}]
{:id id
:client-id (:id client)
:account-id (:id override)})
account-overrides)
:address address
:primary-contact primary-contact
:secondary-contact secondary-contact
:invoice-reminder-schedule invoice-reminder-schedule}}
events/vendor-query]}]}
:on-success [::save-complete]
:on-error [::forms/save-error ::vendor-form]}})))
(defn form-content [{:keys [data change-event selected-override]}]
(defn default-with-overrides [{:keys [override-key override-value-key change-event default-key data]} template]
(let [clients @(re-frame/subscribe [::subs/clients])]
[:div
[horizontal-field
[:label.label "Default"]
[bind-field
(assoc-in template [1 :field ] default-key)]]
[horizontal-field
[:label.label "Overrides"]
(for [[i overrides] (map vector (range) (conj (override-key data) {}))]
^{:key i}
[:div
[:div.columns
[:div.column
[bind-field
[typeahead-entity {:matches clients
:match->text :name
:type "typeahead-entity"
:field [override-key i :client]
:event change-event
:subscription data}]]]
[:div.column
[bind-field
(assoc-in template [1 :field ] [override-key i :override])]]]])]]))
(defn form-content [{:keys [data change-event]}]
(let [chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
root-path (cond-> [] selected-override (into [:overrides selected-override]))]
clients @(re-frame/subscribe [::subs/clients])]
[:div
[horizontal-field
[:label.label "Name"]
@@ -96,7 +140,7 @@
[bind-field
[:input.input {:type "text"
:auto-focus true
:field (conj root-path :name)
:field :name
:spec ::entity/name
:event change-event
:subscription data}]]]]
@@ -106,46 +150,44 @@
[:div.control
[bind-field
[:input.input {:type "text"
:field (conj root-path :print-as)
:field :print-as
:spec ::entity/print-as
:event change-event
:subscription data}]]]]
[horizontal-field
[:label.label "Terms"]
[:div.control
[bind-field
[:input.input {:type "number"
:step "1"
:field (conj root-path :terms)
:spec ::entity/terms
:event change-event
:subscription data}]]]]
[horizontal-field
[:label.label "Hidden"]
[:div.control
[bind-field
[:input {:type "checkbox"
:field (conj root-path :hidden)
:field :hidden
:spec ::entity/hidden
:event change-event
:subscription data}]]]]
[:h2.subtitle "Terms"]
[default-with-overrides {:data data :change-event change-event
:default-key :terms
:override-key :terms-overrides}
[:input.input {:type "number"
:step "1"
:style {:width "4em"}
:size 3
:spec ::entity/terms
:event change-event
:subscription data}]]
[:h2.subtitle "Expense Accounts"]
[horizontal-field
[:label.label "Default"]
[bind-field
[typeahead-entity {:matches chooseable-expense-accounts
:match->text (fn [x ] (str (:numeric-code x) " - " (:name x)))
:type "typeahead-entity"
:field (conj root-path :default-account)
:event change-event
:subscription data}]]]
[default-with-overrides {:data data :change-event change-event
:default-key :default-account
:override-key :account-overrides}
[typeahead-entity {:matches chooseable-expense-accounts
:match->text (fn [x ] (str (:numeric-code x) " - " (:name x)))
:type "typeahead-entity"
:event change-event
:subscription data}]]
[:h2.subtitle "Address"]
[address-field {:field (conj root-path :address)
[address-field {:field [:address]
:event change-event
:subscription data}]
[:h2.subtitle "Contact"]
@@ -154,7 +196,7 @@
[:div.control.has-icons-left
[bind-field
[:input.input.is-expanded {:type "text"
:field (into root-path [:primary-contact :name])
:field [:primary-contact :name]
:spec ::contact/name
:event change-event
:subscription data}]]
@@ -166,7 +208,7 @@
[:i.fa.fa-envelope]]
[bind-field
[:input.input {:type "email"
:field (into root-path [:primary-contact :email])
:field [:primary-contact :email]
:spec ::contact/email
:event change-event
:subscription data}]]]
@@ -174,7 +216,7 @@
[:div.control.has-icons-left
[bind-field
[:input.input {:type "phone"
:field (into root-path [:primary-contact :phone])
:field [:primary-contact :phone]
:spec ::contact/phone
:event change-event
:subscription data}]]
@@ -185,7 +227,7 @@
[:div.control.has-icons-left
[bind-field
[:input.input.is-expanded {:type "text"
:field (into root-path [:secondary-contact :name])
:field [:secondary-contact :name]
:spec ::contact/name
:event change-event
:subscription data}]]
@@ -196,14 +238,14 @@
[:i.fa.fa-envelope]]
[bind-field
[:input.input {:type "email"
:field (into root-path [:secondary-contact :email])
:field [:secondary-contact :email]
:spec ::contact/email
:event change-event
:subscription data}]]]
[:div.control.has-icons-left
[bind-field
[:input.input {:type "phone"
:field (into root-path [:secondary-contact :phone])
:field [:secondary-contact :phone]
:spec ::contact/phone
:event change-event
:subscription data}]]
@@ -217,7 +259,7 @@
[:input {:type "radio"
:name "schedule"
:value "Weekly"
:field (conj root-path :invoice-reminder-schedule)
:field :invoice-reminder-schedule
:spec ::entity/invoice-reminder-schedule
:event change-event
:subscription data}]]
@@ -228,7 +270,7 @@
[:input {:type "radio"
:name "schedule"
:value "Never"
:field (conj root-path :invoice-reminder-schedule)
:field :invoice-reminder-schedule
:spec ::entity/invoice-reminder-schedule
:event change-event
:subscription data}]]
@@ -237,7 +279,6 @@
(defn vendor-dialog [{:keys [save-event] }]
(let [clients @(re-frame/subscribe [::subs/clients])
all-vendors @(re-frame/subscribe [::subs/vendors])
selected-override @(re-frame/subscribe [::selected-override])
{:keys [data error ] :as f} @(re-frame/subscribe [::forms/form ::vendor-form])
change-event [::forms/change ::vendor-form]]
[action-modal {:id ::dialog
@@ -255,32 +296,5 @@
:save-event [::save]
:can-submit? (s/valid? ::entity/vendor data)}
[:div
[:div.level
[:div.level-left
[:div.tabs
[:ul
[:li {:class (active-when selected-override = nil)}
[:a {:on-click (dispatch-event [::override-selected nil])} "All"]]
[:li {:class (active-when selected-override = "123")}
[:a {:on-click (dispatch-event [::override-selected "123"])}
[:span "Campbell Brewing Company"]]]
]]]
[:div.level-right
[drop-down {:id [::add-new-override]
:is-right? true
:header [:a.button.badge {:aria-haspopup true
:on-click (dispatch-event [::events/toggle-menu [::add-new-override]])}
[:span.icon
[:span.icon-add]]
[:span "Override"]]}
[drop-down-contents
[:div
[:div.field
[:label.label "Client"]
[typeahead-entity {:matches clients
:match->text :name
:type "typeahead-entity"
:event [:override-added]}]]]]]]]
[form-content {:data data :change-event change-event
:selected-override selected-override}]]]))
[form-content {:data data :change-event change-event}]]]))