Supports editing expense accounts

This commit is contained in:
Bryce Covert
2019-04-12 12:00:42 -07:00
parent bdb06d802b
commit e64820d71a
6 changed files with 119 additions and 53 deletions

View File

@@ -8,14 +8,31 @@
(->graphql (d-accounts/get-accounts (<-graphql args)))) (->graphql (d-accounts/get-accounts (<-graphql args))))
(defn upsert-account [context args value] (defn upsert-account [context args value]
@(d/transact (d/connect uri) (let [{{:keys [id numeric-code account-set name type]} :account} (<-graphql args)]
[(remove-nils (when-not id
{:db/id (:id (:account args)) (when (seq (d/query {:query {:find ['?e]
:account/name (:name (:account args)) :in '[$ ?account-set ?numeric-code]
:account/type (keyword "account-type" (name (:type (:account args)))) :where ['[?e :account/account-set ?account-set]
:account/account-set (:account_set (:account args)) '[?e :account/numeric-code ?numeric-code]]}
:account/numeric-code (if-not (:id (:account args)) :args [(d/db (d/connect uri)) account-set numeric-code]}))
(:numeric_code (:account args)))
})]) (throw (ex-info (str "Account set " account-set " already has an account for code " numeric-code)
(:account args)) {} ))))
(let [
result @(d/transact (d/connect uri)
[(remove-nils
{:db/id (or id "new-account")
:account/name name
:account/type (keyword "account-type" (clojure.core/name type))
:account/account-set account-set
:account/numeric-code (if-not id
numeric-code)
:account/code (if-not id
(str numeric-code))})])]
(->graphql
(if id
(:account args)
(assoc (:account args)
:id (get-in result [:tempids "new-account"])))))))

View File

@@ -0,0 +1,16 @@
(ns auto-ap.entities.account
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[auto-ap.entities.shared :as shared]))
(def numeric-regex #"^[0-9]+$")
(s/def ::account-set string?)
(s/def ::numeric-code (s/or :numericstring? (s/and string?
#(re-matches numeric-regex %))
:numeric? int?))
(s/def ::name string?)
(s/def ::type #{:dividend :expense :asset :liability :equities :revenue})
(s/def ::account (s/keys :req-un [::account-set ::numeric-code ::name ::type]))

View File

@@ -19,12 +19,18 @@
existing)) existing))
(defn replace-by [xs f x] (defn replace-by [xs f x]
(mapv (let [found? (atom false)
(fn [t] replaced (mapv
(if (= (f t) (f x)) (fn [t]
x (if (= (f t) (f x))
t)) (do (reset! found? true)
xs)) (println "found" (f t) t (f x) x)
x)
t))
xs)]
(if @found?
replaced
(conj replaced x))))
(defn dollars-0? [amt] (defn dollars-0? [amt]
(< -0.001 amt 0.001)) (< -0.001 amt 0.001))

View File

@@ -29,13 +29,17 @@
:id (random-uuid) :id (random-uuid)
:status nil :status nil
:data data})) :data data}))
(defn saved-form [db form data]
(update-in db [::forms form]
assoc :error nil :status nil :data data))
(defn stop-form [db form] (defn stop-form [db form]
(update db ::forms dissoc form)) (update db ::forms dissoc form))
(re-frame/reg-event-db (re-frame/reg-event-db
::form-closing ::form-closing
(fn [db [_ f]] (fn [db [_ f]]
(-> db (-> db
(stop-form f)))) (stop-form f))))
@@ -56,7 +60,6 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::save-error ::save-error
(fn [db [_ form result]] (fn [db [_ form result]]
(-> db (-> db
(assoc-in [::forms form :status] :error) (assoc-in [::forms form :status] :error)
(assoc-in [::forms form :error] (or (:message (first result)) (assoc-in [::forms form :error] (or (:message (first result))

View File

@@ -16,35 +16,36 @@
(-> db (-> db
(update :accounts replace-by :id edit-account)))) (update :accounts replace-by :id edit-account))))
(defn accounts-table [{:keys [accounts]} ] (defn accounts-table [{:keys [accounts]}]
[:div [:div
(for [[account-set accounts] (group-by :account-set accounts)] (for [[account-set accounts] (group-by :account-set accounts)]
(do (println accounts) ^{:key (or account-set "blank")}
^{:key account-set} [:div
[:div [:h2.title.is-4 account-set]
[:h2.title.is-4 account-set] [:table.table.compact.is-fullwidth
[:table.table.compact [:thead
[:thead [:tr
[:th "Code"] [:th "Code"]
[:th "Name"] [:th "Name"]
[:th "Type"] [:th "Type"]
[:th {:style {:width "5em"}}] [:th {:style {:width "5em"}}]]]
] [:tbody
[:tbody (for [{:keys [id numeric-code name type] :as account} (sort-by :numeric-code accounts)]
(for [{:keys [id numeric-code name type] :as account} (sort-by :numeric-code accounts)] ^{:key id}
^{:key id} [:tr
[:tr [:td numeric-code]
[:td numeric-code] [:td name]
[:td name] [:td type]
[:td type] [:td [:a.button {:on-click (dispatch-event [::account-form/editing account])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]])])
[:td [:a.button {:on-click (dispatch-event [::account-form/editing account])} [:span [:span.icon [:i.fa.fa-pencil]]]]]])]]]))])
(defn admin-accounts-content [] (defn admin-accounts-content []
[:div [:div
(let [accounts @(re-frame/subscribe [::subs/accounts])] (let [accounts @(re-frame/subscribe [::subs/accounts])]
[:div [:div
[:h1.title "Accounts"] [:h1.title "Accounts"]
[:div.is-pulled-right
[:a.button.is-success {:on-click (dispatch-event [::account-form/editing {:type :asset
:account-set "default"}])} "New Account"]]
[accounts-table {:accounts accounts}]])]) [accounts-table {:accounts accounts}]])])
(defn admin-accounts-page [] (defn admin-accounts-page []

View File

@@ -1,6 +1,8 @@
(ns auto-ap.views.pages.admin.accounts.form (ns auto-ap.views.pages.admin.accounts.form
(:require [auto-ap.forms :as forms] (:require [auto-ap.forms :as forms]
[clojure.string :as str] [clojure.string :as str]
[clojure.spec.alpha :as s]
[auto-ap.entities.account :as entity]
[auto-ap.views.utils :refer [bind-field dispatch-event]] [auto-ap.views.utils :refer [bind-field dispatch-event]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
@@ -11,6 +13,16 @@
(fn [db] (fn [db]
true)) true))
(re-frame/reg-sub
::account-request
:<- [::forms/form ::form]
(fn [{{:keys [id type numeric-code name account-set]} :data}]
{:id id
:type (keyword type)
:numeric-code numeric-code
:name name
:account-set account-set}))
(re-frame/reg-event-db (re-frame/reg-event-db
::editing ::editing
(fn [db [_ which]] (fn [db [_ which]]
@@ -20,8 +32,7 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::edited ::edited
(fn [{:keys [db]} [_ edit-completed {:keys [upsert-account]}]] (fn [{:keys [db]} [_ edit-completed {:keys [upsert-account]}]]
{:db (-> db {:db (-> db (forms/saved-form ::form upsert-account))
(forms/stop-form ::form))
:dispatch (conj edit-completed upsert-account)})) :dispatch (conj edit-completed upsert-account)}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -35,11 +46,7 @@
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "UpsertAccount"} :operation/name "UpsertAccount"}
:venia/queries [{:query/data [:upsert-account :venia/queries [{:query/data [:upsert-account
{:account {:id id {:account @(re-frame/subscribe [::account-request])}
:type (keyword type)
:numeric-code numeric-code
:name name
:account-set account-set}}
[:id :type :name :account-set :numeric-code]]}]} [:id :type :name :account-set :numeric-code]]}]}
:on-success [::edited edit-completed] :on-success [::edited edit-completed]
:on-error [::forms/save-error ::form]}})))) :on-error [::forms/save-error ::form]}}))))
@@ -50,7 +57,23 @@
[forms/side-bar-form {:form ::form} [forms/side-bar-form {:form ::form}
[:form [:form
[:h1.title.is-2 "Add/edit account"] [:h1.title.is-2 (if (:id account)
"Edit account"
"Add account")]
[:div.field
[:p.help "Account Set"]
[:div.control
[bind-field
[:input.input {:type "text"
:field :account-set
:disabled (if (:id account)
"disabled"
"")
:spec ::entity/account-set
:event change-event
:subscription account}]]]]
[:div.field [:div.field
[:p.help "Code"] [:p.help "Code"]
[:div.control [:div.control
@@ -60,7 +83,7 @@
:disabled (if (:id account) :disabled (if (:id account)
"disabled" "disabled"
"") "")
#_:spec #_:entity/name :spec ::entity/numeric-code
:event change-event :event change-event
:subscription account}]]]] :subscription account}]]]]
@@ -70,12 +93,14 @@
[bind-field [bind-field
[:input.input {:type "text" [:input.input {:type "text"
:field :name :field :name
#_:spec #_:entity/name :spec ::entity/name
:event change-event :event change-event
:subscription account}]]]] :subscription account}]]]]
[:div.field [:div.field
[:p.help "Location"] [:p.help "Account Type"]
[:div.control [:div.control
[:div.select [:div.select
[bind-field [bind-field
@@ -85,15 +110,13 @@
:event change-event :event change-event
:subscription account} :subscription account}
(map (fn [l] (map (fn [l]
[:option {:value (name l)} (str/capitalize (name l))]) types)]]]]] [:option {:value (name l)} (str/capitalize (name l))]) types)]]]]]
(when error (when error
[:div.notification.is-warning.animated.fadeInUp [:div.notification.is-warning.animated.fadeInUp
error]) error])
[:button.button.is-large.is-primary {:disabled (if (s/valid? ::entity/account @(re-frame/subscribe [::account-request]))
[:submit.button.is-large.is-primary {#_:disabled #_(if (s/valid? :entity/client @(re-frame/subscribe [::new-client-request]))
"" ""
"disabled") "disabled")
:on-click (dispatch-event [::saving edit-completed]) :on-click (dispatch-event [::saving edit-completed])