you can kind of set up more than one account in an invoice.

This commit is contained in:
Bryce Covert
2019-04-16 20:27:14 -07:00
parent e61aaaf721
commit bc4287befe
4 changed files with 84 additions and 33 deletions

View File

@@ -406,6 +406,7 @@
:add_invoice :add_invoice
{:fields {:id {:type :id} {:fields {:id {:type :id}
:invoice_number {:type 'String} :invoice_number {:type 'String}
:expense_accounts {:type '(list :edit_expense_account)}
:location {:type 'String} :location {:type 'String}
:date {:type 'String} :date {:type 'String}
:client_id {:type :id} :client_id {:type :id}

View File

@@ -9,7 +9,7 @@
[auto-ap.time :refer [parse iso-date]] [auto-ap.time :refer [parse iso-date]]
[auto-ap.utils :refer [dollars=]] [auto-ap.utils :refer [dollars=]]
[datomic.api :as d] [datomic.api :as d]
[auto-ap.datomic :refer [uri]] [auto-ap.datomic :refer [uri remove-nils]]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clojure.set :as set])) [clojure.set :as set]))
@@ -51,7 +51,7 @@
:invoice/client client_id})) :invoice/client client_id}))
(throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number :validation-error (str "Invoice '" invoice_number "' already exists.")})))) (throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number :validation-error (str "Invoice '" invoice_number "' already exists.")}))))
(defn add-invoice-transaction [{:keys [total invoice_number location client_id vendor_id vendor_name date] :as in}] (defn add-invoice-transaction [{:keys [total invoice_number location client_id vendor_id vendor_name date expense_accounts] :as in}]
(let [vendor (d-vendors/get-by-id vendor_id) (let [vendor (d-vendors/get-by-id vendor_id)
account (:vendor/default-account vendor) account (:vendor/default-account vendor)
_ (when-not (:db/id account) _ (when-not (:db/id account)
@@ -65,9 +65,12 @@
:invoice/outstanding-balance total :invoice/outstanding-balance total
:invoice/status :invoice-status/unpaid :invoice/status :invoice-status/unpaid
:invoice/date (coerce/to-date date) :invoice/date (coerce/to-date date)
:invoice/expense-accounts [{:invoice-expense-account/account (:db/id account) :invoice/expense-accounts (map (fn [ea]
:invoice-expense-account/location (:account/location account location) (remove-nils {:db/id (:id ea)
:invoice-expense-account/amount total}]})) :invoice-expense-account/account (:account_id ea)
:invoice-expense-account/location (:location ea)
:invoice-expense-account/amount (Double/parseDouble (:amount ea))}))
expense_accounts)}))
(defn add-invoice [context {{:keys [total invoice_number location client_id vendor_id vendor_name date] :as in} :invoice} value] (defn add-invoice [context {{:keys [total invoice_number location client_id vendor_id vendor_name date] :as in} :invoice} value]
(assert-no-conflicting in) (assert-no-conflicting in)
@@ -117,8 +120,10 @@
:invoice/total total :invoice/total total
:invoice/outstanding-balance (- total paid-amount) :invoice/outstanding-balance (- total paid-amount)
:invoice/expense-accounts (map (fn [ea] :invoice/expense-accounts (map (fn [ea]
{:db/id (:id ea) (remove-nils {:db/id (:id ea)
:invoice-expense-account/amount (Double/parseDouble (:amount ea))}) :invoice-expense-account/location (:location ea)
:invoice-expense-account/account (:account_id ea)
:invoice-expense-account/amount (Double/parseDouble (:amount ea))}))
expense_accounts)})] expense_accounts)})]
(-> updated-invoice (-> updated-invoice
(->graphql)))) (->graphql))))

View File

