makes all invoice operations work again

This commit is contained in:
2023-03-27 14:11:20 -07:00
3 changed files with 222 additions and 61 deletions

View File

@@ -317,7 +317,8 @@
}]
(map
(fn [iea]
[:db/retract (:db/id i) :invoice/expense-accounts (:db/id iea)])
{:db/id (:db/id iea)
:invoice-expense-account/amount 0.0})
(:invoice/expense-accounts i))))))
(:id context))
{:message (str "Succesfully voided " (count all-ids))}))
@@ -336,6 +337,8 @@
:in ['$ '?e]}
:args [history id]})
[last-transaction] (->> txs (sort-by first) (last))]
(mu/log ::here
:txes txs)
(transact-with-ledger [(->> txs
(filter (fn [[tx]] (= tx last-transaction)))
(reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]]
@@ -385,6 +388,20 @@
(:id context))))
;; TODO - multiple versions of this now exist. fix in datomic migration?
;; Approach could be something like this: (thanks chatgpt)
;; (defn distribute-cents [amount categories]
;; (let [dollars (quot amount 1)
;; cents (rem amount 1)
;; cents-per-category (quot cents (count categories))
;; extra-cents (rem cents (count categories))]
;; (zipmap categories (repeat cents-per-category))
;; (reduce-kv (fn [acc i category]
;; (update acc category + (if (< i extra-cents) 1 0)))
;; acc
;; categories)))
;; convert to/from decimal first
;; for categories, pass in the accounts, and return a tuple of [account amount]
;; after return, just use (assoc account :invoice-expnese-account/amount amount)
(defn maybe-code-accounts [invoice account-rules valid-locations]
(with-precision 2
(let [accounts (vec (mapcat
@@ -397,11 +414,13 @@
(->> valid-locations
(map
(fn [cents location]
{:invoice-expense-account/account (:account_id ar)
{:db/id (random-tempid)
:invoice-expense-account/account (:account_id ar)
:invoice-expense-account/amount (* 0.01 cents)
:invoice-expense-account/location location})
(rm/spread-cents cents-to-distribute (count valid-locations)))))
[(cond-> {:invoice-expense-account/account (:account_id ar)
[(cond-> {:db/id (random-tempid)
:invoice-expense-account/account (:account_id ar)
:invoice-expense-account/amount (* 0.01 cents-to-distribute)}
(:location ar) (assoc :invoice-expense-account/location (:location ar)))])))
account-rules))
@@ -417,7 +436,7 @@
accounts (if (seq accounts)
(update-in accounts [(dec (count accounts)) :invoice-expense-account/amount] #(+ % (double leftover)))
[])]
[:reset (:db/id invoice) :invoice/expense-accounts accounts])))
accounts)))
@@ -451,7 +470,8 @@
(log/info "Bulk coding " (count all-ids) args)
(transact-batch-with-ledger
(map (fn [i]
(maybe-code-accounts i (:accounts args) locations))
`(upsert-entity ~{:db/id (:db/id i)
:invoice/expense-accounts (maybe-code-accounts i (:accounts args) locations)}))
invoices)
(:id context))
{:message (str "Successfully coded " (count all-ids) " invoices.")}))