diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index bfbcaf9f..069d8b50 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -63,6 +63,10 @@ (defn approve [] (j/update! (get-conn) :invoices {:imported true} [] )) +(defn update [v] + (j/update! (get-conn) :invoices v ["id = ?" (:id v)]) + (get-by-id (:id v))) + (defn reject [] (j/delete! (get-conn) :invoices ["imported = false"])) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index fac7889b..1e278021 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -258,6 +258,12 @@ :company_id {:type 'Int} :vendor_id {:type 'Int} :vendor_name {:type 'String} + :total {:type 'Float}}} + + :edit_invoice + {:fields {:id {:type 'Int} + :invoice_number {:type 'String} + :date {:type 'String} :total {:type 'Float}}}} :mutations @@ -281,6 +287,9 @@ :add_invoice {:type :invoice :args {:invoice {:type :add_invoice}} :resolve :mutation/add-invoice} + :edit_invoice {:type :invoice + :args {:invoice {:type :edit_invoice}} + :resolve :mutation/edit-invoice} :edit_expense_accounts {:type :invoice :args {:invoice_id {:type 'Int} :expense_accounts {:type '(list :edit_expense_account)}} @@ -494,6 +503,7 @@ :mutation/print-checks print-checks :mutation/edit-user gq-users/edit-user :mutation/add-invoice gq-invoices/add-invoice + :mutation/edit-invoice gq-invoices/edit-invoice :mutation/edit-expense-accounts gq-invoices/edit-expense-accounts :get-vendor get-vendor :get-expense-account expense-accounts/get-expense-account diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 8770051a..6e9f30cd 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -33,6 +33,17 @@ (->graphql)))) +(defn edit-invoice [context {{:keys [id] :as in} :invoice} value] + (let [invoice (invoices/get-by-id id) + paid-amount (- (:total invoice) (:outstanding-balance invoice)) + _ (assert-can-see-company (:id context) (:company-id invoice)) + updated-invoice (invoices/update (-> in + (update :date parse iso-date) + (assoc :outstanding_balance (- (:total in) paid-amount))))] + (-> updated-invoice + (->graphql)))) + + (defn get-invoices-expense-accounts [context args value] (->graphql (invoices-expense-accounts/get-for-invoice (:id value)))) diff --git a/src/cljc/auto_ap/expense_accounts.cljc b/src/cljc/auto_ap/expense_accounts.cljc index 0804b21e..a2b7358b 100644 --- a/src/cljc/auto_ap/expense_accounts.cljc +++ b/src/cljc/auto_ap/expense_accounts.cljc @@ -69,34 +69,34 @@ 7550 {:name "Franchise Fee" :parent 7500} 7560 {:name "Unassigned Expenses" :parent 7500} 8100 {:name "Operational" :parent nil} - 8110 {:name "Professional Fees" :parent 8100} - 8120 {:name "Accounting" :parent 8100} - 8130 {:name "Membership Dues and Associations" :parent 8100} + 8110 {:name "Professional Fees - HQ" :parent 8100} + 8120 {:name "Accounting - HQ" :parent 8100} + 8130 {:name "Membership Dues and Associations - HQ" :parent 8100} 8200 {:name "Occupancy Costs" :parent nil} - 8210 {:name "Rent" :parent 8200} - 8220 {:name "CAM" :parent 8200} - 8230 {:name "Real Estate Taxes" :parent 8200} + 8210 {:name "Rent - HQ" :parent 8200} + 8220 {:name "CAM - HQ" :parent 8200} + 8230 {:name "Real Estate Taxes - HQ" :parent 8200} 8300 {:name "Utilities" :parent nil} - 8310 {:name "Electric" :parent 8300} - 8320 {:name "Gas" :parent 8300} - 8330 {:name "Trash Removal" :parent 8300} - 8340 {:name "Water and Sewage" :parent 8300} - 8350 {:name "Internet" :parent 8300} + 8310 {:name "Electric - HQ" :parent 8300} + 8320 {:name "Gas - HQ" :parent 8300} + 8330 {:name "Trash Removal - HQ" :parent 8300} + 8340 {:name "Water and Sewage - HQ" :parent 8300} + 8350 {:name "Internet - HQ" :parent 8300} 8400 {:name "Equipment Rental" :parent nil} - 8410 {:name "Kitchen Equipment Rental" :parent 8400} - 8420 {:name "POS System" :parent 8400} - 8430 {:name "Other Rental" :parent 8400} + 8410 {:name "Kitchen Equipment Rental - HQ" :parent 8400} + 8420 {:name "POS System - HQ" :parent 8400} + 8430 {:name "Other Rental - HQ" :parent 8400} 8500 {:name "Taxes and Insurance" :parent nil} - 8510 {:name "Liability Insurance" :parent 8500} - 8610 {:name "Business License" :parent 8500} - 8620 {:name "Health Permit" :parent 8500} - 8710 {:name "Personal Property Taxes" :parent nil} + 8510 {:name "Liability Insurance - HQ" :parent 8500} + 8610 {:name "Business License - HQ" :parent 8500} + 8620 {:name "Health Permit - HQ" :parent 8500} + 8710 {:name "Personal Property Taxes - HQ" :parent nil} 8800 {:name "Depriciation" :parent nil} - 8810 {:name "Amortization of Lease" :parent 8800} - 8820 {:name "Amortization of Leasehold Improvements" :parent 8800} - 8830 {:name "Amortization of Start Up Costs" :parent 8800} - 8850 {:name "Depreciation on Building" :parent 8800} - 8860 {:name "Depreciation on Furnitire and Fixtures" :parent 8800} + 8810 {:name "Amortization of Lease - HQ" :parent 8800} + 8820 {:name "Amortization of Leasehold Improvements - HQ" :parent 8800} + 8830 {:name "Amortization of Start Up Costs - HQ" :parent 8800} + 8850 {:name "Depreciation on Building - HQ" :parent 8800} + 8860 {:name "Depreciation on Furnitire and Fixtures - HQ" :parent 8800} 9100 {:name "HQ Promotion and Outreach" :parent nil} 9110 {:name "Marketing and Advertising - HQ" :parent 9100} 9120 {:name "Marketing Consultant - HQ" :parent 9100} @@ -138,7 +138,7 @@ 9620 {:name "Amortization of Leasehold Improvements - HQ" :parent 9600} 9630 {:name "Amortization of Start Up Costs - HQ" :parent 9600} 9650 {:name "Depreciation on Building - HQ" :parent 9600} - 9660 {:name "Depreciation on Furniture and Fixtures" :parent 9600} + 9660 {:name "Depreciation on Furniture and Fixtures - HQ" :parent 9600} 9700 {:name "HQ Taxes" :parent nil} 9710 {:name "Federal Taxes - HQ" :parent 9700} 9720 {:name "State Taxes - HQ" :parent 9700} diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index 0ded844d..0c06ef77 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -55,7 +55,7 @@ -(defn invoice-table [{:keys [id invoice-page status on-params-change vendors params check-boxes checked on-check-changed expense-event]}] +(defn invoice-table [{:keys [id invoice-page status on-params-change vendors params check-boxes checked on-check-changed on-edit-invoice expense-event]}] (let [state (reagent/atom (or @params {})) visible-checks @(re-frame/subscribe [::visible-checks]) visible-expense-accounts @(re-frame/subscribe [::visible-expense-accounts]) @@ -182,6 +182,7 @@ [:a.dropdown-item.is-primary {:on-click (dispatch-event (conj expense-event id))} "Change"]]]]) [:span {:style {:margin-left "1em"}}] + [:button.button {:on-click (fn [] (on-edit-invoice i))} [:span [:span.icon [:i.fa.fa-pencil]] " Edit"]] (when (seq checks) [:div.dropdown.is-right {:class (if (= id visible-checks) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index c49f1630..3f864908 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -53,6 +53,11 @@ (fn [db] (-> db ::new-invoice))) +(re-frame/reg-sub + ::edit-invoice + (fn [db] + (-> db ::edit-invoice))) + (re-frame/reg-sub ::params (fn [db] @@ -226,6 +231,14 @@ :db (assoc-in db [::new-invoice] {:company-id (:id @(re-frame/subscribe [::subs/company])) :date (date->str (c/now) standard)})})) +(re-frame/reg-event-fx + ::edit-invoice + (fn [{:keys [db]} [_ which]] + (let [edit-invoice (update which :date #(date->str % standard)) + edit-invoice (assoc edit-invoice :original edit-invoice)] + {:dispatch [::events/modal-status ::edit-invoice {:visible? true}] + :db (assoc-in db [::edit-invoice] edit-invoice)}))) + (re-frame/reg-event-fx ::create-invoice (fn [{:keys [db]} _] @@ -246,6 +259,25 @@ ]]}]} :on-success [::invoice-created]}}))) +(re-frame/reg-event-fx + ::edit-invoice-save + (fn [{:keys [db]} _] + (let [{:keys [date total invoice-number id]} @(re-frame/subscribe [::edit-invoice])] + {:graphql + {:token (-> db :user) + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "EditInvoice"} + + :venia/queries [{:query/data [:edit-invoice + {:invoice {:id id :invoice-number invoice-number :date date :total total}} + [:id :total :outstanding-balance :date :invoice-number + [:company [:id :name :locations]] + [:vendor [:id :name]] + [:expense_accounts [:amount :id :expense_account_id + :location + [:expense_account [:id :name [:parent [:id :name]]]]]]]]}]} + :on-success [::invoice-edited]}}))) + (re-frame/reg-event-fx ::handwrite-checks-save (fn [{:keys [db]} _] @@ -289,6 +321,19 @@ is))) (dissoc ::new-invoice))})) +(re-frame/reg-event-fx + ::invoice-edited + (fn [{:keys [db]} [_ {:keys [edit-invoice]}]] + {:dispatch [::events/modal-completed ::edit-invoice] + :db (-> db + (update-in [::invoice-page :invoices] + (fn [is] + (mapv (fn [i] + (if (= (:id i) (:id edit-invoice)) + (assoc edit-invoice :class "live-added") + i)) is))) + (dissoc ::edit-invoice))})) + (re-frame/reg-event-fx ::change-expense-accounts (fn [{:keys [db]} [_ id]] @@ -612,6 +657,58 @@ :step "0.01"}]]]]] ])) +(defn edit-invoice-modal [] + (let [data @(re-frame/subscribe [::edit-invoice]) + change-event [::events/change-form [::edit-invoice]] + locations (get-in @(re-frame/subscribe [::subs/companies-by-id]) [(:company-id data) :locations]) + min-total (- (:total (:original data)) (:outstanding-balance (:original data))) + + should-select-location? (and locations + (> (count locations) 1))] + [action-modal {:id ::edit-invoice + :title "Update Invoice" + :action-text "Save" + :save-event [::edit-invoice-save] + :can-submit? (and #_(s/valid? ::invoice/invoice data) + (>= (:total data) min-total))} + + [horizontal-field + [:label.label "Date"] + + + [bind-field + [:input.input {:type "date" + :field [:date] + :event change-event + :spec ::invoice/date + :subscription data}]]] + + + + [horizontal-field + [:label.label "Invoice #"] + [bind-field + [:input.input {:type "text" + :field [:invoice-number] + :event change-event + :spec ::invoice/invoice-number + :subscription data}]]] + + [horizontal-field + [:label.label "Total"] + [:div.field.has-addons.is-extended + [:p.control [:a.button.is-static "$"]] + [:p.control + [bind-field + [:input.input {:type "number" + :field [:total] + :event change-event + :min min-total + :subscription data + :spec ::invoice/total + :step "0.01"}]]]]] + ])) + @@ -656,6 +753,8 @@ :params (re-frame/subscribe [::params]) :invoice-page (re-frame/subscribe [::invoice-page]) :status (re-frame/subscribe [::subs/status]) + :on-edit-invoice (fn [which] + (re-frame/dispatch [::edit-invoice which])) :on-params-change (fn [params] (re-frame/dispatch [::params-change params])) :check-boxes true @@ -666,6 +765,7 @@ [print-checks-modal] [new-invoice-modal] + [edit-invoice-modal] [handwrite-checks-modal] [change-expense-accounts-modal] (when check-results-shown?