Fixes issue with JFC import, fixes issue where overriden account ids did not take effect.

This commit is contained in:
Bryce
2023-07-11 09:36:40 -07:00
parent 265eebe084
commit 7b0799e7ad
3 changed files with 57 additions and 50 deletions

View File

@@ -293,52 +293,55 @@
vec)
[]))
(defn code-invoice [invoice]
(mu/log ::trying-to-code-invoice
:invoice invoice)
(let [db (dc/db auto-ap.datomic/conn)
client-id (or (:db/id (:invoice/client invoice))
(:invoice/client invoice))
vendor-id (or (:db/id (:invoice/vendor invoice))
(:invoice/vendor invoice))
date (:invoice/date invoice)
vendor (dc/pull db '[*] vendor-id)
due (when (:vendor/terms vendor)
(-> date
(coerce/to-date-time)
(time/plus (time/days (d-vendors/terms-for-client-id vendor client-id)))
coerce/to-date))
automatically-paid? (boolean (seq (map first (dc/q '[:find ?c
:in $ ?v ?c
:where [?v :vendor/automatically-paid-when-due ?c]]
db
vendor-id
client-id))))
[schedule-payment-dom] (map first (dc/q '[:find ?dom
:in $ ?v ?c
:where [?v :vendor/schedule-payment-dom ?sp ]
[?sp :vendor-schedule-payment-dom/client ?c]
[?sp :vendor-schedule-payment-dom/dom ?dom]]
db
vendor-id
client-id))
(defn code-invoice
([invoice]
(code-invoice invoice nil))
([invoice override-account-id]
(mu/log ::trying-to-code-invoice
:invoice invoice)
(let [db (dc/db auto-ap.datomic/conn)
client-id (or (:db/id (:invoice/client invoice))
(:invoice/client invoice))
vendor-id (or (:db/id (:invoice/vendor invoice))
(:invoice/vendor invoice))
date (:invoice/date invoice)
vendor (dc/pull db '[*] vendor-id)
due (when (:vendor/terms vendor)
(-> date
(coerce/to-date-time)
(time/plus (time/days (d-vendors/terms-for-client-id vendor client-id)))
coerce/to-date))
automatically-paid? (boolean (seq (map first (dc/q '[:find ?c
:in $ ?v ?c
:where [?v :vendor/automatically-paid-when-due ?c]]
db
vendor-id
client-id))))
[schedule-payment-dom] (map first (dc/q '[:find ?dom
:in $ ?v ?c
:where [?v :vendor/schedule-payment-dom ?sp ]
[?sp :vendor-schedule-payment-dom/client ?c]
[?sp :vendor-schedule-payment-dom/dom ?dom]]
db
vendor-id
client-id))
scheduled-payment (cond automatically-paid?
due
scheduled-payment (cond automatically-paid?
due
schedule-payment-dom
(-> date
coerce/to-date-time
(next-dom schedule-payment-dom)
coerce/to-date)
:else nil)
default-expense-account #:invoice-expense-account {:db/id (random-tempid)
:account (d-vendors/account-for-client-id vendor client-id)
:location (:invoice/location invoice)
:amount (:invoice/total invoice)}]
(cond-> invoice
true (assoc :invoice/expense-accounts [default-expense-account])
due (assoc :invoice/due due)
scheduled-payment (assoc :invoice/scheduled-payment scheduled-payment))))
schedule-payment-dom
(-> date
coerce/to-date-time
(next-dom schedule-payment-dom)
coerce/to-date)
:else nil)
default-expense-account #:invoice-expense-account {:db/id (random-tempid)
:account (or override-account-id (d-vendors/account-for-client-id vendor client-id))
:location (:invoice/location invoice)
:amount (:invoice/total invoice)}]
(cond-> invoice
true (assoc :invoice/expense-accounts [default-expense-account])
due (assoc :invoice/due due)
scheduled-payment (assoc :invoice/scheduled-payment scheduled-payment)))))