Files
integreat/scratch-sessions/toast_testing.repl
2023-12-03 20:20:32 -08:00

183 lines
8.3 KiB
Plaintext

(ns toast-testing
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
[auto-ap.time :as atime]
[clj-time.format :as ft]
[amazonica.aws.s3 :as s3]
[datomic.api :as dc]
[auto-ap.datomic :refer [conn]]
[clj-time.coerce :as coerce]))
(defn csv-data->maps [csv-data]
(map zipmap
(->> (first csv-data) ;; First row is the header
repeat)
(rest csv-data)))
(def toast-date "MM/dd/yy HH:mm aa")
(atime/parse "11/12/01 12:33 AM" toast-date)
(defn payment-detail->charge [pd client {:toast-integration/keys [include-service-charge-as-tip? client-location] }]
#:charge {:type-name (get pd "Type")
:external-id (format "toast/payment/%s/%s" (:client/code client) (get pd "Payment Id"))
:db/id (get pd "Payment Id")
:total (Double/parseDouble (get pd "Total"))
:location client-location
:client (:db/id client)
:date (coerce/to-date (atime/parse (get pd "Order Date") toast-date))
:tip (+ (Double/parseDouble (get pd "Tip"))
(if include-service-charge-as-tip?
(Double/parseDouble (get pd "Gratuity"))
0.0))
:note (get pd "Tab Name")
:processor (condp = (get pd "Type")
"Other"
(condp = (get pd "Other Type")
"DoorDash" :ccp-processor/doordash
"Uber Eats" :ccp-processor/uber-eats
"GrubHub" :ccp-processor/grubhub
:ccp-processor/na)
"Credit"
:ccp-processor/toast
"Gift Card"
:ccp-processor/toast
"Cash"
:ccp-processor/na
:ccp-processor/na)})
(defn order-detail->sales-order [od client {:toast-integration/keys [include-service-charge-as-tip? client-location category-source-column] }]
#:sales-order
{:date (coerce/to-date (atime/parse (get od "Paid") toast-date))
:client (:db/id client)
:location client-location
:external-id (str "toast/order/" (:client/code client) "-" client-location "-" (get od "Order Id"))
:source "Toast" ;; TODO probably want to come from charge
:vendor :vendor/ccp-toast
#_#_:reference-link (str (url/url "https://squareup.com/dashboard/sales/transactions" (:id order) "by-unit" (:square-location/square-id location))) ;; todo
:total (-> od (get "Total") (Double/parseDouble))
:tax (-> od (get "Tax") (Double/parseDouble))
:tip (+ (-> od (get "Tip") (Double/parseDouble))
(if include-service-charge-as-tip?
(-> od (get "Gratuity") (Double/parseDouble))
0.0))
:discount (reduce + 0.0 (map #(Double/parseDouble (get % "Discount" "0.00")) (-> od (get "Item Selections") )))
:service-charge 0.0 ;; TODO
;; TODO
;; :returns (+ (- (-> order :return_amounts :total_money amount->money)
;; (-> order :return_amounts :tax_money amount->money)
;; (-> order :return_amounts :tip_money amount->money)
;; (-> order :return_amounts :service_charge_money amount->money))
;; (-> order :return_amounts :discount_money amount->money))
:charges (map #(get % "Payment Id")
(get od "Payments"))
:line-items (map (fn [item]
#:order-line-item
{:external-id (str "toast/order/" (:client/code client) "-" client-location "-" (get od "Order Id") "-" (get item "Item Selection Id"))
:item-name (get item "Menu Item")
:category (condp = category-source-column
:toast-category-column/menu-group
(get item "Menu Group")
:toast-category-column/menu
(get item "Menu")
:toast-category-column/sales-category
(get item "Sales Category")
)
:total (Double/parseDouble (get item "Gross Price"))
:tax (Double/parseDouble (get item "Tax"))
:discount (Double/parseDouble (get item "Discount"))})
(get od "Item Selections"))})
(defn payment-detail->expected-deposit [payment-detail client {:toast-integration/keys [include-service-charge-as-tip? client-location] }]
(let [first-date (get (first payment-detail) "Order Date")]
#:expected-deposit {:external-id (format "toast/payout/%s/%s" (:client/code client) first-date)
:vendor :vendor/ccp-toast
:status :expected-deposit-status/pending
:total (reduce + 0.0 (map (comp #(Double/parseDouble %) #(get % "Swiped Card Amount")) payment-detail))
:client (:db/id client)
:location client-location
#_#_:fee (- (reduce + 0.0 (map (fn [entry]
(if (= (:type entry) "REFUND")
(- (amount->money (:fee_amount_money entry)))
(amount->money (:fee_amount_money entry))))
(:payout_entries payout))))
:date (coerce/to-date (atime/parse first-date toast-date))
:sales-date (coerce/to-date (atime/parse first-date toast-date))
:charges (map #(get % "Payment Id") payment-detail)}))
(defn slurp-s3 [fl]
(with-open [is (-> (s3/get-object {:bucket-name "toast.prod.app.integreatconsult.com"
:key fl})
:input-stream)]
(slurp is)))
(defn load []
(let [
integration {:toast-integration/client-location "DM"
:toast-integration/category-source-column :toast-category-column/menu-group}
client (dc/pull (dc/db conn) '[:db/id :client/code] [:client/code "TDMO"])
payment-detail (csv-data->maps (csv/read-csv (slurp-s3 "incoming/53860/20231119/PaymentDetails.csv")))
order-detail (csv-data->maps (csv/read-csv (slurp-s3 "incoming/53860/20231119/OrderDetails.csv")))
item-selection-detail-by-order (group-by
#(get % "Order Id")
(csv-data->maps (csv/read-csv (slurp-s3 "incoming/53860/20231119/ItemSelectionDetails.csv")))
)
payment-by-order (group-by
#(get % "Order Id")
payment-detail)
order-detail (map
#(assoc % "Item Selections" (get item-selection-detail-by-order (get % "Order Id") [])
"Payments" (get payment-by-order (get % "Order Id") []))
order-detail)]
(into []
(concat
(map #(payment-detail->charge % client integration) payment-detail)
[(payment-detail->expected-deposit payment-detail client integration)]
(map #(order-detail->sales-order % client integration) order-detail)
))))
(defn init-processor []
@(dc/transact
conn [
{:db/ident :vendor/ccp-toast
:db/id (->>
(dc/q '[:find ?p
:where [?p :vendor/name "CCP Toast"]]
(dc/db conn))
ffirst)}])
)
(comment
(user/init-repl)
(def z (doall (load)))
(count z)
(do
@(dc/transact conn
z)
(println "Done"))
(auto-ap.datomic/transact-schema conn)
)