added graphs
This commit is contained in:
@@ -749,3 +749,15 @@
|
||||
|
||||
(defn migrate-users [conn]
|
||||
[(load-users (users/get-all))])
|
||||
|
||||
(defn merge-query [query-part-1 query-part-2]
|
||||
(-> query-part-1
|
||||
(update-in [:query :find] into (get-in query-part-2 [:query :find]))
|
||||
(update-in [:query :in] into (get-in query-part-2 [:query :in]))
|
||||
(update-in [:query :where] into (get-in query-part-2 [:query :where]))
|
||||
(update-in [:args] into (get-in query-part-2 [:args]))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -69,3 +69,4 @@
|
||||
(d/release conn)
|
||||
(println "Done")))
|
||||
#_(-main)
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-client? assert-can-see-client]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.expense-accounts :as e-expense-accounts]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.users :as d-users]
|
||||
@@ -199,12 +202,31 @@
|
||||
:start {:type 'Int}
|
||||
:end {:type 'Int}}}
|
||||
:check_result {:fields {:invoices {:type '(list :invoice)}
|
||||
:pdf_url {:type 'String}}}}
|
||||
:pdf_url {:type 'String}}}
|
||||
|
||||
:expense_account_stat {:fields {:expense_account_id {:type 'Int}
|
||||
:expense_account_name {:type 'String}
|
||||
:total {:type 'String}}}
|
||||
|
||||
:invoice_stat {:fields {:name {:type 'String}
|
||||
:paid {:type 'String}
|
||||
:unpaid {:type 'String}}}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
:queries
|
||||
{:invoice_page {:type '(list :invoice_page)
|
||||
{:expense_account_stats {:type '(list :expense_account_stat)
|
||||
:args {:client_id {:type :id}}
|
||||
:resolve :get-expense-account-stats}
|
||||
|
||||
:invoice_stats {:type '(list :invoice_stat)
|
||||
:args {:client_id {:type :id}}
|
||||
:resolve :get-invoice-stats}
|
||||
|
||||
:invoice_page {:type '(list :invoice_page)
|
||||
:args {:import_status {:type 'String}
|
||||
:status {:type 'String}
|
||||
:client_id {:type :id}
|
||||
@@ -465,6 +487,61 @@
|
||||
(:bank_account_id args)
|
||||
(:type args))))
|
||||
|
||||
(defn get-expense-account-stats [context {:keys [client_id] } value]
|
||||
(let [result (cond-> {:query {:find ['?expense-account-id '(sum ?amount)]
|
||||
:in ['$]
|
||||
:where []}
|
||||
:args [(d/db (d/connect uri)) client_id]}
|
||||
client_id (merge-query {:query {:in ['?c]}
|
||||
|
||||
:args [client_id]})
|
||||
(not client_id) (merge-query {:query {:where ['[?c :client/name]]}})
|
||||
|
||||
true (merge-query {:query {:where ['[?i :invoice/client ?c]
|
||||
'[?i :invoice/expense-accounts ?expense-account]
|
||||
'[?expense-account :invoice-expense-account/expense-account-id ?expense-account-id]
|
||||
'[?expense-account :invoice-expense-account/amount ?amount]]}})
|
||||
true (doto println)
|
||||
|
||||
true (d/query ))]
|
||||
(for [[expense-account-id total] result]
|
||||
{:expense_account_id expense-account-id :total total :expense_account_name (-> expense-account-id e-expense-accounts/expense-accounts :name)})))
|
||||
|
||||
(defn categorize [x]
|
||||
(cond (<= x 0) :due
|
||||
(<= x 30 ) :due-30
|
||||
(<= x 60 ) :due-60
|
||||
:else :due-later))
|
||||
|
||||
(defn get-invoice-stats [context {:keys [client_id] } value]
|
||||
(let [result (cond-> {:query {:find ['?name '(sum ?outstanding-balance) '(sum ?total)]
|
||||
:in ['$]
|
||||
:where []}
|
||||
:args [(d/db (d/connect uri)) client_id]}
|
||||
client_id (merge-query {:query {:in ['?c]}
|
||||
:args [client_id]})
|
||||
(not client_id) (merge-query {:query {:where ['[?c :client/name]]}})
|
||||
|
||||
true (merge-query {:query {:where ['[?i :invoice/client ?c]
|
||||
'[?i :invoice/outstanding-balance ?outstanding-balance]
|
||||
'[?i :invoice/total ?total]
|
||||
'[?i :invoice/date ?date]
|
||||
'[(.toInstant ^java.util.Date ?date) ?d2]
|
||||
'[(.between java.time.temporal.ChronoUnit/DAYS (java.time.Instant/now) ?d2 ) ?d3]
|
||||
'[(+ 30 ?d3) ?d4]
|
||||
'[(auto-ap.graphql/categorize ?d4) ?name]]}})
|
||||
|
||||
true (d/query ))
|
||||
result (group-by first result)]
|
||||
|
||||
(for [[id name] [[:due "Due"] [:due-30 "0-30 days"] [:due-60 "31-60 days"] [:due-later ">60 days"] ]
|
||||
:let [[[_ outstanding-balance total] ] (id result nil)
|
||||
outstanding-balance (or outstanding-balance 0)
|
||||
total (or total 0)]]
|
||||
{:name name :unpaid outstanding-balance :paid (if (= :due id)
|
||||
0
|
||||
(- total outstanding-balance))})))
|
||||
|
||||
|
||||
|
||||
(def schema
|
||||
@@ -474,6 +551,8 @@
|
||||
:get-all-payments get-all-payments
|
||||
:get-payment-page gq-checks/get-payment-page
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
:get-expense-account-stats get-expense-account-stats
|
||||
:get-invoice-stats get-invoice-stats
|
||||
|
||||
:get-client gq-clients/get-client
|
||||
:get-user get-user
|
||||
|
||||
Reference in New Issue
Block a user