@@ -50,7 +50,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::change
(fn [db [_ form & path-pairs]] (fn [db [_ form & path-pairs]]
(println path-pairs) (println "CHANGED" path-pairs)
(reduce (reduce
(fn [db [path value]] (fn [db [path value]]
(assoc-in db (into [::forms form :data] path) value)) (assoc-in db (into [::forms form :data] path) value))

View File

@@ -40,7 +40,13 @@
:client-id client-id :client-id client-id
:invoice-number invoice-number :invoice-number invoice-number
:location location :location location
:total total}} :total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)})
expense-accounts)}}
invoice-read]}]})) invoice-read]}]}))
(defmethod submit-query :edit [db] (defmethod submit-query :edit [db]
@@ -54,6 +60,8 @@
:total total :total total
:expense-accounts (map (fn [ea] :expense-accounts (map (fn [ea]
{:id (:id ea) {:id (:id ea)
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)}) :amount (:amount ea)})
expense-accounts)}} expense-accounts)}}
invoice-read]}]})) invoice-read]}]}))
@@ -68,7 +76,13 @@
:client-id client-id :client-id client-id
:invoice-number invoice-number :invoice-number invoice-number
:location location :location location
:total total} :total total
:expense-accounts (map (fn [ea]
{:id (:id ea)
:account_id (:id (:account ea))
:location (:location ea)
:amount (:amount ea)})
expense-accounts)}
:bank-account-id bank-account-id :bank-account-id bank-account-id
:type type} :type type}
[:pdf-url [:invoices invoice-read]]]}]})) [:pdf-url [:invoices invoice-read]]]}]}))
@@ -81,7 +95,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::adding ::adding
(fn [db [_ new]] (fn [db [_ new]]
(-> db (forms/start-form ::form new)))) (-> db (forms/start-form ::form (assoc new :expense-accounts [{}])))))
(re-frame/reg-event-db (re-frame/reg-event-db
::editing ::editing
@@ -112,6 +126,12 @@
[:client-id] value [:client-id] value
[:location] first-location]}))) [:location] first-location]})))
(re-frame/reg-event-db
::add-expense-account
[(forms/in-form ::form) (re-frame/path [:data])]
(fn [form]
(update form :expense-accounts conj {:amount 0})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::submitted ::submitted
(fn [{:keys [db]} [_ params command bank-account-id type]] (fn [{:keys [db]} [_ params command bank-account-id type]]
@@ -257,32 +277,57 @@
:spec ::invoice/total :spec ::invoice/total
:step "0.01"}]]]]]] :step "0.01"}]]]]]]
(when exists? [:div
[:div [:div.columns
[:h2.subtitle "Expense Accounts"] [:div.column
[:h1.subtitle.is-4.is-inline "Expense Accounts"]]
[:div.column.is-narrow
[:p.buttons
[:a.button {:on-click (dispatch-event [::add-expense-account])} "Add"]]]]
(for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))]
^{:key id}
[:div.columns
[:div.column account-numeric-code " - " account-name]
(when multi-location? (for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))]
[:div.column location]) ^{:key id}
[:div.columns
[:div.column
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) @(re-frame/subscribe [::subs/chooseable-expense-accounts]))
:type "typeahead"
:field [:expense-accounts index :account :id]
:event [::forms/change ::form ]
:subscription data}]]]
[:div.column (when multi-location?
[:div.control
[:div.field.has-addons.is-extended [:div.column.is-narrow
[:p.control [:a.button.is-static "$"]] (if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))]
[:p.control [:div.select
[:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]]
[:div.select
[bind-field [bind-field
[:input.input {:type "number" [:select {:type "select"
:field [:expense-accounts index :amount] :field [:expense-accounts index :location]
:style {:text-align "right"} :allow-nil? true
:event [::forms/change ::form] :spec (set locations)
:subscription data :event [::forms/change ::form]
:value (get-in expense-account [:amount]) :subscription data}
:max (:total data) (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
:step "0.01"}]]]]]]])])
[:div.column.is-narrow
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [:expense-accounts index :amount]
:style {:text-align "right" :width "7em"}
:event [::forms/change ::form]
:subscription data
:value (get-in expense-account [:amount])
:max (:total data)
:step "0.01"}]]]]]]])]
(when error (when error
^{:key error} [:div.notification.is-warning.animated.fadeInUp ^{:key error} [:div.notification.is-warning.animated.fadeInUp