diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 9cf900f9..348cc095 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -527,6 +527,11 @@ :edit_transaction {:type :transaction :args {:transaction {:type :edit_transaction}} :resolve :mutation/edit-transaction} + + :match_transaction {:type :transaction + :args {:transaction_id {:type :id} + :payment_id {:type :id}} + :resolve :mutation/match-transaction} :void_invoice {:type :invoice :args {:invoice_id {:type :id}} :resolve :mutation/void-invoice} @@ -580,18 +585,11 @@ m)) - - - - (defn get-all-payments [context args value] (assert-admin (:id context)) (map ->graphql - (d-checks/get-graphql (assoc (<-graphql args) - :count Integer/MAX_VALUE)))) - - + (d-checks/get-graphql (assoc (<-graphql args) :count Integer/MAX_VALUE)))) (defn get-user [context args value] @@ -699,6 +697,7 @@ :mutation/add-and-print-invoice gq-invoices/add-and-print-invoice :mutation/edit-invoice gq-invoices/edit-invoice :mutation/edit-transaction gq-transactions/edit-transaction + :mutation/match-transaction gq-transactions/match-transaction :mutation/edit-client gq-clients/edit-client :mutation/upsert-vendor gq-vendors/upsert-vendor :mutation/upsert-account gq-accounts/upsert-account diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index 747344e8..1b44a1c1 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -2,6 +2,7 @@ (:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client]] [auto-ap.datomic.transactions :as d-transactions] [auto-ap.datomic.vendors :as d-vendors] + [auto-ap.datomic.checks :as d-checks] [datomic.api :as d] [auto-ap.datomic :refer [uri remove-nils]] [com.walmartlabs.lacinia :refer [execute]] @@ -11,7 +12,8 @@ [auto-ap.time :refer [parse normal-date]] [auto-ap.datomic.clients :as d-clients] [clojure.set :as set] - [clojure.string :as str])) + [clojure.string :as str] + [auto-ap.datomic.accounts :as a])) (defn get-transaction-page [context args value] (let [args (assoc args :id (:id context)) @@ -42,6 +44,7 @@ (defn edit-transaction [context {{:keys [id exclude_from_ledger accounts vendor_id] :as transaction} :transaction} value] (let [existing-transaction (d-transactions/get-by-id id) + _ (assert-can-see-client (:id context) (:transaction/client existing-transaction) ) deleted (deleted-accounts existing-transaction accounts) account-total (reduce + 0 (map (fn [x] (Double/parseDouble (:amount x))) accounts)) missing-locations (seq (set/difference @@ -53,7 +56,7 @@ set (conj "A") (conj "HQ"))))] - (assert-can-see-client (:id context) (:transaction/client existing-transaction) ) + (when-not (dollars= (Math/abs (:transaction/amount existing-transaction)) account-total) (let [error (str "Expense account total (" account-total ") does not equal transaction total (" (Math/abs (:transaction/amount existing-transaction)) ")")] (throw (ex-info error {:validation-error error})))) @@ -70,3 +73,31 @@ [:db/retract id :transaction/accounts d]) deleted))) (->graphql (d-transactions/get-by-id id)))) + +(defn match-transaction [context {:keys [transaction_id payment_id]} value] + (let [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) (:payment/client payment) )] + (when (not= (:db/id (:transaction/client transaction)) + (:db/id (:payment/client payment))) + (throw (ex-info "Clients don't match" {:validation-error "Payment and client do not match."}))) + + (when-not (dollars= (- (:transaction/amount transaction)) + (:payment/amount payment)) + (throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"}))) + (d/transact (d/connect uri) + (into + [{:db/id (:db/id payment) + :payment/status :payment-status/cleared} + + {:db/id (:db/id transaction) + :transaction/payment (:db/id payment) + :transaction/vendor (:db/id (:payment/vendor payment)) + :transaction/location "A" + :transaction/accounts [{:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"])) + :transaction-account/location "A" + :transaction-account/amount (Math/abs (:transaction/amount transaction))}]}] + (map (fn [x] [:db/retractEntity (:db/id x)] ) + (:transaction/accounts transaction))))) + (->graphql (d-transactions/get-by-id transaction_id))) diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index c4ecdd86..89584ec9 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -35,11 +35,11 @@ ;; EVENTS (re-frame/reg-event-fx ::edited - (fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction]}]] + (fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction match-transaction]}]] {:db (-> db (forms/stop-form ::edit-transaction)) - :dispatch (conj edit-completed edit-transaction)})) + :dispatch (conj edit-completed (or edit-transaction match-transaction))})) (re-frame/reg-event-db ::editing @@ -87,6 +87,22 @@ :on-success [::edited edit-completed] :on-error [::forms/save-error ::edit-transaction]}})))) +(re-frame/reg-event-fx + ::matching + (fn [{:keys [db]} [_ edit-completed payment-id]] + (let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])] + {:db (forms/loading db ::edit-transaction ) + :graphql + {:token (-> db :user) + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "MatchTransaction"} + :venia/queries [{:query/data [:match-transaction + {:transaction_id id + :payment-id payment-id} + transaction-read]}]} + :on-success [::edited edit-completed] + :on-error [::forms/save-error ::edit-transaction]}}))) + (re-frame/reg-event-fx ::change-account (fn [{:keys [db]} [_ f a]] @@ -162,12 +178,13 @@ [:table.table.compact.is-borderless (list - (for [{:keys [memo vendor]} (:potential-payment-matches data)] + (for [{:keys [memo check-number vendor id]} (:potential-payment-matches data)] [:tr [:td.no-border (:name vendor)] - [:td.no-border memo] + [:td.no-border (when check-number (str "Check " check-number " ")) memo] [:td.no-border - [:a.button.is-primary.is-small "Match"]]]))] + [:a.button.is-primary.is-small {:on-click (dispatch-event [::matching edit-completed id])} + "Match"]]]))] ] [:div [:div.field diff --git a/src/cljs/auto_ap/views/pages/transactions/table.cljs b/src/cljs/auto_ap/views/pages/transactions/table.cljs index 9bea9da0..4adcf7c0 100644 --- a/src/cljs/auto_ap/views/pages/transactions/table.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/table.cljs @@ -9,16 +9,12 @@ (re-frame/reg-event-fx ::editing-matches-found (fn [{:keys [db]} [_ which payment-matches]] - (println "FOUND" payment-matches) - {:dispatch [::edit/editing which (:potential-payment-matches payment-matches)]})) (re-frame/reg-event-fx ::editing-matches-failed (fn [{:keys [db]} [_ which payment-matches]] - (println "FAILED" payment-matches) - {:dispatch [::edit/editing which payment-matches]})) @@ -29,7 +25,7 @@ {:token (-> db :user) :query-obj {:venia/queries [{:query/data [:potential-payment-matches {:transaction_id (:id which)} - [:id :memo [:vendor [:name]]]]}]} + [:id :memo :check-number [:vendor [:name]]]]}]} :on-success [::editing-matches-found which] :on-error [::editing-matches-failed which]}} ))