diff --git a/backend/src/chicken_master/orders.clj b/backend/src/chicken_master/orders.clj index 01fdb23..28bfda1 100644 --- a/backend/src/chicken_master/orders.clj +++ b/backend/src/chicken_master/orders.clj @@ -97,17 +97,6 @@ :else ; single item modified (update-single-item tx user-id order-date order updates)))) -;; (db/get-by-id db/db-uri 2 :orders 267) -;; (upsert-order! db/db-uri 2 15 {:notes "no recur1" :day "2022-04-20"}) -;; (upsert-order! db/db-uri 2 15 {:notes "recur2" :day "2022-04-18" :recurrence "FREQ=DAILY;COUNT=10"}) -;; (upsert-order! db/db-uri 2 15 {:update-type :all :id 279 :notes "recur2" :day "2022-04-18" :recurrence "FREQ=DAILY;COUNT=10"}) -;; (upsert-order! db/db-uri 2 15 {:id 267 :notes "main-default" :day "2022-04-18" :order-date "2022-04-18"}) -;; (upsert-order! db/db-uri 2 15 {:id 267 :notes "main" :day "2022-04-18"}) -;; (upsert-order! db/db-uri 2 15 {:id 267 :notes "main-all" :update-type :all :day "2022-04-18" :order-date "2022-04-18"}) -;; (upsert-order! db/db-uri 2 15 {:id 267 :notes "main-from" :update-type :from-here :day "2022-04-18" :order-date "2022-04-22"}) -;; (upsert-order! db/db-uri 2 15 {:id 267 :notes "12323" :update-type :single :day "2022-04-18" :order-date "2022-04-18"}) -;; (upsert-order! db/db-uri 2 15 {:id 260 :notes "1dsf2a" :update-type :single :day "2022-04-19"}) - (defn structure-order [items] {:id (-> items first :orders/id) :notes (-> items first :orders/notes) @@ -184,14 +173,15 @@ (group-by :day) (merge (reduce #(assoc %1 (t/format-date %2) {}) {} days))))) -(defn replace! [user-id {:keys [who products day order-date] :as order}] - (jdbc/with-transaction [tx db/db-uri] - (let [customer-id (or (:id who) - (customers/get-or-create-by-name tx user-id (:name who)))] - (products/update-products-mapping! tx user-id :order - (upsert-order! tx user-id customer-id order) - products) - (orders-for-days tx user-id day order-date)))) +(defn replace! + ([user-id order] (jdbc/with-transaction [tx db/db-uri] (replace! tx user-id order))) + ([tx user-id {:keys [who products day order-date] :as order}] + (let [customer-id (or (:id who) + (customers/get-or-create-by-name tx user-id (:name who)))] + (products/update-products-mapping! tx user-id :order + (upsert-order! tx user-id customer-id order) + products) + (orders-for-days tx user-id day order-date)))) (defn change-state! "Update the state of the given order and also modify the number of products available: @@ -225,22 +215,12 @@ (cond ;; Delete the order along with all recurrences (or (->> id (db/get-by-id tx user-id :orders) :orders/recurrence nil?) - (= :all action-type)) + (= "all" action-type)) (full-delete tx user-id id) + (= "from-here" action-type) nil + ;; TODO: handle partial deletions + ;; Only delete the one day :else (change-state! tx user-id id day "canceled")))) - -;; (delete! 2 "2022-04-20" 240) -;; (delete! 2 nil 241) - -;; (change-state! 2 240 "2022-04-20" "waiting") -;; (change-state! 2 250 "2022-04-23" "fulfilled") -;; (get-orders db/db-uri (t/to-inst #inst "2022-04-20T00:00:00Z") (t/to-inst #inst "2022-04-20T00:00:00Z") nil nil) -;; (get-orders db/db-uri (t/to-inst #inst "2022-04-23T00:00:00Z") (t/to-inst #inst "2022-04-24T00:00:00Z") nil nil) -;; (get-order db/db-uri 2 242 (t/to-inst #inst "2022-04-20T00:00:00Z")) -;; (orders-for-days db/db-uri 2 #inst "2022-04-23T00:00:00Z" #inst "2022-04-23T00:00:00Z") -;; (orders-for-days db/db-uri 2 #inst "2022-04-23T00:00:00Z") -;; (orders-for-days db/db-uri 2 "2022-04-19") -;; (get-all 2) diff --git a/backend/test/chicken_master/orders_test.clj b/backend/test/chicken_master/orders_test.clj index d9abb49..01e3dc7 100644 --- a/backend/test/chicken_master/orders_test.clj +++ b/backend/test/chicken_master/orders_test.clj @@ -230,9 +230,9 @@ sql/query (constantly (raw-order-row :id 4 :recurrence "FREQ=DAILY;COUNT=1")) sql/update! (fn [_ table status key] (swap! invocations conj ["updating" table status key])) sql/insert! (fn [_ table values] (swap! invocations conj ["inserting" table values]))] - (testing "deleting with :all remove the whole order" + (testing "deleting with \"all\" removes the whole order" (reset! invocations []) - (is (= (sut/delete! :user-id nil :all 1) + (is (= (sut/delete! :user-id nil "all" 1) {"2020-01-01" [{:id 4, :notes "note", :state :waiting, :day "2020-01-01", :who {:id 2, :name "mr blobby"}, :recurrence {:times 1, :until nil, :unit "day", :every 1} :products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]})) diff --git a/frontend/src/chicken_master/calendar.cljs b/frontend/src/chicken_master/calendar.cljs index 09d9f4d..c4b26f6 100644 --- a/frontend/src/chicken_master/calendar.cljs +++ b/frontend/src/chicken_master/calendar.cljs @@ -107,6 +107,17 @@ ;; On success :on-submit (fn [form] (re-frame/dispatch [::event/save-order (format-raw-order form)])))) +(defn choose-order-type [] + (let [{:keys [event]} @(re-frame/subscribe [::subs/order-type-edit])] + (html/modal + :order-type-edit + [:div + (html/input :single "tylko to" {:type :radio :name :type-choose :defaultChecked true}) + ;; (html/input :from-here "od tego" {:type :radio :name :type-choose}) + (html/input :all "wszystkie" {:type :radio :name :type-choose})] + ;; On success + :on-submit (fn [form] (re-frame/dispatch (conj event (form "type-choose" "single"))) :close-modal)))) + (defn format-order [settings {:keys [id who day hour notes state products]}] [:div {:class [:order state] :key (gensym) :draggable true @@ -123,7 +134,7 @@ [:button {:on-click #(re-frame/dispatch [::event/confirm-action "na pewno usunąć?" - ::event/remove-order id day])} "-"]] + ::event/change-order-type id [::event/remove-order id day]])} "-"]] [:div {:class :who} (:name who)] (if (settings :show-order-time) [:div {:class :when} hour]) diff --git a/frontend/src/chicken_master/events.cljs b/frontend/src/chicken_master/events.cljs index 0131ef7..4cb6a3b 100644 --- a/frontend/src/chicken_master/events.cljs +++ b/frontend/src/chicken_master/events.cljs @@ -75,6 +75,15 @@ [:dispatch [::log-error (str response)]] [:dispatch [::stop-loading]]]})) +(re-frame/reg-event-fx + ::change-order-type + (fn [{db :db} [_ id event]] + (prn "changing" (-> db :orders (get id))) + (prn event) + (if (-> db :orders (get id) :recurrence) + {:db (assoc db :order-type-edit {:show true :order (-> db :orders (get id)) :event event})} + {:db db :dispatch event}))) + (re-frame/reg-event-fx ::remove-order (fn [_ [_ id day action-type]] diff --git a/frontend/src/chicken_master/html.cljs b/frontend/src/chicken_master/html.cljs index 8e0147e..15315d8 100644 --- a/frontend/src/chicken_master/html.cljs +++ b/frontend/src/chicken_master/html.cljs @@ -5,9 +5,10 @@ (defn extract-input [elem] (condp = (.-tagName elem) "CHECKBOX" [(.-name elem) (.-checked elem)] - "INPUT" [(.-name elem) (if (some-> (.-type elem) clojure.string/lower-case #{"checkbox"}) - (.-checked elem) - (.-value elem))] + "INPUT" (condp = (some-> (.-type elem) clojure.string/lower-case) + "checkbox" [(.-name elem) (.-checked elem)] + "radio" (when (.-checked elem) [(.-name elem) (.-id elem)]) + [(.-name elem) (.-value elem)]) "SELECT" [(.-name elem) (some->> elem (filter #(.-selected %)) first @@ -25,12 +26,13 @@ ([id label] (input id label {})) ([id label options] [:div {:class :input-item} - (if label [:label {:for id} label]) - [:input (-> options - (assoc :defaultValue (:default options)) - (dissoc :default) - (merge {:name id :id id}))]])) - + (when label [:label {:for id} label]) + [:input (merge {:name id :id id} + (if-not (:default options) + options + (-> options + (assoc :defaultValue (:default options)) + (dissoc :default))))]])) (defn modal ([modal-id content] diff --git a/frontend/src/chicken_master/subs.cljs b/frontend/src/chicken_master/subs.cljs index 721c101..cfdb135 100644 --- a/frontend/src/chicken_master/subs.cljs +++ b/frontend/src/chicken_master/subs.cljs @@ -21,7 +21,9 @@ (re-frame/reg-sub ::show-stock-modal (partial show-modal? :stock)) (re-frame/reg-sub ::show-settings-modal (partial show-modal? :settings)) (re-frame/reg-sub ::show-customers-modal (fn [db] (-> db :clients :show))) +(re-frame/reg-sub ::show-order-type-modal (partial show-modal? :order-type-edit)) (re-frame/reg-sub ::editted-order (fn [db] (:order-edit db))) +(re-frame/reg-sub ::order-type-edit (fn [db] (:order-type-edit db))) (re-frame/reg-sub ::current-days (fn [db] (:current-days db))) diff --git a/frontend/src/chicken_master/views.cljs b/frontend/src/chicken_master/views.cljs index 978736e..d372f67 100644 --- a/frontend/src/chicken_master/views.cljs +++ b/frontend/src/chicken_master/views.cljs @@ -36,7 +36,8 @@ @(re-frame/subscribe [::subs/show-stock-modal]) (stock/show-available) @(re-frame/subscribe [::subs/show-settings-modal]) (show-settings) @(re-frame/subscribe [::subs/show-customers-modal]) (cust/show-customers) - @(re-frame/subscribe [::subs/show-edit-modal]) (cal/edit-order)) + @(re-frame/subscribe [::subs/show-edit-modal]) (cal/edit-order) + @(re-frame/subscribe [::subs/show-order-type-modal]) (cal/choose-order-type)) [:button {:id :show-stock-button :class :menu-button :on-click #(re-frame/dispatch [::event/show-stock])} "Magazyn"] [:button {:id :show-clients-button :class :menu-button :on-click #(re-frame/dispatch [::event/show-customers])} "Klienci"]