made the profit and loss report work correctly.

This commit is contained in:
Bryce Covert
2019-04-18 07:20:19 -07:00
parent 4c7cd1ff3e
commit 81a13bf2e0
3 changed files with 116 additions and 72 deletions

View File

@@ -68,6 +68,7 @@
:balance_sheet_account :balance_sheet_account
{:fields {:id {:type 'String} {:fields {:id {:type 'String}
:amount {:type 'String} :amount {:type 'String}
:location {:type 'String}
:numeric_code {:type 'Int} :numeric_code {:type 'Int}
:account_type {:type :account_type} :account_type {:type :account_type}
:name {:type 'String}}} :name {:type 'String}}}

View File

@@ -53,6 +53,7 @@
{:name (str (or (:bank-account/name account) (:account/name account)) (when-not (#{"A" } location) {:name (str (or (:bank-account/name account) (:account/name account)) (when-not (#{"A" } location)
(str (str
"-" location))) "-" location)))
:location location
:id (str (:db/id account) "-" location) :id (str (:db/id account) "-" location)
:numeric-code (or (:account/numeric-code account) :numeric-code (or (:account/numeric-code account)
(and (:bank-account/type account) (and (:bank-account/type account)
@@ -106,39 +107,11 @@
starting-accounts (roll-up starting-results) starting-accounts (roll-up starting-results)
ending-accounts (roll-up ending-results) ending-accounts (roll-up ending-results)
pnl starting-accounts
comparable-pnl ending-accounts]
accounts (reduce-kv
(fn [result account line-item]
(update-in result [(cond
(expense-account? account)
"Expenses"
(credit-account? account)
"Liabilities"
(debit-account? account)
"Assets")
(if (credit-account? account)
"Accounts Payable"
"1100 Cash and Bank Accounts" )
]
conj
(update line-item :amount #(- % (:amount (starting-accounts account 0) 0)) )
))
{}
ending-accounts)
]
(->graphql (->graphql
{:balance-sheet-groupings {:balance-sheet-accounts pnl
(-> [] :comparable-balance-sheet-accounts comparable-pnl})))
(into (->> (get accounts "Expenses")
(map (fn [[n accounts]]
{:name n
:grouping-type "Expenses"
:accounts accounts}
)))))})))
#_(get-profit-and-loss nil {:client_id [:client/code "CBC"] #_(get-profit-and-loss nil {:client_id [:client/code "CBC"]
:from_date "2018-01-01" :from_date "2018-01-01"

View File

