adds support for bulk voids.

This commit is contained in:
2022-02-18 07:53:34 -08:00
parent ea490c5859
commit 0f8dcd43e1
6 changed files with 325 additions and 193 deletions

View File

@@ -1,7 +1,7 @@
(ns auto-ap.graphql.checks
(:require
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [audit-transact remove-nils]]
[auto-ap.datomic :refer [audit-transact remove-nils conn]]
[auto-ap.datomic.accounts :as a]
[auto-ap.datomic.bank-accounts :as d-bank-accounts]
[auto-ap.datomic.checks :as d-checks]
@@ -22,8 +22,10 @@
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[config.core :refer [env]]
[datomic.api :as d]
[digest])
(:import
(java.io ByteArrayOutputStream)
@@ -396,6 +398,7 @@
(defn get-payment-page [context args _]
(let [args (assoc args :id (:id context))
[payments checks-count] (d-checks/get-graphql (-> args
:filters
(<-graphql)
(update :payment-type enum->keyword "payment-type")
(update :status enum->keyword "payment-status")))]
@@ -451,7 +454,7 @@
:invoices (d-invoices/get-multi (map :invoice_id (:invoice_payments args)))})))
(defn void-check [context {id :payment_id} _]
(defn void-payment [context {id :payment_id} _]
(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))))
@@ -470,13 +473,55 @@
updated-payment {:db/id id
:payment/amount 0.0
:payment/status :payment-status/voided}]
(audit-transact (conj removing-payments updated-payment)
(:id context)))
(-> (d-checks/get-by-id id)
(->graphql))))
(defn void-payments [context args _]
(assert-admin (:id context))
(let [args (assoc args :id (:id context))
ids (some-> args
:filters
(assoc :id (:id context))
(<-graphql)
(update :payment-type enum->keyword "payment-type")
(update :status enum->keyword "payment-status")
(assoc :per-page Integer/MAX_VALUE)
d-checks/raw-graphql-ids
:ids)
specific-ids (d-checks/filter-ids (:ids args))
all-ids (into (set ids) specific-ids)]
(log/info "Voiding " (count all-ids) args)
(audit-transact (->> all-ids
(d/q '[:find [(pull ?p [:db/id
{:invoice-payment/_payment [:invoice-payment/amount
:db/id
{:invoice-payment/invoice [:db/id :invoice/outstanding-balance]}]}]) ...]
:in $ [?p ...]
:where
(not [_ :transaction/payment ?p])
(not [?p :payment/status :payment-status/voided])]
(d/db conn))
(mapcat (fn [{:keys [:db/id]
invoices :invoice-payment/_payment}]
(into
[{:db/id id
:payment/amount 0.0
:payment/status :payment-status/voided}]
(->> invoices
(mapcat (fn [{:keys [:invoice-payment/invoice :db/id :invoice-payment/amount]}]
(let [new-balance (+ (:invoice/outstanding-balance invoice)
amount)]
[[:db.fn/retractEntity id]
{:db/id (:db/id invoice)
:invoice/outstanding-balance new-balance
:invoice/status (if (dollars-0? new-balance)
(:invoice/status invoice)
:invoice-status/unpaid)}]))))))))
(:id context))
{:message (str "Succesfully voided " (count all-ids))}))
(defn get-all-payments [context args _]
(assert-admin (:id context))
(map
@@ -484,7 +529,6 @@
(first (d-checks/get-graphql (assoc (<-graphql args) :count Integer/MAX_VALUE)))))
(defn print-checks [context args _]
(assert-can-see-client (:id context) (:client_id args))
(->graphql
(print-checks-internal (map (fn [i] {:invoice-id (:invoice_id i)
@@ -531,19 +575,7 @@
:statuses {:type '(list String)}}
:resolve :get-all-payments}
:payment_page {:type '(list :payment_page)
:args {:client_id {:type :id}
:vendor_id {:type :id}
:payment_type {:type :payment_type}
:status {:type :payment_status}
:exact_match_id {:type :id}
:date_range {:type :date_range}
:amount_lte {:type :money}
:amount_gte {:type :money}
:check_number_like {:type 'String}
:invoice_number {:type 'String}
:start {:type 'Int}
:per_page {:type 'Int}
:sort {:type '(list :sort_item)}}
:args {:filters {:type :payment_filters}}
:resolve :get-payment-page}
:potential_payment_matches {:type '(list :payment)
:args {:transaction_id {:type :id}}
@@ -564,11 +596,29 @@
:resolve :mutation/add-handwritten-check}
:void_payment {:type :payment
:args {:payment_id {:type :id}}
:resolve :mutation/void-payment}})
:resolve :mutation/void-payment}
:void_payments {:type :message
:args {:filters {:type :payment_filters}
:ids {:type '(list :id)}}
:resolve :mutation/void-payments}})
(def input-objects
{:invoice_payment_amount {:fields {:invoice_id {:type :id}
:amount {:type :money}}}})
:amount {:type :money}}}
:payment_filters {:fields {:client_id {:type :id}
:vendor_id {:type :id}
:payment_type {:type :payment_type}
:status {:type :payment_status}
:exact_match_id {:type :id}
:date_range {:type :date_range}
:amount_lte {:type :money}
:amount_gte {:type :money}
:check_number_like {:type 'String}
:invoice_number {:type 'String}
:start {:type 'Int}
:per_page {:type 'Int}
:sort {:type '(list :sort_item)}}}})
(def enums
{:payment_type {:values [{:enum-value :check}
@@ -584,7 +634,8 @@
{:get-potential-payments get-potential-payments
:get-payment-page get-payment-page
:get-all-payments get-all-payments
:mutation/void-payment void-check
:mutation/void-payment void-payment
:mutation/void-payments void-payments
:mutation/print-checks print-checks
:mutation/add-handwritten-check add-handwritten-check
})