Supports spreading cents.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -9,6 +9,7 @@
|
||||
[auto-ap.routes.invoice :as route]
|
||||
[auto-ap.routes.utils
|
||||
:refer [wrap-client-redirect-unauthenticated]]
|
||||
[auto-ap.rule-matching :as rm]
|
||||
[auto-ap.solr :as solr]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.common-handlers :refer [add-new-entity-handler]]
|
||||
@@ -29,6 +30,7 @@
|
||||
[datomic.api :as dc]
|
||||
[hiccup.util :as hu]
|
||||
[iol-ion.query :refer [dollars=]]
|
||||
[iol-ion.utils :refer [random-tempid]]
|
||||
[malli.core :as mc]
|
||||
[malli.util :as mut]))
|
||||
|
||||
@@ -472,6 +474,36 @@
|
||||
(when-not (dollars= total expense-account-total)
|
||||
(form-validation-error (str "Expense account total (" expense-account-total ") does not equal invoice total (" total ")")))))
|
||||
|
||||
(defn maybe-spread-locations [invoice]
|
||||
(let [valid-locations (pull-attr (dc/db conn) :client/locations (:invoice/client invoice))]
|
||||
(with-precision 2
|
||||
(let [expense-accounts (vec (mapcat
|
||||
(fn [ea]
|
||||
(let [cents-to-distribute (int (Math/round (Math/abs (* (:invoice-expense-account/amount ea) 100))))]
|
||||
(if (= "Shared" (:invoice-expense-account/location ea))
|
||||
(->> valid-locations
|
||||
(map
|
||||
(fn [cents location]
|
||||
(assoc ea
|
||||
:db/id (random-tempid)
|
||||
:invoice-expense-account/amount (* 0.01 cents)
|
||||
:invoice-expense-account/location location))
|
||||
(rm/spread-cents cents-to-distribute (count valid-locations))))
|
||||
[ea])))
|
||||
(:invoice/expense-accounts invoice)))
|
||||
expense-accounts (mapv
|
||||
(fn [a]
|
||||
(update a :invoice-expense-account/amount
|
||||
#(with-precision 2
|
||||
(double (.setScale (bigdec %) 2 java.math.RoundingMode/HALF_UP)))))
|
||||
expense-accounts)
|
||||
leftover (with-precision 2 (.round (bigdec (- (Math/abs (:invoice/total invoice))
|
||||
(Math/abs (reduce + 0.0 (map #(:invoice-expense-account/amount %) expense-accounts)))))
|
||||
*math-context*))
|
||||
accounts (if (seq expense-accounts)
|
||||
(update-in expense-accounts [(dec (count expense-accounts)) :invoice-expense-account/amount] #(+ % (double leftover)))
|
||||
[])]
|
||||
(assoc invoice :invoice/expense-accounts (into [] accounts))))))
|
||||
|
||||
(defrecord NewWizard2 [_ current-step]
|
||||
mm/LinearModalWizard
|
||||
@@ -516,14 +548,7 @@
|
||||
:invoice/outstanding-balance (:invoice/total (:snapshot multi-form-state))
|
||||
:invoice/import-status :import-status/imported
|
||||
:invoice/status :invoice-status/unpaid)
|
||||
|
||||
(update :invoice/expense-accounts
|
||||
(fn [eas]
|
||||
(mapv (fn [ea]
|
||||
(-> ea
|
||||
(assoc :invoice-expense-account/amount
|
||||
(:invoice/total (:snapshot multi-form-state)))))
|
||||
eas)))
|
||||
(maybe-spread-locations)
|
||||
(update :invoice/date coerce/to-date)
|
||||
(update :invoice/due coerce/to-date))]]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user