From bc4287befe398c1dcc8c0731521faec213d27190 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 16 Apr 2019 20:27:14 -0700 Subject: [PATCH] you can kind of set up more than one account in an invoice. --- src/clj/auto_ap/graphql.clj | 1 + src/clj/auto_ap/graphql/invoices.clj | 19 ++-- src/cljs/auto_ap/forms.cljs | 2 +- .../auto_ap/views/pages/invoices/form.cljs | 95 ++++++++++++++----- 4 files changed, 84 insertions(+), 33 deletions(-) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index f357eb49..66f203fb 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -406,6 +406,7 @@ :add_invoice {:fields {:id {:type :id} :invoice_number {:type 'String} + :expense_accounts {:type '(list :edit_expense_account)} :location {:type 'String} :date {:type 'String} :client_id {:type :id} diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 97ceefd6..625891a5 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -9,7 +9,7 @@ [auto-ap.time :refer [parse iso-date]] [auto-ap.utils :refer [dollars=]] [datomic.api :as d] - [auto-ap.datomic :refer [uri]] + [auto-ap.datomic :refer [uri remove-nils]] [clj-time.coerce :as coerce] [clojure.set :as set])) @@ -51,7 +51,7 @@ :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.")})))) -(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) account (:vendor/default-account vendor) _ (when-not (:db/id account) @@ -65,9 +65,12 @@ :invoice/outstanding-balance total :invoice/status :invoice-status/unpaid :invoice/date (coerce/to-date date) - :invoice/expense-accounts [{:invoice-expense-account/account (:db/id account) - :invoice-expense-account/location (:account/location account location) - :invoice-expense-account/amount total}]})) + :invoice/expense-accounts (map (fn [ea] + (remove-nils {:db/id (:id ea) + :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] (assert-no-conflicting in) @@ -117,8 +120,10 @@ :invoice/total total :invoice/outstanding-balance (- total paid-amount) :invoice/expense-accounts (map (fn [ea] - {:db/id (:id ea) - :invoice-expense-account/amount (Double/parseDouble (:amount ea))}) + (remove-nils {:db/id (:id ea) + :invoice-expense-account/location (:location ea) + :invoice-expense-account/account (:account_id ea) + :invoice-expense-account/amount (Double/parseDouble (:amount ea))})) expense_accounts)})] (-> updated-invoice (->graphql)))) diff --git a/src/cljs/auto_ap/forms.cljs b/src/cljs/auto_ap/forms.cljs index 95a57d28..db3e6b0d 100644 --- a/src/cljs/auto_ap/forms.cljs +++ b/src/cljs/auto_ap/forms.cljs @@ -50,7 +50,7 @@ (re-frame/reg-event-db ::change (fn [db [_ form & path-pairs]] - (println path-pairs) + (println "CHANGED" path-pairs) (reduce (fn [db [path value]] (assoc-in db (into [::forms form :data] path) value)) diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 1ef909f9..1769fad4 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -40,7 +40,13 @@ :client-id client-id :invoice-number invoice-number :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]}]})) (defmethod submit-query :edit [db] @@ -54,6 +60,8 @@ :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]}]})) @@ -68,7 +76,13 @@ :client-id client-id :invoice-number invoice-number :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 :type type} [:pdf-url [:invoices invoice-read]]]}]})) @@ -81,7 +95,7 @@ (re-frame/reg-event-db ::adding (fn [db [_ new]] - (-> db (forms/start-form ::form new)))) + (-> db (forms/start-form ::form (assoc new :expense-accounts [{}]))))) (re-frame/reg-event-db ::editing @@ -112,6 +126,12 @@ [:client-id] value [: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 ::submitted (fn [{:keys [db]} [_ params command bank-account-id type]] @@ -257,32 +277,57 @@ :spec ::invoice/total :step "0.01"}]]]]]] - (when exists? - [:div - [:h2.subtitle "Expense Accounts"] + [:div + [:div.columns + [: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? - [:div.column 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))] + ^{: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 - [:div.control - [:div.field.has-addons.is-extended - [:p.control [:a.button.is-static "$"]] - [:p.control + (when multi-location? + + [:div.column.is-narrow + (if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))] + [:div.select + [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] + [:div.select [bind-field - [:input.input {:type "number" - :field [:expense-accounts index :amount] - :style {:text-align "right"} - :event [::forms/change ::form] - :subscription data - :value (get-in expense-account [:amount]) - :max (:total data) - :step "0.01"}]]]]]]])]) + [:select {:type "select" + :field [:expense-accounts index :location] + :allow-nil? true + :spec (set locations) + :event [::forms/change ::form] + :subscription data} + (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) + + [: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 ^{:key error} [:div.notification.is-warning.animated.fadeInUp