diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index b3080aea..aa7fe0b7 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -18,7 +18,6 @@ (map :a) (map namespace) set)] - (println namespaces changes) (cond (namespaces "invoice" ) [[:invoice e]] (namespaces "invoice-expense-account" ) [[:invoice (:db/id (:invoice/_expense-accounts entity))]] (namespaces "transaction-account" ) [[:transaction (:db/id (:transaction/_accounts entity))]] diff --git a/src/clj/auto_ap/square/core.clj b/src/clj/auto_ap/square/core.clj index 1cb465fa..8f4e7aa2 100644 --- a/src/clj/auto_ap/square/core.clj +++ b/src/clj/auto_ap/square/core.clj @@ -77,21 +77,22 @@ (log/warn "couldn't look up" i) "Uncategorized")))) -(defn pc [d] {"query" {"filter" {"date_time_filter" - { - "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"))) - "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"))) - } +(defn pc [start end] + {"query" {"filter" {"date_time_filter" + { + "created_at" { + "start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") start) + "end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") end) + } - + - }} + }} - "sort" { - "sort_field" "CREATED_AT" - "sort_order" "DESC" - }}}) + "sort" { + "sort_field" "CREATED_AT" + "sort_order" "DESC" + }}}) (defn get-order ([client location order-id] @@ -103,33 +104,33 @@ )] result))) -(defn continue-search [client location d cursor] +(defn continue-search [client location start end 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)))) + start (merge (pc start end)))) :as :json}) :body)] (log/info "found " (count (:orders result))) (if (not-empty (:cursor result)) - (concat (:orders result) (continue-search client location d (:cursor result))) + (concat (:orders result) (continue-search client location start end (:cursor result))) (:orders result)))) (defn search - ([client location d] + ([client location start end] (log/info "Searching for" (:square-location/client-location location)) (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} - d (merge (pc d)))) + start (merge (pc start end)))) :as :json}) :body)] (log/info "found " (count (:orders result))) (if (not-empty (:cursor result)) - (concat (:orders result) (continue-search client location d (:cursor result))) + (concat (:orders result) (continue-search client location start end (:cursor result))) (:orders result))))) @@ -205,9 +206,9 @@ (defn daily-results ([client location] - (daily-results client location nil)) - ([client location d] - (let [search-results (search client location d) + (daily-results client location (time/plus (time/now) (time/days -3)) (time/now))) + ([client location start end] + (let [search-results (search client location start end) koala-production-ids (->> search-results (filter #(= "koala-production" (:name (:source %)))) (map :id) @@ -372,8 +373,8 @@ ([client ] (doseq [square-location (:client/square-locations client) :when (:square-location/client-location square-location)] - (upsert client square-location (atime/unparse-local (time/now) atime/iso-date)))) - ([client location d] + (upsert client square-location (time/plus (time/now) (time/days -3)) (time/now)))) + ([client location start end] (lc/with-context {:source "Square loading"} (try (let [existing (->> (d/query {:query {:find ['?external-id] @@ -385,7 +386,7 @@ set) _ (log/info (count existing) "Sales orders already exist") to-create (filter #(not (existing (:sales-order/external-id %))) - (daily-results client location d))] + (daily-results client location start end))] (doseq [x (partition-all 20 to-create)] (log/info "Loading " (count x)) @(d/transact conn x)))