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

@@ -11,6 +11,7 @@
[auto-ap.datomic :refer [uri merge-query]]
[datomic.api :as d]
[clj-time.coerce :as coerce]
[clj-time.core :as t]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.checks :as d-checks]
[auto-ap.datomic.users :as d-users]
@@ -343,6 +344,10 @@
:existing {:type '(list :import_ledger_entry_result)}
:errors {:type '(list :import_ledger_entry_result)}
}}
:cash_flow_result {:fields {:beginning_balance {:type :money}
:invoices_due_soon {:type '(list :invoice)}
:outstanding_payments {:type :money}}}
}
@@ -364,6 +369,9 @@
:args {:client_id {:type :id}}
:resolve :get-invoice-stats}
:cash_flow {:type :cash_flow_result
:args {:client_id {:type :id}}
:resolve :get-cash-flow}
:potential_payment_matches {:type '(list :payment)
:args {:transaction_id {:type :id}}
:resolve :get-potential-payments}
@@ -880,7 +888,47 @@
0
(- total outstanding-balance))})))
(defn get-cash-flow [context {:keys [client_id]} value]
(let [total-cash (reduce
(fn [total [credit debit]]
(- (+ total credit)
debit))
0.0
(d/query {:query {:find '[?debit ?credit]
:in '[$ ?client]
:where ['[?j :journal-entry/client ?client]
'[?j :journal-entry/line-items ?je]
'[?je :journal-entry-line/account ?ba]
'[?ba :bank-account/type :bank-account-type/check]
'[(get-else $ ?je :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?je :journal-entry-line/credit 0.0) ?credit]]}
:args [(d/db (d/connect uri)) client_id]}))
bills-due-soon (d/query {:query {:find '[?due ?outstanding]
:in '[$ ?client ?due-before]
:where ['[?i :invoice/client ?client]
'[?i :invoice/status :invoice-status/unpaid]
'[?i :invoice/due ?due]
'[(<= ?due ?due-before)]
'[?i :invoice/outstanding-balance ?outstanding]]}
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 7)))]})
outstanding-checks (reduce
+
0.0
(map first (d/query {:query {:find '[?amount]
:in '[$ ?client ?due-before]
:where ['[?p :payment/client ?client]
'[?p :payment/status :payment-status/pending]
'[?p :payment/amount ?amount]
'(or
[?p :payment/type :payment-type/debit]
[?p :payment/type :payment-type/check])]}
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (auto-ap.time/local-now) (t/days 7)))]})))]
{:beginning_balance total-cash
:outstanding_payments outstanding-checks
:invoices_due_soon (mapv (fn [[due outstanding]]
{:due (coerce/to-date-time due)
:outstanding_balance outstanding})
bills-due-soon)}))
(def schema
(-> integreat-schema
@@ -898,6 +946,7 @@
:get-transaction-rule-matches gq-transaction-rules/get-transaction-rule-matches
:get-expense-account-stats get-expense-account-stats
:get-invoice-stats get-invoice-stats
:get-cash-flow get-cash-flow
:get-yodlee-merchants ym/get-yodlee-merchants
:get-client gq-clients/get-client
:get-user get-user

View File

@@ -227,7 +227,47 @@
@(d/transact conn txes)))
(defn cash-flow-simple []
(->> (d/query {:query {:find '[?account-type-ident ?date ?debit ?credit]
(let [total-cash (reduce
(fn [total [credit debit]]
(- (+ total credit)
debit))
0.0
(d/query {:query {:find '[?debit ?credit]
:in '[$ ?client]
:where ['[?j :journal-entry/client ?c]
'[?c :client/code ?client]
'[?j :journal-entry/line-items ?je]
'[?je :journal-entry-line/account ?ba]
'[?ba :bank-account/type :bank-account-type/check]
'[(get-else $ ?je :journal-entry-line/debit 0.0) ?debit]
'[(get-else $ ?je :journal-entry-line/credit 0.0) ?credit]]}
:args [(d/db (d/connect uri)) "CBC"]}))
bills-due-soon (d/query {:query {:find '[?due ?outstanding]
:in '[$ ?client ?due-before]
:where ['[?i :invoice/client ?c]
'[?c :client/code ?client]
'[?i :invoice/status :invoice-status/unpaid]
'[?i :invoice/due ?due]
'[(<= ?due ?due-before)]
'[?i :invoice/outstanding-balance ?outstanding]]}
:args [(d/db (d/connect uri)) "CBC" (c/to-date (t/plus (auto-ap.time/local-now) (t/days 7)))]})
outstanding-checks (reduce
+
0.0
(map first (d/query {:query {:find '[?amount]
:in '[$ ?client ?due-before]
:where ['[?p :payment/client ?c]
'[?c :client/code ?client]
'[?p :payment/status :payment-status/pending]
'[?p :payment/amount ?amount]
'(or
[?p :payment/type :payment-type/debit]
[?p :payment/type :payment-type/check])]}
:args [(d/db (d/connect uri)) "CBC" (c/to-date (t/plus (auto-ap.time/local-now) (t/days 7)))]})))]
[total-cash bills-due-soon outstanding-checks])
#_(->> (d/query {:query {:find '[?account-type-ident ?date ?debit ?credit]
:in '[$ ?client]
:where ['[?j :journal-entry/line-items ?je]
'[?j :journal-entry/date ?date]
@@ -253,3 +293,9 @@
credit)))
(update-in [year-month account-type :count] #(inc (or % 0)))))))
{})))
(defn attach-signature [client-code filename]
@(d/transact (d/connect uri)
[{:db/id [:client/code client-code]
:client/signature-file (str "https://s3.amazonaws.com/integreat-signature-images/" filename)}]))