lots of changes to make ledger actually visible.
This commit is contained in:
28
src/clj/auto_ap/datomic/accounts.clj
Normal file
28
src/clj/auto_ap/datomic/accounts.clj
Normal file
@@ -0,0 +1,28 @@
|
||||
(ns auto-ap.datomic.accounts
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.graphql.utils :refer [->graphql]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]))
|
||||
|
||||
(defn get-accounts [args]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]}])]
|
||||
:in ['$]
|
||||
:where [['?e :account/name]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(:account-set args) (merge-query {:query {:in ['?account-set]
|
||||
:where [['?e :account/account-set '?account-set]]}
|
||||
:args [(:account-set args)]}))]
|
||||
(->>
|
||||
(d/query query)
|
||||
(map first))))
|
||||
|
||||
(defn get-account-by-numeric-code-and-sets [numeric-code sets]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]}])]
|
||||
:in ['$ '?numeric-code]
|
||||
:where ['[?e :account/numeric-code ?numeric-code]]}
|
||||
:args [(d/db (d/connect uri)) numeric-code]})]
|
||||
(->>
|
||||
(d/query query)
|
||||
(map first)
|
||||
(first))))
|
||||
|
||||
#_(get-account-by-numeric-code-and-sets 5110 nil)
|
||||
@@ -1,15 +1,105 @@
|
||||
(ns auto-ap.datomic.ledger
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]))
|
||||
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
||||
[auto-ap.datomic :refer [merge-query ]]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[clj-time.coerce :as c]))
|
||||
|
||||
(defn sort-fn [sort-by]
|
||||
(cond
|
||||
(= "client" sort-by)
|
||||
#(-> % :transaction/client :client/name)
|
||||
|
||||
(= "account" sort-by)
|
||||
#(vector (-> % :transaction/account :account/name) (-> % :db/id))
|
||||
|
||||
:else
|
||||
(keyword "transaction" sort-by)))
|
||||
|
||||
(defn add-sorter-field [q sort-map args]
|
||||
(merge-query q
|
||||
{:query {:find ['?sorter]
|
||||
:where (sort-map
|
||||
(:sort-by args)
|
||||
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
|
||||
|
||||
(defn apply-sort [args sort-fn results ]
|
||||
(cond->> results
|
||||
sort-fn (sort-by sort-fn)
|
||||
(= (:asc args) false) (reverse)))
|
||||
|
||||
(defn apply-sort-2 [args results ]
|
||||
(let [comparator (if (= (:asc args) false)
|
||||
(fn [x y] (compare y x))
|
||||
(fn [x y] (compare x y)))]
|
||||
(sort comparator results )))
|
||||
|
||||
(defn apply-pagination [args results]
|
||||
|
||||
{:ids (->> results
|
||||
(drop (:start args 0))
|
||||
(take (:count args 100))
|
||||
(map last))
|
||||
:count (count results)})
|
||||
|
||||
(defn raw-graphql-ids [db args]
|
||||
(let [query (cond-> {:query {:find []
|
||||
:in ['$ ]
|
||||
:where []}
|
||||
:args [db]}
|
||||
|
||||
(:sort-by args) (add-sorter-field {"client" ['[?e :journal-entry/client ?c]
|
||||
'[?c :client/name ?sorter]]
|
||||
"date" ['[?e :journal-entry/date ?sorter]]
|
||||
"amount" ['[?e :journal-entry/amount ?sorter]]}
|
||||
args)
|
||||
|
||||
(limited-clients (:id args))
|
||||
(merge-query {:query {:in ['[?xx ...]]
|
||||
:where ['[?e :journal-entry/client ?xx]]}
|
||||
:args [(set (map :db/id (limited-clients (:id args))))]})
|
||||
|
||||
#_(:bank-account-id args)
|
||||
#_(merge-query {:query {:in ['?bank-account-id]
|
||||
:where ['[?e :transaction/bank-account ?bank-account-id]]}
|
||||
:args [(:bank-account-id args)]})
|
||||
|
||||
(:client-id args)
|
||||
(merge-query {:query {:in ['?client-id]
|
||||
:where ['[?e :journal-entry/client ?client-id]]}
|
||||
:args [(:client-id args)]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?e] :where ['[?e :journal-entry/original-entity]]}}))]
|
||||
(cond->> query
|
||||
true (d/query)
|
||||
(:sort-by args) (apply-sort-2 args)
|
||||
true (apply-pagination args))))
|
||||
|
||||
(defn graphql-results [ids db args]
|
||||
(->> (d/pull-many db '[* {:journal-entry/client [:client/name :client/code :db/id]
|
||||
:journal-entry/vendor [:vendor/name :db/id]
|
||||
:journal-entry/line-items [* {:journal-entry-line/account [*
|
||||
{:account/type [*]}]}]
|
||||
|
||||
}]
|
||||
ids)
|
||||
(map #(update % :journal-entry/date c/from-date))
|
||||
(apply-sort args (some-> (:sort-by args) sort-fn))))
|
||||
|
||||
(defn get-graphql [args]
|
||||
(->> (d/q '[:find (pull ?e [* {:journal-entry/client [:client/name :client/code :db/id]
|
||||
:journal-entry/vendor [:vendor/name :db/id]
|
||||
:journal-entry/line-items [* {:journal-entry-line/account [*
|
||||
{:account/type [*]}]}]
|
||||
|
||||
}])
|
||||
:where [?e :journal-entry/original-entity]]
|
||||
(d/db (d/connect uri)))
|
||||
(map first)))
|
||||
(let [db (d/db (d/connect uri))
|
||||
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
||||
|
||||
[(->> (graphql-results ids-to-retrieve db args))
|
||||
matching-count]))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#_(get-graphql {})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.users :as gq-users]
|
||||
[auto-ap.graphql.ledger :as gq-ledger]
|
||||
[auto-ap.graphql.accounts :as gq-accounts]
|
||||
[auto-ap.graphql.clients :as gq-clients]
|
||||
[auto-ap.graphql.vendors :as gq-vendors]
|
||||
@@ -92,9 +93,22 @@
|
||||
:scheduled {:type 'String}
|
||||
:sent {:type 'String}
|
||||
:vendor {:type :vendor}
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
:journal_entry_line
|
||||
{:fields {:id {:type :id}
|
||||
:account {:type :account}
|
||||
:location {:type 'String}
|
||||
:debit {:type 'String}
|
||||
:credit {:type 'String}}}
|
||||
:journal_entry
|
||||
{:fields {:id {:type :id}
|
||||
:source {:type 'String}
|
||||
:amount {:type 'String}
|
||||
:client {:type :client}
|
||||
:vendor {:type :vendor}
|
||||
:date {:type 'String}
|
||||
:line_items {:type '(list :journal_entry_line)}}}
|
||||
|
||||
|
||||
:check {:fields {:id {:type :id}
|
||||
@@ -209,6 +223,12 @@
|
||||
:start {:type 'Int}
|
||||
:end {:type 'Int}}}
|
||||
|
||||
:ledger_page {:fields {:journal_entries {:type '(list :journal_entry)}
|
||||
:count {:type 'Int}
|
||||
:total {:type 'Int}
|
||||
:start {:type 'Int}
|
||||
:end {:type 'Int}}}
|
||||
|
||||
:reminder_page {:fields {:reminders {:type '(list :reminder)}
|
||||
:count {:type 'Int}
|
||||
:total {:type 'Int}
|
||||
@@ -275,6 +295,15 @@
|
||||
|
||||
:resolve :get-transaction-page}
|
||||
|
||||
:ledger_page {:type :ledger_page
|
||||
:args {:client_id {:type :id}
|
||||
:bank_account_id {:type :id}
|
||||
:start {:type 'Int}
|
||||
:sort_by {:type 'String}
|
||||
:asc {:type 'Boolean}}
|
||||
|
||||
:resolve :get-ledger-page}
|
||||
|
||||
:payment_page {:type '(list :payment_page)
|
||||
:args {:client_id {:type :id}
|
||||
:vendor_id {:type :id}
|
||||
@@ -582,6 +611,7 @@
|
||||
:get-payment-page gq-checks/get-payment-page
|
||||
:get-accounts gq-accounts/get-accounts
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
:get-ledger-page gq-ledger/get-ledger-page
|
||||
:get-expense-account-stats get-expense-account-stats
|
||||
:get-invoice-stats get-invoice-stats
|
||||
|
||||
@@ -647,4 +677,4 @@
|
||||
|
||||
(throw e)))))
|
||||
|
||||
#_(query nil "{ accounts(account_set: \"default\") { numeric_code, name }}" nil)
|
||||
#_(query nil "{ ledger_page { count }}" nil)
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
(ns auto-ap.graphql.accounts
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.graphql.utils :refer [->graphql]]
|
||||
[auto-ap.datomic.accounts :as d-accounts]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql] ]
|
||||
[auto-ap.datomic :refer [uri merge-query]]))
|
||||
|
||||
(defn get-accounts [context args value]
|
||||
(let [query (cond-> {:query {:find ['(pull ?e [* {:account/type [:db/ident :db/id]}])]
|
||||
:in ['$]
|
||||
:where [['?e :account/name]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(:account_set args) (merge-query {:query {:in ['?account-set]
|
||||
:where [['?e :account/account-set '?account-set]]}
|
||||
:args [(:account_set args)]}))]
|
||||
(->>
|
||||
(d/query query)
|
||||
(map first)
|
||||
(->graphql ))))
|
||||
(->graphql (d-accounts/get-accounts (<-graphql args))))
|
||||
|
||||
|
||||
17
src/clj/auto_ap/graphql/ledger.clj
Normal file
17
src/clj/auto_ap/graphql/ledger.clj
Normal file
@@ -0,0 +1,17 @@
|
||||
(ns auto-ap.graphql.ledger
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic.ledger :as l]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql]]
|
||||
[auto-ap.datomic :refer [uri merge-query]]))
|
||||
|
||||
(defn get-ledger-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
[journal-entries journal-entries-count] (l/get-graphql (<-graphql args))
|
||||
journal-entries (map ->graphql journal-entries)]
|
||||
{:journal_entries journal-entries
|
||||
:total journal-entries-count
|
||||
:count (count journal-entries)
|
||||
:start (:start args 0)
|
||||
:end (+ (:start args 0) (count journal-entries))}))
|
||||
|
||||
#_(get-ledger-page nil nil nil)
|
||||
@@ -1,5 +1,6 @@
|
||||
(ns auto-ap.ledger
|
||||
(:require [datomic.api :as d]
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[auto-ap.datomic :refer [uri remove-nils]]))
|
||||
|
||||
|
||||
@@ -41,15 +42,15 @@
|
||||
:journal-entry/vendor (:db/id (:invoice/vendor entity))
|
||||
:journal-entry/amount (:invoice/total entity)
|
||||
|
||||
:journal-entry/line-items (mapcat (fn [ea]
|
||||
[{:journal-entry-line/expense-account 2110
|
||||
:journal-entry-line/location (:invoice-expense-account/location ea)
|
||||
:journal-entry-line/debit (:invoice-expense-account/amount ea)}
|
||||
{:journal-entry-line/expense-account (:invoice-expense-account/expense-account-id ea)
|
||||
:journal-entry-line/location (:invoice-expense-account/location ea)
|
||||
:journal-entry-line/credit (:invoice-expense-account/amount ea)}
|
||||
])
|
||||
(:invoice/expense-accounts entity))
|
||||
:journal-entry/line-items (into [{:journal-entry-line/account (a/get-account-by-numeric-code-and-sets 2110 ["default"])
|
||||
:journal-entry-line/credit (:invoice/total entity)}]
|
||||
(map (fn [ea]
|
||||
{:journal-entry-line/account (a/get-account-by-numeric-code-and-sets
|
||||
(:invoice-expense-account/expense-account-id ea)
|
||||
["default"])
|
||||
:journal-entry-line/location (:invoice-expense-account/location ea)
|
||||
:journal-entry-line/debit (:invoice-expense-account/amount ea)})
|
||||
(:invoice/expense-accounts entity)))
|
||||
#_#_:general-ledger/from-expense-account 2110
|
||||
#_#_:general-ledger/to-expense-account (:invoice-expense-account/expense-account-id ea)
|
||||
:journal-entry/cleared (and (< (:invoice/outstanding-balance entity) 0.01)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[auto-ap.utils :refer [by]]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri remove-nils]]
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[clj-time.coerce :as coerce]
|
||||
[digest :refer [sha-256]]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
@@ -11,15 +12,15 @@
|
||||
|
||||
|
||||
|
||||
(defn transaction->check-id [_ check-number client-id bank-account-id amount]
|
||||
(defn transaction->payment [_ check-number client-id bank-account-id amount]
|
||||
|
||||
(cond (and check-number client-id bank-account-id)
|
||||
(-> (d-checks/get-graphql {:client-id client-id
|
||||
:bank-account-id bank-account-id
|
||||
:check-number check-number
|
||||
:amount (- amount)
|
||||
:status :payment-status/pending})
|
||||
first
|
||||
:db/id)
|
||||
first)
|
||||
|
||||
(and client-id bank-account-id amount)
|
||||
|
||||
@@ -28,7 +29,7 @@
|
||||
:amount (- amount)
|
||||
:status :payment-status/pending})]
|
||||
(if (= 1 (count matching-checks))
|
||||
(:db/id (first matching-checks))
|
||||
(first matching-checks)
|
||||
nil))
|
||||
|
||||
:else
|
||||
@@ -64,7 +65,7 @@
|
||||
check-number (extract-check-number transaction)
|
||||
client-id (transaction->client transaction)
|
||||
bank-account-id (transaction->bank-account-id transaction)
|
||||
check-id (transaction->check-id transaction check-number client-id bank-account-id amount)]]
|
||||
check (transaction->payment transaction check-number client-id bank-account-id amount)]]
|
||||
(try
|
||||
(when client-id
|
||||
@(->> [(remove-nils #:transaction
|
||||
@@ -80,10 +81,17 @@
|
||||
:client client-id
|
||||
:check-number check-number
|
||||
:bank-account (transaction->bank-account-id transaction)
|
||||
:payment (when check-id
|
||||
{:db/id check-id
|
||||
:payment (when check
|
||||
{:db/id (:db/id check)
|
||||
:payment/status :payment-status/cleared}
|
||||
)})]
|
||||
)
|
||||
|
||||
:vendor (when check
|
||||
(:db/id (:payment/vendor check)))
|
||||
:account (when check
|
||||
(:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))
|
||||
#_(:db/id (:payment/vendor check)))
|
||||
})]
|
||||
|
||||
|
||||
(d/transact (d/connect uri))))
|
||||
|
||||
Reference in New Issue
Block a user