mirror of
https://github.com/mruwnik/chicken-master.git
synced 2025-06-08 13:24:42 +02:00
empty price marker
This commit is contained in:
parent
06059b830d
commit
ccf3214aba
@ -18,7 +18,7 @@
|
|||||||
(into {}))))
|
(into {}))))
|
||||||
|
|
||||||
(defn- update-product [tx user-id prod values]
|
(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 ", "))
|
cols (->> to-update (map name) (str/join ", "))
|
||||||
params (concat [(name prod) user-id] (map values to-update))
|
params (concat [(name prod) user-id] (map values to-update))
|
||||||
updates (->> to-update
|
updates (->> to-update
|
||||||
|
@ -73,6 +73,21 @@
|
|||||||
"ON CONFLICT (name, user_id) DO UPDATE SET "
|
"ON CONFLICT (name, user_id) DO UPDATE SET "
|
||||||
"deleted = NULL, amount = EXCLUDED.amount") "milk" :user-id 3]])))))
|
"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"
|
(testing "non selected items get removed"
|
||||||
(let [updates (atom [])]
|
(let [updates (atom [])]
|
||||||
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
(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}})
|
(sut/update! :user-id {:eggs {:amount 2} :milk {:amount 3} :cows {:amount 2}})
|
||||||
(is (= @updates [{} :products {:deleted true} ["name NOT IN (?, ?, ?)" "eggs" "milk" "cows"]])))))
|
(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))
|
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||||
jdbc/execute! (constantly nil)
|
jdbc/execute! (constantly nil)
|
||||||
sql/update! (constantly nil)
|
sql/update! (constantly nil)
|
||||||
@ -102,7 +117,7 @@
|
|||||||
(is (= id {:bla_id item-id})))]
|
(is (= id {:bla_id item-id})))]
|
||||||
(sut/update-products-mapping! :tx 123 :bla item-id {:eggs 34 :milk 25 :carrots 13}))))
|
(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]
|
(let [item-id 123]
|
||||||
(with-redefs [sut/products-map (constantly {"eggs" 1 "milk" 2 "carrots" 3})
|
(with-redefs [sut/products-map (constantly {"eggs" 1 "milk" 2 "carrots" 3})
|
||||||
sql/delete! (constantly :ok)
|
sql/delete! (constantly :ok)
|
||||||
|
@ -34,6 +34,11 @@
|
|||||||
(prod/calc-price (:id who) prod price amount))) products)
|
(prod/calc-price (:id who) prod price amount))) products)
|
||||||
(assoc order :products)))
|
(assoc order :products)))
|
||||||
|
|
||||||
|
(defn merge-product-values [& products]
|
||||||
|
(apply merge-with
|
||||||
|
(fn [& values] (some->> values (remove nil?) seq (reduce +)))
|
||||||
|
products))
|
||||||
|
|
||||||
(defn order-form
|
(defn order-form
|
||||||
([order] (order-form order #{:who :day :notes :products :group-products}))
|
([order] (order-form order #{:who :day :notes :products :group-products}))
|
||||||
([order fields]
|
([order fields]
|
||||||
@ -126,7 +131,7 @@
|
|||||||
[:div {:class :header} "w sumie:"]
|
[:div {:class :header} "w sumie:"]
|
||||||
(->> orders
|
(->> orders
|
||||||
(map :products)
|
(map :products)
|
||||||
(apply merge-with (partial merge-with +))
|
(apply merge-with merge-product-values)
|
||||||
(sort-by first)
|
(sort-by first)
|
||||||
(map (partial prod/format-product settings))
|
(map (partial prod/format-product settings))
|
||||||
(into [:div {:class :products-sum}]))])]]]))
|
(into [:div {:class :products-sum}]))])]]]))
|
||||||
|
@ -32,7 +32,8 @@
|
|||||||
:editable-number-inputs (get-setting :editable-number-inputs false) ; only allow number modifications in the edit modal
|
: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)
|
: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
|
:backend-url (get-setting :backend-url
|
||||||
(if (= (.. js/window -location -href) "http://localhost:8280/")
|
(if (= (.. js/window -location -href) "http://localhost:8280/")
|
||||||
@ -97,6 +98,7 @@
|
|||||||
|
|
||||||
[:h3 "Ustawienia magazynu"]
|
[:h3 "Ustawienia magazynu"]
|
||||||
(input :prices "pokaż ceny" {:type :checkbox})
|
(input :prices "pokaż ceny" {:type :checkbox})
|
||||||
|
(input :empty-price-marker "co wyświetlić jezeli nie ma ceny" {})
|
||||||
|
|
||||||
[:h3 "Ustawienia tyłu"]
|
[:h3 "Ustawienia tyłu"]
|
||||||
(input :backend-url "backend URL" {})
|
(input :backend-url "backend URL" {})
|
||||||
|
@ -11,8 +11,9 @@
|
|||||||
(when-not (js/isNaN i) i)))
|
(when-not (js/isNaN i) i)))
|
||||||
|
|
||||||
(defn round [num digits]
|
(defn round [num digits]
|
||||||
(let [div (js/Math.pow 10 digits)]
|
(when num
|
||||||
(/ (js/Math.round (* num div)) div)))
|
(let [div (js/Math.pow 10 digits)]
|
||||||
|
(/ (js/Math.round (* num div)) div))))
|
||||||
|
|
||||||
(defn format-price [price] (when price (round (/ price 100) 2)))
|
(defn format-price [price] (when price (round (/ price 100) 2)))
|
||||||
(defn normalise-price [price] (when price (round (* price 100) 0)))
|
(defn normalise-price [price] (when price (round (* price 100) 0)))
|
||||||
@ -100,7 +101,7 @@
|
|||||||
[:span {:class :product-amount} amount])
|
[:span {:class :product-amount} amount])
|
||||||
(when (settings :prices)
|
(when (settings :prices)
|
||||||
[:span {:class :product-price}
|
[: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]
|
(defn item-adder [& {:keys [type value callback button class]
|
||||||
:or {type :text value "" button nil}}]
|
:or {type :text value "" button nil}}]
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#(swap! state assoc-in
|
#(swap! state assoc-in
|
||||||
[product :price]
|
[product :price]
|
||||||
(some-> % .-target .-value prod/num-or-nil prod/normalise-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]
|
(defn process-form [form]
|
||||||
(->> form
|
(->> form
|
||||||
@ -47,4 +47,4 @@
|
|||||||
[:h2 "Magazyn"]
|
[:h2 "Magazyn"]
|
||||||
[stock-form @(re-frame/subscribe [::subs/available-products])]]
|
[stock-form @(re-frame/subscribe [::subs/available-products])]]
|
||||||
;; On success
|
;; 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)]))))
|
||||||
|
@ -52,6 +52,35 @@
|
|||||||
:milk {:amount 3 :price 5 :final-price 15}
|
:milk {:amount 3 :price 5 :final-price 15}
|
||||||
:carrots {:amount 6 :final-price nil}})))))))
|
: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
|
(deftest format-raw-order-test
|
||||||
(testing "no products"
|
(testing "no products"
|
||||||
(is (= (sut/format-raw-order {}) {:who {:name nil :id nil} :day nil :notes nil :products {}}))
|
(is (= (sut/format-raw-order {}) {:who {:name nil :id nil} :day nil :notes nil :products {}}))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user