194 lines
7.5 KiB
Clojure
194 lines
7.5 KiB
Clojure
(ns auto-ap.routes.ezcater-xls
|
|
(:require
|
|
[auto-ap.datomic :refer [audit-transact conn]]
|
|
[auto-ap.logging :as alog]
|
|
[auto-ap.parse :as parse]
|
|
[auto-ap.shared-views.admin.side-bar :refer [admin-side-bar]]
|
|
[auto-ap.ssr-routes :as ssr-routes]
|
|
[auto-ap.ssr.ui :refer [base-page]]
|
|
[auto-ap.ssr.utils :refer [html-response]]
|
|
[auto-ap.time :as atime]
|
|
[bidi.bidi :as bidi]
|
|
[clj-time.coerce :as coerce]
|
|
[clojure.java.io :as io]
|
|
[com.brunobonacci.mulog :as mu]
|
|
[datomic.api :as dc]
|
|
[dk.ative.docjure.spreadsheet :as doc]
|
|
[hiccup2.core :as hiccup]))
|
|
|
|
(defn fmt-amount [a]
|
|
(with-precision 2
|
|
(some-> a
|
|
bigdec
|
|
(.setScale 2 java.math.RoundingMode/HALF_UP)
|
|
(double))))
|
|
|
|
(defn extract-sheet-details [f]
|
|
(into []
|
|
(for [row (->> (doc/load-workbook f)
|
|
(doc/sheet-seq)
|
|
first
|
|
(doc/row-seq)
|
|
)]
|
|
(mapv doc/read-cell (doc/cell-seq row))
|
|
)))
|
|
|
|
(defn rows->maps [rows]
|
|
(let [[headers & rows] rows]
|
|
(for [r rows]
|
|
(into {}
|
|
(map vector headers r)))))
|
|
|
|
|
|
(defn map->sales-order [r clients]
|
|
(let [order-number (get r "Order Number")
|
|
event-date (get r "Event Date")
|
|
store-name (get r "Store Name")
|
|
adjustments (get r "Adjustments")
|
|
tax (get r "Sales Tax")
|
|
food-total (get r "Food Total")
|
|
commission (get r "Commission")
|
|
fee (get r "Payment Transaction Fee")
|
|
tip (get r "Tip")
|
|
caterer-name (get r "Caterer Name")
|
|
client (some->> caterer-name
|
|
not-empty
|
|
(parse/exact-match clients))
|
|
client-id (:db/id client)
|
|
location (first (:client/locations client))]
|
|
(cond (and event-date client-id location )
|
|
[:order #:sales-order
|
|
{:date (coerce/to-date (atime/localize (coerce/to-date-time event-date)))
|
|
:external-id (str "ezcater/order/" client-id "-" location "-" order-number)
|
|
:client client-id
|
|
:location location
|
|
:reference-link (str order-number)
|
|
:line-items [#:order-line-item
|
|
{:external-id (str "ezcater/order/" client-id "-" location "-" order-number "-" 0)
|
|
:item-name "EZCater Catering"
|
|
:category "EZCater Catering"
|
|
:discount (fmt-amount (or adjustments 0.0))
|
|
:tax (fmt-amount tax)
|
|
:total (fmt-amount (+ food-total
|
|
tax
|
|
tip))}]
|
|
|
|
:charges [#:charge
|
|
{:type-name "CARD"
|
|
:date (coerce/to-date (atime/localize (coerce/to-date-time event-date)))
|
|
:client client-id
|
|
:location location
|
|
:external-id (str "ezcater/charge/" client-id "-" location "-" order-number "-" 0)
|
|
:processor :ccp-processor/ezcater
|
|
:total (fmt-amount (+ food-total
|
|
tax
|
|
tip))
|
|
:tip (fmt-amount tip)}]
|
|
:total (fmt-amount (+ food-total
|
|
tax
|
|
tip))
|
|
:discount (fmt-amount (or adjustments 0.0))
|
|
:service-charge (fmt-amount (+ fee commission))
|
|
:tax (fmt-amount tax)
|
|
:tip (fmt-amount tip)
|
|
:returns 0.0
|
|
:vendor :vendor/ccp-ezcater}]
|
|
|
|
caterer-name
|
|
(do
|
|
(alog/warn ::missing-client
|
|
:order order-number
|
|
:store-name store-name
|
|
:caterer-name caterer-name)
|
|
[:missing caterer-name])
|
|
|
|
:else
|
|
nil)))
|
|
|
|
(defn stream->sales-orders [s]
|
|
(let [clients (map first (dc/q '[:find (pull ?c [:client/code
|
|
:db/id
|
|
:client/feature-flags
|
|
{:client/location-matches [:location-match/matches :location-match/location]}
|
|
:client/name
|
|
:client/matches
|
|
:client/locations])
|
|
:where [?c :client/code]]
|
|
(dc/db conn)))]
|
|
(into []
|
|
(->> s
|
|
extract-sheet-details
|
|
rows->maps
|
|
(map #(map->sales-order % clients))
|
|
(filter identity)))))
|
|
|
|
(defn import-stuff []
|
|
(with-open [s (io/input-stream "/home/brycecovert/Downloads/test_2023-04-01_2023-04-30_2023-05-04.xlsx")]
|
|
(stream->sales-orders s)))
|
|
|
|
(defn page* []
|
|
[:div
|
|
[:h1.title "EZCater XLS Import"]
|
|
[:div.card.block {:style {:width "500px"}}
|
|
[:div.card-content
|
|
"Please go to "
|
|
[:a {:href "https://www.ezcater.com/ez_manage/reports/new" :target "_blank"} "EZCater's report page"]
|
|
" to generate a new report. Then drop it below."]]
|
|
[:div#page-notification.notification.block {:style {:display "none"}}]
|
|
[:div.card.block
|
|
[:div.card-content
|
|
[:form {:action (bidi/path-for ssr-routes/only-routes
|
|
:admin-ezcater-xls)
|
|
:method "POST"
|
|
:class "dropzone"
|
|
:id "ezcater"}]]]
|
|
[:script
|
|
(hiccup/raw
|
|
"
|
|
Dropzone.options.ezcater = {
|
|
success: function (file, response) {
|
|
document.getElementById(\"page-notification\").innerHTML = response;
|
|
document.getElementById(\"page-notification\").style[\"display\"] = \"block\";
|
|
}
|
|
}")]])
|
|
|
|
(defn upload-xls [{:keys [identity] :as request}]
|
|
|
|
(let [file (or (get (:params request) :file)
|
|
(get (:params request) "file"))]
|
|
(mu/log ::uploading-file
|
|
:file file)
|
|
(with-open [s (io/input-stream (:tempfile file))]
|
|
(try
|
|
(let [parse-results (stream->sales-orders s)
|
|
new-orders (->> parse-results
|
|
(filter (comp #{:order} first))
|
|
(map last))
|
|
|
|
missing-location (->> parse-results
|
|
(filter (comp #{:missing} first))
|
|
(map last))]
|
|
(audit-transact new-orders identity)
|
|
(html-response [:div (format "Successfully imported %d orders." (count new-orders))
|
|
(when (seq missing-location)
|
|
[:div "Missing the following locations"
|
|
[:ul.ul
|
|
(for [ml (into #{} missing-location)]
|
|
[:li ml])]])]))
|
|
(catch Exception e
|
|
(alog/error ::import-error
|
|
:error e)
|
|
(html-response [:div (.getMessage e)]))))))
|
|
|
|
(defn page [{:keys [matched-route request-method] :as request}]
|
|
(mu/log ::method
|
|
:method request-method)
|
|
(if (= :post request-method)
|
|
(upload-xls request)
|
|
(base-page
|
|
request
|
|
(page*)
|
|
|
|
(admin-side-bar matched-route))))
|
|
|