percentage-based splitting.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
(:require [auto-ap.forms :as forms]
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.views.components.typeahead :refer [typeahead]]
|
||||
[auto-ap.views.utils :refer [bind-field dispatch-event]]
|
||||
[auto-ap.views.utils :refer [bind-field dispatch-event ->$]]
|
||||
[goog.string :as gstring]
|
||||
[re-frame.core :as re-frame]
|
||||
[clojure.string :as str]))
|
||||
@@ -14,7 +14,9 @@
|
||||
::add-expense-account
|
||||
(fn [_ [_ event expense-accounts]]
|
||||
{:dispatch (conj event (conj expense-accounts
|
||||
{:amount 0 :id (str "new-" (random-uuid))}))}))
|
||||
{:amount 0 :id (str "new-" (random-uuid))
|
||||
:amount-mode "%"
|
||||
:amount-percentage 0}))}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::remove-expense-account
|
||||
@@ -26,13 +28,30 @@
|
||||
[]
|
||||
expense-accounts))}))
|
||||
|
||||
(defn recalculate-amounts [expense-accounts total]
|
||||
(mapv
|
||||
(fn [ea]
|
||||
(assoc ea :amount
|
||||
(js/parseFloat
|
||||
(goog.string/format "%.2f"
|
||||
(* (/ (js/parseFloat (:amount-percentage ea)) 100.0) (js/parseFloat total))))))
|
||||
expense-accounts))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::expense-account-changed
|
||||
(fn [_ [_ event expense-accounts field value]]
|
||||
(fn [_ [_ event expense-accounts max-value field value]]
|
||||
(let [updated-accounts (cond-> expense-accounts
|
||||
true (assoc-in field value)
|
||||
(= (list :account :id) (drop 1 field)) (assoc-in [(first field) :account] @(re-frame/subscribe [::subs/account value]))
|
||||
)
|
||||
(= (list :amount-percentage) (drop 1 field)) (assoc-in [(first field) :amount]
|
||||
(js/parseFloat
|
||||
(goog.string/format "%.2f"
|
||||
(do
|
||||
(println value max-value)
|
||||
(* (/ (cond-> value
|
||||
(not (float? value)) (js/parseFloat )) 100.0)
|
||||
(cond-> max-value
|
||||
(not (float? max-value)) (js/parseFloat))))))))
|
||||
updated-accounts (if-let [location (get-in updated-accounts [(first field) :account :location])]
|
||||
(assoc-in updated-accounts [(first field) :location] location)
|
||||
updated-accounts)]
|
||||
@@ -43,16 +62,18 @@
|
||||
(defn expense-accounts-field [{expense-accounts :value max-value :max locations :locations event :event descriptor :descriptor}]
|
||||
(let [chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
|
||||
accounts-by-id @(re-frame/subscribe [::subs/accounts-for-client-by-id])]
|
||||
(println expense-accounts)
|
||||
|
||||
[:div
|
||||
[:div.columns
|
||||
[:div.column
|
||||
[:h1.subtitle.is-4.is-inline (str/capitalize descriptor) "s"]]
|
||||
[:h1.subtitle.is-4.is-inline (str/capitalize descriptor) "s"]
|
||||
[:p.help "Remaining " (->$ (- max-value (reduce + 0 (map (comp js/parseFloat :amount) expense-accounts))))]]
|
||||
[:div.column.is-narrow
|
||||
[:p.buttons
|
||||
[:a.button {:on-click (dispatch-event [::add-expense-account event expense-accounts])} "Add"]]]]
|
||||
|
||||
(for [[index {:keys [account id location amount] :as expense-account}] (map vector (range) expense-accounts)
|
||||
(for [[index {:keys [account id location amount amount-mode] :as expense-account}] (map vector (range) expense-accounts)
|
||||
:let [account (accounts-by-id (:id account))]]
|
||||
^{:key id}
|
||||
[:div.box
|
||||
@@ -74,7 +95,9 @@
|
||||
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) chooseable-expense-accounts)
|
||||
:type "typeahead"
|
||||
:field [index :account :id]
|
||||
:event [::expense-account-changed event expense-accounts]
|
||||
#_#_:text-field [index :account :name]
|
||||
|
||||
:event [::expense-account-changed event expense-accounts max-value]
|
||||
:subscription expense-accounts}]]]]
|
||||
[:div.column.is-narrow
|
||||
[:p.help "Location"]
|
||||
@@ -92,7 +115,7 @@
|
||||
:field [index :location]
|
||||
:allow-nil? true
|
||||
:spec (set locations)
|
||||
:event [::expense-account-changed event expense-accounts]
|
||||
:event [::expense-account-changed event expense-accounts max-value]
|
||||
:subscription expense-accounts}
|
||||
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]]]]
|
||||
|
||||
@@ -101,14 +124,34 @@
|
||||
[:p.help "Amount"]
|
||||
[:div.control
|
||||
[:div.field.has-addons.is-extended
|
||||
[:p.control [:a.button.is-static "$"]]
|
||||
[:p.control [:span.select
|
||||
[bind-field
|
||||
[:select {:type "select"
|
||||
:field [index :amount-mode]
|
||||
:allow-nil? false
|
||||
:event [::expense-account-changed event expense-accounts max-value]
|
||||
:subscription expense-accounts}
|
||||
[:option "$"]
|
||||
[:option "%"]]]]]
|
||||
[:p.control
|
||||
[bind-field
|
||||
[:input.input {:type "number"
|
||||
:field [index :amount]
|
||||
:style {:text-align "right" :width "7em"}
|
||||
:event [::expense-account-changed event expense-accounts]
|
||||
:subscription expense-accounts
|
||||
:value (get-in expense-account [:amount])
|
||||
:max max-value
|
||||
:step "0.01"}]]]]]]])]))
|
||||
(if (= "$" amount-mode)
|
||||
[bind-field
|
||||
[:input.input {:type "number"
|
||||
:field [index :amount]
|
||||
:style {:text-align "right" :width "7em"}
|
||||
:event [::expense-account-changed event expense-accounts max-value]
|
||||
:subscription expense-accounts
|
||||
:precision 2
|
||||
:value (get-in expense-account [:amount])
|
||||
:max max-value
|
||||
:step "0.01"}]]
|
||||
[bind-field
|
||||
[:input.input {:type "number"
|
||||
:field [index :amount-percentage]
|
||||
:style {:text-align "right" :width "7em"}
|
||||
:event [::expense-account-changed event expense-accounts max-value]
|
||||
:precision 2
|
||||
:subscription expense-accounts
|
||||
:value (get-in expense-account [:amount-percentage])
|
||||
:max "100"
|
||||
:step "0.01"}]])]]]]])]))
|
||||
|
||||
Reference in New Issue
Block a user