From e53ee46b3cf0d68801d3700f89b435e51fd35e01 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 16 Nov 2020 19:28:04 -0800 Subject: [PATCH] Vendor autopay re-instated. --- src/clj/auto_ap/datomic/vendors.clj | 10 ++++++++++ src/clj/auto_ap/graphql/invoices.clj | 9 +++++++-- src/clj/auto_ap/parse/templates.clj | 1 + src/cljs/auto_ap/views/pages/invoices/form.cljs | 5 +++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/clj/auto_ap/datomic/vendors.clj b/src/clj/auto_ap/datomic/vendors.clj index 46df18dc..c402b4d5 100644 --- a/src/clj/auto_ap/datomic/vendors.clj +++ b/src/clj/auto_ap/datomic/vendors.clj @@ -98,3 +98,13 @@ first :vendor-account-override/account) (:vendor/default-account vendor))) + +(defn automatically-paid-for-client-id? [vendor client-id] + (->> + (:vendor/automatically-paid-when-due vendor) + (filter + (fn [client] + (= (:db/id client) + client-id))) + first + boolean)) diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index c16e9194..54293ec2 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -62,6 +62,13 @@ (defn add-invoice-transaction [{:keys [total invoice_number location scheduled_payment client_id vendor_id vendor_name date due expense_accounts] :as in}] (let [vendor (d-vendors/get-by-id vendor_id) account (:vendor/default-account vendor) + due (or (and (:vendor/terms vendor) + (coerce/to-date + (time/plus date (time/days (d-vendors/terms-for-client-id vendor client_id))))) + due) + scheduled_payment (or scheduled_payment + (and (d-vendors/automatically-paid-for-client-id? vendor client_id) + due)) _ (when-not (:db/id account) (throw (ex-info (str "Vendor '" (:vendor/name vendor) "' does not have a default expense acount.") {:vendor-id vendor_id} )))] (cond-> @@ -76,8 +83,6 @@ :invoice/date (coerce/to-date date) :invoice/expense-accounts (map expense-account->entity expense_accounts)} - (:vendor/terms vendor) (assoc :invoice/due (coerce/to-date - (time/plus date (time/days (d-vendors/terms-for-client-id vendor client_id))))) due (assoc :invoice/due (coerce/to-date due)) scheduled_payment (assoc :invoice/scheduled-payment (coerce/to-date scheduled_payment))))) diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index d68db2b2..873af3dc 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -119,6 +119,7 @@ :keywords [#"GOLDEN BRANDS"] :extract {:date #"(?:.*\n){4}(.*)" ;; HOW TO GO TO SPCIFIC LINE :customer-identifier #"Account:(?:.*\n)(.*(?=\s{2,}))" + :account-number #"Account:\s*(.*?)\n" :invoice-number #"Invoice#: (\d+)" :total #"Invoice Total\s+([0-9,]+\.[0-9]{2})"} :parser {:date [:clj-time "EEE MMM dd, yyyy HH:mm aa"] diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 951f1328..9d558b9f 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -141,10 +141,13 @@ ::editing (fn [db [_ which]] (let [accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id (:client which)]) + vendor (get @(re-frame/subscribe [::subs/vendors-by-id]) (:id (:vendor which))) edit-invoice (update which :date #(date->str % standard)) edit-invoice (update edit-invoice :due #(date->str % standard)) edit-invoice (update edit-invoice :scheduled-payment #(date->str % standard)) edit-invoice (assoc edit-invoice :original edit-invoice) + edit-invoice (assoc edit-invoice :vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor))) + (:id (:client which))))) locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))]) ] (-> db @@ -153,6 +156,8 @@ :status (:status edit-invoice) :date (:date edit-invoice) :due (:due edit-invoice) + :vendor-autopay? (boolean ((set (map :id (:automatically-paid-when-due vendor))) + (:id (:client which)))) :scheduled-payment (:scheduled-payment edit-invoice) :invoice-number (:invoice-number edit-invoice) :total (:total edit-invoice)