outstanding balance and addresses.
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
[honeysql.core :as sql]
|
||||
[honeysql.helpers :as helpers]))
|
||||
|
||||
(def all-fields #{:name :email :data :id})
|
||||
|
||||
(def base-query (sql/build :select :*
|
||||
:from :companies))
|
||||
|
||||
@@ -17,7 +19,9 @@
|
||||
(defn fields->data [x]
|
||||
(-> x
|
||||
(assoc-in [:data :bank-accounts] (:bank-accounts x))
|
||||
(dissoc :bank-accounts)))
|
||||
(assoc-in [:data :address] (:address x))
|
||||
(dissoc :bank-accounts)
|
||||
(dissoc :address)))
|
||||
|
||||
(defn get-all []
|
||||
(map data->fields (query base-query)))
|
||||
@@ -28,10 +32,9 @@
|
||||
(helpers/merge-where [:= :id id]))))))
|
||||
|
||||
(defn upsert [id data]
|
||||
(prn (clj->db (select-keys data entity/all-keys)))
|
||||
(-> (sql/build
|
||||
:update :companies
|
||||
:set (clj->db (select-keys (fields->data data) entity/all-keys ))
|
||||
:set (clj->db (select-keys (fields->data data) all-fields))
|
||||
:where [:= :id (if (int? id)
|
||||
id
|
||||
(Integer/parseInt id))])
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
[honeysql.core :as sql]
|
||||
[honeysql.helpers :as helpers]))
|
||||
|
||||
(def all-keys #{:company-id :vendor-id :imported :potential-duplicate :total :invoice-number :date})
|
||||
(def all-keys #{:company-id :vendor-id :imported :potential-duplicate :total :invoice-number :date :outstanding-balance})
|
||||
|
||||
(defn insert-multi! [rows]
|
||||
(j/insert-multi! (get-conn)
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
|
||||
(defn upsert-multi! [rows]
|
||||
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported :status]))
|
||||
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported :status :outstanding-balance]))
|
||||
column-names (str/join "," (map name k))]
|
||||
(reduce
|
||||
(fn [affected rows]
|
||||
@@ -31,7 +31,7 @@
|
||||
(map clj->db )
|
||||
(map (apply juxt k))))]]
|
||||
:insert-into [[:invoices k]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported :v.status]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported :v.status :v.outstanding-balance]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
@@ -136,15 +136,15 @@
|
||||
:potential-duplicate false)
|
||||
:vendor-code)))))
|
||||
|
||||
(defn mark-finished-if-complete [invoice-id]
|
||||
(let [{:keys [total]} (get-by-id invoice-id)
|
||||
_ (println "TOTAL total" total)
|
||||
|
||||
paid (invoices-checks/get-sum-by-invoice invoice-id)
|
||||
_ (println "PAID" paid)]
|
||||
(when (< (Math/abs (float (- paid total))) 0.01)
|
||||
(j/db-do-prepared (get-conn)
|
||||
(-> (helpers/update :invoices)
|
||||
(helpers/sset {:status "paid"})
|
||||
(helpers/where [:= :id invoice-id])
|
||||
(sql/format))))))
|
||||
(defn apply-payment [invoice-id amount]
|
||||
(j/db-do-prepared (get-conn)
|
||||
(-> (helpers/update :invoices)
|
||||
(helpers/sset {:outstanding-balance (sql/call :- :outstanding-balance amount)})
|
||||
(helpers/where [:= :id invoice-id])
|
||||
(sql/format)))
|
||||
(j/db-do-prepared (get-conn)
|
||||
(-> (helpers/update :invoices)
|
||||
(helpers/sset {:status "paid"})
|
||||
(helpers/where [:and [:< :outstanding-balance 0.01]
|
||||
[:= :id invoice-id]])
|
||||
(sql/format))))
|
||||
|
||||
@@ -66,6 +66,7 @@
|
||||
:invoice
|
||||
{:fields {:id {:type 'Int}
|
||||
:total {:type 'String}
|
||||
:outstanding_balance {:type 'String}
|
||||
:invoice_number {:type 'String}
|
||||
:date {:type 'String}
|
||||
:company_id {:type 'Int}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.numeric :refer [num->words]]
|
||||
[auto-ap.db.checks :as checks]
|
||||
[auto-ap.db.invoices-checks :as invoices-checks]
|
||||
[auto-ap.db.utils :refer [query]]
|
||||
@@ -26,12 +27,13 @@
|
||||
[{:left-margin 10 :right-margin 0 :top-margin 0 :bottom-margin 0}
|
||||
(let [{:keys [paid-to company check date amount memo] {vendor-name :name :as vendor} :vendor} check
|
||||
df (DecimalFormat. "#,###.00")
|
||||
_ (println amount (class amount))
|
||||
word-amount (num->words amount)
|
||||
|
||||
amount (str "--" (.format df amount) "--")]
|
||||
|
||||
[:table {:num-cols 12 :border false :leading 11}
|
||||
[(let [{:keys [name address1 city state zip bank]} company]
|
||||
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" address1 "\n" (str city ", " state zip)] ])
|
||||
[(let [{:keys [name bank] {:keys [street1 street2 city state zip ]} :address} company]
|
||||
[:cell {:colspan 4 } [:paragraph {:leading 14} name "\n" street1 "\n" (str city ", " state " " zip)] ])
|
||||
(let [{{:keys [name acct]} :bank} company]
|
||||
[:cell {:colspan 6 :align :center} [:paragraph {:style :bold} name] [:paragraph acct]])
|
||||
[:cell {:colspan 2 :size 13}
|
||||
@@ -46,7 +48,7 @@
|
||||
[:cell {:colspan 3} amount]]
|
||||
|
||||
[[:cell {}]
|
||||
[:cell {:colspan 8} "One thousand two hundred thirty four and fifty six cents" [:line]]
|
||||
[:cell {:colspan 8} word-amount [:line]]
|
||||
[:cell {:colspan 3}]]
|
||||
|
||||
[[:cell {:colspan 12} [:spacer]]]
|
||||
@@ -67,11 +69,12 @@
|
||||
[[:cell]
|
||||
(into
|
||||
[:cell {:colspan 9}]
|
||||
(let [{:keys [name address1 city state zip bank]} company]
|
||||
(let [{:keys [name]
|
||||
{:keys [street1 city state zip bank]} :address} company]
|
||||
(list
|
||||
[:paragraph name]
|
||||
[:paragraph address1]
|
||||
[:paragraph city ", " state zip]
|
||||
[:paragraph street1]
|
||||
[:paragraph city ", " state " " zip]
|
||||
)))
|
||||
[:cell {:colspan 2 :size 13}
|
||||
check]]
|
||||
@@ -177,10 +180,7 @@
|
||||
:memo memo
|
||||
:date "5/10/2018"
|
||||
:company {:name (:name company)
|
||||
:address1 "123 main st"
|
||||
:city "Campbell"
|
||||
:state "CA"
|
||||
:zip "95008"
|
||||
:address (:address company)
|
||||
:bank {:name "Bank of America, NA"
|
||||
:acct "11-35/2010"
|
||||
:acct-number (:number bank-account)}}}
|
||||
@@ -216,9 +216,9 @@
|
||||
|
||||
(make-pdfs (map second checks))
|
||||
(companies/upsert company-id updated-company)
|
||||
(doseq [{:keys [invoice-id]} invoice-payments]
|
||||
(invoices/mark-finished-if-complete invoice-id))
|
||||
{:invoices (mapcat first checks)
|
||||
(doseq [{:keys [invoice-id amount]} invoice-payments]
|
||||
(invoices/apply-payment invoice-id amount))
|
||||
{:invoices (invoices/get-multi (map :id (doto (mapcat first checks) println)))
|
||||
:pdf-url (merge-pdfs (map (comp :s3-key second) checks))}))
|
||||
|
||||
|
||||
|
||||
@@ -134,6 +134,7 @@
|
||||
{:vendor-id vendor-id
|
||||
:company-id company-id
|
||||
:total total
|
||||
:outstanding-balance total
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:invoice-number invoice-number
|
||||
|
||||
Reference in New Issue
Block a user