fixing percentages.

This commit is contained in:
Bryce Covert
2020-06-24 07:47:26 -07:00
parent d4559a7c2d
commit 3de59d3fdc
6 changed files with 119 additions and 31 deletions

View File

@@ -41,6 +41,7 @@
first
first
(update :user/role :db/ident))]
(println "USER" user)
(if user
user
(let [new-user-trans @(d/transact (d/connect uri) [(cond-> new-user

View File

@@ -127,7 +127,7 @@
:transaction/payment (:db/id payment)
:transaction/vendor (:db/id (:payment/vendor payment))
:transaction/location "A"
:transaction/accounts [{:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))
:transaction/accounts [{:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
:transaction-account/location "A"
:transaction-account/amount (Math/abs (:transaction/amount transaction))}]}]
(map (fn [x] [:db/retractEntity (:db/id x)] )

View File

@@ -81,7 +81,9 @@
valid-locations (or (:bank-account/locations bank-account) (:client/locations client))
check (transaction->payment transaction check-number client-id bank-account-id amount id)]
:when (and client-id
(not (existing (sha-256 (str id)))))]
(not (existing (sha-256 (str id))))
(= "POSTED" status)
)]
(->
#:transaction
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
@@ -94,7 +96,9 @@
:amount (double amount)
:description-original description-original
:description-simple description-simple
:approval-status :transaction-approval-status/unapproved
:approval-status (if check
:transaction-approval-status/approved
:transaction-approval-status/unapproved)
:type type
:status status
:client client-id

View File

@@ -242,8 +242,8 @@
[]
rows)]
#_txes
@(d/transact conn txes)))
txes
#_@(d/transact conn txes)))

View File

