Matches checks based on amount

This commit is contained in:
BC
2018-07-05 21:14:54 -07:00
parent a90c3d18af
commit cd82f62c15
3 changed files with 45 additions and 14 deletions

View File

@@ -68,12 +68,14 @@
:else
q)))
(defn base-graphql [{:keys [company-id vendor-id check-number bank-account-id]}]
(defn base-graphql [{:keys [company-id vendor-id check-number bank-account-id status amount]}]
(cond-> base-query
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])
(not (nil? bank-account-id)) (helpers/merge-where [:= :bank-account-id bank-account-id])
(not (nil? vendor-id)) (helpers/merge-where [:= :vendor-id vendor-id])
(not (nil? check-number)) (helpers/merge-where [:= :check-number check-number])))
(not (nil? check-number)) (helpers/merge-where [:= :check-number check-number])
(not (nil? status)) (helpers/merge-where [:= :status status])
(not (nil? amount)) (helpers/merge-where [:= :amount amount])))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
(query

View File

@@ -11,7 +11,7 @@
(sql/format (-> (helpers/insert-into :transactions)
(helpers/values [row])
(postgres-helpers/upsert (-> (postgres-helpers/on-conflict :id)
(postgres-helpers/do-update-set :post_date :status)))))))
(postgres-helpers/do-update-set :post_date :status :check_id)))))))
(def base-query (sql/build :select :*
:from :transactions))

View File

@@ -9,13 +9,27 @@
(defn transaction->check-id [_ check-number company-id bank-account-id]
(when check-number
(-> (checks/get-graphql {:company-id company-id
:bank-account-id bank-account-id
:check-number check-number})
first
:id)))
(defn transaction->check-id [_ check-number company-id bank-account-id amount]
(cond (and check-number company-id bank-account-id)
(-> (checks/get-graphql {:company-id company-id
:bank-account-id bank-account-id
:check-number check-number
:status "pending"})
first
:id)
(and company-id bank-account-id amount)
(let [matching-checks (checks/get-graphql {:company-id company-id
:bank-account-id bank-account-id
:amount amount
:status "pending"})]
(if (= 1 (count matching-checks))
(:id (first matching-checks))
nil))
:else
nil))
(defn extract-check-number [{{description-original :original} :description}]
(if-let [[_ check-number] (re-find #"(?i)check[^0-9]+([0-9]*)" description-original)]
@@ -34,9 +48,6 @@
(companies/get-all))
account->company (by :yodlee-account-id :company-id all-bank-accounts)
yodlee-account-id->bank-account-id (by :yodlee-account-id :bank-account-id all-bank-accounts)]
(println "importing " (count transactions) "transactions")
(println "yodlee account->company" account->company)
(println "yodlee account->bank-account-id" yodlee-account-id->bank-account-id)
(doseq [transaction transactions
:let [{post-date :postDate
account-id :accountId
@@ -53,7 +64,7 @@
check-number (extract-check-number transaction)
company-id (account->company account-id)
bank-account-id (yodlee-account-id->bank-account-id account-id)
check-id (transaction->check-id transaction check-number company-id bank-account-id)
check-id (transaction->check-id transaction check-number company-id bank-account-id amount)
]]
(try
@@ -77,3 +88,21 @@
(catch Exception e
(println e))))))
#_(defn match-old-checks []
(let [transactions (transactions/get-unmatched)]
(doseq [transaction transactions]
(when-let (transaction->check-id transaction
(:check-number transaction)
(:company-id transaction)
(:band-account-id transaction)
(:amount transaction))
(transactions/upsert!
{:id id
:check-id check-id})
(when check-id
(checks/update! {:id check-id :status "cleared"}))
)
)
)
)