Fixes issues with square loading

This commit is contained in:
2022-05-10 08:50:02 -07:00
parent 8f47b4bb2b
commit e8f1a0d734
2 changed files with 64 additions and 32 deletions

View File

@@ -1,20 +1,20 @@
(ns auto-ap.square.core (ns auto-ap.square.core
(:require [auto-ap.datomic :refer [conn remove-nils]] (:require [auto-ap.datomic :refer [conn remove-nils]]
[auto-ap.time :as atime] [auto-ap.time :as atime]
[auto-ap.utils :refer [allow-once]] [c(daily-results )lj-http.client :as client]
[clj-http.client :as client]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clj-time.core :as time] [clj-time.core :as time]
[clj-time.format :as f]
[clj-time.periodic :as periodic] [clj-time.periodic :as periodic]
[clojure.core.async :as async] [clj-time.format :as f]
[clojure.data.json :as json]
[clojure.string :as str] [clojure.string :as str]
[clojure.data.json :as json]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[datomic.api :as d] [datomic.api :as d]
[mount.core :as mount] [mount.core :as mount]
[unilog.context :as lc] [unilog.context :as lc]
[yang.scheduler :as scheduler])) #_{:clj-kondo/ignore [:unused-namespace]}
[yang.scheduler :as scheduler]
[clojure.core.async :as async]))
(defn client-base-headers [client] (defn client-base-headers [client]
{"Square-Version" "2021-08-18" {"Square-Version" "2021-08-18"
@@ -79,20 +79,20 @@
"Uncategorized")))) "Uncategorized"))))
(defn pc [d] {"query" {"filter" {"date_time_filter" (defn pc [d] {"query" {"filter" {"date_time_filter"
{ {
"created_at" { "created_at" {
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles"))) "start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
"end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (time/plus (coerce/to-date-time d) (time/days 1)) (time/time-zone-for-id "America/Los_Angeles"))) "end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (time/plus (coerce/to-date-time d) (time/days 1)) (time/time-zone-for-id "America/Los_Angeles")))
} }
}} }}
"sort" { "sort" {
"sort_field" "CREATED_AT" "sort_field" "CREATED_AT"
"sort_order" "DESC" "sort_order" "DESC"
}}}) }}})
(defn get-order (defn get-order
([client location order-id] ([client location order-id]
@@ -104,6 +104,21 @@
)] )]
result))) result)))
(defn continue-search [client location d cursor]
(log/info "Continuing search for" cursor)
(let [result (->> (client/post "https://connect.squareup.com/v2/orders/search"
{:headers (client-base-headers client)
:body (json/write-str (cond-> {"location_ids" [(:square-location/square-id location)]
"limit" 10000
"cursor" cursor}
d (merge (pc d))))
:as :json})
:body)]
(log/info "found " (count (:orders result)))
(if (not-empty (:cursor result))
(concat (:orders result) (continue-search client location cursor))
(:orders result))))
(defn search (defn search
([client location d] ([client location d]
(log/info "Searching for" (:square-location/client-location location)) (log/info "Searching for" (:square-location/client-location location))
@@ -112,10 +127,11 @@
:body (json/write-str (cond-> {"location_ids" [(:square-location/square-id location)] "limit" 10000} :body (json/write-str (cond-> {"location_ids" [(:square-location/square-id location)] "limit" 10000}
d (merge (pc d)))) d (merge (pc d))))
:as :json}) :as :json})
:body :body)]
:orders)] (log/info "found " (count (:orders result)))
(log/info "found " (count result)) (if (not-empty (:cursor result))
result))) (concat (:orders result) (continue-search client location d (:cursor result)))
(:orders result)))))
@@ -210,9 +226,10 @@
(filter (fn has-linked-koala-production? [order] (filter (fn has-linked-koala-production? [order]
;; if a POS order is linked (via note) to a koala-production order, we want ;; if a POS order is linked (via note) to a koala-production order, we want
;; to keep the koala-production order, because it has taxes correct ;; to keep the koala-production order, because it has taxes correct
(not (->> (:line_items order) (not (and (:line_items order) ;; returns do not have line items, so they should be allowed
(map :note) (->> (:line_items order)
(every? koala-production-ids))))) (map :note)
(every? koala-production-ids))))))
(map #(order->sales-order client location %)))))) (map #(order->sales-order client location %))))))
@@ -481,9 +498,12 @@
(log/info "Loading refunds") (log/info "Loading refunds")
(upsert-refunds client)))) (upsert-refunds client))))
(def upsert-all (allow-once upsert-all))
(mount/defstate square-loader (mount/defstate square-loader
:start (scheduler/every (* 4 59 60 1000) upsert-all) :start (scheduler/every (* 4 59 60 1000) upsert-all)
:stop (scheduler/stop square-loader)) :stop (scheduler/stop square-loader))

View File

@@ -485,8 +485,8 @@
(println "orders") (println "orders")
(lc/with-context {:source "Historical loading data"} (lc/with-context {:source "Historical loading data"}
(doseq [d (clj-time.periodic/periodic-seq (t/plus (t/now) (t/days (- days))) (doseq [d (clj-time.periodic/periodic-seq (t/plus (t/today) (t/days (- days)))
(t/now) (t/today)
(t/days 1))] (t/days 1))]
(println d) (println d)
(square/upsert client square-location d))) (square/upsert client square-location d)))
@@ -497,8 +497,8 @@
(println "settlements") (println "settlements")
(with-redefs [square/lookup-dates (fn lookup-dates [] (with-redefs [square/lookup-dates (fn lookup-dates []
(->> (clj-time.periodic/periodic-seq (t/plus (t/now) (t/days (- days))) (->> (clj-time.periodic/periodic-seq (t/plus (t/today) (t/days (- days)))
(t/now) (t/today)
(t/days 2)) (t/days 2))
(map (fn [d] (map (fn [d]
[(atime/unparse (t/plus d (t/days 1)) atime/iso-date) [(atime/unparse (t/plus d (t/days 1)) atime/iso-date)
@@ -507,10 +507,22 @@
(square/upsert-settlements client square-location))))) (square/upsert-settlements client square-location)))))
(defn load-sales-for-day [date]
(doseq [client (d/q [:find [(list 'pull '?e square/square-read ) '...]
:where ['?e :client/square-locations ]]
(d/db auto-ap.datomic/conn))
square-location (:client/square-locations client)
:when (:square-location/client-location square-location)]
(println client)
(println "orders")
(lc/with-context {:source "Historical loading data"}
(square/upsert client square-location (c/to-date-time date)))))
(defn upsert-invoice-amounts [tsv] (defn upsert-invoice-amounts [tsv]
(let [data (with-open [reader (io/reader (char-array tsv))] (let [data (with-open [reader (io/reader (char-array tsv))]
(doall (csv/read-csv reader :separator \tab))) (doall (csv/read-csv reader :separator \tab)))
db (d/db auto-ap.datomic/conn) db (d/db auto-ap.datomic/conn)
invoice-totals (->> data invoice-totals (->> data
(drop 1) (drop 1)
(group-by first) (group-by first)