Adds ability to find expected deposits and link them
This commit is contained in:
109
src/clj/user.clj
109
src/clj/user.clj
@@ -635,4 +635,113 @@
|
||||
|
||||
(auto-ap.square.core/upsert-settlements client-code)))
|
||||
|
||||
(defn upsert-invoice-amounts [tsv]
|
||||
(let [data (with-open [reader (io/reader (char-array tsv))]
|
||||
(doall (csv/read-csv reader :separator \tab)))
|
||||
db (d/db auto-ap.datomic/conn)
|
||||
invoice-totals (->> data
|
||||
(drop 1)
|
||||
(group-by first)
|
||||
(map (fn [[k values]]
|
||||
[(Long/parseLong k)
|
||||
(reduce + 0.0
|
||||
(->> values
|
||||
(map (fn [[_ _ amount]]
|
||||
(- (Double/parseDouble amount))))))
|
||||
]))
|
||||
(into {}))]
|
||||
(->>
|
||||
(for [[invoice-id expense-account-id amount expense-account location] (drop 1 data)
|
||||
:let [
|
||||
invoice-id (Long/parseLong invoice-id)
|
||||
|
||||
invoice (d/entity db invoice-id)
|
||||
current-total (:invoice/total invoice)
|
||||
target-total (invoice-totals invoice-id)
|
||||
|
||||
expense-account-id (Long/parseLong expense-account-id)
|
||||
current-expense-account-code (:account/numeric-code (:invoice-expense-account/account (d/entity db expense-account-id)))
|
||||
target-expense-account-code (Long/parseLong (str/trim expense-account))
|
||||
[[target-expense-account-id]] (vec (d/q
|
||||
'[:find ?a
|
||||
:in $ ?c
|
||||
:where [?a :account/numeric-code ?c]
|
||||
]
|
||||
db target-expense-account-code))
|
||||
|
||||
current-expense-account-amount (:invoice-expense-account/amount (d/entity db expense-account-id))
|
||||
target-expense-account-amount (- (Double/parseDouble amount))
|
||||
|
||||
|
||||
current-expense-account-location (:invoice-expense-account/location (d/entity db expense-account-id))
|
||||
target-expense-account-location location
|
||||
|
||||
|
||||
[[payment-id payment-amount]] (vec (d/q
|
||||
'[:find ?p ?a
|
||||
:in $ ?i
|
||||
:where [?ip :invoice-payment/invoice ?i]
|
||||
[?ip :invoice-payment/amount ?a]
|
||||
[?ip :invoice-payment/payment ?p]
|
||||
]
|
||||
db invoice-id))]]
|
||||
|
||||
[
|
||||
(when (not (auto-ap.utils/dollars= current-total target-total))
|
||||
(if payment-id
|
||||
(println "Cannot update" invoice-id " of " current-total "to be" target-total "because it has a payment (" payment-id ") of" payment-amount )
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total}))
|
||||
|
||||
(when (and (not (auto-ap.utils/dollars= current-expense-account-amount target-expense-account-amount))
|
||||
(or (auto-ap.utils/dollars= current-total target-total)
|
||||
(not payment-id)))
|
||||
{:db/id expense-account-id
|
||||
:invoice-expense-account/amount target-expense-account-amount})
|
||||
|
||||
(when (not= current-expense-account-location
|
||||
target-expense-account-location)
|
||||
{:db/id expense-account-id
|
||||
:invoice-expense-account/location target-expense-account-location})
|
||||
|
||||
(when (not= target-expense-account-code current-expense-account-code )
|
||||
{:db/id expense-account-id
|
||||
:invoice-expense-account/account target-expense-account-id})]
|
||||
|
||||
#_(println (auto-ap.utils/dollars= current-total amount) current-total amount current-expense-account-code expense-account-code)
|
||||
)
|
||||
(mapcat identity)
|
||||
(filter identity)
|
||||
vec)))
|
||||
|
||||
|
||||
(defn get-schema [prefix]
|
||||
(->> (d/q '[:find ?i
|
||||
:in $ ?p
|
||||
:where [_ :db/ident ?i]
|
||||
[(namespace ?i) ?p]] (d/db auto-ap.datomic/conn) prefix)
|
||||
(mapcat identity)
|
||||
vec
|
||||
))
|
||||
|
||||
(defn manually-add-transaction []
|
||||
(auto-ap.yodlee.import/transactions->txs [{:postDate "2014-01-04"
|
||||
:accountId 1234
|
||||
:date "2021-06-05"
|
||||
:id 1
|
||||
:amount {:amount -1743.25}
|
||||
:description {:original "original-description"
|
||||
:simple "simple-description"}
|
||||
:merchant {:id "123"
|
||||
:name "456"}
|
||||
:baseType "DEBIT"
|
||||
:status "POSTED"
|
||||
|
||||
:bank-account {:db/id [:bank-account/code "NGAK-1"]
|
||||
:client/_bank-accounts {:db/id 17592186045456
|
||||
:client/locations ["MH"]}}}]
|
||||
:bank-account
|
||||
(fn noop-rule [transaction locations]
|
||||
transaction)
|
||||
#{}))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user