makes check voiding work correctly

This commit is contained in:
Bryce Covert
2018-08-16 18:51:58 -07:00
parent ad2dd386ee
commit 1208f972ef
5 changed files with 56 additions and 41 deletions

View File

@@ -4,6 +4,13 @@
[clojure.set :refer [rename-keys]] [clojure.set :refer [rename-keys]]
[clj-time.coerce :as c])) [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] (defn add-arg [query name value where & rest]
(let [query (-> query (let [query (-> query
(update :args conj value) (update :args conj value)
@@ -11,12 +18,14 @@
(update-in [:query :where] conj where))] (update-in [:query :where] conj where))]
(reduce #(update-in %1 [:query :where] conj %2) query rest))) (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] (defn raw-graphql [args]
(let [query (cond-> {:query {:find ['(pull ?e [* (let [query (cond-> {:query {:find [default-read]
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
{:payment/client [:client/name :db/id]}
{:payment/vendor [:vendor/name :db/id]}
{:payment/status [:db/ident]}])]
:in ['$] :in ['$]
:where ['[?e :payment/original-id]]} :where ['[?e :payment/original-id]]}
:args [(d/db (d/connect uri))]} :args [(d/db (d/connect uri))]}
@@ -29,12 +38,7 @@
(->> (d/query (->> (d/query
query) query)
(map first) (<-datomic))))
(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)))))
(defn sort-fn [args] (defn sort-fn [args]
(cond (cond
@@ -53,6 +57,15 @@
true (take (:count args 20))))) true (take (:count args 20)))))
(defn count-graphql [args] (defn count-graphql [args]
(->> (raw-graphql args) (->> (raw-graphql args)
(count))) (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)))

View File

@@ -316,8 +316,8 @@
:void_invoice {:type :invoice :void_invoice {:type :invoice
:args {:invoice_id {:type 'Int}} :args {:invoice_id {:type 'Int}}
:resolve :mutation/void-invoice} :resolve :mutation/void-invoice}
:void_check {:type :check :void_check {:type :payment
:args {:check_id {:type 'Int}} :args {:payment_id {:type 'String}}
:resolve :mutation/void-check} :resolve :mutation/void-check}
:edit_expense_accounts {:type :invoice :edit_expense_accounts {:type :invoice
:args {:invoice_id {:type 'Int} :args {:invoice_id {:type 'Int}

View File

@@ -1,5 +1,6 @@
(ns auto-ap.graphql.checks (ns auto-ap.graphql.checks
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]] (:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]]
[datomic.api :as d]
[com.walmartlabs.lacinia :refer [execute]] [com.walmartlabs.lacinia :refer [execute]]
[com.walmartlabs.lacinia.executor :as executor] [com.walmartlabs.lacinia.executor :as executor]
@@ -9,6 +10,7 @@
[auto-ap.datomic.checks :as d-checks] [auto-ap.datomic.checks :as d-checks]
[auto-ap.db.vendors :as vendors] [auto-ap.db.vendors :as vendors]
[auto-ap.db.invoices :as invoices] [auto-ap.db.invoices :as invoices]
[auto-ap.datomic :refer [uri]]
[auto-ap.utils :refer [by]] [auto-ap.utils :refer [by]]
[auto-ap.db.companies :as companies] [auto-ap.db.companies :as companies]
[auto-ap.time :refer [parse normal-date iso-date]])) [auto-ap.time :refer [parse normal-date iso-date]]))
@@ -49,27 +51,27 @@
:invoices [(invoices/get-by-id (:invoice_id args))]}))) :invoices [(invoices/get-by-id (:invoice_id args))]})))
(defn void-check [context {id :check_id} value] (defn void-check [context {id :payment_id} value]
(let [check (checks/get-by-id id) (let [check (d-checks/get-by-id id)]
invoices-checks (invoices-checks/get-by-check-id id)] (assert (or (= :payment-status/pending (:payment/status check))
(assert (or (= "pending" (:status check)) (#{:payment-type/cash :payment-type/debit} (:payment/type check))))
(#{"cash" "debit"} (:type check))))
(assert-can-see-company (:id context) (:company-id check)) (assert-can-see-company (:id context) (:company-id check))
(doseq [ci invoices-checks (let [removing-payments (mapcat (fn [x]
:let [{:keys [outstanding-balance status] :as invoice invoice-id :id} (invoices/get-by-id (:invoice-id ci)) (let [invoice (:invoice-payment/invoice x)
new-balance (+ outstanding-balance (:amount ci)) new-balance (+ (:invoice/outstanding-balance invoice)
updated-invoice {:id invoice-id (:invoice-payment/amount x))]
:outstanding-balance new-balance [[:db.fn/retractEntity (:db/id x)]
:status (if (> new-balance 0) {:db/id (:db/id invoice)
"unpaid" :invoice/outstanding-balance new-balance
status)}] ] :invoice/status (if (> new-balance 0)
(println updated-invoice) :invoice-status/unpaid
(invoices-checks/delete! (:id ci)) (:invoice/status invoice))}]))
(invoices/update updated-invoice)) (:payment/invoices check))
updated-payment {:db/id (Long/parseLong id)
:payment/amount 0.0
:payment/status :payment-status/voided}]
(checks/update! {:id id @(d/transact (d/connect uri) (conj removing-payments updated-payment)))
:amount 0
:status "voided"}) (-> (d-checks/get-by-id id)
(-> (checks/get-by-id id)
(->graphql)))) (->graphql))))

View File

@@ -51,15 +51,15 @@
:operation/name "VoidCheck"} :operation/name "VoidCheck"}
:venia/queries [{:query/data [:void-check :venia/queries [{:query/data [:void-check
{:check-id (:id check)} {:payment-id (:id check)}
[:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]]}]} [:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]}]}
:on-success [::check-voided]}})) :on-success [::check-voided]}}))
(re-frame/reg-event-db (re-frame/reg-event-db
::check-voided ::check-voided
(fn [db [_ {:keys [void-check]}]] (fn [db [_ {:keys [void-check]}]]
(-> db (-> db
(update-in [::check-page :checks] (fn [checks] (update-in [::check-page :payments] (fn [checks]
(mapv (fn [c] (mapv (fn [c]
(if (= (:id c) (:id void-check)) (if (= (:id c) (:id void-check))
(assoc void-check :class "live-removed") (assoc void-check :class "live-removed")
@@ -162,8 +162,8 @@
[:td (gstring/format "$%.2f" amount )] [:td (gstring/format "$%.2f" amount )]
[:td status] [:td status]
[:td [:td
(when (or (= "pending" status) (when (or (= ":pending" status)
(#{"cash" "debit"} type)) (#{":cash" ":debit"} type))
[:button.button.is-warning.is-outlined {:on-click (dispatch-event [::void-check i])} [:span [:span.icon [:i.fa.fa-minus-circle]]]]) [:button.button.is-warning.is-outlined {:on-click (dispatch-event [::void-check i])} [:span [:span.icon [:i.fa.fa-minus-circle]]]])
(if s3-url (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 ) ")")] [: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 ) ")")]