You change change expense accounts on invoices
This commit is contained in:
@@ -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