lots of changes to make ledger actually visible.

This commit is contained in:
Bryce Covert
2019-04-11 19:41:41 -07:00
parent d335055de8
commit 9549afb3ef
15 changed files with 454 additions and 55 deletions

View 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)

View File

@@ -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 {})

View File

@@ -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)

View File

@@ -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))))

View 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)

View File

@@ -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)

View File

@@ -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))))

View File

@@ -20,4 +20,5 @@
"paid" :paid-invoices
"voided" :voided-invoices
"new" :new-invoice}
"transactions/" {"" :transactions}}])
"transactions/" {"" :transactions}
"ledger/" {"" :ledger}}])

View File

@@ -73,7 +73,10 @@
"Payments" ]
[:a.navbar-item {:class [(active-when ap = :transactions)]
:href (bidi/path-for routes/routes :transactions)}
"Transactions" ]]
"Transactions" ]
[:a.navbar-item {:class [(active-when ap = :ledger)]
:href (bidi/path-for routes/routes :ledger)}
"Ledger" ]]
[:div {:class "navbar-burger burger", :data-target "navMenu"}
[:span]
[:span]

View File

@@ -12,6 +12,7 @@
[auto-ap.views.pages.paid-invoices :refer [paid-invoices-page]]
[auto-ap.views.pages.needs-activation :refer [needs-activation-page]]
[auto-ap.views.pages.transactions :refer [transactions-page]]
[auto-ap.views.pages.ledger :refer [ledger-page]]
[auto-ap.views.pages.login :refer [login-page]]
[auto-ap.views.pages.checks :refer [checks-page]]
[auto-ap.views.pages.admin :refer [admin-page]]
@@ -46,6 +47,9 @@
(defmethod page :transactions [_]
(transactions-page))
(defmethod page :ledger [_]
(ledger-page))
(defmethod page :admin [_]
(admin-page))

View File

@@ -99,10 +99,6 @@
(assoc-in [:clients (:id (:edit-client client))] (update (:edit-client client) :bank-accounts (fn [bas] (->> bas (sort-by :sort-order) vec)))))))
(re-frame/reg-event-db
::add-new-location
[(forms/in-form ::new-client) (re-frame/path [:data])]

View File

@@ -43,7 +43,9 @@
(-> params
(assoc :client-id (:id @(re-frame/subscribe [::subs/client])))
(dissoc :check-number-like-current))
[[:payments [:id :status :amount :type :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]
[[:payments [:id :status :amount :type :check_number :s3_url
[:bank-account [:name]]
:date [:vendor [:name :id]] [:client [:name :id]]]]
:total
:start
:end]]]}
@@ -60,7 +62,7 @@
:venia/queries [{:query/data [:void-payment
{:payment-id (:id payment)}
[:id :status :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]}]}
[:id :status [:bank-account [:name]] :amount :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]]}]}
:on-success [::payment-voided]}}))
(re-frame/reg-event-db
@@ -155,6 +157,12 @@
:sort-by sort-by
:asc asc}
"Vendor"]
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "vendor"
:sort-by sort-by
:asc asc}
"Bank Account"]
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "check-number"
@@ -189,13 +197,14 @@
[:tr
[:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]]
(for [{:keys [client s3-url payments type check-number date amount id vendor status] :as i} (:payments @payment-page)]
(for [{:keys [client s3-url bank-account payments type check-number date amount id vendor status] :as i} (:payments @payment-page)]
^{:key id}
[:tr {:class (:class i)}
(when-not selected-client
[:td (:name client)])
[:td (:name vendor)]
[:td (:name bank-account)]
[:td (cond
(= :cash type) "Cash"
(= :debit type) "Debit"

View File

@@ -0,0 +1,101 @@
(ns auto-ap.views.pages.ledger
(:require [auto-ap.events :as events]
[auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
[auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
[auto-ap.views.components.modal :refer [action-modal]]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.pages.ledger.table :as table]
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
[auto-ap.utils :refer [replace-by]]
[auto-ap.views.pages.transactions.manual :as manual]
[auto-ap.views.utils :refer [bind-field date->str dispatch-event nf]]
[goog.string :as gstring]
[re-frame.core :as re-frame]))
(re-frame/reg-sub
::ledger-page
(fn [db]
(-> db ::ledger-page)))
(re-frame/reg-sub
::params
(fn [db]
(-> db (::params {}))))
(re-frame/reg-event-fx
::params-change
(fn [cofx [_ params]]
{:db (-> (:db cofx)
(assoc-in [:status :loading] true)
(assoc-in [::params] params))
:graphql {:token (-> cofx :db :user)
:query-obj {:venia/queries [[:ledger-page
(assoc params :client-id (:id @(re-frame/subscribe [::subs/client])))
[[:journal-entries [:id
:source
:amount
[:vendor
[:name :id]]
[:client
[:name :id]]
[:line-items
[:id :debit :credit :location
[:account [:id :name]]]]
:date]]
:total
:start
:end]]]}
:on-success [::received]}}))
(re-frame/reg-event-db
::received
(fn [db [_ data]]
(-> db
(assoc ::ledger-page (:ledger-page data))
(assoc-in [:status :loading] false))))
(re-frame/reg-event-db
::change-selected-bank-account
(fn [db [_ key value]]
(let [[key] key
updated (assoc-in db [::ledger-page :bank-account-filter key] value)]
(if (and (= key :id)
(not= value (get-in db [::params :bank-account-id])))
(do
(re-frame/dispatch [::params-change (assoc (::params updated) :bank-account-id value)])
(assoc-in updated [::params :bank-account-id] value))
updated))))
(def ledger-content
(with-meta
(fn []
(let [current-client @(re-frame/subscribe [::subs/client])
user @(re-frame/subscribe [::subs/user])]
[:div
[:h1.title "Ledger"]
[table/table {:id :ledger
:params (re-frame/subscribe [::params])
:ledger-page (re-frame/subscribe [::ledger-page])
:status (re-frame/subscribe [::subs/status])
:on-params-change (fn [params]
(re-frame/dispatch [::params-change params]))}]
[manual/modal {:import-completed [::manual-import-completed ]}]]))
{:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))
(defn ledger-page []
[side-bar-layout
{:side-bar [:div
[:p.menu-label "Bank Account"]
[:div
[bank-account-filter
{:on-change-event [::change-selected-bank-account]
:value (:bank-acount-filter @(re-frame/subscribe [::ledger-page]))
:bank-accounts @(re-frame/subscribe [::subs/bank-accounts])}]]]
:main [ledger-content]}])

View File

@@ -0,0 +1,116 @@
(ns auto-ap.views.pages.ledger.table
(:require [auto-ap.subs :as subs]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.utils :refer [date->str dispatch-event nf]]
[goog.string :as gstring]
[re-frame.core :as re-frame]))
(defn table [{:keys [id ledger-page status on-params-change vendors params check-boxes checked on-check-changed expense-event]}]
(let [opc (fn [p]
(on-params-change (merge @params p )))]
(fn [{:keys [id ledger-page status on-params-change vendors checked]}]
(let [{:keys [sort-by asc]} @params
{:keys [journal-entries start end count total]} @ledger-page
selected-client @(re-frame/subscribe [::subs/client])
percentage-size (if selected-client "25%" "33%")]
[:div
[paginator {:start start :end end :count count :total total
:on-change (fn [p ]
(on-params-change (merge @params p)))}]
"Showing " (inc start) "-" end "/" total
[:table.table.is-fullwidth
[:thead
[:tr
(when-not selected-client
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "client"
:sort-by sort-by
:asc asc}
"Client"])
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "description-original"
:sort-by sort-by
:asc asc}
"Vendor"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "date"
:sort-by sort-by
:asc asc}
"Date"]
[sorted-column {:on-sort opc
:style {:width percentage-size :cursor "pointer"}
:sort-key "account"
:sort-by sort-by
:asc asc}
"Account"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "amount"
:class "has-text-right"
:sort-by sort-by
:asc asc}
"Debit"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "amount"
:class "has-text-right"
:sort-by sort-by
:asc asc}
"Credit"]
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "status"
:sort-by sort-by
:asc asc}
"Status"]]]
[:tbody
(if (:loading @status)
[:tr
[:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]]
(->>
(for [{:keys [client vendor status date amount id line-items] :as i} (:journal-entries @ledger-page)]
(into
[^{:key id}
[:tr {:class (:class i)}
(when-not selected-client
[:td (:name client)])
[:td (if vendor
(:name vendor)
(str "Merchant '" "Hello" "'"))]
#_[:td description-original]
[:td (date->str date) ]
[:td ]
[:td.has-text-right (nf amount )]
[:td.has-text-right (nf amount )]
[:td status]]]
(for [{:keys [debit credit account]} line-items]
[:tr {:class (:class i)}
(when-not selected-client
[:td ])
[:td ]
#_[:td description-original]
[:td ]
[:td (:name account)]
[:td.has-text-right (when debit (nf debit ))]
[:td.has-text-right (when credit (nf credit ))]
[:td status]]
)))
(mapcat identity)))]]]))))

