avoids duplicate invoices atomic, prevents query that causes warnings.
This commit is contained in:
@@ -79,6 +79,24 @@
|
||||
(seq vs) (conj {:db/id e
|
||||
a vs})))})}]])
|
||||
|
||||
|
||||
(defn propose-invoice-fn [conn]
|
||||
[[{:db/ident :propose-invoice
|
||||
:db/doc "Adds an invoice if it's not found"
|
||||
:db/fn (d/function '{:lang "clojure"
|
||||
:params [db invoice]
|
||||
:code (let [existing? (boolean (seq (d/query {:query {:find ['?i]
|
||||
:in ['$ '?invoice-number '?client '?vendor]
|
||||
:where ['[?i :invoice/invoice-number ?invoice-number]
|
||||
'[?i :invoice/client ?client]
|
||||
'[?i :invoice/vendor ?vendor]
|
||||
'(not [?i :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
:args [db (:invoice/invoice-number invoice) (:invoice/client invoice) (:invoice/vendor invoice)]})))]
|
||||
(if existing?
|
||||
[]
|
||||
[invoice]))})}]])
|
||||
|
||||
(defn add-include-in-reports [conn]
|
||||
(let [existing-accounts (->> (d/query {:query {:find ['?e]
|
||||
:in ['$]
|
||||
@@ -335,7 +353,13 @@
|
||||
:db/cardinality :db.cardinality/one}]]}
|
||||
:auto-ap/add-power-user-schema {:txes [[{:db/ident :user-role/power-user}]]}
|
||||
:auto-ap/add-transaction-date-index {:txes [[{:db/ident :transaction/date
|
||||
:db/index true}]]}}
|
||||
:db/index true}]]}
|
||||
:auto-ap/add-invoice-index {:txes [[{:db/ident :invoice/invoice-number
|
||||
:db/index true}]]
|
||||
:depends-on [:auto-ap/base-schema]}
|
||||
|
||||
:auto-ap/add-propose-invoice {:txes-fn `propose-invoice-fn
|
||||
:depends-on [:auto-ap/base-schema]}}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@
|
||||
:status :payment-status/cleared
|
||||
:date (:invoice/date invoice)})}
|
||||
)]
|
||||
[invoice payment])))
|
||||
[[:propose-invoice invoice] payment])))
|
||||
(filter identity)
|
||||
(map remove-nils)
|
||||
))
|
||||
@@ -280,26 +280,26 @@
|
||||
result
|
||||
|
||||
:else
|
||||
(conj result (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
|
||||
:invoice/client-identifier customer-identifier
|
||||
:invoice/vendor (:db/id matching-vendor)
|
||||
:invoice/invoice-number invoice-number
|
||||
:invoice/total (Double/parseDouble total)
|
||||
:invoice/date (to-date date)
|
||||
:invoice/import-status :import-status/pending
|
||||
:invoice/outstanding-balance (or existing-outstanding-balance (Double/parseDouble total))
|
||||
:invoice/status (or existing-status :invoice-status/unpaid)
|
||||
:invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (d-vendors/account-for-client-id matching-vendor (:db/id matching-client))
|
||||
:location matching-location
|
||||
:amount (Double/parseDouble total)}])
|
||||
:db/id existing-id
|
||||
})
|
||||
(:vendor/terms matching-vendor) (assoc :invoice/due (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client))))))
|
||||
(boolean (automatically-paid-for (:db/id matching-client))) (assoc :invoice/scheduled-payment (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client))))))
|
||||
schedule-payment-dom (assoc :invoice/scheduled-payment (to-date
|
||||
(next-dom date schedule-payment-dom))))))
|
||||
(conj result [:propose-invoice (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
|
||||
:invoice/client-identifier customer-identifier
|
||||
:invoice/vendor (:db/id matching-vendor)
|
||||
:invoice/invoice-number invoice-number
|
||||
:invoice/total (Double/parseDouble total)
|
||||
:invoice/date (to-date date)
|
||||
:invoice/import-status :import-status/pending
|
||||
:invoice/outstanding-balance (or existing-outstanding-balance (Double/parseDouble total))
|
||||
:invoice/status (or existing-status :invoice-status/unpaid)
|
||||
:invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (d-vendors/account-for-client-id matching-vendor (:db/id matching-client))
|
||||
:location matching-location
|
||||
:amount (Double/parseDouble total)}])
|
||||
:db/id existing-id
|
||||
})
|
||||
(:vendor/terms matching-vendor) (assoc :invoice/due (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client))))))
|
||||
(boolean (automatically-paid-for (:db/id matching-client))) (assoc :invoice/scheduled-payment (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client))))))
|
||||
schedule-payment-dom (assoc :invoice/scheduled-payment (to-date
|
||||
(next-dom date schedule-payment-dom))))]))
|
||||
))
|
||||
[]
|
||||
imports)]
|
||||
|
||||
Reference in New Issue
Block a user