advanced print checks
This commit is contained in:
144
src/cljs/auto_ap/views/pages/invoices/advanced_print_checks.cljs
Normal file
144
src/cljs/auto_ap/views/pages/invoices/advanced_print_checks.cljs
Normal file
@@ -0,0 +1,144 @@
|
||||
(ns auto-ap.views.pages.invoices.advanced-print-checks
|
||||
(:require [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.invoices.common :refer [invoice-read does-amount-exceed-outstanding?]]
|
||||
[auto-ap.views.pages.invoices.form :as form]
|
||||
[auto-ap.views.utils :refer [dispatch-event horizontal-field with-user]]
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit-advanced-print-checks
|
||||
:<- [::forms/form ::advanced-print-checks]
|
||||
(fn [{ {:keys [invoices invoice-amounts]} :data}]
|
||||
(cond (seq (filter
|
||||
(fn [{:keys [id outstanding-balance]}]
|
||||
(does-amount-exceed-outstanding? (get-in invoice-amounts [id :amount]) outstanding-balance ))
|
||||
invoices))
|
||||
false
|
||||
|
||||
: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}))
|
||||
|
||||
|
||||
(defn print-checks-modal []
|
||||
(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 [form-inline horizontal-field field raw-field error-notification submit-button]} advanced-print-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])]]]]]
|
||||
|
||||
[:table.table.is-fullwidth
|
||||
[:thead
|
||||
[:tr
|
||||
[:th "Vendor"]
|
||||
[:th "Invoice ID"]
|
||||
[:th {:style {"width" "10em"}} "Payment"]]]
|
||||
[:tbody
|
||||
(doall
|
||||
(for [{:keys [vendor payment outstanding-balance invoice-number id] :as i} (:invoices data)]
|
||||
^{:key id}
|
||||
[:tr
|
||||
[:td (:name vendor)]
|
||||
[: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]
|
||||
:step "0.01"}])]]]]))]]])))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::show
|
||||
(fn [{:keys [db]} [_ invoices]]
|
||||
{:dispatch [::modal/modal-requested {:title "Print Checks"
|
||||
:body [print-checks-modal]
|
||||
:confirm {:value "Print checks"
|
||||
:status-from [::status/single ::advanced-print-checks]
|
||||
: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]}}]
|
||||
:db (-> db
|
||||
(forms/stop-form ::form/form)
|
||||
(forms/start-form ::advanced-print-checks
|
||||
{: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))}))}))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(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) ]
|
||||
(fn [{:keys [db user]} [_ bank-account-id]]
|
||||
(let [type (->> @(re-frame/subscribe [::subs/client])
|
||||
:bank-accounts
|
||||
(filter #(= bank-account-id (:id %)))
|
||||
first
|
||||
:type)
|
||||
{: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))
|
||||
|
||||
:on-success (fn [result]
|
||||
[::checks-printed
|
||||
(:invoices (:print-checks result))
|
||||
(:pdf-url (:print-checks result))])}})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::checks-printed
|
||||
(fn [{:keys [db]} [_ data]]
|
||||
{:dispatch [::modal/modal-closed]}))
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
(ns auto-ap.views.pages.invoices.common)
|
||||
|
||||
(defn does-amount-exceed-outstanding? [amount outstanding-balance ]
|
||||
(let [amount (js/parseFloat amount)
|
||||
outstanding-balance (js/parseFloat outstanding-balance)]
|
||||
(or (and (> outstanding-balance 0)
|
||||
(> amount outstanding-balance))
|
||||
(and (> outstanding-balance 0)
|
||||
(<= amount 0))
|
||||
(and (< outstanding-balance 0)
|
||||
(< amount outstanding-balance))
|
||||
(and (< outstanding-balance 0)
|
||||
(>= amount 0)))))
|
||||
|
||||
(def invoice-read [:id :total :outstanding-balance :date :due :invoice-number :status
|
||||
:automatically-paid-when-due
|
||||
[:client [:id :name :locations]]
|
||||
|
||||
Reference in New Issue
Block a user