diff --git a/migrator/migrations/1523022636-DOWN-change_company_to_id.sql b/migrator/migrations/1523022636-DOWN-change_company_to_id.sql new file mode 100644 index 00000000..9c5da794 --- /dev/null +++ b/migrator/migrations/1523022636-DOWN-change_company_to_id.sql @@ -0,0 +1,6 @@ +-- 1523022636 DOWN change_company_to_id + +delete from invoices; + +alter table invoices drop column company_id; +alter table invoices add column company varchar(255); diff --git a/migrator/migrations/1523022636-UP-change_company_to_id.sql b/migrator/migrations/1523022636-UP-change_company_to_id.sql new file mode 100644 index 00000000..4b816aa5 --- /dev/null +++ b/migrator/migrations/1523022636-UP-change_company_to_id.sql @@ -0,0 +1,6 @@ +-- 1523022636 UP change_company_to_id +delete from invoices; + +alter table invoices drop column company; +alter table invoices add column company_id integer; +alter table invoices add constraint fk_invoices__company_id foreign key (company_id) references companies(id); diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index 29134405..1e7bcf05 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -19,10 +19,10 @@ (defn get-unpaid [company] (if company - (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE imported=true AND company = ?" company])) + (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE imported=true AND company_id = ?" (Integer/parseInt company)])) (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=true")))) (defn get-pending [company] (if company - (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company = ?" company])) + (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company_id = ?" (Integer/parseInt company)])) (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null")))) diff --git a/src/clj/auto_ap/handler.clj b/src/clj/auto_ap/handler.clj index 91d145da..f8a06e5d 100644 --- a/src/clj/auto_ap/handler.clj +++ b/src/clj/auto_ap/handler.clj @@ -160,7 +160,7 @@ (for [{:keys [total date invoice-number customer-identifier vendor] :as row} (parse/parse-file (.getPath tempfile) filename)] (assoc row - :company (:name (parse/best-match companies customer-identifier)) + :company-id (:id (parse/best-match companies customer-identifier)) :imported false :potential-duplicate (boolean (seq (filter #(and (= vendor (:vendor %)) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 0944e874..24d5e587 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -1,6 +1,7 @@ (ns auto-ap.events (:require [re-frame.core :as re-frame] [auto-ap.db :as db] + [auto-ap.subs :as subs] [auto-ap.routes :as routes] [auto-ap.effects :as effects] [bidi.bidi :as bidi])) @@ -48,7 +49,7 @@ (re-frame/reg-event-db ::swap-company (fn [db [_ company]] - (assoc db :company company))) + (assoc db :company (:id company)))) (re-frame/reg-event-fx ::set-active-page @@ -69,8 +70,8 @@ {:http {:method :post :token (-> cofx :db :user) :uri (str "/api/invoices/approve" - (when-let [company-name (-> cofx :db :company :name)] - (str "?company=" company-name))) + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) :on-success [::received-invoices :pending] }})) @@ -81,8 +82,8 @@ :http {:method :get :token (-> cofx :db :user) :uri (str "/api/invoices/pending" - (when-let [company-name (-> cofx :db :company :name)] - (str "?company=" company-name))) + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) :on-success [::received-invoices :pending]}})) (re-frame/reg-event-fx @@ -92,8 +93,8 @@ :http {:method :get :token (-> cofx :db :user) :uri (str "/api/invoices/unpaid" - (when-let [company-name (-> cofx :db :company :name)] - (str "?company=" company-name))) + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) :on-success [::received-invoices :unpaid]}})) (re-frame/reg-event-fx @@ -102,8 +103,8 @@ {:http {:method :post :token (-> cofx :db :user) :uri (str "/api/invoices/reject" - (when-let [company-name (-> cofx :db :company :name)] - (str "?company=" company-name))) + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) :on-success [::received-invoices :pending] }})) (re-frame/reg-event-db diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index 69c7b338..268628d5 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -6,7 +6,7 @@ (re-frame/reg-sub ::company (fn [db] - (:company db))) + (get (:companies db) (:company db)))) (re-frame/reg-sub ::companies diff --git a/src/cljs/auto_ap/views/pages/import_invoices.cljs b/src/cljs/auto_ap/views/pages/import_invoices.cljs index 6120984e..9742cdf1 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -25,7 +25,7 @@ :paramName "file" :headers {"Authorization" (str "Token " @token)} :url (str "/pdf-upload" - (when-let [company-name (-> @company :name)] + (when-let [company-name (-> @company :id)] (str "?company=" company-name))) :previewsContainer "#dz-hidden" :previewTemplate "
"})))}))) @@ -56,12 +56,12 @@ [:th "Date"] [:th "Amount"] [:th]]] - [:tbody (for [{:keys [vendor potential-duplicate company customer-identifier invoice-number date total id] :as i} @invoices] - ^{:key (str company "-" invoice-number "-" date "-" total "-" id)} + [:tbody (for [{:keys [vendor potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices] + ^{:key (str company-id "-" invoice-number "-" date "-" total "-" id)} [:tr [:td vendor] - (if company - [:td company] + (if company-id + [:td company-id] [:td [:i.icon.fa.fa-warning {:title "potential duplicate"}] (str "'" customer-identifier "' doesn't match any known company")]) [:td invoice-number]