Simplify sysco line item allocation: use actual amounts, default unmatched to food cost (50000)

This commit is contained in:
2026-05-27 13:22:29 -07:00
parent 7db1e07512
commit e5a2d0bbba
2 changed files with 16 additions and 21 deletions

View File

@@ -114,27 +114,17 @@
{}
line-items)
total-line-amount (reduce + 0.0 (vals expense-accounts))
accounts (if (zero? total-line-amount)
[]
(vec (for [[account amount] expense-accounts]
(let [ratio (/ amount total-line-amount)
cents (int (Math/round (* ratio abs-total 100)))]
#:invoice-expense-account {:db/id (random-tempid)
:account account
:location (:invoice/location invoice)
:amount (* 0.01 cents)}))))
accounts (mapv
(fn [a]
(update a :invoice-expense-account/amount
#(with-precision 2
(double (.setScale (bigdec %) 2 java.math.RoundingMode/HALF_UP)))))
accounts)
leftover (with-precision 2 (.round (bigdec (- abs-total
(reduce + 0.0 (map :invoice-expense-account/amount accounts))))
*math-context*))
accounts (if (seq accounts)
(update-in accounts [(dec (count accounts)) :invoice-expense-account/amount] #(+ % (double leftover)))
[])]
leftover (- abs-total total-line-amount)
food-cost-account (get-line-account nil)
adjusted-accounts (if (zero? leftover)
expense-accounts
(update expense-accounts food-cost-account (fnil + 0.0) leftover))
accounts (vec (for [[account amount] adjusted-accounts]
#:invoice-expense-account {:db/id (random-tempid)
:account account
:location (:invoice/location invoice)
:amount (with-precision 2
(double (.setScale (bigdec amount) 2 java.math.RoundingMode/HALF_UP)))}))]
(dissoc (assoc invoice :invoice/expense-accounts accounts) :line-items))))
(defn maybe-code-line-items [invoice]