View File

@@ -77,14 +77,14 @@
[:tr
[:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]]
(for [{:keys [client account vendor check status bank-account description-original date amount id ] :as i} (:transactions @transaction-page)]
(for [{:keys [client account vendor payment status bank-account description-original date amount id ] :as i} (:transactions @transaction-page)]
^{:key id}
[:tr {:class (:class i)}
(when-not selected-client
[:td (:name client)])
[:td (if vendor
(:name vendor)
(str "Merchant '" "Hello" "'"))]
[:i.has-text-grey (str "Merchant '" "Hello" "'")])]
#_[:td description-original]
[:td (date->str date) ]
[:td.has-text-right (nf amount )]
@@ -93,6 +93,9 @@
[:td (:name bank-account )]
[:td
[:a.button {:on-click (dispatch-event [::edit/editing i])} [:span [:span.icon [:i.fa.fa-pencil]]]]
(when check
[:a.tag {:href (:s3-url check) :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " (:check-number check) " (" (gstring/format "$%.2f" amount ) ")")])]
(when payment
[:a.tag {:href (:s3-url payment) :target "_new"}
#_[:i.fa.fa-money-check]
[:span.icon [:i.fa.fa-money]]
(str " " (:check-number payment) " (" (gstring/format "$%.2f" amount ) ")")])]
]))]]]))))