supports bulk voiding invoices, searches for iol duplicates.
This commit is contained in:
@@ -25,129 +25,132 @@
|
||||
(update :invoice/status :db/ident)
|
||||
(rename-keys {:invoice-payment/_invoice :invoice/payments})))
|
||||
|
||||
(defn raw-graphql-ids [db args]
|
||||
(->> (cond-> {:query {:find []
|
||||
:in ['$]
|
||||
:where ['[?e :invoice/client]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(defn raw-graphql-ids
|
||||
([args]
|
||||
(raw-graphql-ids (d/db conn) args))
|
||||
([db args]
|
||||
(->> (cond-> {:query {:find []
|
||||
:in ['$]
|
||||
:where ['[?e :invoice/client]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
|
||||
(:exact-match-id args)
|
||||
(merge-query {:query {:in ['?e]
|
||||
:where []}
|
||||
:args [(:exact-match-id args)]})
|
||||
(limited-clients (:id args))
|
||||
(merge-query {:query {:in ['[?xx ...]]
|
||||
:where ['[?e :invoice/client ?xx]]}
|
||||
:args [ (set (map :db/id (limited-clients (:id args))))]})
|
||||
(:client-id args)
|
||||
(merge-query {:query {:in ['?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]]}
|
||||
:args [ (:client-id args)]})
|
||||
(:exact-match-id args)
|
||||
(merge-query {:query {:in ['?e]
|
||||
:where []}
|
||||
:args [(:exact-match-id args)]})
|
||||
(limited-clients (:id args))
|
||||
(merge-query {:query {:in ['[?xx ...]]
|
||||
:where ['[?e :invoice/client ?xx]]}
|
||||
:args [ (set (map :db/id (limited-clients (:id args))))]})
|
||||
(:client-id args)
|
||||
(merge-query {:query {:in ['?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]]}
|
||||
:args [ (:client-id args)]})
|
||||
|
||||
(:client-code args)
|
||||
(merge-query {:query {:in ['?client-code]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?client-id :client/code ?client-code]]}
|
||||
:args [ (:client-code args)]})
|
||||
(:client-code args)
|
||||
(merge-query {:query {:in ['?client-code]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?client-id :client/code ?client-code]]}
|
||||
:args [ (:client-code args)]})
|
||||
|
||||
(:original-id args)
|
||||
(merge-query {:query {:in ['?original-id]
|
||||
:where [
|
||||
'[?e :invoice/client ?c]
|
||||
'[?c :client/original-id ?original-id]]}
|
||||
:args [ (cond-> (:original-id args)
|
||||
(string? (:original-id args)) Long/parseLong )]})
|
||||
(:original-id args)
|
||||
(merge-query {:query {:in ['?original-id]
|
||||
:where [
|
||||
'[?e :invoice/client ?c]
|
||||
'[?c :client/original-id ?original-id]]}
|
||||
:args [ (cond-> (:original-id args)
|
||||
(string? (:original-id args)) Long/parseLong )]})
|
||||
|
||||
(:start (:date-range args)) (merge-query {:query {:in '[?start-date]
|
||||
:where ['[?e :invoice/date ?date]
|
||||
'[(>= ?date ?start-date)]]}
|
||||
:args [(coerce/to-date (:start (:date-range args)))]})
|
||||
(:start (:date-range args)) (merge-query {:query {:in '[?start-date]
|
||||
:where ['[?e :invoice/date ?date]
|
||||
'[(>= ?date ?start-date)]]}
|
||||
:args [(coerce/to-date (:start (:date-range args)))]})
|
||||
|
||||
(:end (:date-range args)) (merge-query {:query {:in '[?end-date]
|
||||
:where ['[?e :invoice/date ?date]
|
||||
'[(<= ?date ?end-date)]]}
|
||||
:args [(coerce/to-date (:end (:date-range args)))]})
|
||||
(:end (:date-range args)) (merge-query {:query {:in '[?end-date]
|
||||
:where ['[?e :invoice/date ?date]
|
||||
'[(<= ?date ?end-date)]]}
|
||||
:args [(coerce/to-date (:end (:date-range args)))]})
|
||||
|
||||
(:start (:due-range args)) (merge-query {:query {:in '[?start-due]
|
||||
:where ['[?e :invoice/due ?due]
|
||||
'[(>= ?due ?start-due)]]}
|
||||
:args [(coerce/to-date (:start (:due-range args)))]})
|
||||
(:start (:due-range args)) (merge-query {:query {:in '[?start-due]
|
||||
:where ['[?e :invoice/due ?due]
|
||||
'[(>= ?due ?start-due)]]}
|
||||
:args [(coerce/to-date (:start (:due-range args)))]})
|
||||
|
||||
(:end (:due-range args)) (merge-query {:query {:in '[?end-due]
|
||||
:where ['[?e :invoice/due ?due]
|
||||
'[(<= ?due ?end-due)]]}
|
||||
:args [(coerce/to-date (:end (:due-range args)))]})
|
||||
(:end (:due-range args)) (merge-query {:query {:in '[?end-due]
|
||||
:where ['[?e :invoice/due ?due]
|
||||
'[(<= ?due ?end-due)]]}
|
||||
:args [(coerce/to-date (:end (:due-range args)))]})
|
||||
|
||||
(:import-status args)
|
||||
(merge-query {:query {:in ['?import-status]
|
||||
:where ['[?e :invoice/import-status ?import-status]]}
|
||||
:args [ (keyword "import-status" (:import-status args))]})
|
||||
(:status args)
|
||||
(merge-query {:query {:in ['?status]
|
||||
:where ['[?e :invoice/status ?status]]}
|
||||
:args [ (:status args)]})
|
||||
(:vendor-id args)
|
||||
(merge-query {:query {:in ['?vendor-id]
|
||||
:where ['[?e :invoice/vendor ?vendor-id]]}
|
||||
:args [ (:vendor-id args)]})
|
||||
(:import-status args)
|
||||
(merge-query {:query {:in ['?import-status]
|
||||
:where ['[?e :invoice/import-status ?import-status]]}
|
||||
:args [ (keyword "import-status" (:import-status args))]})
|
||||
(:status args)
|
||||
(merge-query {:query {:in ['?status]
|
||||
:where ['[?e :invoice/status ?status]]}
|
||||
:args [ (:status args)]})
|
||||
(:vendor-id args)
|
||||
(merge-query {:query {:in ['?vendor-id]
|
||||
:where ['[?e :invoice/vendor ?vendor-id]]}
|
||||
:args [ (:vendor-id args)]})
|
||||
|
||||
(:amount-gte args)
|
||||
(merge-query {:query {:in ['?amount-gte]
|
||||
:where ['[?e :invoice/total ?total-filter]
|
||||
'[(>= ?total-filter ?amount-gte)]]}
|
||||
:args [(:amount-gte args)]})
|
||||
(:amount-gte args)
|
||||
(merge-query {:query {:in ['?amount-gte]
|
||||
:where ['[?e :invoice/total ?total-filter]
|
||||
'[(>= ?total-filter ?amount-gte)]]}
|
||||
:args [(:amount-gte args)]})
|
||||
|
||||
(:amount-lte args)
|
||||
(merge-query {:query {:in ['?amount-lte]
|
||||
:where ['[?e :invoice/total ?total-filter]
|
||||
'[(<= ?total-filter ?amount-lte)]]}
|
||||
:args [(:amount-lte args)]})
|
||||
(:amount-lte args)
|
||||
(merge-query {:query {:in ['?amount-lte]
|
||||
:where ['[?e :invoice/total ?total-filter]
|
||||
'[(<= ?total-filter ?amount-lte)]]}
|
||||
:args [(:amount-lte args)]})
|
||||
|
||||
(seq (:invoice-number-like args))
|
||||
(merge-query {:query {:in ['?invoice-number-like]
|
||||
:where ['[?e :invoice/invoice-number ?invoice-number]
|
||||
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
|
||||
:args [(:invoice-number-like args)]})
|
||||
(seq (:invoice-number-like args))
|
||||
(merge-query {:query {:in ['?invoice-number-like]
|
||||
:where ['[?e :invoice/invoice-number ?invoice-number]
|
||||
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
|
||||
:args [(:invoice-number-like args)]})
|
||||
|
||||
(:scheduled-payments args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['[?e :invoice/scheduled-payment]]}
|
||||
:args []})
|
||||
(:scheduled-payments args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['[?e :invoice/scheduled-payment]]}
|
||||
:args []})
|
||||
|
||||
(:unresolved args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['(or-join [?e]
|
||||
(not [?e :invoice/expense-accounts ])
|
||||
(and [?e :invoice/expense-accounts ?ea]
|
||||
(not [?ea :invoice-expense-account/account])))]}
|
||||
:args []})
|
||||
(:unresolved args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['(or-join [?e]
|
||||
(not [?e :invoice/expense-accounts ])
|
||||
(and [?e :invoice/expense-accounts ?ea]
|
||||
(not [?ea :invoice-expense-account/account])))]}
|
||||
:args []})
|
||||
|
||||
(seq (:location args))
|
||||
(merge-query {:query {:in ['?location]
|
||||
:where ['[?e :invoice/expense-accounts ?eas]
|
||||
'[?eas :invoice-expense-account/location ?location]]}
|
||||
:args [(:location args)]})
|
||||
(seq (:location args))
|
||||
(merge-query {:query {:in ['?location]
|
||||
:where ['[?e :invoice/expense-accounts ?eas]
|
||||
'[?eas :invoice-expense-account/location ?location]]}
|
||||
:args [(:location args)]})
|
||||
|
||||
(:sort args) (add-sorter-fields {"client" ['[?e :invoice/client ?c]
|
||||
'[?c :client/name ?sort-client]]
|
||||
"vendor" ['[?e :invoice/vendor ?v]
|
||||
'[?v :vendor/name ?sort-vendor]]
|
||||
"description-original" ['[?e :transaction/description-original ?sort-description-original]]
|
||||
"location" ['[?e :invoice/expense-accounts ?iea]
|
||||
'[?iea :invoice-expense-account/location ?sort-location]]
|
||||
"date" ['[?e :invoice/date ?sort-date]]
|
||||
"due" ['[(get-else $ ?e :invoice/due #inst "2050-01-01") ?sort-due]]
|
||||
"invoice-number" ['[?e :invoice/invoice-number ?sort-invoice-number]]
|
||||
"total" ['[?e :invoice/total ?sort-total]]
|
||||
"outstanding-balance" ['[?e :invoice/outstanding-balance ?sort-outstanding-balance]]}
|
||||
args)
|
||||
true
|
||||
(merge-query {:query {:find ['?sort-default '?e ]
|
||||
:where ['[?e :invoice/client]
|
||||
'[?e :invoice/date ?sort-default]]}}) )
|
||||
(d/query)
|
||||
(apply-sort-3 args)
|
||||
(apply-pagination args)))
|
||||
(:sort args) (add-sorter-fields {"client" ['[?e :invoice/client ?c]
|
||||
'[?c :client/name ?sort-client]]
|
||||
"vendor" ['[?e :invoice/vendor ?v]
|
||||
'[?v :vendor/name ?sort-vendor]]
|
||||
"description-original" ['[?e :transaction/description-original ?sort-description-original]]
|
||||
"location" ['[?e :invoice/expense-accounts ?iea]
|
||||
'[?iea :invoice-expense-account/location ?sort-location]]
|
||||
"date" ['[?e :invoice/date ?sort-date]]
|
||||
"due" ['[(get-else $ ?e :invoice/due #inst "2050-01-01") ?sort-due]]
|
||||
"invoice-number" ['[?e :invoice/invoice-number ?sort-invoice-number]]
|
||||
"total" ['[?e :invoice/total ?sort-total]]
|
||||
"outstanding-balance" ['[?e :invoice/outstanding-balance ?sort-outstanding-balance]]}
|
||||
args)
|
||||
true
|
||||
(merge-query {:query {:find ['?sort-default '?e ]
|
||||
:where ['[?e :invoice/client]
|
||||
'[?e :invoice/date ?sort-default]]}}) )
|
||||
(d/query)
|
||||
(apply-sort-3 args)
|
||||
(apply-pagination args))))
|
||||
|
||||
|
||||
(defn graphql-results [ids db args]
|
||||
@@ -168,7 +171,7 @@
|
||||
:where ['[?id :invoice/outstanding-balance ?o]]
|
||||
}
|
||||
:args [(d/db conn)
|
||||
ids]})
|
||||
ids]})
|
||||
(map last)
|
||||
(reduce
|
||||
+
|
||||
@@ -182,7 +185,7 @@
|
||||
:where ['[?id :invoice/total ?o]]
|
||||
}
|
||||
:args [(d/db conn)
|
||||
ids]})
|
||||
ids]})
|
||||
(map last)
|
||||
(reduce
|
||||
+
|
||||
@@ -190,10 +193,10 @@
|
||||
|
||||
(defn get-graphql [args]
|
||||
|
||||
(let [db (d/db (d/connect uri))
|
||||
(let [db (d/db (d/connect uri))
|
||||
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)
|
||||
outstanding (sum-outstanding ids-to-retrieve)
|
||||
total-amount (sum-total-amount ids-to-retrieve)]
|
||||
outstanding (sum-outstanding ids-to-retrieve)
|
||||
total-amount (sum-total-amount ids-to-retrieve)]
|
||||
|
||||
|
||||
[(->> (graphql-results ids-to-retrieve db args))
|
||||
@@ -258,7 +261,16 @@
|
||||
set)]
|
||||
(into vendored-results vendorless-results)))
|
||||
|
||||
|
||||
(defn filter-ids [ids]
|
||||
(if ids
|
||||
(->> {:query {:find ['?e]
|
||||
:in ['$ '[?e ...]]
|
||||
:where ['[?e :invoice/date]]}
|
||||
:args [(d/db conn) ids]}
|
||||
(d/query)
|
||||
(map first)
|
||||
vec)
|
||||
[]))
|
||||
|
||||
(defn code-invoice [invoice]
|
||||
(let [db (d/db auto-ap.datomic/conn)
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
(ns auto-ap.graphql.invoices
|
||||
(:require
|
||||
[auto-ap.datomic :refer [audit-transact conn remove-nils uri]]
|
||||
[auto-ap.datomic
|
||||
:refer [audit-transact audit-transact-batch conn remove-nils uri]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.checks :as gq-checks]
|
||||
[auto-ap.graphql.utils
|
||||
:as u
|
||||
:refer [<-graphql
|
||||
assert-admin
|
||||
assert-can-see-client
|
||||
assert-power-user
|
||||
enum->keyword]
|
||||
:as u]
|
||||
enum->keyword]]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
@@ -31,6 +32,7 @@
|
||||
|
||||
(let [args (assoc args :id (:id context))
|
||||
[invoices invoice-count outstanding total-amount] (-> args
|
||||
:filters
|
||||
(assoc :id (:id context))
|
||||
(<-graphql)
|
||||
(update :status enum->keyword "invoice-status")
|
||||
@@ -208,18 +210,55 @@
|
||||
|
||||
(defn void-invoice [context {id :invoice_id} _]
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))]
|
||||
(audit-transact [{:db/id id
|
||||
:invoice/total 0.0
|
||||
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))]
|
||||
(audit-transact [{:db/id id
|
||||
:invoice/total 0.0
|
||||
:invoice/outstanding-balance 0.0
|
||||
:invoice/status :invoice-status/voided
|
||||
:invoice/expense-accounts (map (fn [ea] {:db/id (:db/id ea)
|
||||
:invoice-expense-account/amount 0.0})
|
||||
:invoice/status :invoice-status/voided
|
||||
:invoice/expense-accounts (map (fn [ea] {:db/id (:db/id ea)
|
||||
:invoice-expense-account/amount 0.0})
|
||||
(:invoice/expense-accounts invoice))}]
|
||||
(:id context))
|
||||
|
||||
(-> (d-invoices/get-by-id id) (->graphql (:id context)))))
|
||||
|
||||
(defn void-invoices [context args _]
|
||||
(let [_ (assert-admin (:id context))
|
||||
args (assoc args :id (:id context))
|
||||
ids (some-> args
|
||||
:filters
|
||||
(assoc :id (:id context))
|
||||
(<-graphql)
|
||||
(update :status enum->keyword "invoice-status")
|
||||
(assoc :per-page Integer/MAX_VALUE)
|
||||
d-invoices/raw-graphql-ids
|
||||
:ids)
|
||||
specific-ids (d-invoices/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 ?i [:db/id {:invoice/expense-accounts [:db/id]}]) ...]
|
||||
:in $ [?i ...]
|
||||
:where (not [_ :invoice-payment/invoice ?i])]
|
||||
(d/db conn)
|
||||
)
|
||||
(mapcat
|
||||
(fn [i]
|
||||
(into
|
||||
[{:db/id (:db/id i)
|
||||
:invoice/total 0.0
|
||||
:invoice/outstanding-balance 0.0
|
||||
:invoice/status :invoice-status/voided
|
||||
}]
|
||||
(map
|
||||
(fn [iea]
|
||||
[:db/retract (:db/id i) :invoice/expense-accounts (:db/id iea)])
|
||||
(:invoice/expense-accounts i))))))
|
||||
(:id context))
|
||||
{:message (str "Succesfully voided " (count all-ids))}))
|
||||
|
||||
(defn unvoid-invoice [context {id :invoice_id} _]
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
|
||||
@@ -316,22 +355,7 @@
|
||||
|
||||
(def queries
|
||||
{:invoice_page {:type '(list :invoice_page)
|
||||
:args {:import_status {:type 'String}
|
||||
:exact_match_id {:type :id}
|
||||
:date_range {:type :date_range}
|
||||
:due_range {:type :date_range}
|
||||
:status {:type :invoice_status}
|
||||
:unresolved {:type 'Boolean}
|
||||
:scheduled_payments {:type 'Boolean}
|
||||
:client_id {:type :id}
|
||||
:vendor_id {:type :id}
|
||||
:amount_lte {:type :money}
|
||||
:amount_gte {:type :money}
|
||||
:invoice_number_like {:type 'String}
|
||||
:location {:type 'String}
|
||||
:start {:type 'Int}
|
||||
:per_page {:type 'Int}
|
||||
:sort {:type '(list :sort_item)}}
|
||||
:args {:filters {:type :invoice_filters}}
|
||||
|
||||
:resolve :get-invoice-page}
|
||||
:all_invoices {:type '(list :invoice)
|
||||
@@ -351,6 +375,10 @@
|
||||
:void_invoice {:type :invoice
|
||||
:args {:invoice_id {:type :id}}
|
||||
:resolve :mutation/void-invoice}
|
||||
:void_invoices {:type :message
|
||||
:args {:filters {:type :invoice_filters}
|
||||
:ids {:type '(list :id)}}
|
||||
:resolve :mutation/void-invoices}
|
||||
:unvoid_invoice {:type :invoice
|
||||
:args {:invoice_id {:type :id}}
|
||||
:resolve :mutation/unvoid-invoice}
|
||||
@@ -400,7 +428,24 @@
|
||||
{:fields {:id {:type :id}
|
||||
:account_id {:type :id}
|
||||
:location {:type 'String}
|
||||
:amount {:type :money}}}})
|
||||
:amount {:type :money}}}
|
||||
|
||||
:invoice_filters {:fields {:import_status {:type 'String}
|
||||
:exact_match_id {:type :id}
|
||||
:date_range {:type :date_range}
|
||||
:due_range {:type :date_range}
|
||||
:status {:type :invoice_status}
|
||||
:unresolved {:type 'Boolean}
|
||||
:scheduled_payments {:type 'Boolean}
|
||||
:client_id {:type :id}
|
||||
:vendor_id {:type :id}
|
||||
:amount_lte {:type :money}
|
||||
:amount_gte {:type :money}
|
||||
:invoice_number_like {:type 'String}
|
||||
:location {:type 'String}
|
||||
:start {:type 'Int}
|
||||
:per_page {:type 'Int}
|
||||
:sort {:type '(list :sort_item)}}}})
|
||||
|
||||
(def enums
|
||||
{:invoice_status {:values [{:enum-value :paid}
|
||||
@@ -416,6 +461,7 @@
|
||||
:mutation/add-and-print-invoice add-and-print-invoice
|
||||
:mutation/edit-invoice edit-invoice
|
||||
:mutation/void-invoice void-invoice
|
||||
:mutation/void-invoices void-invoices
|
||||
:mutation/unvoid-invoice unvoid-invoice
|
||||
:mutation/unautopay-invoice unautopay-invoice
|
||||
:mutation/edit-expense-accounts edit-expense-accounts})
|
||||
|
||||
@@ -197,6 +197,7 @@
|
||||
:journal-entry-line/credit
|
||||
{:journal-entry-line/account [:bank-account/include-in-reports
|
||||
:bank-account/bank-name
|
||||
:bank-account/numeric-code
|
||||
:bank-account/code
|
||||
:bank-account/visible
|
||||
:bank-account/name
|
||||
@@ -312,6 +313,7 @@
|
||||
(log/info "Executing raw query " (get query-params "query" ))
|
||||
(statsd/time! [(str "export.time") {:tags #{"export:raw"}}]
|
||||
(into (list) (apply d/q (read-string (get query-params "query" )) (into [(d/db conn)] (read-string (get query-params "args" "[]")))))))))
|
||||
|
||||
(defroutes export-routes
|
||||
(routes
|
||||
(wrap-routes api-key-authed-routes
|
||||
|
||||
@@ -11,6 +11,47 @@
|
||||
[unilog.context :as lc]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
(def known-bad-yodlee-ids ;; these yodlee ids are ones we wish we could manually delete
|
||||
(set [
|
||||
3432269925
|
||||
3432269925
|
||||
3440074569
|
||||
3440074568
|
||||
3451368964
|
||||
3453413513
|
||||
3453413398
|
||||
3325396245
|
||||
3451368962
|
||||
2987488002
|
||||
3021618678
|
||||
3021618674
|
||||
3025625892
|
||||
3052944039
|
||||
3052944035
|
||||
3079067192
|
||||
3079067193
|
||||
3079067193
|
||||
3122287847
|
||||
3122287842
|
||||
3189405320
|
||||
3189405320
|
||||
3189405321
|
||||
3189405321
|
||||
3189405319
|
||||
3189405319
|
||||
3248171256
|
||||
3248171256
|
||||
3248171257
|
||||
3248171257
|
||||
3248171254
|
||||
3291944265
|
||||
3291944267
|
||||
3291944267
|
||||
3310943251
|
||||
3387770449
|
||||
3387770447
|
||||
3432269929]))
|
||||
|
||||
(defn auth-header
|
||||
([cob-session] (str "{cobSession=" cob-session "}"))
|
||||
([cob-session user-session] (str "{cobSession=" cob-session ",userSession=" user-session "}")))
|
||||
|
||||
Reference in New Issue
Block a user