@@ -5,7 +5,7 @@
[goog.string :as gstring]
[auto-ap.utils :refer [dollars-0? by ]]
[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 ->$ str->date]]
[auto-ap.views.utils :refer [date->str date-picker bind-field standard dispatch-event local-now ->% ->$ str->date]]
[cljs-time.core :as t]
[re-frame.core :as re-frame]))
(def ranges
@@ -84,6 +84,30 @@
(<= (get-in ranges [type 0]) numeric-code (get-in ranges [type 1])))))
(sort-by :numeric-code))))
(re-frame/reg-sub
::amount
(fn [[_ type only-location]]
[(re-frame/subscribe [::accounts type only-location])])
(fn [[accounts] _]
(reduce + 0 (map :amount accounts))))
(re-frame/reg-sub
::comparable-amount
(fn [[_ type only-location]]
[(re-frame/subscribe [::comparable-accounts-by-id type only-location])])
(fn [[accounts] _]
(reduce + 0 (map :amount (vals accounts)))))
(re-frame/reg-sub
::comparable-percent-of-sales
(fn [[_ type only-location]]
[(re-frame/subscribe [::comparable-amount :sales only-location])
(re-frame/subscribe [::comparable-amount type only-location])])
(fn [[sales accounts] _]
(if (> (or sales 0) 0 )
(/ accounts sales)
0.0)))
@@ -268,38 +292,55 @@
(defn grouping [{:keys [header accounts comparable-accounts groupings location]}]
(defn grouping [{:keys [header accounts comparable-accounts groupings location sales comparable-sales]}]
(for [[grouping-name from to] groupings
:let [matching-accounts (filter
#(<= from (:numeric-code %) to)
accounts)]
accounts)
total (reduce + 0 (map :amount matching-accounts))
comparable-total (reduce + 0 (map #(:amount (get comparable-accounts (:id %)) 0) matching-accounts))]
:when (seq matching-accounts)
]
(list
^{:key "title"}
[:tr [:th "---" grouping-name "---"]
[:td]
[:td]
[:td]
[:td]
[:td]
]
^{:key "detail"}
(for [account matching-accounts]
^{:key (:name account)}
[:tr [:td (:name account)]
[: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 (->% (if (> sales 0)
(/ (:amount account) sales)
0.0))]
[: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 (->% (if (> comparable-sales 0)
(/ (:amount (get comparable-accounts (:id account)) 0) sales)
0.0))]
[:td.has-text-right (->$ (- (:amount account ) (:amount (get comparable-accounts (:id account)) 0)))]])
^{:key "total"}
[:tr [:th "---" grouping-name "---"]
[:th.has-text-right.total [:a
{:on-click (dispatch-event [::investigate-clicked location from to :current])}
(->$ (reduce + 0 (map :amount
matching-accounts)))] ]
(->$ total)] ]
[:th.has-text-right.total (->% (if (> sales 0)
(/ total sales)
0.0))]
[: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)))]
(->$ comparable-total)]]
[:th.has-text-right.total (->% (if (> comparable-sales 0)
(/ comparable-total sales)
0.0))]
[:th.has-text-right.total (->$ (- total comparable-total))]
[:td]
])))
@@ -308,24 +349,32 @@
min-numeric-code (or (first (map :numeric-code accounts)) 0)
max-numeric-code (or (last (map :numeric-code accounts)) 0)]
(list
^{:key "title"}
[:tr [:th.has-text-centered title]
[:td]
[:td]
[:td]]
^{:key "grouping"}
(grouping {:accounts accounts
:location location
:groupings (type groupings)
:comparable-accounts @(re-frame/subscribe [::comparable-accounts-by-id type location])
})
:sales @(re-frame/subscribe [::amount :sales location])
:comparable-sales @(re-frame/subscribe [::comparable-amount :sales location])})
^{:key "total"}
[:tr [:th.has-text-centered title]
[:th.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location min-numeric-code max-numeric-code :current])}
(->$ (reduce + 0 (map :amount @(re-frame/subscribe [::accounts type location]))))]]
(->$ @(re-frame/subscribe [::amount type location]))]]
[:th.has-text-right (->% @(re-frame/subscribe [::percent-of-sales type location]))]
[:th.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location min-numeric-code max-numeric-code :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]))))))]])))
(->$ @(re-frame/subscribe [::comparable-amount type location]))]]
[:th.has-text-right (->% @(re-frame/subscribe [::comparable-percent-of-sales type location]))]
[:th.has-text-right (->$ (- @(re-frame/subscribe [::amount type location])
@(re-frame/subscribe [::comparable-amount type location])))]])))
(defn subtotal [types negs title location]
(let [accounts (transduce (comp
@@ -348,17 +397,26 @@
[]
types)
min-numeric-code (or (first (map :numeric-code accounts)) 0)
max-numeric-code (or (last (map :numeric-code accounts)) 0)]
(list
max-numeric-code (or (last (map :numeric-code accounts)) 0)
sales @(re-frame/subscribe [::amount :sales location])
comparable-sales @(re-frame/subscribe [::comparable-amount :sales location])]
[:tr [:th.has-text-centered title]
[:th.has-text-right [:a
[:td.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location min-numeric-code max-numeric-code :current])}
(->$ (reduce + 0 (map :amount accounts)))]]
[:th.has-text-right [:a
[:td.has-text-right (->% (if (> sales 0)
(/ (reduce + 0 (map :amount accounts))
sales)
0))]
[:td.has-text-right [:a
{:on-click (dispatch-event [::investigate-clicked location min-numeric-code max-numeric-code :comparable])}
(->$ (reduce + 0 (map :amount comparable)))]]
[:th.has-text-right (->$ (- (reduce + 0 (map :amount accounts))
(reduce + 0 (map :amount comparable))))]])))
[:td.has-text-right (->% (if (> comparable-sales 0)
(/ (reduce + 0 (map :amount comparable))
comparable-sales)
0))]
[:td.has-text-right (->$ (- (reduce + 0 (map :amount accounts))
(reduce + 0 (map :amount comparable))))]]))
(def profit-and-loss-content
(with-meta
@@ -494,24 +552,38 @@
[:div
[:table.table.compact.balance-sheet
(list
^{:key "title"}
[:tr
[:td.has-text-right "Period ending"]
[:td.has-text-right (date->str (str->date (:to-date params) standard))]
[:td.has-text-right (when (:to-date params)
(date->str (t/minus (str->date (:to-date params) standard) (t/years 1))))]
[:td]]
^{:key "report"}
(for [location @(re-frame/subscribe [::locations])]
^{:key location}
(list
^{:key "sales"}
(overall-grouping :sales (str location " Sales") location)
^{:key "cogs"}
(overall-grouping :cogs (str location " COGS") location)
^{:key "payroll"}
(overall-grouping :payroll (str location " Payroll") location)
^{:key "prime"}
(subtotal [:payroll :cogs] #{} (str location " Prime Costs") location)
^{:key "gross profit"}
(subtotal [:sales :payroll :cogs] #{:payroll :cogs} (str location " Gross Profits") location)
^{:key "controllable"}
(overall-grouping :controllable (str location " Controllable Expenses") location)
^{:key "fixed overhead"}
(overall-grouping :fixed-overhead (str location " Fixed Overhead") location)
^{:key "ownership"}
(overall-grouping :ownership-controllable (str location " Ownership Controllable") location)
^{:key "sub-overhead"}
(subtotal [:controllable :fixed-overhead :ownership-controllable] #{} (str location " Overhead") location)
^{:key "sub-loc-income"}
(subtotal [:sales :cogs :payroll :controllable :fixed-overhead :ownership-controllable] #{:cogs :payroll :controllable :fixed-overhead :ownership-controllable} (str location " Net Income") location)
^{:key "sub-net-income"}
(subtotal [:sales :cogs :payroll :controllable :fixed-overhead :ownership-controllable] #{:cogs :payroll :controllable :fixed-overhead :ownership-controllable} "Net Income" nil))))]])])))
{: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)}]) }))

View File

@@ -28,6 +28,17 @@
(defn- nf%
[num]
(.format (doto
(NumberFormat. Format/PERCENT)
(.setMaximumFractionDigits 1)
(.setMinimumFractionDigits 1))
(str num)))
(defn ->% [x]
(nf% x))
(defn active-when= [active-page candidate]
(when (= active-page candidate) " is-active"))