From 2cb0dd2c70b163fdfacc4cab35fc8a58b74aae50 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 27 Jul 2018 08:08:55 -0700 Subject: [PATCH] forces expense account A or HQ. --- src/clj/auto_ap/graphql.clj | 1 + src/clj/auto_ap/graphql/invoices.clj | 10 +++--- .../views/components/invoice_table.cljs | 2 +- .../auto_ap/views/pages/unpaid_invoices.cljs | 32 +++++++++++-------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 385f83b8..404ed84e 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -127,6 +127,7 @@ :resolve :get-user-companies}}} :expense_account {:fields {:id {:type 'Int} + :location {:type 'String} :name {:type 'String} :parent {:type :expense_account :resolve :get-expense-account-parent}}} diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 6969692f..44653fc3 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -4,6 +4,7 @@ [auto-ap.db.vendors :as vendors] [auto-ap.db.companies :as companies] [auto-ap.db.invoices-expense-accounts :as invoices-expense-accounts] + [auto-ap.expense-accounts :as expense-accounts] [auto-ap.time :refer [parse iso-date]])) (defn -create-or-get-vendor [vendor-id vendor-name] @@ -61,10 +62,11 @@ (defn edit-expense-accounts [context args value] (assert-can-see-company (:id context) (:company-id (invoices/get-by-id (:invoice_id args)))) (invoices-expense-accounts/replace-for-invoice (:invoice_id args) (map (fn [{:keys [id expense_account_id amount location]}] - { - :expense-account-id expense_account_id - :location location - :amount (Double/parseDouble amount)} ) + (let [forced-location (get-in expense-accounts/expense-accounts [expense_account_id :location])] + { + :expense-account-id expense_account_id + :location (or forced-location location) + :amount (Double/parseDouble amount)}) ) (:expense_accounts args))) (->graphql (invoices/get-by-id (:invoice_id args)))) diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index fa0ee398..4270516d 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -45,7 +45,7 @@ [[:invoices [:id :total :outstanding-balance :invoice-number :date [:vendor [:name :id]] [:expense_accounts [:amount :id :expense_account_id :location - [:expense_account [:id :name [:parent [:id :name]]]]]] + [:expense_account [:id :name :location [:parent [:id :name]]]]]] [:company [:name :id :locations]] [:checks [:amount [:check [:amount :s3_url :check_number ]]]] ]] diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 292c631d..26bd37eb 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -8,6 +8,7 @@ [auto-ap.entities.companies :as company] [auto-ap.entities.invoice :as invoice] [auto-ap.entities.vendors :as vendor] + [auto-ap.expense-accounts :as expense-accounts] [auto-ap.entities.invoices-expense-accounts :as invoices-expense-accounts] [auto-ap.views.utils :refer [dispatch-event bind-field horizontal-field date->str str->date pretty standard]] [auto-ap.utils :refer [by replace-if]] @@ -412,8 +413,9 @@ (re-frame/reg-event-db ::add-expense-account-split (fn [db _] - (update-in db [::change-expense-accounts :invoice :expense-accounts] - conj {:amount 0 :expense-account-id nil}))) + (let [{{{:keys [locations]} :company} :invoice} @(re-frame/subscribe [::change-expense-accounts])] + (update-in db [::change-expense-accounts :invoice :expense-accounts] + conj {:amount 0 :expense-account-id nil :location (first locations)})))) (re-frame/reg-event-db ::remove-expense-account-split @@ -425,7 +427,7 @@ (defn change-expense-accounts-modal [] (let [{{:keys [expense-accounts total ] :or {expense-accounts [] total 0} {:keys [locations]} :company} :invoice :as data} @(re-frame/subscribe [::change-expense-accounts]) - should-select-location? (> (count locations) 1) + multi-location? (> (count locations) 1) change-event [::events/change-form [::change-expense-accounts]] expense-accounts-total (->> expense-accounts (map :amount) @@ -446,7 +448,7 @@ [:thead [:tr [:th {:style {:width "500px"}} "Expense Account"] - (when should-select-location? + (when multi-location? [:th {:style {:width "500px"}} "Location"]) [:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "5em"}}]]] @@ -462,17 +464,19 @@ :spec ::invoice/vendor-id :subscription data}]]]] - (when should-select-location? + (when multi-location? [:td - [:div.select - [bind-field - [:select {:type "select" - :field [:invoice :expense-accounts index :location] - :spec (set locations) - :event change-event - :subscription data} - (map (fn [l] [:option {:value l} l]) locations)]]] - ]) + (if-let [forced-location (-> expense-account :expense-account-id expense-accounts/expense-accounts :location)] + [:div.select + [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] + [:div.select + [bind-field + [:select {:type "select" + :field [:invoice :expense-accounts index :location] + :spec (set locations) + :event change-event + :subscription data} + (map (fn [l] [:option {:value l} l]) locations)]]])]) [:td [:div.control