diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index 94542de6..9b0ec54a 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -62,6 +62,7 @@ :auto-ap/add-default-location-2 {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location-2 :requires [:auto-ap/add-default-location]} :auto-ap/add-import-status {:txes auto-ap.datomic.migrate.invoice-converter/add-import-status :requires [:auto-ap/add-default-location-2]} :auto-ap/add-import-status-existing-invoices {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-import-status-existing-invoices :requires [:auto-ap/add-import-status]} + :auto-ap/fix-check-numbers {:txes-fn 'auto-ap.datomic.migrate.check-numbers/fix-check-numbers :requires [:auto-ap/add-import-status-existing-invoices]} }] (println "Conforming database...") (println (c/ensure-conforms conn norms-map)) diff --git a/src/clj/auto_ap/datomic/migrate/check_numbers.clj b/src/clj/auto_ap/datomic/migrate/check_numbers.clj new file mode 100644 index 00000000..40d09d79 --- /dev/null +++ b/src/clj/auto_ap/datomic/migrate/check_numbers.clj @@ -0,0 +1,8 @@ +(ns auto-ap.datomic.migrate.check-numbers + (:require [datomic.api :as d])) + +(defn fix-check-numbers [conn] + (let [max-check-numbers (d/query {:query {:find '[?e ?check-number (max ?d)] :in ['$] :where ['[?e :bank-account/check-number ?check-number] '[?c :payment/bank-account ?e] '[?c :payment/check-number ?d]]} :args [(d/db conn)]})] + [(for [[bank-account check-number max-check client] max-check-numbers + :when (>= max-check check-number)] + {:db/id bank-account :payment/check-number (inc max-check)})])) diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index 27e0ac88..58144b6d 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -254,7 +254,6 @@ (-> [] (conj payment) - (conj [:inc (:db/id bank-account) :bank-account/check-number 1]) (into (invoice-payments invoices invoice-amounts))))) @@ -289,7 +288,10 @@ checks (->> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))] (invoices->entities invoices (vendors vendor-id) client bank-account type index invoice-amounts)) (reduce into []) - doall)] + doall) + checks (if (= type :payment-type/check) + (conj checks [:inc (:db/id bank-account) :bank-account/check-number (count invoices-grouped-by-vendor)]) + checks)] (when (= type :payment-type/check) (make-pdfs (filter #(= :payment-type/check (:payment/type %)) checks))) @(d/transact (d/connect uri) checks)