staging now has autopay matching
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
[auto-ap.datomic.transaction-rules :as tr]
|
[auto-ap.datomic.transaction-rules :as tr]
|
||||||
[auto-ap.datomic.transactions :as d-transactions]
|
[auto-ap.datomic.transactions :as d-transactions]
|
||||||
[auto-ap.graphql.transaction-rules :as g-tr]
|
[auto-ap.graphql.transaction-rules :as g-tr]
|
||||||
|
[auto-ap.yodlee.import :as import]
|
||||||
[auto-ap.graphql.utils
|
[auto-ap.graphql.utils
|
||||||
:refer
|
:refer
|
||||||
[->graphql
|
[->graphql
|
||||||
@@ -86,7 +87,7 @@
|
|||||||
(assert-admin (:id context))
|
(assert-admin (:id context))
|
||||||
(let [transaction (d-transactions/get-by-id (:transaction_id args))]
|
(let [transaction (d-transactions/get-by-id (:transaction_id args))]
|
||||||
|
|
||||||
(let [matches-set (auto-ap.yodlee.import/match-transaction-to-unfulfilled-autopayments (:transaction/amount transaction)
|
(let [matches-set (import/match-transaction-to-unfulfilled-autopayments (:transaction/amount transaction)
|
||||||
(:db/id (:transaction/client transaction)))]
|
(:db/id (:transaction/client transaction)))]
|
||||||
(->graphql (for [matches matches-set]
|
(->graphql (for [matches matches-set]
|
||||||
(for [[_ invoice-id ] matches]
|
(for [[_ invoice-id ] matches]
|
||||||
@@ -281,22 +282,43 @@
|
|||||||
approval-status->graphql
|
approval-status->graphql
|
||||||
->graphql))
|
->graphql))
|
||||||
|
|
||||||
(defn match-transaction-autopay-invoices [context {:keys [transaction_id payment_id]} value]
|
(defn match-transaction-autopay-invoices [context {:keys [transaction_id autopay_invoice_ids]} value]
|
||||||
(let [_ (assert-admin (:id context))
|
(let [_ (assert-admin (:id context))
|
||||||
transaction (d-transactions/get-by-id transaction_id)
|
transaction (d-transactions/get-by-id transaction_id)
|
||||||
payment (d-checks/get-by-id payment_id)
|
|
||||||
_ (assert-can-see-client (:id context) (:transaction/client transaction) )
|
_ (assert-can-see-client (:id context) (:transaction/client transaction) )
|
||||||
_ (assert-can-see-client (:id context) (:payment/client payment) )]
|
db (d/db conn)
|
||||||
(when (not= (:db/id (:transaction/client transaction))
|
invoice-clients (set (map (comp :db/id :invoice/client #(d/entity db %)) autopay_invoice_ids))
|
||||||
(:db/id (:payment/client payment)))
|
invoice-amount (reduce + 0.0 (map (comp :invoice/total #(d/entity db %)) autopay_invoice_ids))]
|
||||||
(throw (ex-info "Clients don't match" {:validation-error "Payment and client do not match."})))
|
(when (or (> (count invoice-clients) 1)
|
||||||
|
(not= (:db/id (:transaction/client transaction))
|
||||||
|
(first invoice-clients)))
|
||||||
|
(throw (ex-info "Clients don't match" {:validation-error "Invoice(s) and transaction client do not match."})))
|
||||||
|
|
||||||
(when-not (dollars= (- (:transaction/amount transaction))
|
(when-not (dollars= (- (:transaction/amount transaction))
|
||||||
(:payment/amount payment))
|
invoice-amount)
|
||||||
(throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"}))))
|
(throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"})))
|
||||||
(-> (d-transactions/get-by-id transaction_id)
|
#_(log/info [#_(select-keys (d/entity db transaction_id) #{:transaction/amount :db/id})]
|
||||||
approval-status->graphql
|
(->> autopay_invoice_ids
|
||||||
->graphql))
|
(map (fn [id]
|
||||||
|
(let [entity (d/entity db id)]
|
||||||
|
[(-> entity :invoice/vendor :db/id)
|
||||||
|
(-> entity :db/id)
|
||||||
|
(-> entity :invoice/total)])))))
|
||||||
|
(let [payment-tx (import/add-new-payment [(select-keys (d/entity db transaction_id) #{:transaction/amount :transaction/date :db/id})]
|
||||||
|
(map (fn [id]
|
||||||
|
(let [entity (d/entity db id)]
|
||||||
|
[(-> entity :invoice/vendor :db/id)
|
||||||
|
(-> entity :db/id)
|
||||||
|
(-> entity :invoice/total)]))
|
||||||
|
autopay_invoice_ids)
|
||||||
|
(:db/id (:transaction/bank-account transaction))
|
||||||
|
(:db/id (:transaction/client transaction)))]
|
||||||
|
(log/info "Adding a new payment" payment-tx)
|
||||||
|
@(d/transact conn payment-tx))
|
||||||
|
|
||||||
|
(-> (d-transactions/get-by-id transaction_id)
|
||||||
|
approval-status->graphql
|
||||||
|
->graphql)))
|
||||||
|
|
||||||
(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id all]} value]
|
(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id all]} value]
|
||||||
(let [_ (assert-admin (:id context))
|
(let [_ (assert-admin (:id context))
|
||||||
|
|||||||
@@ -74,7 +74,7 @@
|
|||||||
(group-by first) ;; group by vendors
|
(group-by first) ;; group by vendors
|
||||||
vals)
|
vals)
|
||||||
considerations (for [candidate-invoices candidate-invoices-vendor-groups
|
considerations (for [candidate-invoices candidate-invoices-vendor-groups
|
||||||
invoice-count (range 1 3)
|
invoice-count (range 1 30)
|
||||||
consideration (partition invoice-count 1 candidate-invoices)
|
consideration (partition invoice-count 1 candidate-invoices)
|
||||||
:when (dollars= (reduce (fn [acc [_ _ amount]]
|
:when (dollars= (reduce (fn [acc [_ _ amount]]
|
||||||
(+ acc amount)) 0.0 consideration)
|
(+ acc amount)) 0.0 consideration)
|
||||||
|
|||||||
Reference in New Issue
Block a user