basic support for pnl
This commit is contained in:
@@ -1,9 +1,140 @@
|
||||
(ns auto-ap.views.pages.ledger.profit-and-loss
|
||||
(:require [auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]]
|
||||
[auto-ap.views.components.layouts :refer [side-bar-layout]]))
|
||||
(:require [auto-ap.subs :as subs]
|
||||
[auto-ap.views.components.layouts :refer [side-bar-layout]]
|
||||
[goog.string :as gstring]
|
||||
[auto-ap.utils :refer [dollars-0?]]
|
||||
[auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]]
|
||||
[auto-ap.views.utils :refer [date->str date-picker bind-field standard]]
|
||||
[cljs-time.core :as t]
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
(defn profit-and-loss-content []
|
||||
[:div "Profit and loss"])
|
||||
|
||||
(re-frame/reg-sub
|
||||
::report
|
||||
(fn [db]
|
||||
(-> db ::report)))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::assets
|
||||
(fn [db]
|
||||
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Assets" grouping-type))))))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::expenses
|
||||
(fn [db]
|
||||
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Expenses" grouping-type))))))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::liabilities
|
||||
(fn [db]
|
||||
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Liabilities" grouping-type))))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::received
|
||||
(fn [db [_ data]]
|
||||
(println (:profit-and-loss data))
|
||||
(-> db
|
||||
(assoc ::report (:profit-and-loss data))
|
||||
(assoc-in [:status :loading] false))))
|
||||
|
||||
(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 [[:profit-and-loss
|
||||
(assoc params
|
||||
:client-id (:id @(re-frame/subscribe [::subs/client])))
|
||||
[[:balance-sheet-groupings [:grouping-type :name [:accounts [:name :amount]]]]]]]}
|
||||
:on-success [::received]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::date-picked
|
||||
(fn [cofx [_ f date]]
|
||||
(println date)
|
||||
{:dispatch [::params-change (assoc-in @(re-frame/subscribe [::params])
|
||||
f
|
||||
date)]}))
|
||||
|
||||
(defn grouping [groupings]
|
||||
|
||||
[:table.table
|
||||
(for [grouping groupings]
|
||||
(list
|
||||
[:tr [:td {:col-span "2"} "----" (:name grouping) "----"]]
|
||||
(for [account (:accounts grouping)
|
||||
|
||||
:when [(not (dollars-0? (js/parseFloat (:amount account))))]]
|
||||
|
||||
[:tr
|
||||
[:td (:name account) ]
|
||||
[:td.has-text-right (gstring/format "$%.2f" (:amount account))]])
|
||||
|
||||
[:tr [:td "----" (:name grouping) "----"]
|
||||
[:td.has-text-right
|
||||
(->> grouping
|
||||
:accounts
|
||||
(map :amount)
|
||||
(map #(js/parseFloat %))
|
||||
(reduce + 0)
|
||||
(gstring/format "$%.2f" ))]]))])
|
||||
|
||||
(def profit-and-loss-content
|
||||
(with-meta
|
||||
(fn []
|
||||
(let [current-client @(re-frame/subscribe [::subs/client])
|
||||
user @(re-frame/subscribe [::subs/user])
|
||||
params @(re-frame/subscribe [::params])]
|
||||
[:div
|
||||
[:h1.title "Profit and Loss"]
|
||||
[:div.field
|
||||
[:p.help "From"]
|
||||
[bind-field
|
||||
[date-picker {:class-name "input"
|
||||
:class "input"
|
||||
:format-week-number (fn [] "")
|
||||
:previous-month-button-label ""
|
||||
:placeholder "mm/dd/yyyy"
|
||||
:next-month-button-label ""
|
||||
:next-month-label ""
|
||||
:type "date"
|
||||
:field [:from-date]
|
||||
:event [::date-picked]
|
||||
:popper-props (clj->js {:placement "right"})
|
||||
:subscription params}]]]
|
||||
|
||||
[:div.field
|
||||
[:p.help "To"]
|
||||
[bind-field
|
||||
[date-picker {:class-name "input"
|
||||
:class "input"
|
||||
:format-week-number (fn [] "")
|
||||
:previous-month-button-label ""
|
||||
:placeholder "mm/dd/yyyy"
|
||||
:next-month-button-label ""
|
||||
:next-month-label ""
|
||||
:type "date"
|
||||
:field [:to-date]
|
||||
:event [::date-picked]
|
||||
:popper-props (clj->js {:placement "right"})
|
||||
:subscription params}]]]
|
||||
|
||||
[:h2.title "Assets"]
|
||||
[grouping @(re-frame/subscribe [::assets])]
|
||||
[:h2.title "Expenses"]
|
||||
[grouping @(re-frame/subscribe [::expenses])]
|
||||
[:h2.title "Liabilities"]
|
||||
[grouping @(re-frame/subscribe [::liabilities])]
|
||||
]))
|
||||
{:component-will-mount #(re-frame/dispatch-sync [::params-change {:from-date (date->str (t/minus (t/now) (t/period :years 1)) standard)
|
||||
:to-date (date->str (t/now) standard)}]) }))
|
||||
|
||||
(defn profit-and-loss-page []
|
||||
[side-bar-layout
|
||||
|
||||
Reference in New Issue
Block a user