lots of new fixes.

This commit is contained in:
Bryce Covert
2020-06-19 10:05:55 -07:00
parent ef843ac9a2
commit d4559a7c2d
23 changed files with 451 additions and 56 deletions

View File

@@ -278,7 +278,11 @@
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one}]]
:depends-on [:auto-ap/add-manager-schema]}
:auto-ap/migrate-include-in-reports {:txes-fn `add-include-in-reports :depends-on [:auto-ap/add-include-in-reports1] }}
:auto-ap/migrate-include-in-reports {:txes-fn `add-include-in-reports :depends-on [:auto-ap/add-include-in-reports1] }
:auto-ap/add-forecasted-transaction-match {:txes [[{:db/ident :transaction/forecast-match
:db/doc "Which forecast this transaction matches, for cashflow"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}]]}}
]
(println "Conforming database...")
(c/ensure-conforms conn norms-map)

View File

@@ -126,6 +126,7 @@
(let [results (->> (d/pull-many db '[* {:transaction/client [:client/name :db/id :client/code]
:transaction/approval-status [:db/ident :db/id]
:transaction/bank-account [:bank-account/name :bank-account/code :bank-account/yodlee-account-id :db/id :bank-account/locations]
:transaction/forecast-match [:db/id :forecasted-transaction/identifier]
:transaction/vendor [:db/id :vendor/name]
:transaction/matched-rule [:db/id :transaction-rule/note]
:transaction/accounts [:transaction-account/amount

View File

@@ -208,11 +208,14 @@
:yodlee_id {:type 'String}
:name {:type 'String}}}
:forecast_match {:fields {:id {:type :id}
:identifier {:type 'String}}}
:transaction {:fields {:id {:type :id}
:amount {:type 'String}
:description_original {:type 'String}
:description_simple {:type 'String}
:location {:type 'String}
:forecast_match {:type :forecast_match}
:status {:type 'String}
:yodlee_merchant {:type :yodlee_merchant}
:client {:type :client}
@@ -355,6 +358,7 @@
}}
:upcoming_transaction {:fields {:amount {:type :money}
:identifier {:type 'String}
:date {:type :iso_date}}}
:cash_flow_result {:fields {:beginning_balance {:type :money}
@@ -633,6 +637,7 @@
:edit_transaction
{:fields {:id {:type :id}
:vendor_id {:type :id}
:forecast_match {:type :id}
:approval_status {:type :transaction_approval_status}
:accounts {:type '(list :edit_expense_account)}}}
@@ -919,7 +924,7 @@
(defn get-cash-flow [context {:keys [client_id]} value]
(when client_id
(let [{:client/keys [weekly-credits weekly-debits ]} (d/pull (d/db (d/connect uri)) '[*] client_id )
(let [{:client/keys [weekly-credits weekly-debits forecasted-transactions ]} (d/pull (d/db (d/connect uri)) '[*] client_id )
total-cash (reduce
(fn [total [credit debit]]
(- (+ total credit)
@@ -953,23 +958,35 @@
'(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 31)))]})))]
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 31)))]})))
forecasted-transactions (->> forecasted-transactions
(filter (fn [{:forecasted-transaction/keys [day-of-month]}]
(>= day-of-month (t/day (time/local-now)))))
(mapv (fn [{:forecasted-transaction/keys [amount identifier day-of-month]}]
{:identifier identifier
:amount amount
:date (coerce/to-date-time (t/local-date (t/year (time/local-now)) (t/month (time/local-now)) day-of-month ))}
)))]
{: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 (mapv
(fn [date]
{:amount (or weekly-credits 0)
:date (coerce/to-date-time date)})
(take 5 (auto-ap.time/day-of-week-seq 1)))
:upcoming_debits (mapv
(fn [date]
{:amount (- (or weekly-debits 0))
:date (coerce/to-date-time date)})
(take 5 (auto-ap.time/day-of-week-seq 1)))})))
:upcoming_credits (into (mapv
(fn [date]
{:amount (or weekly-credits 0)
:date (coerce/to-date-time date)})
(take 5 (time/day-of-week-seq 1)))
(filter #(>= (:amount %) 0) forecasted-transactions))
:upcoming_debits (doto (into (mapv
(fn [date]
{:amount (- (or weekly-debits 0))
:date (coerce/to-date-time date)})
(take 5 (time/day-of-week-seq 1)))
(filter #(< (:amount %) 0) forecasted-transactions))
println)
})))
(def schema
(-> integreat-schema

View File

@@ -58,7 +58,7 @@
set)]
(set/difference existing-ids specified-ids)))
(defn edit-transaction [context {{:keys [id accounts vendor_id approval_status] :as transaction} :transaction} value]
(defn edit-transaction [context {{:keys [id accounts vendor_id approval_status forecast_match] :as transaction} :transaction} value]
(let [existing-transaction (d-transactions/get-by-id id)
_ (assert-can-see-client (:id context) (:transaction/client existing-transaction) )
deleted (deleted-accounts existing-transaction accounts)
@@ -80,17 +80,28 @@
(throw (ex-info (str "Location '" (str/join ", " missing-locations) "' not found on client.") {})) )
@(d/transact (d/connect uri)
(concat [(remove-nils {:db/id id
:transaction/vendor vendor_id
:transaction/approval-status (some->> approval_status
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction/accounts (map transaction-account->entity accounts)
})]
(map (fn [d]
[:db/retract id :transaction/accounts d])
deleted)))
(doto (concat [(remove-nils {:db/id id
:transaction/vendor vendor_id
:transaction/approval-status (some->> approval_status
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction/accounts (map transaction-account->entity accounts)
})
]
(cond forecast_match
[[:db/add id :transaction/forecast-match forecast_match]]
(:db/id (:transaction/forecast-match existing-transaction))
[[:db/retract id :transaction/forecast-match (:db/id (:transaction/forecast-match existing-transaction))]]
:else
[])
(map (fn [d]
[:db/retract id :transaction/accounts d])
deleted))
clojure.pprint/pprint))
(-> (d-transactions/get-by-id id)
approval-status->graphql
->graphql)))

View File

@@ -174,6 +174,15 @@
:multi #"\n"
:multi-match? #"^\s+.*?\d{6,}.*?\$"}
{:vendor "General Produce Company"
:keywords [#"generalproduce.com"]
:extract {:date #"INVOICE DATE.*\n.*?([0-9]+/[0-9]+/[0-9]+)"
:customer-identifier #"SHIP TO.*\n.*\n\s+(.*?)\s{2,}"
:invoice-number #"INVOICE NO.*\n.*?(\d{7,}?)\s+"
:total #"TOTAL\s+([\-0-9,]+\.[0-9]+)"}
:parser {:date [:clj-time "MM/dd/yy"]
:total [:trim-commas-and-negate nil]}}
;; Young's Market Co new statement
{:vendor "Youngs Market"
:keywords [#"(YOUNG'S MARKET COMPANY|Young.*Statement)"]

View File

@@ -329,7 +329,7 @@
:errors (map #(dissoc % :date) error-rows)})
:headers {"Content-Type" "application/edn"}})
(catch Exception e
(println (.toString e))
(println e)
{:status 500
:body (pr-str {:message (.getMessage e)
:error (.toString e)

View File

@@ -97,23 +97,23 @@
(defn apply-rule [transaction rule valid-locations]
(with-precision 2
(let [accounts (vec (mapcat
(fn [tra]
(if (= "Shared" (:transaction-rule-account/location tra))
(map
(fn [location]
{:transaction-account/account (:db/id (:transaction-rule-account/account tra))
:transaction-account/amount (Math/abs (* (/ 1.0 (count valid-locations))
(:transaction-rule-account/percentage tra)
(:transaction/amount transaction)))
:transaction-account/location location})
(fn [tra]
(if (= "Shared" (:transaction-rule-account/location tra))
(map
(fn [location]
{:transaction-account/account (:db/id (:transaction-rule-account/account tra))
:transaction-account/amount (Math/abs (* (/ 1.0 (count valid-locations))
(:transaction-rule-account/percentage tra)
(:transaction/amount transaction)))
:transaction-account/location location})
valid-locations)
[(cond-> {:transaction-account/account (:db/id (:transaction-rule-account/account tra))
:transaction-account/amount (Math/abs (* (:transaction-rule-account/percentage tra)
(:transaction/amount transaction)))}
(:transaction-rule-account/location tra) (assoc :transaction-account/location (:transaction-rule-account/location tra)))]))
(:transaction-rule/accounts rule)))
valid-locations)
[(cond-> {:transaction-account/account (:db/id (:transaction-rule-account/account tra))
:transaction-account/amount (Math/abs (* (:transaction-rule-account/percentage tra)
(:transaction/amount transaction)))}
(:transaction-rule-account/location tra) (assoc :transaction-account/location (:transaction-rule-account/location tra)))]))
(filter (comp seq :transaction-rule-account/account) (:transaction-rule/accounts rule))))
accounts (mapv
(fn [a]
(update a :transaction-account/amount
@@ -123,7 +123,9 @@
leftover (with-precision 2 (.round (bigdec (- (Math/abs (:transaction/amount transaction))
(Math/abs (reduce + 0.0 (map #(:transaction-account/amount %) accounts)))))
*math-context*))
accounts (update-in accounts [(dec (count accounts)) :transaction-account/amount] #(+ % (double leftover)))]
accounts (if (seq accounts)
(update-in accounts [(dec (count accounts)) :transaction-account/amount] #(+ % (double leftover)))
[])]
(assoc transaction
:transaction/matched-rule (:db/id rule)
:transaction/approval-status (:transaction-rule/transaction-approval-status rule)

View File

@@ -23,7 +23,7 @@
(defn day-of-week-seq [day]
(let [next-day (loop [d (auto-ap.time/local-now)]
(let [next-day (loop [d (local-now)]
(if (= (time/day-of-week d) day)
d
(recur (time/plus d (time/days 1)))))]

View File

@@ -141,7 +141,7 @@
(mapcat (fn [client]
(->> client
:client/bank-accounts
(filter :bank-account/yodlee-account-id)
#_(filter :bank-account/yodlee-account-id)
(filter :db/id)
(map (fn [{:keys [:db/id :bank-account/yodlee-account-id] :as bank-account}]
(assoc bank-account
@@ -168,8 +168,9 @@
all-bank-accounts (by :db/id (get-all-bank-accounts))
transaction->bank-account (comp all-bank-accounts :bank-account-id)]
(println "importing manual transactions" transformed-transactions)
(batch-transact
(transactions->txs transformed-transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing)))))
(let [result (batch-transact
(transactions->txs transformed-transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing)))]
(println "imported " (count result)))))
(defn do-import
([]

View File

@@ -201,14 +201,29 @@
:where ['[?e :account/name]]}
:args [(d/db conn)]})))
_ (if-let [bad-rows (seq (->> rows
(group-by (fn [[_ account]]
account))
vals
(filter #(> (count %) 1))
(filter (fn [duplicates]
(apply not= (map rest duplicates))))
#_(map (fn [[[_ account]]]
account))
))]
(throw (Exception. (str "These accounts are duplicated:" (str bad-rows)))))
rows (vec (set (map rest rows)))
txes (transduce
(comp
(filter (fn [[_ account _ override-name]]
(filter (fn [[account _ override-name]]
(and
(not (str/blank? override-name))
(not (str/blank? account)))))
(map (fn parse-map [[_ account account-name override-name _ type]]
(map (fn parse-map [[account account-name override-name _ type]]
(let [code (Integer/parseInt account)
existing-id (:db/id (code->existing-account code))]
(cond-> {:account/client-overrides [{:account-client-override/client client-id
@@ -219,11 +234,15 @@
:account/account-set "default"
:account/numeric-code code
:account/code (str code)
:account/type (keyword "account-type" (str/lower-case type))))))))
:account/type (if (str/blank? type)
:account-type/expense
(keyword "account-type" (str/lower-case type)))))))))
conj
[]
rows)]
#_txes
@(d/transact conn txes)))