minor tweak to make it go better
This commit is contained in:
@@ -23,16 +23,14 @@
|
||||
io/reader
|
||||
csv/read-csv)))
|
||||
|
||||
(defn register-invoice-import [args]
|
||||
(let [{:keys [ledger-url]} args
|
||||
data (s3->csv ledger-url)
|
||||
db (d/db conn)
|
||||
i->invoice-id (fn [i]
|
||||
(defn register-invoice-import* [data]
|
||||
(let [db (d/db conn)
|
||||
i->invoice-id (fn [i]
|
||||
(try (Long/parseLong i)
|
||||
(catch Exception e
|
||||
(:db/id (d/pull db '[:db/id]
|
||||
[:invoice/original-id (Long/parseLong (first (str/split i #"-")))])))))
|
||||
invoice-totals (->> data
|
||||
invoice-totals (->> data
|
||||
(drop 1)
|
||||
(group-by first)
|
||||
(map (fn [[k values]]
|
||||
@@ -42,92 +40,96 @@
|
||||
(map (fn [[_ _ _ _ amount]]
|
||||
(- (Double/parseDouble amount))))))
|
||||
]))
|
||||
(into {}))
|
||||
changes (->>
|
||||
(for [[i
|
||||
invoice-expense-account-id
|
||||
target-account
|
||||
target-date
|
||||
amount
|
||||
_
|
||||
location] (drop 1 data)
|
||||
:let [invoice-id (i->invoice-id i)
|
||||
(into {}))]
|
||||
(->>
|
||||
(for [[i
|
||||
invoice-expense-account-id
|
||||
target-account
|
||||
target-date
|
||||
amount
|
||||
target-account-code
|
||||
location] (drop 1 data)
|
||||
:let [invoice-id (i->invoice-id i)
|
||||
|
||||
invoice (d/entity db invoice-id)
|
||||
current-total (:invoice/total invoice)
|
||||
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))))))
|
||||
invoice (d/entity db invoice-id)
|
||||
current-total (:invoice/total invoice)
|
||||
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))))))
|
||||
|
||||
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?
|
||||
(or (d/entity db invoice-expense-account-id)
|
||||
(d/entity db [:invoice-expense-account/original-id 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))
|
||||
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?
|
||||
(or (d/entity db invoice-expense-account-id)
|
||||
(d/entity db [:invoice-expense-account/original-id 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))
|
||||
|
||||
target-date (coerce/to-date (atime/parse target-date atime/normal-date))
|
||||
current-date (:invoice/date invoice)
|
||||
|
||||
target-date (coerce/to-date (atime/parse target-date atime/normal-date))
|
||||
current-date (:invoice/date invoice)
|
||||
|
||||
|
||||
current-expense-account-amount (:invoice-expense-account/amount invoice-expense-account 0.0)
|
||||
target-expense-account-amount (- (Double/parseDouble amount))
|
||||
current-expense-account-amount (:invoice-expense-account/amount invoice-expense-account 0.0)
|
||||
target-expense-account-amount (- (Double/parseDouble amount))
|
||||
|
||||
|
||||
current-expense-account-location (:invoice-expense-account/location invoice-expense-account)
|
||||
target-expense-account-location location
|
||||
current-expense-account-location (:invoice-expense-account/location invoice-expense-account)
|
||||
target-expense-account-location location
|
||||
|
||||
|
||||
[[_ _ 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 current-total]
|
||||
[[_ _ 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 current-total]
|
||||
|
||||
[
|
||||
(when (not (dollars= current-total target-total))
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total})
|
||||
[
|
||||
(when (not (dollars= current-total target-total))
|
||||
{:db/id invoice-id
|
||||
:invoice/total target-total})
|
||||
|
||||
(when new-account?
|
||||
{:db/id invoice-id
|
||||
:invoice/expense-accounts invoice-expense-account-id})
|
||||
(when new-account?
|
||||
{:db/id invoice-id
|
||||
:invoice/expense-accounts invoice-expense-account-id})
|
||||
|
||||
(when (and target-date (not= current-date target-date))
|
||||
{:db/id invoice-id
|
||||
:invoice/date target-date})
|
||||
(when (and target-date (not= current-date target-date))
|
||||
{:db/id invoice-id
|
||||
:invoice/date target-date})
|
||||
|
||||
(when (and
|
||||
(not (dollars= current-total target-total))
|
||||
invoice-payment)
|
||||
[:db/retractEntity invoice-payment])
|
||||
(when (and
|
||||
(not (dollars= current-total target-total))
|
||||
invoice-payment)
|
||||
[:db/retractEntity invoice-payment])
|
||||
|
||||
(when (or new-account?
|
||||
(not (dollars= current-expense-account-amount target-expense-account-amount)))
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/amount target-expense-account-amount})
|
||||
(when (or new-account?
|
||||
(not (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 invoice-expense-account-id
|
||||
:invoice-expense-account/location target-expense-account-location})
|
||||
(when (not= current-expense-account-location
|
||||
target-expense-account-location)
|
||||
{:db/id invoice-expense-account-id
|
||||
:invoice-expense-account/location target-expense-account-location})
|
||||
|
||||
(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)]
|
||||
(doseq [n (partition-all 50 changes)]
|
||||
(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)))
|
||||
|
||||
(defn register-invoice-import [args]
|
||||
(let [{:keys [ledger-url]} args
|
||||
data (s3->csv ledger-url)]
|
||||
(log/info "contains " (count data) " rows")
|
||||
(doseq [n (partition-all 50 (register-invoice-import* data))]
|
||||
(log/info "transacting" n)
|
||||
@(d/transact conn n))
|
||||
))
|
||||
@(d/transact conn n))))
|
||||
|
||||
(defn -main [& _]
|
||||
(execute "register-invoice-import" #(register-invoice-import (:args env))))
|
||||
|
||||
@@ -131,16 +131,9 @@
|
||||
(defn register-invoice-import-button []
|
||||
[form-builder/builder {:submit-event [::request :register-invoice-import]
|
||||
:id ::register-invoice-import-form}
|
||||
[:p.field.has-addons
|
||||
[:p.control
|
||||
[:a.button.is-static.is-small
|
||||
"s3://data.prod.app.integreatconsult.com/bulk-import/"]]
|
||||
[:p.control
|
||||
[form-builder/raw-field-v2 {:field :ledger-url}
|
||||
[:input.input.is-small {:placeholder "invoices.csv"}]]]
|
||||
[:p.control
|
||||
[form-builder/submit-button {:class "is-small"} "Register Invoice Import"]
|
||||
]]])
|
||||
[:p.control
|
||||
[form-builder/submit-button {:class "is-small"} "Register Invoice Import"]
|
||||
]])
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user