From 9cb42be9e862a736dd6dc79353725ea151b00b99 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 21 Apr 2020 07:13:42 -0700 Subject: [PATCH] lots of fixes. --- src/clj/auto_ap/datomic/migrate.clj | 22 +++++++- src/clj/auto_ap/datomic/vendors.clj | 30 ++++++++--- src/clj/auto_ap/graphql/vendors.clj | 81 +++++++++++++++-------------- src/clj/auto_ap/parse.clj | 1 - src/clj/auto_ap/routes/invoices.clj | 46 ++++++++-------- 5 files changed, 108 insertions(+), 72 deletions(-) diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index 024fefa9..f6c803c4 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -33,7 +33,7 @@ :db/fn (d/function '{:lang "clojure" :params [db e a amount] :code [[:db/add e a - (-> (d/entity db e) a (+ amount))]] })}]] ) + (-> (d/entity db e) a (+ amount))]] })}]] ) (defn fix-pay-function [conn] [[{:db/ident :pay @@ -53,6 +53,24 @@ :db/valueType :db.type/string :db/cardinality :db.cardinality/one}]]) +(defn reset-function [conn] + [[{:db/ident :reset + :db/doc "Resets a relationship to the values specified " + :db/fn (d/function '{:lang "clojure" + :params [db e a vs] + :code (let [ids (when-not (string? e) + (->> (d/query {:query {:find ['?z] + :in ['$ '?e '?a] + :where [['?e '?a '?z]]} + :args [db e a]}) + (map first))) + new-id-set (set (map :db/id vs)) + retract-ids (filter (complement new-id-set) ids)] + (cond-> [] + true (into (map (fn [i] [:db/retractEntity i ]) retract-ids)) + (seq vs) (conj {:db/id e + a vs})))})}]]) + (defn -main [& args] (println "Creating database ..." uri) (doto (d/create-database uri) println) @@ -174,7 +192,7 @@ :db/doc "Overrides per-client" :db/valueType :db.type/ref :db/cardinality :db.cardinality/many}]]} - }] + :auto-ap/add-reset-rels {:txes-fn `reset-function}}] (println "Conforming database...") (c/ensure-conforms conn norms-map) (when (not (seq args)) diff --git a/src/clj/auto_ap/datomic/vendors.clj b/src/clj/auto_ap/datomic/vendors.clj index 8140f138..fcc166f9 100644 --- a/src/clj/auto_ap/datomic/vendors.clj +++ b/src/clj/auto_ap/datomic/vendors.clj @@ -39,11 +39,25 @@ (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)) + (or + (->> + (filter + (fn [to] + (= (:db/id (:vendor-terms-override/client to)) + client-id)) + (:vendor/terms-overrides vendor)) + first + :vendor-terms-override/terms) + (:vendor/terms vendor))) + +(defn account-for-client-id [vendor client-id] + (or + (->> + (filter + (fn [to] + (= (:db/id (:vendor-account-override/client to)) + client-id)) + (:vendor/account-overrides vendor)) + first + :vendor-account-override/account) + (:vendor/default-account vendor))) diff --git a/src/clj/auto_ap/graphql/vendors.clj b/src/clj/auto_ap/graphql/vendors.clj index 21134b67..b5070a0d 100644 --- a/src/clj/auto_ap/graphql/vendors.clj +++ b/src/clj/auto_ap/graphql/vendors.clj @@ -11,7 +11,7 @@ (defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule terms_overrides account_overrides] :as in} :vendor} value] (when id (assert-admin (:id context))) - (let [term-overrides (mapv + (let [terms-overrides (mapv (fn [to] (cond-> #:vendor-terms-override {:client (:client_id to) @@ -25,47 +25,48 @@ :account (:account_id ao)} (:id ao) (assoc :db/id (:id ao)))) account_overrides) - transaction [(remove-nils #:vendor {:db/id (if id - id - "vendor") - :name name - :code code - :hidden hidden - :terms terms - :print-as print_as - :default-account default_account_id - :invoice-reminder-schedule (keyword invoice_reminder_schedule) - :term-overrides (when (is-admin? (:id context)) - term-overrides) - :account-overrides (when (is-admin? (:id context)) - account-overrides) - :address (when address - (remove-nils #:address {:db/id (if (:id address) - (:id address) - "address") - :street1 (:street1 address) - :street2 (:street2 address) - :city (:city address) - :state (:state address) - :zip (:zip address)})) - :primary-contact (when primary_contact + transaction (cond-> + [(remove-nils #:vendor {:db/id (if id + id + "vendor") + :name name + :code code + :hidden hidden + :terms terms + :print-as print_as + :default-account default_account_id + :invoice-reminder-schedule (keyword invoice_reminder_schedule) + + + :address (when address + (remove-nils #:address {:db/id (if (:id address) + (:id address) + "address") + :street1 (:street1 address) + :street2 (:street2 address) + :city (:city address) + :state (:state address) + :zip (:zip address)})) + :primary-contact (when primary_contact - (remove-nils #:contact {:db/id (if (:id primary_contact) - (:id primary_contact) - "primary") - :name (:name primary_contact) - :phone (:phone primary_contact) - :email (:email primary_contact)})) - - :secondary-contact (when secondary_contact + (remove-nils #:contact {:db/id (if (:id primary_contact) + (:id primary_contact) + "primary") + :name (:name primary_contact) + :phone (:phone primary_contact) + :email (:email primary_contact)})) + + :secondary-contact (when secondary_contact - (remove-nils #:contact {:db/id (if (:id secondary_contact) - (:id secondary_contact) - "secondary") - :name (:name secondary_contact) - :phone (:phone secondary_contact) - :email (:email secondary_contact)}) - )})] + (remove-nils #:contact {:db/id (if (:id secondary_contact) + (:id secondary_contact) + "secondary") + :name (:name secondary_contact) + :phone (:phone secondary_contact) + :email (:email secondary_contact)}) + )})] + (is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/account-overrides account-overrides]) + (is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides])) transaction-result @(d/transact (d/connect uri) transaction)] (-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor")) diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index b90da455..c09fb54e 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -15,7 +15,6 @@ (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 77781b8b..79612bb5 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -11,7 +11,9 @@ [auto-ap.parse.util :as parse-u] [auto-ap.graphql.utils :refer [assert-admin]] [auto-ap.routes.utils :refer [wrap-secure]] - [clj-time.coerce :refer [to-date]] + + [clj-time.core :as time] + [clj-time.coerce :as coerce :refer [to-date]] [ring.middleware.json :refer [wrap-json-response]] [compojure.core :refer [GET POST context defroutes wrap-routes]] @@ -126,26 +128,28 @@ (defn invoice-rows->transaction [rows] (->> rows (mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor]}] - (println account-id vendor) - (let [invoice #:invoice {:db/id (.toString (java.util.UUID/randomUUID)) - :vendor vendor-id - :client client-id - :default-location default-location - :import-status :import-status/imported - #_#_:default-expense-account default-expense-account - :total total - :outstanding-balance (if (= "Cash" check) - 0.0 - total) - :status (if (= "Cash" check) - :invoice-status/paid - :invoice-status/unpaid) - :invoice-number invoice-number - :date (to-date date) - :expense-accounts [#:invoice-expense-account {:account (or account-id - (-> vendor :vendor/default-account :db/id)) - :location default-location - :amount total}]} + (let [invoice (cond-> + #:invoice {:db/id (.toString (java.util.UUID/randomUUID)) + :vendor vendor-id + :client client-id + :default-location default-location + :import-status :import-status/imported + #_#_:default-expense-account default-expense-account + :total total + :outstanding-balance (if (= "Cash" check) + 0.0 + total) + :status (if (= "Cash" check) + :invoice-status/paid + :invoice-status/unpaid) + :invoice-number invoice-number + :date (to-date date) + :expense-accounts [#:invoice-expense-account {:account (or account-id + (:db/id (d-vendors/account-for-client-id vendor client-id))) + :location default-location + :amount total}]} + (:vendor/terms vendor) (assoc :invoice/due (coerce/to-date + (time/plus date (time/days (d-vendors/terms-for-client-id vendor client-id)))))) payment (if (= :invoice-status/paid (:invoice/status invoice)) #:invoice-payment {:invoice (:db/id invoice) :amount (:invoice/total invoice)