@@ -2,12 +2,25 @@
(:require [auto-ap.subs :as subs] (:require [auto-ap.subs :as subs]
[auto-ap.views.components.layouts :refer [side-bar-layout]] [auto-ap.views.components.layouts :refer [side-bar-layout]]
[goog.string :as gstring] [goog.string :as gstring]
[auto-ap.utils :refer [dollars-0?]] [auto-ap.utils :refer [dollars-0? by ]]
[auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]] [auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]]
[auto-ap.views.utils :refer [date->str date-picker bind-field standard dispatch-event local-now]] [auto-ap.views.utils :refer [date->str date-picker bind-field standard dispatch-event local-now ->$ str->date]]
[cljs-time.core :as t] [cljs-time.core :as t]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
(re-frame/reg-sub
::locations
(fn [db]
(->> db
::report
:balance-sheet-accounts
(map :location)
(concat (->> db
::report
:comparable-balance-sheet-accounts
(map :location)))
(set)
(sort))))
(re-frame/reg-sub (re-frame/reg-sub
::report ::report
@@ -20,19 +33,42 @@
(-> db ::loading))) (-> db ::loading)))
(re-frame/reg-sub (re-frame/reg-sub
::assets ::accounts
(fn [db] (fn [db [_ type only-location]]
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Assets" grouping-type)))))) (->> db
::report
:balance-sheet-accounts
(map #(update % :amount js/parseFloat))
(filter (fn [{:keys [account-type location ]}]
(and (= only-location location)
(= type account-type))))
(sort-by :numeric-code))))
(re-frame/reg-sub (re-frame/reg-sub
::expenses ::accounts-by-id
(fn [db] (fn [db [_ type only-location]]
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Expenses" grouping-type)))))) (->> db
::report
:balance-sheet-accounts
(map #(update % :amount js/parseFloat))
(filter (fn [{:keys [account-type location]}]
(and (= only-location location)
(= type account-type))))
(by :id))))
(re-frame/reg-sub (re-frame/reg-sub
::liabilities ::comparable-accounts-by-id
(fn [db] (fn [db [_ type only-location]]
(->> db ::report :balance-sheet-groupings (filter (fn [{:keys [grouping-type]}] (= "Liabilities" grouping-type)))))) (->> db
::report
:comparable-balance-sheet-accounts
(map #(update % :amount js/parseFloat))
(filter (fn [{:keys [account-type location]}]
(and (= only-location location)
(= type account-type))))
(by :id))))
(re-frame/reg-event-db (re-frame/reg-event-db
::received ::received
@@ -57,7 +93,8 @@
{:client-id (:id @(re-frame/subscribe [::subs/client])) {:client-id (:id @(re-frame/subscribe [::subs/client]))
:from-date (:from-date params) :from-date (:from-date params)
:to-date (:to-date params)} :to-date (:to-date params)}
[[:balance-sheet-groupings [:grouping-type :name [:accounts [:name :amount]]]]]]]} [[:balance-sheet-accounts [:name :amount :account-type :id :numeric-code :location]]
[:comparable-balance-sheet-accounts [:name :amount :account-type :id :numeric-code :location]]]]]}
:on-success [::received]}})) :on-success [::received]}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -74,33 +111,58 @@
:from-date from :from-date from
:to-date to :to-date to
:selected selected)]})) :selected selected)]}))
(def groupings
{:expense [["Expenses" 4000 4999]
["Expenses 2" 5000 5999]
["Expenses 3" 6000 6999]
["Expenses 4" 7000 7999]
["Expenses 5" 8000 8999]]})
(defn grouping [groupings] (defn grouping [{:keys [header accounts comparable-accounts groupings]}]
[:table.table (for [[grouping-name from to] groupings
[:tbody :let [matching-accounts (filter
(for [grouping groupings] #(<= from (:numeric-code %) to)
^{:key "items"} accounts)]
(list :when (seq matching-accounts)
^{:key "heading"} ]
[:tr [:td {:col-span "2"} "----" (:name grouping) "----"]] (list
(for [account (:accounts grouping)] [:tr [:th "---" grouping-name "---"]
[:td]
[:td]
[:td]
]
(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 (->$ (- (: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 (->$ (reduce + 0
(map #(- (:amount % ) (:amount (get comparable-accounts (:id %)) 0))
matching-accounts)))]
[:td]
])))
(when (not (dollars-0? (js/parseFloat (:amount account)))) (defn overall-grouping [type title location]
^{:key (:name account)} (list
[:tr [:tr [:th.has-text-centered title]
[:td (:name account) ] [:td]
[:td.has-text-right (gstring/format "$%.2f" (:amount account))]])) [:td]
[:td]]
^{:key "footing"} (grouping {:accounts @(re-frame/subscribe [::accounts type location])
[:tr [:td "----" (:name grouping) "----"] :groupings (type groupings)
[:td.has-text-right :comparable-accounts @(re-frame/subscribe [::comparable-accounts-by-id type location])
(->> grouping })
:accounts [:tr [:th.has-text-centered title]
(map :amount) [:th.has-text-right (->$ (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location]))))]
(map #(js/parseFloat %)) [:th.has-text-right (->$ (reduce + 0 (map :amount (vals @(re-frame/subscribe [::comparable-accounts-by-id type location])))))]
(reduce + 0) [:th.has-text-right (->$ (- (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location])))
(gstring/format "$%.2f" ))]]))]]) (reduce + 0 (map :amount (vals @(re-frame/subscribe [::comparable-accounts-by-id type location]))))))]]))
(def profit-and-loss-content (def profit-and-loss-content
(with-meta (with-meta
@@ -200,9 +262,17 @@
(if @(re-frame/subscribe [::loading]) (if @(re-frame/subscribe [::loading])
[:div [:i.icon.fa.fa-spin.fa-spinner]] [:div [:i.icon.fa.fa-spin.fa-spinner]]
[:div [:div
[:h2.title "Expenses"] [:table.table.compact.balance-sheet
[grouping @(re-frame/subscribe [::expenses])]]) (list
])) [:tr
[:td.has-text-right "Period ending"]
[:td.has-text-right (date->str (str->date (:date params) standard))]
[:td.has-text-right (when (:date params)
(date->str (t/minus (str->date (:date params) standard) (t/years 1))))]
[:td]]
(for [location @(re-frame/subscribe [::locations])]
(list
(overall-grouping :expense location location))))]])]))
{:component-will-mount #(re-frame/dispatch-sync [::params-change {:from-date (date->str (t/minus (local-now) (t/period :years 1)) standard) {: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)}]) })) :to-date (date->str (local-now) standard)}]) }))