invoice fixups.
This commit is contained in:
@@ -646,70 +646,73 @@
|
||||
[(Long/parseLong k)
|
||||
(reduce + 0.0
|
||||
(->> values
|
||||
(map (fn [[_ _ amount]]
|
||||
(map (fn [[_ _ _ _ amount]]
|
||||
(- (Double/parseDouble amount))))))
|
||||
]))
|
||||
(into {}))]
|
||||
(->>
|
||||
(for [[invoice-id expense-account-id amount expense-account location] (drop 1 data)
|
||||
(for [[invoice-id invoice-expense-account-id target-account _ amount expense-account location] (drop 1 data)
|
||||
:let [
|
||||
invoice-id (Long/parseLong invoice-id)
|
||||
|
||||
invoice (d/entity db invoice-id)
|
||||
current-total (:invoice/total invoice)
|
||||
target-total (invoice-totals invoice-id)
|
||||
target-total (invoice-totals invoice-id) ;; TODO should include expense accounts not visible
|
||||
new-account? (not (boolean (or (some-> invoice-expense-account-id not-empty Long/parseLong)
|
||||
(:db/id (first (:invoice/expense-accounts invoice))))))
|
||||
|
||||
expense-account-id (Long/parseLong expense-account-id)
|
||||
current-expense-account-code (:account/numeric-code (:invoice-expense-account/account (d/entity db expense-account-id)))
|
||||
target-expense-account-code (Long/parseLong (str/trim expense-account))
|
||||
[[target-expense-account-id]] (vec (d/q
|
||||
'[:find ?a
|
||||
:in $ ?c
|
||||
:where [?a :account/numeric-code ?c]
|
||||
]
|
||||
db target-expense-account-code))
|
||||
invoice-expense-account-id (or (some-> invoice-expense-account-id not-empty Long/parseLong)
|
||||
(:db/id (first (:invoice/expense-accounts invoice)))
|
||||
(d/tempid :db.part/user))
|
||||
invoice-expense-account (when-not new-account?
|
||||
(d/entity db invoice-expense-account-id))
|
||||
current-account-id (:db/id (:invoice-expense-account/account invoice-expense-account))
|
||||
target-account-id (Long/parseLong (str/trim target-account))
|
||||
|
||||
|
||||
current-expense-account-amount (:invoice-expense-account/amount (d/entity db expense-account-id))
|
||||
current-expense-account-amount (:invoice-expense-account/amount invoice-expense-account)
|
||||
target-expense-account-amount (- (Double/parseDouble amount))
|
||||
|
||||
|
||||
current-expense-account-location (:invoice-expense-account/location (d/entity db expense-account-id))
|
||||
current-expense-account-location (:invoice-expense-account/location invoice-expense-account)
|
||||
target-expense-account-location location
|
||||
|
||||
|
||||
[[payment-id payment-amount]] (vec (d/q
|
||||
'[:find ?p ?a
|
||||
:in $ ?i
|
||||
:where [?ip :invoice-payment/invoice ?i]
|
||||
[?ip :invoice-payment/amount ?a]
|
||||
[?ip :invoice-payment/payment ?p]
|
||||
]
|
||||
db invoice-id))]]
|
||||
[[payment-id payment-amount invoice-payment]] (vec (d/q
|
||||
'[:find ?p ?a ?ip
|
||||
:in $ ?i
|
||||
:where [?ip :invoice-payment/invoice ?i]
|
||||
[?ip :invoice-payment/amount ?a]
|
||||
[?ip :invoice-payment/payment ?p]
|
||||
]
|
||||
db invoice-id))]]
|
||||
|
||||
[
|
||||
(when (not (auto-ap.utils/dollars= current-total target-total))
|
||||
(if payment-id
|
||||
(println "Cannot update" invoice-id " of " current-total "to be" target-total "because it has a payment (" payment-id ") of" payment-amount )
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total}))
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total})
|
||||
|
||||
(when (and (not (auto-ap.utils/dollars= current-expense-account-amount target-expense-account-amount))
|
||||
(or (auto-ap.utils/dollars= current-total target-total)
|
||||
(not payment-id)))
|
||||
{:db/id expense-account-id
|
||||
{:db/id invoice-id
|
||||
:invoice/expense-accounts invoice-expense-account-id}
|
||||
|
||||
(when (and
|
||||
(not (auto-ap.utils/dollars= current-total target-total))
|
||||
invoice-payment)
|
||||
[:db/retractEntity invoice-payment])
|
||||
|
||||
(when (or new-account?
|
||||
(not (auto-ap.utils/dollars= current-expense-account-amount target-expense-account-amount)))
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/amount target-expense-account-amount})
|
||||
|
||||
(when (not= current-expense-account-location
|
||||
target-expense-account-location)
|
||||
{:db/id expense-account-id
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/location target-expense-account-location})
|
||||
|
||||
(when (not= target-expense-account-code current-expense-account-code )
|
||||
{:db/id expense-account-id
|
||||
:invoice-expense-account/account target-expense-account-id})]
|
||||
|
||||
#_(println (auto-ap.utils/dollars= current-total amount) current-total amount current-expense-account-code expense-account-code)
|
||||
)
|
||||
(when (not= current-account-id target-account-id )
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/account target-account-id})])
|
||||
(mapcat identity)
|
||||
(filter identity)
|
||||
vec)))
|
||||
|
||||
Reference in New Issue
Block a user