From 1208f972ef014922bac19a337f1cb30dc266bb7c Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 16 Aug 2018 18:51:58 -0700 Subject: [PATCH] makes check voiding work correctly --- src/clj/auto_ap/datomic.clj | 2 +- src/clj/auto_ap/datomic/checks.clj | 37 +++++++++++++------- src/clj/auto_ap/graphql.clj | 4 +-- src/clj/auto_ap/graphql/checks.clj | 44 +++++++++++++----------- src/cljs/auto_ap/views/pages/checks.cljs | 10 +++--- 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/clj/auto_ap/datomic.clj b/src/clj/auto_ap/datomic.clj index 3e37a3f4..5e81049f 100644 --- a/src/clj/auto_ap/datomic.clj +++ b/src/clj/auto_ap/datomic.clj @@ -735,7 +735,7 @@ @(load-invoices-payments (ic/get-all)) @(load-invoices-expense-accounts (iea/get-all)) @(load-transactions (transactions/get-all)) - @(load-users (users/get-all)) + @(load-users (users/get-all)) (count (clojure.pprint/pprint (query-payments)))) diff --git a/src/clj/auto_ap/datomic/checks.clj b/src/clj/auto_ap/datomic/checks.clj index e81c8c81..f621dc73 100644 --- a/src/clj/auto_ap/datomic/checks.clj +++ b/src/clj/auto_ap/datomic/checks.clj @@ -4,6 +4,13 @@ [clojure.set :refer [rename-keys]] [clj-time.coerce :as c])) +(defn <-datomic [results] + (->> results + (map first) + (map #(update % :payment/date c/from-date)) + (map #(update % :payment/status :db/ident)) + (map #(rename-keys % {:invoice-payment/_payment :payment/invoices})))) + (defn add-arg [query name value where & rest] (let [query (-> query (update :args conj value) @@ -11,12 +18,14 @@ (update-in [:query :where] conj where))] (reduce #(update-in %1 [:query :where] conj %2) query rest))) +(def default-read '(pull ?e [* + {:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]} + {:payment/client [:client/name :db/id]} + {:payment/vendor [:vendor/name :db/id]} + {:payment/status [:db/ident]}])) + (defn raw-graphql [args] - (let [query (cond-> {:query {:find ['(pull ?e [* - {:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]} - {:payment/client [:client/name :db/id]} - {:payment/vendor [:vendor/name :db/id]} - {:payment/status [:db/ident]}])] + (let [query (cond-> {:query {:find [default-read] :in ['$] :where ['[?e :payment/original-id]]} :args [(d/db (d/connect uri))]} @@ -29,12 +38,7 @@ (->> (d/query query) - (map first) - - (map #(update % :payment/date c/from-date)) - (map #(update % :payment/status :db/ident)) - (map #(rename-keys % {:invoice-payment/_payment :payments/invoices})) - #_(map #(update % :transaction/post-date c/from-date))))) + (<-datomic)))) (defn sort-fn [args] (cond @@ -53,6 +57,15 @@ true (take (:count args 20))))) (defn count-graphql [args] - (->> (raw-graphql args) (count))) + +(defn get-by-id [id] + (->> + (d/query (-> {:query {:find [default-read] + :in ['$] + :where []} + :args [(d/db (d/connect uri))]} + (add-arg '?e (cond-> id (string? id) Long/parseLong) ['?e]))) + (<-datomic) + (first))) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 6ecd3827..2d4aa089 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -316,8 +316,8 @@ :void_invoice {:type :invoice :args {:invoice_id {:type 'Int}} :resolve :mutation/void-invoice} - :void_check {:type :check - :args {:check_id {:type 'Int}} + :void_check {:type :payment + :args {:payment_id {:type 'String}} :resolve :mutation/void-check} :edit_expense_accounts {:type :invoice :args {:invoice_id {:type 'Int} diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index ac73f7f2..aa66a14f 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -1,5 +1,6 @@ (ns auto-ap.graphql.checks (:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]] + [datomic.api :as d] [com.walmartlabs.lacinia :refer [execute]] [com.walmartlabs.lacinia.executor :as executor] @@ -9,6 +10,7 @@ [auto-ap.datomic.checks :as d-checks] [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] + [auto-ap.datomic :refer [uri]] [auto-ap.utils :refer [by]] [auto-ap.db.companies :as companies] [auto-ap.time :refer [parse normal-date iso-date]])) @@ -49,27 +51,27 @@ :invoices [(invoices/get-by-id (:invoice_id args))]}))) -(defn void-check [context {id :check_id} value] - (let [check (checks/get-by-id id) - invoices-checks (invoices-checks/get-by-check-id id)] - (assert (or (= "pending" (:status check)) - (#{"cash" "debit"} (:type check)))) - +(defn void-check [context {id :payment_id} value] + (let [check (d-checks/get-by-id id)] + (assert (or (= :payment-status/pending (:payment/status check)) + (#{:payment-type/cash :payment-type/debit} (:payment/type check)))) (assert-can-see-company (:id context) (:company-id check)) - (doseq [ci invoices-checks - :let [{:keys [outstanding-balance status] :as invoice invoice-id :id} (invoices/get-by-id (:invoice-id ci)) - new-balance (+ outstanding-balance (:amount ci)) - updated-invoice {:id invoice-id - :outstanding-balance new-balance - :status (if (> new-balance 0) - "unpaid" - status)}] ] - (println updated-invoice) - (invoices-checks/delete! (:id ci)) - (invoices/update updated-invoice)) + (let [removing-payments (mapcat (fn [x] + (let [invoice (:invoice-payment/invoice x) + new-balance (+ (:invoice/outstanding-balance invoice) + (:invoice-payment/amount x))] + [[:db.fn/retractEntity (:db/id x)] + {:db/id (:db/id invoice) + :invoice/outstanding-balance new-balance + :invoice/status (if (> new-balance 0) + :invoice-status/unpaid + (:invoice/status invoice))}])) + (:payment/invoices check)) + updated-payment {:db/id (Long/parseLong id) + :payment/amount 0.0 + :payment/status :payment-status/voided}] + + @(d/transact (d/connect uri) (conj removing-payments updated-payment))) - (checks/update! {:id id - :amount 0 - :status "voided"}) - (-> (checks/get-by-id id) + (-> (d-checks/get-by-id id) (->graphql)))) diff --git a/src/cljs/auto_ap/views/pages/checks.cljs b/src/cljs/auto_ap/views/pages/checks.cljs index ed073427..0c61d7ff 100644 --- a/src/cljs/auto_ap/views/pages/checks.cljs +++ b/src/cljs/auto_ap/views/pages/checks.cljs @@ -51,15 +51,15 @@ :operation/name "VoidCheck"} :venia/queries [{:query/data [:void-check - {:check-id (:id check)} - [:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]]}]} + {:payment-id (:id check)} + [:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]}]} :on-success [::check-voided]}})) (re-frame/reg-event-db ::check-voided (fn [db [_ {:keys [void-check]}]] (-> db - (update-in [::check-page :checks] (fn [checks] + (update-in [::check-page :payments] (fn [checks] (mapv (fn [c] (if (= (:id c) (:id void-check)) (assoc void-check :class "live-removed") @@ -162,8 +162,8 @@ [:td (gstring/format "$%.2f" amount )] [:td status] [:td - (when (or (= "pending" status) - (#{"cash" "debit"} type)) + (when (or (= ":pending" status) + (#{":cash" ":debit"} type)) [:button.button.is-warning.is-outlined {:on-click (dispatch-event [::void-check i])} [:span [:span.icon [:i.fa.fa-minus-circle]]]]) (if s3-url [:a.tag {:href s3-url :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " check-number " (" (gstring/format "$%.2f" amount ) ")")]