diff --git a/src/clj/auto_ap/datomic/invoices.clj b/src/clj/auto_ap/datomic/invoices.clj index e282fc70..1e94ba90 100644 --- a/src/clj/auto_ap/datomic/invoices.clj +++ b/src/clj/auto_ap/datomic/invoices.clj @@ -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))))) diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index 18693745..ba2df280 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -555,8 +555,8 @@ {:vendor "JFC International" :keywords [#"48490 MILMONT DRIVE"] :extract {:date #"([0-9]+/[0-9]+/[0-9]+)" - :customer-identifier #"SOLD\s+([\S ]+?)(?=(\s{2,}|\n))" - :invoice-number #"(\S+)\s+(?=[0-9]+/[0-9]+/[0-9]+)" + :customer-identifier #"SOLD\s+(?:TO\s+)?([\S ]+?)(?=(\s{2,}|\n))" + :invoice-number #"(\S+)\s+(?:[0-9]+/[0-9]+/[0-9]+)" :total #"(?:INVOICE|TOTAL|CREDIT)\s+([\d\.,\-]+\.[\d\-]+( CR)?)"} :parser {:date [:clj-time "MM/dd/yyyy"] :total [:trim-commas-and-negate nil]}} diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index e16bcd3a..74b995df 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -22,7 +22,8 @@ [digest] [iol-ion.tx :refer [random-tempid]] [ring.middleware.json :refer [wrap-json-response]] - [unilog.context :as lc]) + [unilog.context :as lc] + [auto-ap.logging :as alog]) (:import (java.util UUID))) @@ -105,6 +106,8 @@ (map (c/parse-or-error :invoice-number parse-invoice-number)) (map (c/parse-or-error :total c/parse-amount)) (map (c/parse-or-error :date c/parse-date)))] + (alog/info ::parsed + :rows (take 10 rows)) rows)) @@ -179,7 +182,7 @@ (defn invoice-rows->transaction [rows user] (->> rows - (mapcat (fn [{:keys [vendor-id total client-id date invoice-number default-location check automatically-paid-when-due]}] + (mapcat (fn [{:keys [vendor-id total client-id date invoice-number default-location check automatically-paid-when-due account-id]}] (let [payment-id (.toString (java.util.UUID/randomUUID)) transaction-id (.toString (java.util.UUID/randomUUID)) invoice #:invoice {:db/id (.toString (java.util.UUID/randomUUID)) @@ -234,7 +237,8 @@ :transaction-account/amount (Math/abs (:invoice/total invoice))}]}])) ] [[:propose-invoice (d-invoices/code-invoice (validate-invoice (remove-nils invoice) - user))] + user) + account-id)] (some-> payment remove-nils) transaction]))) (filter identity)))