tweaks for totals.
This commit is contained in:
@@ -505,60 +505,97 @@
|
||||
|
||||
|
||||
(defn get-journal-detail-report [context input _]
|
||||
{:categories
|
||||
(for [client-id (:client_ids input)
|
||||
:let [_ (assert-can-see-client (:id context) client-id)
|
||||
account-lookup (build-account-lookup client-id)
|
||||
c (d/pull (d/db conn) '[:client/locations] client-id) ]
|
||||
location (:client/locations c)
|
||||
category (:categories input)
|
||||
:let [category (<-graphql category)
|
||||
all-journal-entries (->> (get-ledger-page context
|
||||
{:filters {:client_id client-id
|
||||
:location location
|
||||
:date_range (:date_range input)
|
||||
:from_numeric_code (get-in l-reports/ranges [category 0])
|
||||
:to_numeric_code (get-in l-reports/ranges [category 1])
|
||||
:per_page Integer/MAX_VALUE}}
|
||||
nil)
|
||||
:journal_entries
|
||||
(mapcat (fn [je]
|
||||
(->> (je :line_items)
|
||||
(filter (fn [jel]
|
||||
(when-let [account (account-lookup (:id (:account jel)))]
|
||||
(and
|
||||
(<= (get-in l-reports/ranges [category 0])
|
||||
(:numeric_code account)
|
||||
(get-in l-reports/ranges [category 1]))
|
||||
(= location (:location jel)))))
|
||||
)
|
||||
(map (fn [jel ]
|
||||
{:date (:date je)
|
||||
:debit (:debit jel)
|
||||
:credit (:credit jel)
|
||||
:description (or (:name (:vendor je))
|
||||
(:alternate_description je))
|
||||
:account (:account jel)
|
||||
:location (:location jel)}))))))
|
||||
journal-entries-by-account (group-by #(account-lookup (get-in % [:account :id])) all-journal-entries)]
|
||||
[account journal-entries] (conj journal-entries-by-account [nil all-journal-entries])
|
||||
:let [journal-entries (first (reduce
|
||||
(fn [[acc last-je] je]
|
||||
(let [next-je (assoc je :running_balance
|
||||
(- (+ (or (:running_balance last-je 0.0) 0.0)
|
||||
(or (:credit je 0.0) 0.0))
|
||||
(or (:debit je 0.0) 0.0)))]
|
||||
[(conj acc next-je) next-je]))
|
||||
[]
|
||||
(sort-by :date journal-entries)))]]
|
||||
{:category (->graphql category)
|
||||
:client_id client-id
|
||||
:location location
|
||||
:account (or account
|
||||
{:name (str "All " (name category))})
|
||||
:journal_entries (when account (sort-by :date journal-entries))
|
||||
:total (- (or (reduce + 0.0 (map #(or (:credit %) 0.0) journal-entries)) 0.0)
|
||||
(or (reduce + 0.0 (map #(or (:debit %) 0.0) journal-entries)) 0.0))})})
|
||||
(let [category-totals (atom {})
|
||||
base-categories (into []
|
||||
(for [client-id (:client_ids input)
|
||||
:let [_ (assert-can-see-client (:id context) client-id)
|
||||
account-lookup (build-account-lookup client-id)
|
||||
c (d/pull (d/db conn) '[:client/locations] client-id)]
|
||||
location (:client/locations c)
|
||||
category (:categories input)
|
||||
:let [category (<-graphql category)
|
||||
all-journal-entries (->> (get-ledger-page context
|
||||
{:filters {:client_id client-id
|
||||
:location location
|
||||
:date_range (:date_range input)
|
||||
:from_numeric_code (get-in l-reports/ranges [category 0])
|
||||
:to_numeric_code (get-in l-reports/ranges [category 1])
|
||||
:per_page Integer/MAX_VALUE}}
|
||||
nil)
|
||||
:journal_entries
|
||||
(mapcat (fn [je]
|
||||
(->> (je :line_items)
|
||||
(filter (fn [jel]
|
||||
(when-let [account (account-lookup (:id (:account jel)))]
|
||||
(and
|
||||
(<= (get-in l-reports/ranges [category 0])
|
||||
(:numeric_code account)
|
||||
(get-in l-reports/ranges [category 1]))
|
||||
(= location (:location jel)))))
|
||||
)
|
||||
(map (fn [jel ]
|
||||
{:date (:date je)
|
||||
:debit (:debit jel)
|
||||
:credit (:credit jel)
|
||||
:description (or (:name (:vendor je))
|
||||
(:alternate_description je))
|
||||
:account (:account jel)
|
||||
:location (:location jel)}))))))
|
||||
_ (swap! category-totals assoc-in [client-id location category]
|
||||
(- (or (reduce + 0.0 (map #(or (:credit %) 0.0) all-journal-entries)) 0.0)
|
||||
(or (reduce + 0.0 (map #(or (:debit %) 0.0) all-journal-entries)) 0.0)) )
|
||||
journal-entries-by-account (group-by #(account-lookup (get-in % [:account :id])) all-journal-entries)]
|
||||
[account journal-entries] (conj journal-entries-by-account [nil all-journal-entries])
|
||||
:let [journal-entries (first (reduce
|
||||
(fn [[acc last-je] je]
|
||||
(let [next-je (assoc je :running_balance
|
||||
(- (+ (or (:running_balance last-je 0.0) 0.0)
|
||||
(or (:credit je 0.0) 0.0))
|
||||
(or (:debit je 0.0) 0.0)))]
|
||||
[(conj acc next-je) next-je]))
|
||||
[]
|
||||
(sort-by :date journal-entries)))]]
|
||||
{:category (->graphql category)
|
||||
:client_id client-id
|
||||
:location location
|
||||
:account (or account
|
||||
{:name (str "All " (name category))})
|
||||
:journal_entries (when account (sort-by :date journal-entries))
|
||||
:total (- (or (reduce + 0.0 (map #(or (:credit %) 0.0) journal-entries)) 0.0)
|
||||
(or (reduce + 0.0 (map #(or (:debit %) 0.0) journal-entries)) 0.0))}))
|
||||
result {:categories
|
||||
(into base-categories
|
||||
(for [client-id (:client_ids input)
|
||||
:let [_ (assert-can-see-client (:id context) client-id)
|
||||
account-lookup (build-account-lookup client-id)
|
||||
c (d/pull (d/db conn) '[:client/locations] client-id)]
|
||||
location (:client/locations c)
|
||||
line [{:client_id client-id
|
||||
:location location
|
||||
:account {:name "Gross Profit"}
|
||||
:journal_entries nil
|
||||
:total (+ (get-in @category-totals [client-id location :sales] 0.0)
|
||||
(- (get-in @category-totals [client-id location :cogs] 0.0))
|
||||
(- (get-in @category-totals [client-id location :payroll] 0.0)))}
|
||||
{:client_id client-id
|
||||
:location location
|
||||
:account {:name "Overhead"}
|
||||
:journal_entries nil
|
||||
:total (+ (get-in @category-totals [client-id location :controllable] 0.0)
|
||||
(get-in @category-totals [client-id location :fixed-overhead] 0.0)
|
||||
(get-in @category-totals [client-id location :ownership-controllable] 0.0))}
|
||||
{:client_id client-id
|
||||
:location location
|
||||
:account {:name "Net Profit"}
|
||||
:journal_entries nil
|
||||
:total (+ (get-in @category-totals [client-id location :sales] 0.0)
|
||||
(- (get-in @category-totals [client-id location :cogs] 0.0))
|
||||
(- (get-in @category-totals [client-id location :payroll] 0.0))
|
||||
(- (+ (get-in @category-totals [client-id location :controllable] 0.0)
|
||||
(get-in @category-totals [client-id location :fixed-overhead] 0.0)
|
||||
(get-in @category-totals [client-id location :ownership-controllable] 0.0))))}]]
|
||||
line))}]
|
||||
result))
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -244,7 +244,7 @@
|
||||
(conj [:paragraph {:color [128 0 0] :size 9} (:warning report)])
|
||||
(conj
|
||||
(table->pdf report
|
||||
[80 25 80 20 20 20])))
|
||||
[80 25 80 25 25 25])))
|
||||
output-stream)
|
||||
(.toByteArray output-stream)))
|
||||
|
||||
|
||||
@@ -586,24 +586,23 @@
|
||||
{:value (get je :running-balance)
|
||||
:format :dollar}])
|
||||
(:journal-entries category))
|
||||
(when (seq (:journal-entries category))
|
||||
[[
|
||||
{:value (str (client-codes (:client-id category)) " - " (:location category) " - " (:name (:account category)))
|
||||
:bold true
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value (str "Total" )
|
||||
:bold true
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value (:total category)
|
||||
:format :dollar
|
||||
:bold true
|
||||
:border [:top]}]])))
|
||||
[[
|
||||
{:value (str (client-codes (:client-id category)) " - " (:location category) " - " (:name (:account category)))
|
||||
:bold true
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value (str "Total" )
|
||||
:bold true
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value ""
|
||||
:border [:top]}
|
||||
{:value (:total category)
|
||||
:format :dollar
|
||||
:bold true
|
||||
:border [:top]}]]))
|
||||
|
||||
)
|
||||
[]
|
||||
|
||||
Reference in New Issue
Block a user