diff --git a/src/clj/auto_ap/datomic/vendors.clj b/src/clj/auto_ap/datomic/vendors.clj index 388a9a1c..4f0faba5 100644 --- a/src/clj/auto_ap/datomic/vendors.clj +++ b/src/clj/auto_ap/datomic/vendors.clj @@ -23,3 +23,13 @@ id) (map first) (first))) + +(defn terms-for-client-id [vendor client-id] + (->> + (filter + (fn [to] + (= (:db/id (:vendor-terms-override/client to)) + client-id)) + (:vendor/terms-overrides vendor)) + first + :vendor-terms-override/terms)) diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 7f47f49c..182641f1 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -75,8 +75,9 @@ :invoice/expense-accounts (map expense-account->entity expense_accounts)} (:vendor/terms vendor) (assoc :invoice/due (coerce/to-date - (time/plus date (time/days (:vendor/terms vendor))))) - due (assoc :invoice/due (doto (coerce/to-date due) println))))) + (time/plus date (time/days (or (d-vendors/terms-for-client-id vendor client_id) + (:vendor/terms vendor)))))) + due (assoc :invoice/due (coerce/to-date due))))) (defn deleted-expense-accounts [invoice expense-accounts] diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index c09fb54e..b90da455 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -15,6 +15,7 @@ (defn template-applies? [text {:keys [keywords]}] + (println text) (every? #(re-find % text) keywords)) (defn extract-template diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 10b7fa95..77781b8b 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -142,9 +142,8 @@ :invoice-status/unpaid) :invoice-number invoice-number :date (to-date date) - :expense-accounts [#:invoice-expense-account {:account (doto (or account-id + :expense-accounts [#:invoice-expense-account {:account (or account-id (-> vendor :vendor/default-account :db/id)) - println) :location default-location :amount total}]} payment (if (= :invoice-status/paid (:invoice/status invoice)) @@ -168,19 +167,7 @@ (let [clients (d-clients/get-all) transactions (reduce (fn [result {:keys [invoice-number customer-identifier total date vendor-code text full-text] :as info}] (println "searching for" vendor-code) - (let [[matching-vendor default-account] (->> (d/query - {:query {:find ['?vendor '?default-account] - :in ['$ '?vendor-name] - :where ['[?vendor :vendor/name ?vendor-name] - '[?vendor :vendor/default-account ?default-account]]} - :args [(d/db (d/connect uri)) vendor-code]}) - first) - _ (when-not matching-vendor - (throw (ex-info (str "No vendor with the name " vendor-code " was found.") - {:invoice-number invoice-number - :customer-identifier customer-identifier - :vendor-code vendor-code}))) - _ (println "matching" customer-identifier "-" matching-vendor) + (let [ _ (println "matching" customer-identifier) matching-client (or (and customer-identifier (parse/best-match clients customer-identifier)) (if client @@ -192,7 +179,28 @@ {:invoice-number invoice-number :customer-identifier customer-identifier :vendor-code vendor-code}))) - _ (println "invoice \"" invoice-number "\"matches client " (:client/name matching-client) " (" (:db/id matching-client) ")") + + [matching-vendor default-account] (->> (d/query + {:query {:find ['?vendor '?default-account] + :in ['$ '?vendor-name] + :where ['[?vendor :vendor/name ?vendor-name] + '[?vendor :vendor/default-account ?default-account]]} + :args [(d/db (d/connect uri)) vendor-code]}) + first) + [account-override] (->> (d/query + {:query {:find ['?account-override-account] + :in ['$ '?vendor '?client] + :where ['[?vendor :vendor/account-overrides ?account-override] + '[?account-override :vendor-account-override/client ?client] + '[?account-override :vendor-account-override/account ?account-override-account]]} + :args [(d/db (d/connect uri)) matching-vendor (:db/id matching-client)]}) + first) + _ (when-not matching-vendor + (throw (ex-info (str "No vendor with the name " vendor-code " was found.") + {:invoice-number invoice-number + :customer-identifier customer-identifier + :vendor-code vendor-code}))) + _ (println "invoice \"" invoice-number "\"matches client " (:client/name matching-client) " (" (:db/id matching-client) ")") matching-location (or (when-not (str/blank? forced-location) forced-location) (parse/best-location-match matching-client text full-text)) @@ -235,7 +243,7 @@ :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 default-account + :invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (or account-override default-account) :location matching-location :amount (Double/parseDouble total)}]) :db/id existing-id @@ -289,11 +297,12 @@ (context "/invoices" [] (POST "/upload" - {{ files "file" - client "client" - location "location"} :params :as params} + {{ files :file + client :client + location :location} :params :as params} (let [{:keys [filename tempfile]} files] - (println params) + (clojure.pprint/pprint params) + #_(println params (.getPath tempfile) filename) (try (import-uploaded-invoice client location (parse/parse-file (.getPath tempfile) filename)) {:status 200 diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index e8d776fc..a8cbbdad 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -131,10 +131,18 @@ (re-frame/reg-sub ::vendor-default-account - (fn [db [_ v]] - (let [i (if (:default-account v) - (-> v :default-account :id) - (-> (:vendors db) (get v) :default-account :id))] + (fn [db [_ v client]] + (let [vendor (if (:default-account v) + v + (-> (:vendors db) (get v))) + client-override (->> (:account-overrides vendor) + (filter #(= (:id (:client %)) (:id client)) + ) + first + :account + :id) + default-id (:id (:default-account v)) + i (or client-override default-id)] (first (filter #(= (:id %) i) (:accounts db)))))) diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index d11c032a..cbab1759 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -173,8 +173,6 @@ [:td.has-text-right (nf total )] [:td.has-text-right (nf outstanding-balance )] [:td.expandable - - [:div.buttons (when (seq expense-accounts) [drop-down {:id [::expense-accounts id ] @@ -182,7 +180,6 @@ :aria-haspopup true :on-click (dispatch-event [::events/toggle-menu [::expense-accounts id]]) :tab-index "0" - } "Accounts"]} [drop-down-contents [:div diff --git a/src/cljs/auto_ap/views/pages/admin/rules/form.cljs b/src/cljs/auto_ap/views/pages/admin/rules/form.cljs index 9e2c5228..5e9e2021 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/form.cljs @@ -160,10 +160,9 @@ (fn [data field value] (cond (and (= [:vendor] field) value - (doto (expense-accounts-field/can-replace-with-default? (:accounts data)) - println)) + (expense-accounts-field/can-replace-with-default? (:accounts data))) [[:accounts] (expense-accounts-field/default-account (:accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value]) + @(re-frame/subscribe [::subs/vendor-default-account value (:client data)]) (:total data) [])] diff --git a/src/cljs/auto_ap/views/pages/import_invoices.cljs b/src/cljs/auto_ap/views/pages/import_invoices.cljs index b228fde3..762d7c61 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -91,7 +91,8 @@ (re-frame/reg-event-db ::errored (fn [db [_ error]] - (assoc db ::error (:message (edn/read-string error))))) + (assoc db ::error (or (:message (edn/read-string error)) + "An unknown error has occured.")))) (re-frame/reg-event-fx ::reject-invoices-clicked diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index e8b3241a..09db8d23 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -142,7 +142,7 @@ value (expense-accounts-field/can-replace-with-default? (:expense-accounts data))) [[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value]) + @(re-frame/subscribe [::subs/vendor-default-account value (:client data)]) (:total data) locations)] diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index 448cd479..88b62936 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -69,7 +69,7 @@ value (expense-accounts-field/can-replace-with-default? (:accounts data))) [[:accounts] (expense-accounts-field/default-account (:accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value]) + @(re-frame/subscribe [::subs/vendor-default-account value (:client data)]) (:amount data) locations)] [])))))