You change change expense accounts on invoices

This commit is contained in:
Bryce Covert
2018-09-03 16:00:20 -07:00
parent 2e87b9ddff
commit 11a9e3c39c
5 changed files with 45 additions and 19 deletions

View File

@@ -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 [*]}]}]))

View File

@@ -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}}})

View File

@@ -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)))))

View File

@@ -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?)

View File

@@ -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"