Able to shohw ledger entries.

This commit is contained in:
Bryce Covert
2019-04-24 15:31:33 -07:00
parent f975ec100b
commit b5ca106260
5 changed files with 188 additions and 52 deletions

View File

@@ -1,6 +1,7 @@
(ns auto-ap.views.pages.ledger.profit-and-loss
(:require [auto-ap.subs :as subs]
[auto-ap.views.components.layouts :refer [side-bar-layout]]
[auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar]]
[auto-ap.views.pages.ledger.table :refer [table]]
[goog.string :as gstring]
[auto-ap.utils :refer [dollars-0? by ]]
[auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]]
@@ -8,6 +9,7 @@
[cljs-time.core :as t]
[re-frame.core :as re-frame]))
;; SUBS
(re-frame/reg-sub
::locations
(fn [db]
@@ -27,11 +29,31 @@
(fn [db]
(-> db ::report)))
(re-frame/reg-sub
::ledger-list-active?
(fn [db]
(-> db ::ledger-list-active?)))
(re-frame/reg-sub
::loading
(fn [db]
(-> db ::loading)))
(re-frame/reg-sub
::ledger-list-loading
(fn [db]
(-> db ::ledger-list-loading)))
(re-frame/reg-sub
::ledger-list
(fn [db]
(-> db ::ledger-list :ledger-page)))
(re-frame/reg-sub
::ledger-params
(fn [db]
(-> db (::ledger-params {}) )))
(re-frame/reg-sub
::accounts
(fn [db [_ type only-location]]
@@ -46,6 +68,7 @@
(re-frame/reg-sub
::accounts-by-id
(fn [db [_ type only-location]]
@@ -82,6 +105,20 @@
(fn [db]
(-> db ::params)))
;; EVENTS
(re-frame/reg-event-db
::ledger-list-closing
(fn [db]
(assoc db ::ledger-list-active? false)))
(re-frame/reg-event-db
::ledger-list-received
(fn [db [_ ledger-list]]
(assoc db
::ledger-list ledger-list
::ledger-list-loading false)))
(re-frame/reg-event-fx
::params-change
(fn [cofx [_ params]]
@@ -111,6 +148,43 @@
:from-date from
:to-date to
:selected selected)]}))
(re-frame/reg-event-fx
::investigate-clicked
(fn [{:keys [db] } [_ location from-numeric-code to-numeric-code which]]
{:db (assoc db
::ledger-list-active? true
::ledger-list-loading true)
:graphql {:token (-> db :user)
:query-obj {:venia/queries [[:ledger-page
{:client-id (:id @(re-frame/subscribe [::subs/client]))
:from-numeric-code from-numeric-code
:to-numeric-code to-numeric-code
:location location
:from-date (if (= :current which)
(:from-date (::params db))
(date->str (t/minus (str->date (:from-date (::params db)) standard) (t/years 1))
standard))
:to-date (if (= :current which)
(:to-date (::params db))
(date->str (t/minus (str->date (:to-date (::params db)) standard) (t/years 1))
standard))}
[[: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 [::ledger-list-received]}}))
(def groupings
{:expense [["1100 Cash and Bank Accounts" 1100 1199]
@@ -159,9 +233,7 @@
["9800 Other Expenses" 9800 9899]
["9900 Tax Only Expenses" 9900 9999]]})
(defn grouping [{:keys [header accounts comparable-accounts groupings]}]
(defn grouping [{:keys [header accounts comparable-accounts groupings location]}]
(for [[grouping-name from to] groupings
:let [matching-accounts (filter
#(<= from (:numeric-code %) to)
@@ -176,14 +248,20 @@
]
(for [account matching-accounts]
[:tr [:td (:name account)]
[:td.has-text-right (->$ (:amount account))]
[:td.has-text-right (->$ (:amount (get comparable-accounts (:id account)) 0))]
[:td.has-text-right [:a {:on-click (dispatch-event [::investigate-clicked location (:numeric-code account) (:numeric-code account) :current])}
(->$ (:amount account))]]
[:td.has-text-right [:a {:on-click (dispatch-event [::investigate-clicked location (:numeric-code account) (:numeric-code account) :comparable])}
(->$ (:amount (get comparable-accounts (:id account)) 0))]]
[:td.has-text-right (->$ (- (:amount account ) (:amount (get comparable-accounts (:id account)) 0)))]])
[:tr [:th "---" grouping-name "---"]
[:th.has-text-right.total (->$ (reduce + 0 (map :amount
matching-accounts))) ]
[:th.has-text-right.total (->$ (reduce + 0 (map #(:amount (get comparable-accounts (:id %)) 0)
matching-accounts)))]
[:th.has-text-right.total [:a
{:on-click (dispatch-event [::investigate-clicked location from to :current])}
(->$ (reduce + 0 (map :amount
matching-accounts)))] ]
[:th.has-text-right.total [:a
{:on-click (dispatch-event [::investigate-clicked location from to :comparable])}
(->$ (reduce + 0 (map #(:amount (get comparable-accounts (:id %)) 0)
matching-accounts)))]]
[:th.has-text-right.total (->$ (reduce + 0
(map #(- (:amount % ) (:amount (get comparable-accounts (:id %)) 0))
matching-accounts)))]
@@ -197,12 +275,17 @@
[:td]
[:td]]
(grouping {:accounts @(re-frame/subscribe [::accounts type location])
:location location
:groupings (type groupings)
:comparable-accounts @(re-frame/subscribe [::comparable-accounts-by-id type location])
})
[:tr [:th.has-text-centered title]
[:th.has-text-right (->$ (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location]))))]
[:th.has-text-right (->$ (reduce + 0 (map :amount (vals @(re-frame/subscribe [::comparable-accounts-by-id type location])))))]
[:th.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location nil nil :current])}
(->$ (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location]))))]]
[:th.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location nil nil :comparable])}
(->$ (reduce + 0 (map :amount (vals @(re-frame/subscribe [::comparable-accounts-by-id type location])))))]]
[:th.has-text-right (->$ (- (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location])))
(reduce + 0 (map :amount (vals @(re-frame/subscribe [::comparable-accounts-by-id type location]))))))]]))
@@ -318,7 +401,23 @@
{:component-will-mount #(re-frame/dispatch-sync [::params-change {:from-date (date->str (t/minus (local-now) (t/period :years 1)) standard)
:to-date (date->str (local-now) standard)}]) }))
(defn ledger-list [_ ]
(let [ledger-page (re-frame/subscribe [::ledger-list])]
[:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::ledger-list-closing])}]
[:div
[:h1.title "Ledger entries"]
[table {:ledger-page ledger-page
:status? false
:status (re-frame/subscribe [::ledger-list-loading])
:params (re-frame/subscribe [::ledger-params])} ]]]))
(defn profit-and-loss-page []
[side-bar-layout
{:side-bar [ledger-side-bar]
:main [profit-and-loss-content]}])
(let [ledger-list-active? @(re-frame/subscribe [::ledger-list-active?])]
[side-bar-layout
{:side-bar [ledger-side-bar]
:main [profit-and-loss-content]
:right-side-bar [appearing-side-bar
{:visible? ledger-list-active?}
[ledger-list]]}]))

View File

@@ -9,7 +9,8 @@
(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]}]
(fn [{:keys [id ledger-page status on-params-change vendors checked status?]
:or {status? true}}]
(let [{:keys [sort-by asc]} @params
{:keys [journal-entries start end count total]} @ledger-page
selected-client @(re-frame/subscribe [::subs/client])
@@ -70,13 +71,14 @@
"Credit"]
(when status?
[sorted-column {:on-sort opc
:style {:width "8em" :cursor "pointer"}
:sort-key "status"
:sort-by sort-by
:asc asc}
"Status"]]]
[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
@@ -98,7 +100,8 @@
[:td.has-text-right (nf amount )]
[:td.has-text-right (nf amount )]
[:td status]]]
(when status?
[:td status])]]
(for [{:keys [debit credit location account]} line-items]
[:tr {:class (:class i)}
(when-not selected-client
@@ -112,7 +115,8 @@
[:td.has-text-right (when debit (nf debit ))]
[:td.has-text-right (when credit (nf credit ))]
[:td status]]
(when status?
[:td status])]
)))
(mapcat identity)))]]]))))