This commit is contained in:
Bryce
2023-07-13 20:12:49 -07:00
248 changed files with 8002 additions and 1490 deletions

View File

@@ -1,222 +1,234 @@
(ns auto-ap.ssr.company.company-1099
(:require
[auto-ap.datomic :refer [conn remove-nils]]
[auto-ap.graphql.utils :refer [is-admin?]]
[auto-ap.datomic :refer [apply-pagination-raw conn remove-nils]]
[auto-ap.graphql.utils :refer [assert-can-see-client is-admin?]]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.components :as com]
[auto-ap.ssr.grid-page-helper :as helper]
[auto-ap.ssr.svg :as svg]
[auto-ap.ssr.ui :refer [base-page]]
[auto-ap.ssr.utils :refer [html-response form-data->map path->name]]
[auto-ap.ssr.utils :refer [form-data->map html-response path->name]]
[bidi.bidi :as bidi]
[cemerick.url :as url]
[clojure.string :as str]
[datomic.api :as dc]
[iol-ion.query :refer [can-see-client?]]))
<<<<<<< HEAD
(defn get-1099-companies [user session]
=======
(def vendor-read '[:db/id
:vendor/name
{:vendor/legal-entity-1099-type [:db/ident]}
{:vendor/legal-entity-tin-type [:db/ident]}
{:vendor/address [:address/street1
:address/city
:address/state
:address/zip]}
:vendor/legal-entity-tin
:vendor/legal-entity-name
:vendor/legal-entity-first-name
:vendor/legal-entity-middle-name
:vendor/legal-entity-last-name])
(defn sum-for-client-vendor [client-id vendor-id]
(ffirst (dc/q '[:find
(sum ?a)
:with ?d
:in $ ?c ?v
:where
[?p :payment/client ?c]
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
client-id
vendor-id)))
(defn get-1099-companies [user {:keys [client-id] :as args}]
>>>>>>> 425942581295562d2c57843d7b19d9d4de962eda
(let [clients (->> (dc/q '[:find ?c
:in $ ?user
:where [?c :client/code]
[(iol-ion.query/can-see-client? ?user ?c)]]
(dc/db conn) user)
:in $ ?user
:where [?c :client/code]
[(iol-ion.query/can-see-client? ?user ?c)]]
(dc/db conn) user)
(map first)
set)
results (cond
(and (some-> session :client :db/id)
(can-see-client? user
(some-> session :client :db/id)))
(and client-id
(can-see-client? user client-id))
(dc/q '[:find
(pull ?c [:client/code :db/id])
(pull ?v [:db/id
:vendor/name
{:vendor/legal-entity-1099-type [:db/ident]}
{:vendor/legal-entity-tin-type [:db/ident]}
{:vendor/address [:address/street1
:address/city
:address/state
:address/zip]}
:vendor/legal-entity-tin
:vendor/legal-entity-name
:vendor/legal-entity-first-name
:vendor/legal-entity-middle-name
:vendor/legal-entity-last-name])
(sum ?a)
:with ?d
:in $ ?c
:where
[?p :payment/client ?c]
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
(some-> session :client :db/id))
(pull ?c [:client/code :db/id])
(pull ?v vendor-read)
(sum ?a)
:with ?d
:in $ ?c vendor-read
:where
[?p :payment/client ?c]
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
client-id
vendor-read)
(is-admin? user)
(dc/q '[:find
(pull ?c [:client/code :db/id])
(pull ?v [:db/id
:vendor/name
{:vendor/legal-entity-1099-type [:db/ident]}
{:vendor/legal-entity-tin-type [:db/ident]}
{:vendor/address [:address/street1
:address/city
:address/state
:address/zip]}
:vendor/legal-entity-tin
:vendor/legal-entity-name
:vendor/legal-entity-first-name
:vendor/legal-entity-middle-name
:vendor/legal-entity-last-name])
(sum ?a)
:with ?d
:in $
:where
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/client ?c]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn))
(pull ?c [:client/code :db/id])
(pull ?v vendor-read)
(sum ?a)
:with ?d
:in $ vendor-read
:where
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/client ?c]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
vendor-read)
:else
(dc/q '[:find
(pull ?c [:client/code :db/id])
(pull ?v [:db/id
:vendor/name
{:vendor/legal-entity-1099-type [:db/ident]}
{:vendor/legal-entity-tin-type [:db/ident]}
{:vendor/address [:address/street1
:address/city
:address/state
:address/zip]}
:vendor/legal-entity-tin
:vendor/legal-entity-name
:vendor/legal-entity-first-name
:vendor/legal-entity-middle-name
:vendor/legal-entity-last-name])
(sum ?a)
:with ?d
:in $ [?c ...]
:where
[?p :payment/client ?c]
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
clients))]
(->> results
(filter (fn [[_ _ a]]
(>= (or a 0.0) 600.0)))
(sort-by (fn [[client _ amount]]
[(:client/code client ) amount]))
(into []))))
(pull ?c [:client/code :db/id])
(pull ?v vendor-read)
(sum ?a)
:with ?d
:in $ [?c ...] vendor-read
:where
[?p :payment/client ?c]
[?p :payment/date ?d ]
[(>= ?d #inst "2022-01-01T08:00")]
[(< ?d #inst "2023-01-01T08:00")]
[?p :payment/type :payment-type/check]
[?p :payment/amount ?a]
[?p :payment/vendor ?v]]
(dc/db conn)
clients
vendor-read))
all (->> results
(filter (fn [[_ _ a]]
(>= (or a 0.0) 600.0)))
(sort-by (fn [[client _ amount]]
[(:client/code client ) amount]))
(into []))
paginated (apply-pagination-raw args all)]
[(:entries paginated) (:count paginated)]))
(defn row* [{:keys [client vendor amount flash?]}]
(com/data-grid-row
{:class (when flash?
"live-added")}
(com/data-grid-cell {} (:client/code client))
(com/data-grid-cell
{}
[:div.flex.whitespace-nowrap.items-center.gap-4
[:div [:div (:vendor/name vendor)]
[:div.text-sm.text-gray-400
(or (-> vendor :vendor/legal-entity-name not-empty)
(str (-> vendor :vendor/legal-entity-first-name) " "
(-> vendor :vendor/legal-entity-middle-name) " "
(-> vendor :vendor/legal-entity-last-name)))]]
(when-let [t99-type (some-> vendor :vendor/legal-entity-1099-type :db/ident name)]
(com/pill
{:class "text-xs font-medium"
:color :primary}
(str/capitalize t99-type))
)])
(com/data-grid-cell
{:class "hidden md:table-cell"}
[:div.flex.gap-4
(when-let [tin (-> vendor :vendor/legal-entity-tin)]
[:span {:class "text-xs font-medium py-0.5 "}
tin])
(when-let [tin-type (some-> vendor :vendor/legal-entity-tin-type :db/ident name)]
(com/pill {:class "text-xs font-medium"
:color :yellow}
(name tin-type)))])
(com/data-grid-cell
{:class "hidden lg:table-cell"}
(if (-> vendor :vendor/address :address/street1)
[:div
[:div (-> vendor :vendor/address :address/street1)] " "
[:div
(-> vendor :vendor/address :address/street2)] " "
[:div
(-> vendor :vendor/address :address/city) " "
(-> vendor :vendor/address :address/state) ","
(-> vendor :vendor/address :address/zip)]]
[:p.text-sm.italic.text-gray-400 "No address"]))
(com/data-grid-cell {}
(com/pill {:class "text-xs font-medium"
:color :primary}
"Paid $" (Math/round amount)))
(com/data-grid-right-stack-cell
{}
(com/icon-button {:hx-get (bidi/path-for ssr-routes/only-routes
:company-1099-vendor-dialog
:vendor-id (:db/id vendor))
:hx-target "#modal-holder"
:hx-swap "outerHTML"}
svg/pencil))))
(defn table* [{:keys [identity session query-params hx-query-params]} & {:keys [flash-id]}]
(let [start (or (some-> (or (get query-params "start") (get hx-query-params "start")) not-empty (Long/parseLong ))
0)
per-page (or (some-> (or (get query-params "per-page") (get hx-query-params "per-page")) not-empty (Long/parseLong ))
30)
companies (get-1099-companies identity session)
total (count companies)
companies (subvec companies (Math/min start total) (Math/min (+ start per-page) total))]
(com/data-grid-card {:id "vendor-table"
:title "1099 Vendor Info"
:entity-name "vendors"
:route :company-1099-vendor-table
:start start
:per-page per-page
:total total
:action-buttons [(com/button {:color :primary}
(com/button-icon {} svg/refresh)
"Add new product")
(com/button {:color :secondary}
(com/button-icon {} svg/refresh)
"Update stocks 1/250")
(com/icon-button {}
svg/upload)]
:rows (for [[client vendor amount] companies]
(row* {:client client
:vendor vendor
:amount amount
:flash? (= flash-id
(:db/id vendor))}))
:headers [(com/data-grid-header {} "Client")
(com/data-grid-header {} "Vendor Name")
(com/data-grid-header {:class "hidden md:table-cell"} "TIN")
(com/data-grid-header {:class "hidden lg:table-cell"} "Address")
(com/data-grid-header {})
(com/data-grid-header {})]})))
(def grid-page {:id "vendor-table"
:nav (com/company-aside-nav)
:id-fn (comp :db/id second)
:fetch-page (fn [user args]
(get-1099-companies user args)
#_(r/get-graphql (into args {:id user})))
:breadcrumbs [[:a {:href (bidi/path-for ssr-routes/only-routes
:company)}
"My Company"]
(defn vendor-save [{:keys [form-params identity route-params] :as request}]
@(dc/transact conn [(remove-nils
(-> (form-data->map form-params)
(assoc :db/id (Long/parseLong (:vendor-id route-params)))
(update :vendor/legal-entity-1099-type #(some->> % not-empty (keyword "legal-entity-1099-type")))
(update :vendor/legal-entity-tin-type #(some->> % not-empty (keyword "legal-entity-tin-type")))))])
(html-response
(table* request :flash-id (Long/parseLong (:vendor-id route-params)))
:headers {"hx-trigger" "closeModal"}))
[:a {:href (bidi/path-for ssr-routes/only-routes
:company-1099)}
"1099 Vendor Info"]]
:title "1099 Vendors"
:entity-name "Vendors"
:route :company-1099-vendor-table
:action-buttons (fn [user]
nil)
:row-buttons (fn [user e]
[(com/icon-button {:hx-get (str (bidi/path-for ssr-routes/only-routes
:company-1099-vendor-dialog
:vendor-id (:db/id (second e)))
"?"
(url/map->query {:client-id (:db/id (first e))}))
:hx-ext "debug"
:hx-target "#modal-holder"
:hx-swap "outerHTML"}
svg/pencil)])
:headers [{:key "Client"
:name "Client"
:sort-key "client"
:render (comp :client/code first)}
{:key "vendor-name"
:name "Vendor Name"
:sort-key "vendor"
:render (fn [[_ vendor]]
[:div.flex.whitespace-nowrap.items-center.gap-4
[:div [:div (:vendor/name vendor)]
[:div.text-sm.text-gray-400
(or (-> vendor :vendor/legal-entity-name not-empty)
(str (-> vendor :vendor/legal-entity-first-name) " "
(-> vendor :vendor/legal-entity-middle-name) " "
(-> vendor :vendor/legal-entity-last-name)))]]
(when-let [t99-type (some-> vendor :vendor/legal-entity-1099-type :db/ident name)]
(com/pill
{:class "text-xs font-medium"
:color :primary}
(str/capitalize t99-type))
)])}
{:key "tin"
:name "TIN"
:sort-key "tin"
:show-starting "md"
:render (fn [[_ vendor]]
[:div.flex.gap-4
(when-let [tin (-> vendor :vendor/legal-entity-tin)]
[:span {:class "text-xs font-medium py-0.5 "}
tin])
(when-let [tin-type (some-> vendor :vendor/legal-entity-tin-type :db/ident name)]
(com/pill {:class "text-xs font-medium"
:color :yellow}
(name tin-type)))]
)}
{:key "address"
:name "Address"
:sort-key "address"
:show-starting "lg"
:render (fn [[_ vendor]]
(if (-> vendor :vendor/address :address/street1)
[:div
[:div (-> vendor :vendor/address :address/street1)] " "
[:div
(-> vendor :vendor/address :address/street2)] " "
[:div
(-> vendor :vendor/address :address/city) " "
(-> vendor :vendor/address :address/state) ","
(-> vendor :vendor/address :address/zip)]]
[:p.text-sm.italic.text-gray-400 "No address"]))}
{:key "paid"
:name "Paid"
:sort-key "paid"
:render (fn [[_ _ paid]]
(com/pill {:class "text-xs font-medium"
:color :primary}
"Paid $" (Math/round paid)))}]})
(def table* (partial helper/table* grid-page))
(def row* (partial helper/row* grid-page))
(defn vendor-save [{:keys [form-params identity route-params query-params] :as request}]
(let [client-id (Long/parseLong (get query-params "client-id"))
vendor-id (Long/parseLong (:vendor-id route-params))]
(assert-can-see-client identity client-id)
@(dc/transact conn [(remove-nils
(-> (form-data->map form-params)
(assoc :db/id (Long/parseLong (:vendor-id route-params)))
(update :vendor/legal-entity-1099-type #(some->> % not-empty (keyword "legal-entity-1099-type")))
(update :vendor/legal-entity-tin-type #(some->> % not-empty (keyword "legal-entity-tin-type")))))])
(html-response
(row* identity [(dc/pull (dc/db conn) [:db/id :client/code] client-id)
(dc/pull (dc/db conn) vendor-read vendor-id)
(sum-for-client-vendor client-id vendor-id)
] {:flash? true})
:headers {"hx-trigger" "closeModal"})))
(defn vendor-dialog [request]
(let [vendor (dc/pull (dc/db conn) '[* {:vendor/legal-entity-1099-type [:db/ident]
@@ -227,8 +239,10 @@
[:form {:hx-post (str (bidi/path-for ssr-routes/only-routes
:company-1099-vendor-save
:request-method :post
:vendor-id (Long/parseLong (:vendor-id (:params request)))))
:hx-target "#vendor-table"
:vendor-id (Long/parseLong (:vendor-id (:params request))))
"?"
(url/map->query {:client-id (:client-id (:params request))}))
:hx-target (format "#vendor-table tr[data-id=\"%d\"]" (:db/id vendor))
:hx-swap "outerHTML swap:300ms"}
[:fieldset {:class "hx-disable"}
(com/modal-card
@@ -236,6 +250,7 @@
[:div.flex [:div.p-2 "Vendor 1099 Info"] [:p.ml-2.rounded.bg-gray-200.p-2.dark:bg-gray-600 (:vendor/name vendor)]]
[:div.space-y-6
[:div.grid.grid-cols-6.gap-4
[:h4.text-xl.border-b.col-span-6 "Address"]
[:div.col-span-6
(com/field {:label "Street 1"}
@@ -310,20 +325,5 @@
"Save")]]]
[:div])]]))))
(defn vendor-table [request]
(html-response (table* request)
:headers {"hx-push-url" (str "?start=" (get (:query-params request) "start"))}))
(defn page [{:keys [identity matched-route] :as request}]
(base-page
request
(com/page {:nav (com/company-aside-nav)
:active-client (:client (:session request))
:identity (:identity request)}
(com/breadcrumbs {}
[:a {:href (bidi/path-for ssr-routes/only-routes
:company)} "My Company"]
[:a {:href (bidi/path-for ssr-routes/only-routes
:company-1099)} "1099 Vendor Info"])
(table* request))
nil))
(def vendor-table (partial helper/table grid-page))
(def page (partial helper/page grid-page))