start of cash flow

This commit is contained in:
Bryce Covert
2020-05-16 16:08:17 -07:00
parent d5dc62cc6a
commit a7a3966f1b
6 changed files with 170 additions and 22 deletions

View File

@@ -2,6 +2,8 @@
(:require [auto-ap.views.components.layouts :refer [side-bar-layout]]
[re-frame.core :as re-frame]
[auto-ap.subs :as subs]
[cljs-time.core :as t]
[auto-ap.views.utils :refer [local-now date->str]]
[reagent.core :as r]))
@@ -44,9 +46,22 @@
[legend]]
)
(defn make-cash-flow-chart [{:keys [width height data] }]
(println data)
[bar-chart {:width width :height height :data data :fill "#FFFFFF" :stackOffset "sign"}
[tool-tip]
[bar {:dataKey "effective-balance" :fill (get colors 1) :stackId "a" :name "Effective Balance"}]
[bar {:dataKey "outstanding-payments" :fill (get colors 0) :stackId "a" :name "Outstanding Payments"}]
[bar {:dataKey "invoices" :fill (get colors 3) :stackId "a" :name "Invoices"}]
[x-axis {:dataKey "name"}]
[y-axis]
[legend]]
)
(re-frame/reg-event-db
::received
(fn [db [_ {:keys [expense-account-stats invoice-stats]}]]
(fn [db [_ {:keys [expense-account-stats invoice-stats cash-flow]}]]
(let [expense-account-stats (->> expense-account-stats
(map #(update % :total (fn [t] (js/parseFloat t))))
(sort-by :total)
@@ -60,12 +75,14 @@
(assoc ::top-expense-categories (conj top-5 other))
(seq invoice-stats)
(assoc ::invoice-stats invoice-stats)))))
(assoc ::invoice-stats invoice-stats)
(seq cash-flow)
(assoc ::cash-flow cash-flow)))))
(re-frame/reg-sub
::invoice-stats
(fn [db]
(println (::invoice-stats db))
(::invoice-stats db)))
(re-frame/reg-sub
@@ -73,6 +90,39 @@
(fn [db]
(::top-expense-categories db)))
(re-frame/reg-sub
::cash-flow
(fn [db]
(let [{:keys [outstanding-payments beginning-balance invoices-due-soon]} (::cash-flow db)
invoices-due-soon (reduce
(fn [result invoice]
(let [due (if (t/before? (:due invoice) (local-now))
(local-now)
(:due invoice))]
(println due)
(update result (date->str due)
(fn [r] (+ (or r 0.0) (:outstanding-balance invoice))))))
{}
invoices-due-soon)
start-date (local-now)
effective-balance (- beginning-balance outstanding-payments (invoices-due-soon (date->str start-date) 0.0))
]
(reverse
(reduce
(fn [[{:keys [effective-balance] } :as acc] day]
(let [invoices-due-today (invoices-due-soon (date->str (t/plus start-date (t/days day))) 0.0)]
(conj acc
{:name (date->str (t/plus start-date (t/days day)))
:effective-balance (- effective-balance invoices-due-today)
:invoices (- invoices-due-today)})))
(list {:name (date->str start-date)
:effective-balance effective-balance
:invoices (- (invoices-due-soon (date->str start-date) 0.0))
:outstanding-payments (- outstanding-payments)})
(range 1 7))))))
(re-frame/reg-event-fx
::mounted
(fn [{:keys [db]} _]
@@ -83,7 +133,10 @@
[[:account [:id :name]] :total]]
[:invoice_stats
{:client-id (:id @(re-frame/subscribe [::subs/client]))}
[:name :paid :unpaid]]]}
[:name :paid :unpaid]]
[:cash-flow
{:client-id (:id @(re-frame/subscribe [::subs/client]))}
[:beginning-balance :outstanding-payments [:invoices-due-soon [:due :outstanding-balance]]]]]}
:on-success [::received]}}))
(defn home-content []
@@ -97,7 +150,11 @@
(map (fn [x] {:name (:name (:account x)) :value (:total x)}) expense-categories))}))
[:h1.title.is-4 "Upcoming Bills"]
(make-bar-chart {:width 800 :height 500 :data (clj->js
@(re-frame/subscribe [::invoice-stats]))})]}]))
@(re-frame/subscribe [::invoice-stats]))})
[:h1.title.is-4 "Cash Flow"]
(make-cash-flow-chart {:width 800 :height 500
:data (clj->js @(re-frame/subscribe [::cash-flow]))})]}]))
(defn home-page []