76 lines
3.2 KiB
Clojure
76 lines
3.2 KiB
Clojure
(ns auto-ap.graphql.checks
|
|
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]]
|
|
|
|
[com.walmartlabs.lacinia :refer [execute]]
|
|
[com.walmartlabs.lacinia.executor :as executor]
|
|
[com.walmartlabs.lacinia.resolve :as resolve]
|
|
[auto-ap.db.invoices-checks :as invoices-checks]
|
|
[auto-ap.db.checks :as checks]
|
|
[auto-ap.datomic.checks :as d-checks]
|
|
[auto-ap.db.vendors :as vendors]
|
|
[auto-ap.db.invoices :as invoices]
|
|
[auto-ap.utils :refer [by]]
|
|
[auto-ap.db.companies :as companies]
|
|
[auto-ap.time :refer [parse normal-date iso-date]]))
|
|
|
|
|
|
(defn get-payment-page [context args value]
|
|
(let [args (assoc args :id (:id context))
|
|
payments (map
|
|
->graphql
|
|
(d-checks/get-graphql (<-graphql args)))
|
|
checks-count (d-checks/count-graphql (<-graphql args))]
|
|
[{:payments payments
|
|
:total checks-count
|
|
:count (count payments)
|
|
:start (:start args 0)
|
|
:end (+ (:start args 0) (count payments))}]))
|
|
|
|
(defn add-handwritten-check [context args value]
|
|
(let [invoice (invoices/get-by-id (:invoice_id args))
|
|
_ (assert-can-see-company (:id context) (:company-id invoice))
|
|
check (checks/insert! {:s3-uuid nil
|
|
:s3-key nil
|
|
:s3-url nil
|
|
:check-number (:check_number args)
|
|
:date (parse (:date args) iso-date)
|
|
:amount (:amount args)
|
|
:bank-account-id (:bank_account_id args)
|
|
:vendor-id (:vendor-id invoice)
|
|
:company-id (:company-id invoice)
|
|
:invoices [(:invoice_id args)]})]
|
|
|
|
(invoices-checks/insert-multi! [{:invoice-id (:invoice_id args)
|
|
:check-id (:id check)
|
|
:amount (:amount args)}])
|
|
(invoices/apply-payment (:invoice_id args) (:amount args))
|
|
(->graphql
|
|
{:s3-url nil
|
|
: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))))
|
|
|
|
(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))
|
|
|
|
(checks/update! {:id id
|
|
:amount 0
|
|
:status "voided"})
|
|
(-> (checks/get-by-id id)
|
|
(->graphql))))
|