diff --git a/src/clj/auto_ap/datomic/invoices.clj b/src/clj/auto_ap/datomic/invoices.clj index 5f4d19aa..e94e8bfa 100644 --- a/src/clj/auto_ap/datomic/invoices.clj +++ b/src/clj/auto_ap/datomic/invoices.clj @@ -12,7 +12,7 @@ (reduce #(update-in %1 [:query :where] conj %2) query rest))) (def default-read '(pull ?e [* - {:invoice/client [:client/name :db/id]} + {:invoice/client [:client/name :db/id :client/locations]} {:invoice/vendor [:vendor/name :db/id]} {:invoice/status [:db/ident]} {:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}])) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index b2ab0fbc..18aea21e 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -272,7 +272,7 @@ :companies {:type '(list Int)}}} :edit_expense_account - {:fields {:id {:type 'Int} + {:fields {:id {:type 'String} :expense_account_id {:type 'Int} :location {:type 'String} :amount {:type 'String}}} @@ -330,7 +330,7 @@ :args {:payment_id {:type 'String}} :resolve :mutation/void-payment} :edit_expense_accounts {:type :invoice - :args {:invoice_id {:type 'Int} + :args {:invoice_id {:type 'String} :expense_accounts {:type '(list :edit_expense_account)}} :resolve :mutation/edit-expense-accounts}}}) diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 62666d1e..567dec9a 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -10,7 +10,8 @@ [auto-ap.time :refer [parse iso-date]] [datomic.api :as d] [auto-ap.datomic :refer [uri]] - [clj-time.coerce :as coerce])) + [clj-time.coerce :as coerce] + [clojure.set :as set])) (defn -create-or-get-vendor [vendor-id vendor-name] (if vendor-id @@ -77,13 +78,35 @@ (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]}] - (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)))) + (assert-can-see-company (:id context) (:db/id (:client (d-invoices/get-by-id (:invoice_id args))))) + (let [current-expense-accounts (:invoice/expense-accounts (d-invoices/get-by-id (:invoice_id args))) + invoice-id (Long/parseLong (:invoice_id args)) + + specified-ids (->> (:expense_accounts args) + (map #(some-> % :id (Long/parseLong ))) + set) + + existing-ids (->> current-expense-accounts + (map :db/id) + set) + + deleted (set/difference existing-ids specified-ids) + updated (map + (fn [{:keys [id expense_account_id amount location]}] + {:db/id invoice-id + :invoice/expense-accounts [#:invoice-expense-account { + :amount (Double/parseDouble amount) + :db/id (some-> id Long/parseLong) + :expense-account-id expense_account_id + :location location} + ]} + ) + (:expense_accounts args))] + + + @(d/transact (d/connect uri) (concat updated + (map (fn [d] [:db/retract invoice-id :invoice/expense-accounts d])deleted))) + + + (->graphql + (d-invoices/get-by-id (:invoice_id args))))) diff --git a/src/cljc/auto_ap/entities/invoices_expense_accounts.cljc b/src/cljc/auto_ap/entities/invoices_expense_accounts.cljc index 3ea392fc..7be747d8 100644 --- a/src/cljc/auto_ap/entities/invoices_expense_accounts.cljc +++ b/src/cljc/auto_ap/entities/invoices_expense_accounts.cljc @@ -3,7 +3,7 @@ [clojure.string :as str] [auto-ap.entities.shared :as shared])) -(s/def ::vendor-id int?) +(s/def ::vendor-id string?) (s/def ::expense-account-id int?) (s/def ::amount ::shared/money) (s/def ::location string?) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 01eafc86..ce6ddfc7 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -420,8 +420,8 @@ [:vendor [:name :id]] [:expense_accounts [:amount :id :location :expense_account_id [:expense_account [:id :name [:parent [:id :name]]]]]] - [:company [:name :id :locations]] - [:checks [:amount [:check [:amount :s3_url :check_number ]]]] + [:client [:name :id :locations]] + [:payments [:amount [:payment [:amount :s3_url :check_number ]]]] ]]}]} :on-success [::expense-accounts-updated]}}))) @@ -442,7 +442,8 @@ (re-frame/reg-event-db ::add-expense-account-split (fn [db _] - (let [{{{:keys [locations]} :company} :invoice} @(re-frame/subscribe [::change-expense-accounts])] + (let [{{{:keys [locations]} :client} :invoice :as d} @(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)})))) @@ -464,7 +465,9 @@ (map js/parseFloat) (map #(or % 0)) (reduce + 0)) - does-add-up? (= expense-accounts-total (js/parseFloat total))] + _ (println expense-accounts-total (js/parseFloat total) (= expense-accounts-total (js/parseFloat total))) + _ (println expense-accounts) + does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.01)] [action-modal {:id ::change-expense-accounts :title "Change expense accounts" :action-text "Save"