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))) (reduce #(update-in %1 [:query :where] conj %2) query rest)))
(def default-read '(pull ?e [* (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/vendor [:vendor/name :db/id]}
{:invoice/status [:db/ident]} {:invoice/status [:db/ident]}
{:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}])) {:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}]))

View File

@@ -272,7 +272,7 @@
:companies {:type '(list Int)}}} :companies {:type '(list Int)}}}
:edit_expense_account :edit_expense_account
{:fields {:id {:type 'Int} {:fields {:id {:type 'String}
:expense_account_id {:type 'Int} :expense_account_id {:type 'Int}
:location {:type 'String} :location {:type 'String}
:amount {:type 'String}}} :amount {:type 'String}}}
@@ -330,7 +330,7 @@
:args {:payment_id {:type 'String}} :args {:payment_id {:type 'String}}
:resolve :mutation/void-payment} :resolve :mutation/void-payment}
:edit_expense_accounts {:type :invoice :edit_expense_accounts {:type :invoice
:args {:invoice_id {:type 'Int} :args {:invoice_id {:type 'String}
:expense_accounts {:type '(list :edit_expense_account)}} :expense_accounts {:type '(list :edit_expense_account)}}
:resolve :mutation/edit-expense-accounts}}}) :resolve :mutation/edit-expense-accounts}}})

View File

@@ -10,7 +10,8 @@
[auto-ap.time :refer [parse iso-date]] [auto-ap.time :refer [parse iso-date]]
[datomic.api :as d] [datomic.api :as d]
[auto-ap.datomic :refer [uri]] [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] (defn -create-or-get-vendor [vendor-id vendor-name]
(if vendor-id (if vendor-id
@@ -77,13 +78,35 @@
(defn edit-expense-accounts [context args value] (defn edit-expense-accounts [context args value]
(assert-can-see-company (:id context) (:company-id (invoices/get-by-id (:invoice_id args)))) (assert-can-see-company (:id context) (:db/id (:client (d-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 [current-expense-accounts (:invoice/expense-accounts (d-invoices/get-by-id (:invoice_id args)))
(let [forced-location (get-in expense-accounts/expense-accounts [expense_account_id :location])] invoice-id (Long/parseLong (:invoice_id args))
{
:expense-account-id expense_account_id specified-ids (->> (:expense_accounts args)
:location (or forced-location location) (map #(some-> % :id (Long/parseLong )))
:amount (Double/parseDouble amount)}) ) set)
(:expense_accounts args)))
(->graphql existing-ids (->> current-expense-accounts
(invoices/get-by-id (:invoice_id args)))) (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] [clojure.string :as str]
[auto-ap.entities.shared :as shared])) [auto-ap.entities.shared :as shared]))
(s/def ::vendor-id int?) (s/def ::vendor-id string?)
(s/def ::expense-account-id int?) (s/def ::expense-account-id int?)
(s/def ::amount ::shared/money) (s/def ::amount ::shared/money)
(s/def ::location string?) (s/def ::location string?)

View File

@@ -420,8 +420,8 @@
[:vendor [:name :id]] [:vendor [:name :id]]
[:expense_accounts [:amount :id :location :expense_account_id [:expense_accounts [:amount :id :location :expense_account_id
[:expense_account [:id :name [:parent [:id :name]]]]]] [:expense_account [:id :name [:parent [:id :name]]]]]]
[:company [:name :id :locations]] [:client [:name :id :locations]]
[:checks [:amount [:check [:amount :s3_url :check_number ]]]] [:payments [:amount [:payment [:amount :s3_url :check_number ]]]]
]]}]} ]]}]}
:on-success [::expense-accounts-updated]}}))) :on-success [::expense-accounts-updated]}})))
@@ -442,7 +442,8 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::add-expense-account-split ::add-expense-account-split
(fn [db _] (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] (update-in db [::change-expense-accounts :invoice :expense-accounts]
conj {:amount 0 :expense-account-id nil :location (first locations)})))) conj {:amount 0 :expense-account-id nil :location (first locations)}))))
@@ -464,7 +465,9 @@
(map js/parseFloat) (map js/parseFloat)
(map #(or % 0)) (map #(or % 0))
(reduce + 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 [action-modal {:id ::change-expense-accounts
:title "Change expense accounts" :title "Change expense accounts"
:action-text "Save" :action-text "Save"