summaryrefslogtreecommitdiff
path: root/pages/shop/order/index.fnl
blob: 35a48aa819b734aba3ff74dd3e87396684a24a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(import-macros {:compile-html HTML} :macros)
(local forms (require :forms))
(local lib (require :lib))
(local shop (require :shop))
(local templates (require :templates))

(local %page-title "Оформление заказ")
(local %order-form
 [{:title ""
   :fields [
     (forms.text-input "name" "Как к вам обращаться?" true)
     (forms.text-input "contact" "Telegram, Whatsapp или E-mail для связи" true)
     (forms.checkbox-input "correct-order" "Данные заказа верны" true)
     (forms.checkbox-input "consent"
       (..
         "Я согласен с <a target=\"_blank\" href=\"/information/privacy-policy\">условиями обработки персональных данных</a>."
         ;;"а также с <a target=\"_blank\" href=\"/information/offer\">нашей публичной офертой</a>."
         )
       true)]}])

(fn content [db basket data errors agreed-to-cookies? authenticated?]
  [(HTML
    [:aside {}
     (templates.header "/shop/order")
     (if (< 0 (# basket)) (templates.basket basket "/shop/order") "")
     (templates.address-block)
     (templates.conditions-block)
     (templates.contact-block)])
   (HTML
    [:div {:class "content"}
     (if (not agreed-to-cookies?) (templates.cookies-agreement) "")
     [:div {:class "back"} [:a {:href "/shop"} "⟵ Обратно к списку"]]
     [:section {}
      [:h2 {} "Оформление заказа"]
      (forms.render-form %order-form data errors)]])])

(fn check-stocks [db basket]
  (var error nil)

  (each [_ line (ipairs basket) &until error]
    (local product
      (. (_G.must
           (luna.db.query-assoc
             db "SELECT title, stock FROM products WHERE name = ?"
             [line.name]))
         1))
    (when (< (- product.stock line.quantity) 0)
      (set error (.. "К сожалению, товар «" product.title "» закончился."
                     " Пожалуйста, уберите его из корзины и попробуйте оформить заказ снова."))))

  error)

(fn render [request db authenticated?]
  (let [order-id request.cookies.order
        agreed-to-cookies? request.cookies.agreed-to-cookies
        basket (if order-id (shop.basket db order-id) [])]
    (if (= request.method "POST")
      (let [errors (forms.validate %order-form request.form)
            has-errors? (not (lib.empty-table? errors))]
        (if has-errors?
          (values 400 {} (templates.base
                           (content db basket request.form errors
                                    agreed-to-cookies? authenticated?)
                           %page-title))
          (let [data (forms.html-form->data %order-form request.form)
                stock-error (check-stocks db basket)]
            (if (not stock-error)
              (do
                (shop.place-order db order-id data.name data.contact data.consent)
                (lib.notify
                  (.. "Новый заказ "
                      "<a href=\"https://whitetoad.ru/shop/order/" order-id "\">"
                         order-id
                      "</a> от <b>" data.name "</b>.\n\n"
                      "<a href=\"https://whitetoad.ru/shop/order/list\">Список заказов</a>"))
                ;; redirect and clear order cookie
                (values 302 {:Location (.. "/shop/order/" order-id)
                             :Set-Cookie "order=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"}
                        ""))
              (values 400 {}
                (templates.base (content db basket data {:consent stock-error}
                                         agreed-to-cookies? authenticated?))
                %page-title)))))
      (if (< 0 (# basket))
        (values 200 {} (templates.base
                         (content db basket {} {} agreed-to-cookies?
                                  authenticated?)
                         %page-title))
        (values 302 {:Location "/shop"} "")))))

{: render}