From cd82f62c15400f15ec9e7efe04551a0031674526 Mon Sep 17 00:00:00 2001 From: BC Date: Thu, 5 Jul 2018 21:14:54 -0700 Subject: [PATCH] Matches checks based on amount --- src/clj/auto_ap/db/checks.clj | 6 ++-- src/clj/auto_ap/db/transactions.clj | 2 +- src/clj/auto_ap/yodlee/import.clj | 51 ++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/clj/auto_ap/db/checks.clj b/src/clj/auto_ap/db/checks.clj index 629b32f0..98c7c1c0 100644 --- a/src/clj/auto_ap/db/checks.clj +++ b/src/clj/auto_ap/db/checks.clj @@ -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 diff --git a/src/clj/auto_ap/db/transactions.clj b/src/clj/auto_ap/db/transactions.clj index 3eebb77b..799076bf 100644 --- a/src/clj/auto_ap/db/transactions.clj +++ b/src/clj/auto_ap/db/transactions.clj @@ -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)) diff --git a/src/clj/auto_ap/yodlee/import.clj b/src/clj/auto_ap/yodlee/import.clj index d48e0bef..b6be983c 100644 --- a/src/clj/auto_ap/yodlee/import.clj +++ b/src/clj/auto_ap/yodlee/import.clj @@ -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"})) + ) + ) + ) + )