sales orders and transactions better.
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
[auto-ap.datomic.migrate.add-bank-account-codes :refer [add-bank-account-codes]]
|
||||
[auto-ap.datomic.migrate.invoice-converter :refer [add-import-status-existing-invoices]]
|
||||
[auto-ap.datomic.migrate.add-general-ledger :as add-general-ledger]
|
||||
[auto-ap.datomic.migrate.sales :as sales]
|
||||
|
||||
[clojure.java.io :as io]
|
||||
[io.rkn.conformity :as c])
|
||||
(:import [datomic Util])
|
||||
@@ -313,6 +315,7 @@
|
||||
]
|
||||
(println "Conforming database...")
|
||||
(c/ensure-conforms conn norms-map)
|
||||
(c/ensure-conforms conn sales/norms-map)
|
||||
(when (not (seq args))
|
||||
(d/release conn))
|
||||
(println "Done")))
|
||||
|
||||
99
src/clj/auto_ap/datomic/migrate/sales.clj
Normal file
99
src/clj/auto_ap/datomic/migrate/sales.clj
Normal file
@@ -0,0 +1,99 @@
|
||||
(ns auto-ap.datomic.migrate.sales
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]))
|
||||
|
||||
(def norms-map {:add-orders {:txes [[{:db/ident :sales-order/external-id
|
||||
:db/doc "The client for the sale"
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one
|
||||
:db/unique :db.unique/identity}
|
||||
|
||||
{:db/ident :sales-order/client
|
||||
:db/doc "The client for the sale"
|
||||
:db/valueType :db.type/ref
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/location
|
||||
:db/doc "The location of the sale"
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/date
|
||||
:db/doc "The date the order was placed"
|
||||
:db/valueType :db.type/instant
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/total
|
||||
:db/doc "The total amount on the sale"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/tax
|
||||
:db/doc "The tax on the sale"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/tip
|
||||
:db/doc "The tip on the sale"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :sales-order/charges
|
||||
:db/doc "How the sale was paid"
|
||||
:db/valueType :db.type/ref
|
||||
:db/cardinality :db.cardinality/many
|
||||
:db/isComponent true}
|
||||
|
||||
{:db/ident :sales-order/line-items
|
||||
:db/doc "What the person ordered"
|
||||
:db/valueType :db.type/ref
|
||||
:db/cardinality :db.cardinality/many
|
||||
:db/isComponent true}
|
||||
|
||||
|
||||
{:db/ident :charge/type-name
|
||||
:db/doc "The type of the charge"
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :charge/total
|
||||
:db/doc "The total amount on the charge"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :charge/tax
|
||||
:db/doc "The tax on the charge"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :charge/tip
|
||||
:db/doc "The tip on the charge"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :order-line-item/item-name
|
||||
:db/doc "The type of the item"
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :order-line-item/total
|
||||
:db/doc "The total cost on the item"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :order-line-item/tax
|
||||
:db/doc "The tax on the item"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
{:db/ident :order-line-item/tip
|
||||
:db/doc "The tip on the item"
|
||||
:db/valueType :db.type/double
|
||||
:db/cardinality :db.cardinality/one}
|
||||
|
||||
]]}})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
104
src/clj/auto_ap/datomic/sales_orders.clj
Normal file
104
src/clj/auto_ap/datomic/sales_orders.clj
Normal file
@@ -0,0 +1,104 @@
|
||||
(ns auto-ap.datomic.sales-orders
|
||||
(:require [auto-ap.datomic :refer [add-sorter-fields apply-pagination apply-sort-3 merge-query uri]]
|
||||
[auto-ap.graphql.utils :refer [limited-clients]]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[clj-time.coerce :as c]
|
||||
[datomic.api :as d]))
|
||||
|
||||
(defn <-datomic [result]
|
||||
(-> result
|
||||
(update :sales-order/date c/from-date)))
|
||||
|
||||
(def default-read '[*
|
||||
{:sales-order/client [:client/name :db/id :client/code]}])
|
||||
|
||||
(defn raw-graphql-ids [db args]
|
||||
(let [query (cond-> {:query {:find []
|
||||
:in ['$]
|
||||
:where []}
|
||||
:args [db]}
|
||||
(:sort args) (add-sorter-fields {"client" ['[?e :sales-order/client ?c]
|
||||
'[?c :client/name ?sort-client]]
|
||||
"location" ['[?e :sales-order/location ?sort-location]]
|
||||
"date" ['[?e :sales-order/date ?sort-date]]
|
||||
"total" ['[?e :sales-order/total ?sort-total]]
|
||||
"tax" ['[?e :sales-order/tax ?sort-tax]]
|
||||
"tip" ['[?e :sales-order/tip ?sort-tip]]}
|
||||
args)
|
||||
|
||||
(limited-clients (:id args))
|
||||
(merge-query {:query {:in ['[?xx ...]]
|
||||
:where ['[?e :sales-order/client ?xx]]}
|
||||
:args [(set (map :db/id (limited-clients (:id args))))]})
|
||||
|
||||
(:client-id args)
|
||||
(merge-query {:query {:in ['?client-id]
|
||||
:where ['[?e :sales-order/client ?client-id]]}
|
||||
:args [(:client-id args)]})
|
||||
(:client-code args)
|
||||
(merge-query {:query {:in ['?client-code]
|
||||
:where ['[?e :sales-order/client ?client-id]
|
||||
'[?client-id :client/code ?client-code]]}
|
||||
:args [(:client-code args)]})
|
||||
|
||||
(:total-gte args)
|
||||
(merge-query {:query {:in ['?total-gte]
|
||||
:where ['[?e :sales-order/total ?a]
|
||||
'[(>= ?a ?total-gte)]]}
|
||||
:args [(:total-gte args)]})
|
||||
|
||||
(:total-lte args)
|
||||
(merge-query {:query {:in ['?total-lte]
|
||||
:where ['[?e :sales-order/total ?a]
|
||||
'[(<= ?a ?total-lte)]]}
|
||||
:args [(:total-lte args)]})
|
||||
|
||||
(:total args)
|
||||
(merge-query {:query {:in ['?total]
|
||||
:where ['[?e :sales-order/total ?sales-order-total]
|
||||
'[(auto-ap.utils/dollars= ?sales-order-total ?total)]]}
|
||||
:args [(:total args)]})
|
||||
|
||||
|
||||
(:start (:date-range args))
|
||||
(merge-query {:query {:in '[?start-date]
|
||||
:where ['[?e :sales-order/date ?date]
|
||||
'[(>= ?date ?start-date)]]}
|
||||
:args [(c/to-date (:start (:date-range args)))]})
|
||||
|
||||
(:end (:date-range args))
|
||||
(merge-query {:query {:in '[?end-date]
|
||||
:where ['[?e :sales-order/date ?date]
|
||||
'[(<= ?date ?end-date)]]}
|
||||
:args [(c/to-date (:end (:date-range args)))]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?base-date '?e]
|
||||
:where ['[?e :sales-order/date ?base-date]]}}))]
|
||||
|
||||
|
||||
(cond->> query
|
||||
true (d/query)
|
||||
true (apply-sort-3 args)
|
||||
true (apply-pagination args))))
|
||||
|
||||
(defn graphql-results [ids db args]
|
||||
(let [results (->> (d/pull-many db default-read ids)
|
||||
(group-by :db/id))
|
||||
payments (->> ids
|
||||
(map results)
|
||||
(map first)
|
||||
(mapv <-datomic))]
|
||||
payments))
|
||||
|
||||
(defn get-graphql [args]
|
||||
(let [db (d/db (d/connect uri))
|
||||
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
||||
|
||||
[(->> (graphql-results ids-to-retrieve db args))
|
||||
matching-count]))
|
||||
|
||||
(defn get-by-id [id]
|
||||
(->>
|
||||
(d/pull (d/db (d/connect uri)) default-read id)
|
||||
(<-datomic)))
|
||||
@@ -21,6 +21,7 @@
|
||||
[auto-ap.graphql.users :as gq-users]
|
||||
[auto-ap.graphql.yodlee-merchants :as ym]
|
||||
[auto-ap.graphql.ledger :as gq-ledger]
|
||||
[auto-ap.graphql.sales-orders :as gq-sales-orders]
|
||||
[auto-ap.graphql.accounts :as gq-accounts]
|
||||
[auto-ap.graphql.clients :as gq-clients]
|
||||
[auto-ap.graphql.vendors :as gq-vendors]
|
||||
@@ -189,6 +190,29 @@
|
||||
:line_items {:type '(list :journal_entry_line)}}}
|
||||
|
||||
|
||||
:order_line_item
|
||||
{:fields {:id {:type :id}
|
||||
:item_name {:type 'String}
|
||||
:total {:type :money}
|
||||
:tax {:type :money}}}
|
||||
:charge
|
||||
{:fields {:id {:type :id}
|
||||
:type_name {:type 'String}
|
||||
:total {:type :money}
|
||||
:tip {:type :money}}}
|
||||
|
||||
:sales_order
|
||||
{:fields {:id {:type :id}
|
||||
:location {:type 'String}
|
||||
:external_id {:type 'String}
|
||||
:total {:type :money}
|
||||
:tip {:type :money}
|
||||
:tax {:type :money}
|
||||
:client {:type :client}
|
||||
:date {:type 'String}
|
||||
:charges {:type '(list :charge)}
|
||||
:line_items {:type '(list :order_line_item)}}}
|
||||
|
||||
:check {:fields {:id {:type :id}
|
||||
:type {:type 'String}
|
||||
:amount {:type 'String}
|
||||
@@ -348,6 +372,12 @@
|
||||
:start {:type 'Int}
|
||||
:end {:type 'Int}}}
|
||||
|
||||
:sales_order_page {:fields {:sales_orders {:type '(list :sales_order)}
|
||||
:count {:type 'Int}
|
||||
:total {:type 'Int}
|
||||
:start {:type 'Int}
|
||||
:end {:type 'Int}}}
|
||||
|
||||
:reminder_page {:fields {:reminders {:type '(list :reminder)}
|
||||
:count {:type 'Int}
|
||||
:total {:type 'Int}
|
||||
@@ -497,6 +527,16 @@
|
||||
|
||||
:resolve :get-ledger-page}
|
||||
|
||||
:sales_order_page {:type :sales_order_page
|
||||
:args {:client_id {:type :id}
|
||||
:date_range {:type :date_range}
|
||||
:total_lte {:type :money}
|
||||
:total_gte {:type :money}
|
||||
:start {:type 'Int}
|
||||
:sort {:type '(list :sort_item)}}
|
||||
|
||||
:resolve :get-sales-order-page}
|
||||
|
||||
:payment_page {:type '(list :payment_page)
|
||||
:args {:client_id {:type :id}
|
||||
:vendor_id {:type :id}
|
||||
@@ -1053,6 +1093,7 @@
|
||||
:get-accounts gq-accounts/get-accounts
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
:get-ledger-page gq-ledger/get-ledger-page
|
||||
:get-sales-order-page gq-sales-orders/get-sales-orders-page
|
||||
:get-balance-sheet gq-ledger/get-balance-sheet
|
||||
:get-profit-and-loss gq-ledger/get-profit-and-loss
|
||||
:get-transaction-rule-page gq-transaction-rules/get-transaction-rule-page
|
||||
|
||||
8
src/clj/auto_ap/graphql/sales_orders.clj
Normal file
8
src/clj/auto_ap/graphql/sales_orders.clj
Normal file
@@ -0,0 +1,8 @@
|
||||
(ns auto-ap.graphql.sales-orders
|
||||
(:require [auto-ap.datomic.sales-orders :as d-sales-orders2]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql result->page]]))
|
||||
|
||||
(defn get-sales-orders-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
[sales-orders sales-orders-count] (d-sales-orders2/get-graphql (<-graphql args))]
|
||||
(result->page sales-orders sales-orders-count :sales_orders args )))
|
||||
@@ -4,6 +4,7 @@
|
||||
[auto-ap.ledger]
|
||||
[auto-ap.yodlee.core]
|
||||
[auto-ap.background.invoices]
|
||||
[auto-ap.square.core]
|
||||
[nrepl.server :refer [start-server stop-server]]
|
||||
[config.core :refer [env]]
|
||||
[ring.adapter.jetty :refer [run-jetty]]
|
||||
|
||||
118
src/clj/auto_ap/square/core.clj
Normal file
118
src/clj/auto_ap/square/core.clj
Normal file
@@ -0,0 +1,118 @@
|
||||
(ns auto-ap.square.core
|
||||
(:require [auto-ap.datomic :refer [conn remove-nils]]
|
||||
[clj-http.client :as client]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
[clojure.data.json :as json]
|
||||
[datomic.api :as d]
|
||||
[unilog.context :as lc]
|
||||
[clojure.tools.logging :as log]
|
||||
[mount.core :as mount]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
(defn locations []
|
||||
(->> (client/get "https://connect.squareup.com/v2/locations"
|
||||
{:headers {"Square-Version" "2020-08-12"
|
||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:as :json})
|
||||
:body
|
||||
:locations))
|
||||
|
||||
(defn search [l]
|
||||
(log/info "Searching for" l)
|
||||
(->> (client/post "https://connect.squareup.com/v2/orders/search"
|
||||
{:headers {"Square-Version" "2020-08-12"
|
||||
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:body (json/write-str {"location_ids" [l] "limit" 4000})
|
||||
:as :json})
|
||||
:body
|
||||
:orders))
|
||||
|
||||
|
||||
(defn amount->money [amt]
|
||||
(* 0.01 (or (:amount amt) 0.0)))
|
||||
|
||||
(defn location_id->client-location [location]
|
||||
({"2RVBYER6QSV7W" ["NGAK" "MH"]
|
||||
"8JT71V8XGYAT3" ["NGKG" "NB"]
|
||||
"SCX0Y8CTGM1S0" ["NGE1" "UC"]
|
||||
"FNH5VRT890WK8" ["NGMJ" "SC"]
|
||||
"AMQ0NPA8FGDEF" ["NGPG" "SZ"]
|
||||
"4X8T65741AEPS" ["NGVZ" "NP"]
|
||||
"KMVFQ9CRCXJ10" ["NGZO" "VT"]} location))
|
||||
|
||||
(defn daily-results []
|
||||
(->> (locations)
|
||||
(map :id)
|
||||
(filter location_id->client-location)
|
||||
(mapcat search)
|
||||
(map (fn [order]
|
||||
(let [[client loc] (location_id->client-location (:location_id order))]
|
||||
(remove-nils
|
||||
#:sales-order
|
||||
{:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles")))
|
||||
:client [:client/code client]
|
||||
:location loc
|
||||
:external-id (str "square-" client "-" loc "-" (:id order))
|
||||
:total (-> order :net_amounts :total_money amount->money)
|
||||
:tax (-> order :net_amounts :tax_money amount->money)
|
||||
:tip (-> order :net_amounts :tip_money amount->money)
|
||||
:charges (->> (:tenders order)
|
||||
(map (fn [t]
|
||||
(remove-nils
|
||||
#:charge
|
||||
{:type-name (:type t)
|
||||
:total (amount->money (:amount_money t))
|
||||
:tip (amount->money (:tip_money t))}))))
|
||||
:line-items (->> (:line_items order)
|
||||
(map (fn [li]
|
||||
(remove-nils
|
||||
#:order-line-item
|
||||
{:item-name (:name li)
|
||||
:total (amount->money (:total_money li))
|
||||
:tax (amount->money (:total_tax_money li))}))))}))))))
|
||||
|
||||
#_(daily-results)
|
||||
|
||||
|
||||
|
||||
(defn upsert []
|
||||
(lc/with-context {:source "Square loading"}
|
||||
(try
|
||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||
:in ['$]
|
||||
:where ['[_ :sales-order/external-id ?external-id]]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
set)
|
||||
_ (log/info (count existing) "Sales orders already exist")
|
||||
to-create (filter #(not (existing (:sales-order/external-id %)))
|
||||
(daily-results))]
|
||||
(doseq [x (partition-all 20 to-create)]
|
||||
(log/info "Loading " (count x))
|
||||
@(d/transact conn x)))
|
||||
(catch Exception e
|
||||
(log/error e)))))
|
||||
|
||||
(defn reset []
|
||||
(->>
|
||||
(d/query {:query {:find ['?e]
|
||||
:in ['$]
|
||||
:where ['[?e :sales-order/date]]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
(map (fn [x] [:db/retractEntity x]))))
|
||||
|
||||
(mount/defstate square-loader
|
||||
:start (scheduler/every (* 15 60 1000) upsert)
|
||||
:stop (scheduler/stop square-loader))
|
||||
|
||||
(comment
|
||||
(daily-results)
|
||||
|
||||
(do (upsert) nil)
|
||||
|
||||
(do @(d/transact conn (reset)) nil))
|
||||
|
||||
Reference in New Issue
Block a user