outstanding balance and addresses.

This commit is contained in:
Bryce Covert
2018-05-17 19:37:50 -07:00
parent 7b7275f7a4
commit e055a1e120
19 changed files with 351 additions and 98 deletions

View File

@@ -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))])

View File

@@ -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))))