You change change expense accounts on invoices
This commit is contained in:
@@ -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 [*]}]}]))
|
||||
|
||||
@@ -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}}})
|
||||
|
||||
|
||||
@@ -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)))))
|
||||
|
||||
Reference in New Issue
Block a user