more progress on cash flow.
This commit is contained in:
@@ -345,9 +345,14 @@
|
||||
:errors {:type '(list :import_ledger_entry_result)}
|
||||
}}
|
||||
|
||||
:upcoming_transaction {:fields {:amount {:type :money}
|
||||
:date {:type :iso_date}}}
|
||||
|
||||
:cash_flow_result {:fields {:beginning_balance {:type :money}
|
||||
:invoices_due_soon {:type '(list :invoice)}
|
||||
:outstanding_payments {:type :money}}}
|
||||
:outstanding_payments {:type :money}
|
||||
:upcoming_credits {:type '(list :upcoming_transaction)}
|
||||
:upcoming_debits {:type '(list :upcoming_transaction)}}}
|
||||
}
|
||||
|
||||
|
||||
@@ -363,7 +368,7 @@
|
||||
|
||||
:run_transaction_rule {:type '(list :transaction)
|
||||
:args {:transaction_rule_id {:type :id}}
|
||||
:resolve :run-transaction-rule}
|
||||
:resolve :run-transaction-rule}
|
||||
|
||||
:invoice_stats {:type '(list :invoice_stat)
|
||||
:args {:client_id {:type :id}}
|
||||
@@ -893,46 +898,55 @@
|
||||
(- 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)}))
|
||||
(when client_id
|
||||
(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)
|
||||
:upcoming_credits [{:amount 350000.23
|
||||
:date (coerce/to-date-time (t/plus (auto-ap.time/local-now) (t/days 0)))}
|
||||
{:amount 105000.23
|
||||
:date (coerce/to-date-time (t/plus (auto-ap.time/local-now) (t/days 3)))}]
|
||||
:upcoming_debits [{:amount -120000.23
|
||||
:date (coerce/to-date-time (t/plus (auto-ap.time/local-now) (t/days 5)))}
|
||||
{:amount -35000.23
|
||||
:date (coerce/to-date-time (t/plus (auto-ap.time/local-now) (t/days 4)))}]})))
|
||||
|
||||
(def schema
|
||||
(-> integreat-schema
|
||||
|
||||
Reference in New Issue
Block a user