Almost finished with invoice page
This commit is contained in:
@@ -10,8 +10,8 @@
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit-advanced-print-checks
|
||||
:<- [::forms/form ::advanced-print-checks]
|
||||
::can-submit
|
||||
:<- [::forms/form ::form]
|
||||
(fn [{ {:keys [invoices invoice-amounts]} :data}]
|
||||
(cond (seq (filter
|
||||
(fn [{:keys [id outstanding-balance]}]
|
||||
@@ -22,15 +22,15 @@
|
||||
:else
|
||||
true)))
|
||||
|
||||
(def advanced-print-checks-form (forms/vertical-form {:submit-event [::advanced-print-checks-submitted]
|
||||
:change-event [::forms/change ::advanced-print-checks]
|
||||
:can-submit [::can-submit-advanced-print-checks]
|
||||
:id ::advanced-print-checks}))
|
||||
(def advanced-print-checks-form (forms/vertical-form {:submit-event [::save]
|
||||
:change-event [::forms/change ::form]
|
||||
:can-submit [::can-submit]
|
||||
:id ::form}))
|
||||
|
||||
|
||||
(defn print-checks-modal []
|
||||
(defn form []
|
||||
(let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
|
||||
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::advanced-print-checks])
|
||||
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
||||
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} advanced-print-checks-form]
|
||||
|
||||
(form-inline {}
|
||||
@@ -71,16 +71,15 @@
|
||||
::show
|
||||
(fn [{:keys [db]} [_ invoices]]
|
||||
{:dispatch [::modal/modal-requested {:title "Print Checks"
|
||||
:body [print-checks-modal]
|
||||
:body [form]
|
||||
:confirm {:value "Print checks"
|
||||
:status-from [::status/single ::advanced-print-checks]
|
||||
:status-from [::status/single ::form]
|
||||
:class "is-primary"
|
||||
:on-click (dispatch-event [::advanced-print-checks-submitted])
|
||||
:can-submit [::can-submit-advanced-print-checks]
|
||||
:close-event [::status/completed ::advanced-print-checks]}}]
|
||||
:on-click (dispatch-event [::save])
|
||||
:can-submit [::can-submit]
|
||||
:close-event [::status/completed ::form]}}]
|
||||
:db (-> db
|
||||
(forms/stop-form ::form/form)
|
||||
(forms/start-form ::advanced-print-checks
|
||||
(forms/start-form ::form
|
||||
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts])))
|
||||
:invoices invoices
|
||||
:invoice-amounts (into {}
|
||||
@@ -90,48 +89,33 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(defn print-checks-query [invoice-payments bank-account-id type client-id]
|
||||
{:venia/operation {:operation/type :mutation
|
||||
:operation/name "PrintChecks"}
|
||||
|
||||
:venia/queries [[:print-checks
|
||||
{:invoice_payments invoice-payments
|
||||
:type type
|
||||
:bank_account_id bank-account-id
|
||||
:client_id client-id}
|
||||
[[:invoices invoice-read]
|
||||
:pdf_url]]]})
|
||||
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::advanced-print-checks-submitted
|
||||
[with-user (forms/in-form ::advanced-print-checks) ]
|
||||
::save
|
||||
[with-user (forms/in-form ::form) ]
|
||||
(fn [{:keys [db user]} [_ bank-account-id]]
|
||||
(let [type (->> @(re-frame/subscribe [::subs/client])
|
||||
:bank-accounts
|
||||
(filter #(= bank-account-id (:id %)))
|
||||
first
|
||||
:type)
|
||||
(let [type (or (->> @(re-frame/subscribe [::subs/client])
|
||||
:bank-accounts
|
||||
(filter #(= bank-account-id (:id %)))
|
||||
first
|
||||
:type)
|
||||
:check)
|
||||
{:keys [date invoices invoice-amounts check-number bank-account-id client]} (:data db)]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::advanced-print-checks}
|
||||
:query-obj (print-checks-query (map (fn [x]
|
||||
{:invoice-id (:id x)
|
||||
:amount (get-in invoice-amounts [(:id x) :amount])})
|
||||
invoices)
|
||||
bank-account-id
|
||||
(cond (= type :check)
|
||||
:check
|
||||
(= type :cash)
|
||||
:cash
|
||||
|
||||
:else
|
||||
:check)
|
||||
(->> invoices first :client :id))
|
||||
|
||||
:owns-state {:single ::form}
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "PrintChecks"}
|
||||
|
||||
:venia/queries [[:print-checks
|
||||
{:invoice_payments (map (fn [x]
|
||||
{:invoice-id (:id x)
|
||||
:amount (get-in invoice-amounts [(:id x) :amount])})
|
||||
invoices)
|
||||
:type type
|
||||
:bank_account_id bank-account-id
|
||||
:client_id (->> invoices first :client :id)}
|
||||
[[:invoices invoice-read]
|
||||
:pdf_url]]]}
|
||||
:on-success (fn [result]
|
||||
[::checks-printed
|
||||
(:invoices (:print-checks result))
|
||||
|
||||
@@ -176,18 +176,18 @@
|
||||
(re-frame/reg-event-fx
|
||||
::add-and-print
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} [_ params bank-account-id type]]
|
||||
(fn [{:keys [user] {:keys [data]} :db} [_ bank-account-id type]]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj @(re-frame/subscribe [::add-and-print-query bank-account-id type])
|
||||
:on-success [::added-and-printed params]
|
||||
:on-success [::added-and-printed]
|
||||
:on-error [::forms/save-error ::form]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::saving
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} [_ params]]
|
||||
(fn [{:keys [user] {:keys [data]} :db} _]
|
||||
(let [command (if (:id data)
|
||||
:edit
|
||||
:create)]
|
||||
@@ -206,10 +206,15 @@
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::added-and-printed
|
||||
(fn [{:keys [db]} [_ {:keys [invoice-printed]} result]]
|
||||
(fn [{:keys [db]} [_ result]]
|
||||
(let [invoice (first (:invoices (:add-and-print-invoice result)))]
|
||||
{:dispatch-n [[::updated (assoc invoice :class "live-added")]
|
||||
(conj invoice-printed (:pdf-url (:add-and-print-invoice result)))]})))
|
||||
[::checks-printed [invoice] (:pdf-url (:add-and-print-invoice result))]]})))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::checks-printed
|
||||
(fn [db [_ invoices pdf-url]]
|
||||
db))
|
||||
|
||||
|
||||
|
||||
@@ -331,10 +336,10 @@
|
||||
(list
|
||||
(for [{:keys [id number name type]} (->> (:bank-accounts (:client data)) (filter :visible) (sort-by :sort-order))]
|
||||
(if (= :cash type)
|
||||
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :cash])} "With cash"]
|
||||
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :cash])} "With cash"]
|
||||
(list
|
||||
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :check])} "Print checks from " name]
|
||||
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :debit])} "Debit from " name]))))]]])
|
||||
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :check])} "Print checks from " name]
|
||||
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :debit])} "Debit from " name]))))]]])
|
||||
[:div.column
|
||||
|
||||
(submit-button "Save")]]])
|
||||
|
||||
137
src/cljs/auto_ap/views/pages/invoices/handwritten_checks.cljs
Normal file
137
src/cljs/auto_ap/views/pages/invoices/handwritten_checks.cljs
Normal file
@@ -0,0 +1,137 @@
|
||||
(ns auto-ap.views.pages.invoices.handwritten-checks
|
||||
(:require [auto-ap.entities.invoice :as invoice]
|
||||
[auto-ap.forms :as forms]
|
||||
[auto-ap.status :as status]
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.views.components.modal :as modal]
|
||||
[auto-ap.views.pages.data-page :as data-page]
|
||||
[auto-ap.views.pages.invoices.common
|
||||
:refer
|
||||
[does-amount-exceed-outstanding? invoice-read]]
|
||||
[auto-ap.views.pages.invoices.form :as form]
|
||||
[auto-ap.views.utils
|
||||
:refer
|
||||
[date-picker dispatch-event horizontal-field with-user]]
|
||||
[clojure.string :as str]
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
(def handwrite-checks-form (forms/vertical-form {:submit-event [::save]
|
||||
:change-event [::forms/change ::form]
|
||||
:can-submit [::can-submit]
|
||||
:id ::form}))
|
||||
|
||||
(defn form []
|
||||
(let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
|
||||
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
||||
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} handwrite-checks-form]
|
||||
(form-inline {}
|
||||
[:<>
|
||||
[:div.field
|
||||
[:label.label "Pay using"]
|
||||
[:div.control
|
||||
[:span.select
|
||||
[raw-field
|
||||
[:select {:type "select"
|
||||
:field :bank-account-id}
|
||||
(for [{:keys [id number name]} real-bank-accounts]
|
||||
^{:key id} [:option {:value id} name])]]]]]
|
||||
(field "Date"
|
||||
[date-picker {:class-name "input"
|
||||
:class "input"
|
||||
:format-week-number (fn [] "")
|
||||
:previous-month-button-label ""
|
||||
:placeholder "mm/dd/yyyy"
|
||||
:next-month-button-label ""
|
||||
:next-month-label ""
|
||||
:type "date"
|
||||
:field [:date]
|
||||
:spec ::invoice/date}])
|
||||
(field "Check number"
|
||||
[:input.input {:type "number"
|
||||
:field [:check-number]}])
|
||||
[:table.table.is-fullwidth
|
||||
[:thead
|
||||
[:tr
|
||||
[:th "Invoice ID"]
|
||||
[:th {:style {"width" "14em"}} "Payment"]]]
|
||||
[:tbody
|
||||
(doall
|
||||
(for [{:keys [payment outstanding-balance invoice-number id] :as i} (:invoices data)]
|
||||
^{:key id}
|
||||
[:tr
|
||||
[:td invoice-number]
|
||||
[:td [:div.field.has-addons.is-extended
|
||||
[:p.control [:a.button.is-static "$"]]
|
||||
[:p.control
|
||||
|
||||
(raw-field
|
||||
[:input.input.has-text-right {:type "number"
|
||||
:field [:invoice-amounts id :amount]
|
||||
#_#_:max outstanding-balance
|
||||
:step "0.01"}])]]]]))]]])))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit
|
||||
:<- [::forms/form ::form]
|
||||
(fn [{ {:keys [check-number date invoices invoice-amounts]} :data}]
|
||||
(boolean (cond (seq (filter
|
||||
(fn [{:keys [id outstanding-balance]}]
|
||||
(does-amount-exceed-outstanding? (get-in invoice-amounts [id :amount]) outstanding-balance ))
|
||||
invoices))
|
||||
false
|
||||
|
||||
:else
|
||||
(and (not (str/blank? check-number))
|
||||
(not (str/blank? date)))))))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::show
|
||||
(fn [{:keys [db]} [_ invoices]]
|
||||
{:dispatch [::modal/modal-requested {:title "Handwrite checks"
|
||||
:body [form]
|
||||
:confirm {:value "Save handwritten check"
|
||||
:status-from [::status/single ::form]
|
||||
:class "is-primary"
|
||||
:on-click (dispatch-event [::save])
|
||||
:can-submit [::can-submit]
|
||||
:close-event [::status/completed ::form]}}]
|
||||
:db (-> db
|
||||
(forms/start-form ::form
|
||||
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts])))
|
||||
:invoices invoices
|
||||
:invoice-amounts (into {}
|
||||
(map (fn [i] [(:id i)
|
||||
{:amount (:outstanding-balance i)}])
|
||||
invoices))}))}))
|
||||
|
||||
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save
|
||||
[with-user (forms/in-form ::form)]
|
||||
(fn [{:keys [db user]} _]
|
||||
(let [{:keys [date invoices invoice-amounts check-number bank-account-id]} (:data db)]
|
||||
{:graphql
|
||||
{:token user
|
||||
:owns-state {:single ::form}
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddHandwrittenCheck"}
|
||||
:venia/queries [{:query/data [:add-handwritten-check
|
||||
{:date date
|
||||
:invoice_payments (map (fn [x]
|
||||
{:invoice-id (:id x)
|
||||
:amount (get-in invoice-amounts [(:id x) :amount])})
|
||||
invoices)
|
||||
:check-number check-number
|
||||
:bank-account-id bank-account-id}
|
||||
[[:invoices invoice-read]]]}]}
|
||||
:on-success (fn [result]
|
||||
[::succeeded (:invoices (:add-handwritten-check result))])}})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::succeeded
|
||||
[(forms/triggers-stop ::form)]
|
||||
(fn [{:keys [db]} [_ invoices]]
|
||||
{:dispatch [::modal/modal-closed]}))
|
||||
|
||||
Reference in New Issue
Block a user