From 35e7dda7afdb90f9c8669047724e106fe9ac5c6c Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 10 Dec 2021 07:42:50 -0800 Subject: [PATCH] invoice fixups. --- src/clj/auto_ap/graphql/ledger.clj | 3 +- src/clj/auto_ap/parse/templates.clj | 17 ++++++- src/clj/auto_ap/yodlee/import.clj | 4 +- src/clj/user.clj | 77 +++++++++++++++-------------- 4 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 7786f3a2..73b579c4 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -214,7 +214,7 @@ (try (f entry) (catch Exception e - (log/warn e) + (log/warn (.getMessage e)) (assoc entry :error (.getMessage e) :status (or (:status (ex-data e)) :error)))))) @@ -371,6 +371,7 @@ retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:external_id x)]]) success)] (log/info "manual ledger import has " (count success) " new rows") + (log/info errors) (audit-transact-batch retraction (:id context)) diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index 8ce83d8b..f8dbf273 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -166,7 +166,10 @@ :total #"Total\s+([0-9\.,]+)" :account-number #"Customer No.\s+(.*?)\s{2}"} :parser {:date [:clj-time "MM/dd/yy"] - :total [:trim-commas nil]}} + :total [:trim-commas nil]} + :multi (. java.util.regex.Pattern (compile (-> \formfeed str) java.util.regex.Pattern/CASE_INSENSITIVE)) + :multi-match? #"Customer No\." + } ;; AUTO-CHLOR {:vendor "Auto-Chlor" @@ -211,6 +214,18 @@ :parser {:date [:clj-time "MM/dd/yy"] :total [:trim-commas-and-negate nil]}} + + ;;; credits don't have the same format + {:vendor "General Produce Company" + :keywords [#"1330 NORTH B"] + :extract {:date #"DATE.*\n.*\n.*?([0-9]+/[0-9]+/[0-9]+)" + :customer-identifier #"SPECIAL INSTRUCTIONS.*\n\s+(.*?)\s{2,}" + :invoice-number #"CREDIT NO.*\n.*\n.*?(\d{5,}?)\s+" + :account-number #"CUST NO.*\n.*\n\s+(\d+)" + :total #"TOTAL:\s+\|\s*(.*)"} + :parser {:date [:clj-time "MM/dd/yy"] + :total [:trim-commas-and-negate nil]}} + ;; Young's Market Co new statement {:vendor "Youngs Market" :keywords [#"(YOUNG'S MARKET COMPANY|Young.*Statement)"] diff --git a/src/clj/auto_ap/yodlee/import.clj b/src/clj/auto_ap/yodlee/import.clj index 56e768d5..58fd8688 100644 --- a/src/clj/auto_ap/yodlee/import.clj +++ b/src/clj/auto_ap/yodlee/import.clj @@ -367,8 +367,8 @@ (log/info "COUNT" (count (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing)))) (doseq [tx (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))] - (log/info "transacting" tx) - (audit-transact tx {:user/name "Yodlee import" + + (audit-transact tx {:user/name "Yodlee import2" :user/role ":admin"}))))))) diff --git a/src/clj/user.clj b/src/clj/user.clj index cd8f8005..6d701e4a 100644 --- a/src/clj/user.clj +++ b/src/clj/user.clj @@ -646,70 +646,73 @@ [(Long/parseLong k) (reduce + 0.0 (->> values - (map (fn [[_ _ amount]] + (map (fn [[_ _ _ _ amount]] (- (Double/parseDouble amount)))))) ])) (into {}))] (->> - (for [[invoice-id expense-account-id amount expense-account location] (drop 1 data) + (for [[invoice-id invoice-expense-account-id target-account _ amount expense-account location] (drop 1 data) :let [ invoice-id (Long/parseLong invoice-id) invoice (d/entity db invoice-id) current-total (:invoice/total invoice) - target-total (invoice-totals invoice-id) + 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)))))) - expense-account-id (Long/parseLong expense-account-id) - current-expense-account-code (:account/numeric-code (:invoice-expense-account/account (d/entity db expense-account-id))) - target-expense-account-code (Long/parseLong (str/trim expense-account)) - [[target-expense-account-id]] (vec (d/q - '[:find ?a - :in $ ?c - :where [?a :account/numeric-code ?c] - ] - db target-expense-account-code)) + 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? + (d/entity db 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)) + - current-expense-account-amount (:invoice-expense-account/amount (d/entity db expense-account-id)) + current-expense-account-amount (:invoice-expense-account/amount invoice-expense-account) target-expense-account-amount (- (Double/parseDouble amount)) - current-expense-account-location (:invoice-expense-account/location (d/entity db expense-account-id)) + current-expense-account-location (:invoice-expense-account/location invoice-expense-account) target-expense-account-location location - [[payment-id payment-amount]] (vec (d/q - '[:find ?p ?a - :in $ ?i - :where [?ip :invoice-payment/invoice ?i] - [?ip :invoice-payment/amount ?a] - [?ip :invoice-payment/payment ?p] - ] - db invoice-id))]] + [[payment-id payment-amount 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 (not (auto-ap.utils/dollars= current-total target-total)) - (if payment-id - (println "Cannot update" invoice-id " of " current-total "to be" target-total "because it has a payment (" payment-id ") of" payment-amount ) - {:db/id invoice-id - :invoice/total target-total})) + {:db/id invoice-id + :invoice/total target-total}) - (when (and (not (auto-ap.utils/dollars= current-expense-account-amount target-expense-account-amount)) - (or (auto-ap.utils/dollars= current-total target-total) - (not payment-id))) - {:db/id expense-account-id + {:db/id invoice-id + :invoice/expense-accounts invoice-expense-account-id} + + (when (and + (not (auto-ap.utils/dollars= current-total target-total)) + invoice-payment) + [:db/retractEntity invoice-payment]) + + (when (or new-account? + (not (auto-ap.utils/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 expense-account-id + {:db/id invoice-expense-account-id :invoice-expense-account/location target-expense-account-location}) - (when (not= target-expense-account-code current-expense-account-code ) - {:db/id expense-account-id - :invoice-expense-account/account target-expense-account-id})] - - #_(println (auto-ap.utils/dollars= current-total amount) current-total amount current-expense-account-code expense-account-code) - ) + (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)))