empty price marker

This commit is contained in:
Daniel O'Connell 2021-03-27 18:10:05 +01:00
parent 06059b830d
commit ccf3214aba
7 changed files with 62 additions and 10 deletions

View File

@ -18,7 +18,7 @@
(into {}))))
(defn- update-product [tx user-id prod values]
(let [to-update (seq (filter values [:amount :price]))
(let [to-update (seq (filter (partial contains? values) [:amount :price]))
cols (->> to-update (map name) (str/join ", "))
params (concat [(name prod) user-id] (map values to-update))
updates (->> to-update

View File

@ -73,6 +73,21 @@
"ON CONFLICT (name, user_id) DO UPDATE SET "
"deleted = NULL, amount = EXCLUDED.amount") "milk" :user-id 3]])))))
(testing "empty fields aren't ignored"
(let [inserts (atom [])]
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
jdbc/execute! #(swap! inserts conj %2)
sql/update! (constantly nil)
sql/query (constantly [])]
(sut/update! :user-id {:eggs {:amount 2} :milk {:amount 3 :price nil} :cows {}})
(is (= (sort-by second @inserts)
[[(str "INSERT INTO products (name, user_id, amount) VALUES(?, ?, ?) "
"ON CONFLICT (name, user_id) DO UPDATE "
"SET deleted = NULL, amount = EXCLUDED.amount") "eggs" :user-id 2]
[(str "INSERT INTO products (name, user_id, amount, price) VALUES(?, ?, ?, ?) "
"ON CONFLICT (name, user_id) DO UPDATE SET "
"deleted = NULL, amount = EXCLUDED.amount, price = EXCLUDED.price") "milk" :user-id 3 nil]])))))
(testing "non selected items get removed"
(let [updates (atom [])]
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
@ -82,7 +97,7 @@
(sut/update! :user-id {:eggs {:amount 2} :milk {:amount 3} :cows {:amount 2}})
(is (= @updates [{} :products {:deleted true} ["name NOT IN (?, ?, ?)" "eggs" "milk" "cows"]])))))
(testing "non selected items get removed"
(testing "products get returned"
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
jdbc/execute! (constantly nil)
sql/update! (constantly nil)
@ -102,7 +117,7 @@
(is (= id {:bla_id item-id})))]
(sut/update-products-mapping! :tx 123 :bla item-id {:eggs 34 :milk 25 :carrots 13}))))
(testing "items get removed"
(testing "items get added"
(let [item-id 123]
(with-redefs [sut/products-map (constantly {"eggs" 1 "milk" 2 "carrots" 3})
sql/delete! (constantly :ok)

View File

@ -34,6 +34,11 @@
(prod/calc-price (:id who) prod price amount))) products)
(assoc order :products)))
(defn merge-product-values [& products]
(apply merge-with
(fn [& values] (some->> values (remove nil?) seq (reduce +)))
products))
(defn order-form
([order] (order-form order #{:who :day :notes :products :group-products}))
([order fields]
@ -126,7 +131,7 @@
[:div {:class :header} "w sumie:"]
(->> orders
(map :products)
(apply merge-with (partial merge-with +))
(apply merge-with merge-product-values)
(sort-by first)
(map (partial prod/format-product settings))
(into [:div {:class :products-sum}]))])]]]))

View File

@ -32,7 +32,8 @@
:editable-number-inputs (get-setting :editable-number-inputs false) ; only allow number modifications in the edit modal
:hide-fulfilled-orders (get-setting :hide-fulfilled-orders false)
:prices (get-setting :prices true)
:prices (get-setting :prices false)
:empty-price-marker (get-setting :empty-price-marker "-")
:backend-url (get-setting :backend-url
(if (= (.. js/window -location -href) "http://localhost:8280/")
@ -97,6 +98,7 @@
[:h3 "Ustawienia magazynu"]
(input :prices "pokaż ceny" {:type :checkbox})
(input :empty-price-marker "co wyświetlić jezeli nie ma ceny" {})
[:h3 "Ustawienia tyłu"]
(input :backend-url "backend URL" {})

View File

@ -11,8 +11,9 @@
(when-not (js/isNaN i) i)))
(defn round [num digits]
(when num
(let [div (js/Math.pow 10 digits)]
(/ (js/Math.round (* num div)) div)))
(/ (js/Math.round (* num div)) div))))
(defn format-price [price] (when price (round (/ price 100) 2)))
(defn normalise-price [price] (when price (round (* price 100) 0)))
@ -100,7 +101,7 @@
[:span {:class :product-amount} amount])
(when (settings :prices)
[:span {:class :product-price}
(format-price final-price)])])
(or (format-price final-price) (settings :empty-price-marker))])])
(defn item-adder [& {:keys [type value callback button class]
:or {type :text value "" button nil}}]

View File

@ -29,7 +29,7 @@
#(swap! state assoc-in
[product :price]
(some-> % .-target .-value prod/num-or-nil prod/normalise-price)))])]))
[prod/item-adder :callback #(swap! state assoc (keyword %) 0) :button "+"]])))
[prod/item-adder :callback #(swap! state assoc (keyword %) {:amount 0}) :button "+"]])))
(defn process-form [form]
(->> form
@ -47,4 +47,4 @@
[:h2 "Magazyn"]
[stock-form @(re-frame/subscribe [::subs/available-products])]]
;; On success
:on-submit (fn [form] (re-frame/dispatch [::event/save-stock (process-form form)]))))
:on-submit (fn [form] (prn form) (prn (process-form form)) (re-frame/dispatch [::event/save-stock (process-form form)]))))

View File

@ -52,6 +52,35 @@
:milk {:amount 3 :price 5 :final-price 15}
:carrots {:amount 6 :final-price nil}})))))))
(deftest merge-product-values-test
(testing "single item"
(is (= (sut/merge-product-values {:amount 23, :price 32, :final-price 1})
{:amount 23, :price 32, :final-price 1})))
(testing "items with price"
(is (= (sut/merge-product-values {:amount 23, :price 2, :final-price 3}
{:amount 45, :price 1, :final-price 4})
{:amount 68, :price 3, :final-price 7})))
(testing "items without prices"
(is (= (sut/merge-product-values {:amount 23, :price nil, :final-price nil}
{:amount 45, :price nil, :final-price nil})
{:amount 68, :price nil, :final-price nil})))
(testing "items with mixed prices"
(is (= (sut/merge-product-values {:amount 23, :price 2, :final-price 3}
{:amount 45, :price nil, :final-price nil})
{:amount 68, :price 2, :final-price 3})))
(testing "multiple items"
(is (= (sut/merge-product-values {:amount 6, :price 7, :final-price 3}
{:amount 5, :price 2, :final-price 4}
{:amount 4, :price nil, :final-price 5}
{:amount 3, :price 4, :final-price nil}
{:amount 2, :price 5, :final-price 7}
{:amount 1})
{:amount 21, :price 18, :final-price 19}))))
(deftest format-raw-order-test
(testing "no products"
(is (= (sut/format-raw-order {}) {:who {:name nil :id nil} :day nil :notes nil :products {}}))