begun on unpaid invoice match.
This commit is contained in:
@@ -170,7 +170,7 @@
|
||||
transaction)))
|
||||
(map #(dissoc % :transaction/id))
|
||||
(group-by :db/id))]
|
||||
(clojure.pprint/pprint results)
|
||||
|
||||
(->> ids
|
||||
(map results)
|
||||
(map first))))
|
||||
|
||||
@@ -515,6 +515,9 @@
|
||||
:potential_autopay_invoices_matches {:type '(list (list :invoice))
|
||||
:args {:transaction_id {:type :id}}
|
||||
:resolve :get-potential-autopay-invoices-matches}
|
||||
:potential_unpaid_invoices_matches {:type '(list (list :invoice))
|
||||
:args {:transaction_id {:type :id}}
|
||||
:resolve :get-potential-unpaid-invoices-matches}
|
||||
|
||||
:potential_transaction_rule_matches {:type '(list :transaction_rule)
|
||||
:args {:transaction_id {:type :id}}
|
||||
@@ -1239,6 +1242,7 @@
|
||||
:get-payment-page gq-checks/get-payment-page
|
||||
:get-potential-payments gq-checks/get-potential-payments
|
||||
:get-potential-autopay-invoices-matches gq-transactions/get-potential-autopay-invoices-matches
|
||||
:get-potential-unpaid-invoices-matches gq-transactions/get-potential-unpaid-invoices-matches
|
||||
:get-accounts gq-accounts/get-accounts
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
:get-ledger-page gq-ledger/get-ledger-page
|
||||
|
||||
@@ -93,6 +93,16 @@
|
||||
(for [[_ invoice-id ] matches]
|
||||
(d-invoices/get-by-id invoice-id)))))))
|
||||
|
||||
(defn get-potential-unpaid-invoices-matches [context args value]
|
||||
(assert-admin (:id context))
|
||||
(let [transaction (d-transactions/get-by-id (:transaction_id args))]
|
||||
|
||||
(let [matches-set (import/match-transaction-to-unpaid-invoices (:transaction/amount transaction)
|
||||
(:db/id (:transaction/client transaction)))]
|
||||
(->graphql (for [matches matches-set]
|
||||
(for [[_ invoice-id ] matches]
|
||||
(d-invoices/get-by-id invoice-id)))))))
|
||||
|
||||
(defn unlink-transaction [context args value]
|
||||
(let [_ (assert-admin (:id context))
|
||||
args (assoc args :id (:id context))
|
||||
|
||||
@@ -74,7 +74,7 @@
|
||||
(group-by first) ;; group by vendors
|
||||
vals)
|
||||
considerations (for [candidate-invoices candidate-invoices-vendor-groups
|
||||
invoice-count (range 1 30)
|
||||
invoice-count (range 1 32)
|
||||
consideration (partition invoice-count 1 candidate-invoices)
|
||||
:when (dollars= (reduce (fn [acc [_ _ amount]]
|
||||
(+ acc amount)) 0.0 consideration)
|
||||
@@ -84,6 +84,30 @@
|
||||
considerations
|
||||
))
|
||||
|
||||
(defn match-transaction-to-unpaid-invoices [amount client-id]
|
||||
(log/info "trying to find unpaid invoices for " client-id amount)
|
||||
(let [candidate-invoices-vendor-groups (->> (d/query {:query {:find ['?vendor-id '?e '?total '?d]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/status :invoice-status/unpaid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/total ?total]
|
||||
'[?e :invoice/date ?d]]}
|
||||
:args [(d/db conn) client-id]})
|
||||
(sort-by last) ;; sort by scheduled payment date
|
||||
(group-by first) ;; group by vendors
|
||||
vals)
|
||||
considerations (for [candidate-invoices candidate-invoices-vendor-groups
|
||||
invoice-count (range 1 32)
|
||||
consideration (partition invoice-count 1 candidate-invoices)
|
||||
:when (dollars= (reduce (fn [acc [_ _ amount]]
|
||||
(+ acc amount)) 0.0 consideration)
|
||||
(- amount))]
|
||||
consideration)]
|
||||
(log/info "Found " (count considerations) "unpaid invoice considerations for transaction of" amount)
|
||||
considerations))
|
||||
|
||||
(defn match-transaction-to-single-unfulfilled-autopayments [amount client-id]
|
||||
(let [considerations (match-transaction-to-unfulfilled-autopayments amount client-id)]
|
||||
(if (= 1 (count considerations))
|
||||
@@ -127,6 +151,7 @@
|
||||
nil))
|
||||
|
||||
(defn transactions->txs [transactions transaction->bank-account apply-rules existing]
|
||||
(log/info transactions)
|
||||
|
||||
(into []
|
||||
|
||||
@@ -163,8 +188,10 @@
|
||||
(t/after? date (:start-date bank-account)))
|
||||
)]
|
||||
(let [existing-check (transaction->existing-payment transaction check-number client-id bank-account-id amount id)
|
||||
invoices-matches (when-not existing-check
|
||||
(match-transaction-to-single-unfulfilled-autopayments amount client-id ))]
|
||||
autopay-invoices-matches (when-not existing-check
|
||||
(match-transaction-to-unfulfilled-autopayments amount client-id ))
|
||||
unpaid-invoices-matches (when-not existing-check
|
||||
(match-transaction-to-unpaid-invoices amount client-id ))]
|
||||
(cond->
|
||||
[#:transaction
|
||||
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
|
||||
@@ -190,11 +217,12 @@
|
||||
:location "A"
|
||||
:amount (Math/abs (double amount))}]))
|
||||
|
||||
(and (not existing-check)
|
||||
(seq invoices-matches)) (add-new-payment invoices-matches bank-account-id client-id)
|
||||
#_(and (not existing-check)
|
||||
(seq autopay-invoices-matches)) #_(add-new-payment autopay-invoices-matches bank-account-id client-id)
|
||||
|
||||
|
||||
true (update 0 #(apply-rules % valid-locations))
|
||||
(and (not (seq autopay-invoices-matches))
|
||||
(not (seq unpaid-invoices-matches))) (update 0 #(apply-rules % valid-locations))
|
||||
true (update 0 remove-nils))))))
|
||||
|
||||
|
||||
@@ -251,9 +279,10 @@
|
||||
transaction->bank-account (comp all-bank-accounts :bank-account-id)]
|
||||
(log/info "Importing " (count transformed-transactions) " manual transactions")
|
||||
|
||||
(let [result (batch-transact
|
||||
(transactions->txs transformed-transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing)))]
|
||||
(log/info "Imported " (count result) " manual transactions")))))
|
||||
(doseq [tx (transactions->txs transformed-transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))]
|
||||
(audit-transact tx {:user/name "Yodlee import"
|
||||
:user/role ":admin"}))
|
||||
(log/info "Imported manual transactions"))))
|
||||
|
||||
(defn do-import
|
||||
([]
|
||||
|
||||
Reference in New Issue
Block a user