marks as paid.

This commit is contained in:
Bryce Covert
2018-05-15 09:01:02 -07:00
parent d9994b988c
commit 7b7275f7a4
6 changed files with 39 additions and 20 deletions

View File

@@ -2,6 +2,7 @@
(:require [auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]
[auto-ap.parse :as parse]
[auto-ap.db.companies :as companies]
[auto-ap.db.invoices-checks :as invoices-checks]
[auto-ap.db.vendors :as vendors]
[auto-ap.entities.companies :as company]
[auto-ap.entities.vendors :as vendor]
@@ -19,7 +20,7 @@
(defn upsert-multi! [rows]
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported]))
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported :status]))
column-names (str/join "," (map name k))]
(reduce
(fn [affected rows]
@@ -30,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 ]
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported :v.status]
:from [:v]
:left-join [[:invoices :exist]
[:and
@@ -55,6 +56,10 @@
(query (-> base-query
(helpers/merge-where [:in :id ids]))))
(defn get-by-id [id]
(first (query (-> base-query
(helpers/merge-where [:= :id id])))))
(defn approve []
(j/update! (get-conn) :invoices {:imported true} [] ))
@@ -103,9 +108,10 @@
(defn base-graphql [{:keys [imported company-id]}]
(defn base-graphql [{:keys [imported company-id status]}]
(cond-> base-query
(not (nil? imported)) (helpers/merge-where [:= :imported imported])
(not (nil? status)) (helpers/merge-where [:= :status status])
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
@@ -129,3 +135,16 @@
:imported false
: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))))))

View File

@@ -19,6 +19,12 @@
(query (-> base-query
(helpers/merge-where [:= :invoice-id id]))))
(defn get-sum-by-invoice [id]
(:sum (first (query (-> (helpers/select :%sum.amount)
(helpers/from :invoices-checks)
(helpers/merge-where [:= :invoice-id id])
)))))
(defn get-by-id [id]
(first (query (-> base-query
(helpers/merge-where [:= :id id])))))

View File

@@ -100,6 +100,7 @@
:queries
{:invoice_page {:type '(list :invoice_page)
:args {:imported {:type 'Boolean}
:status {:type 'String}
:company_id {:type 'Int}
:start {:type 'Int}
:sort_by {:type 'String}

View File

@@ -216,6 +216,8 @@
(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)
:pdf-url (merge-pdfs (map (comp :s3-key second) checks))}))

View File

@@ -3,6 +3,7 @@
[auto-ap.db.vendors :as vendors]
[auto-ap.db.invoices :as invoices]
[auto-ap.db.utils :refer [query]]
[auto-ap.utils :refer [by]]
[auto-ap.parse :as parse]
[auto-ap.routes.utils :refer [wrap-secure]]
[compojure.core :refer [GET POST context defroutes
@@ -107,20 +108,10 @@
{{:keys [excel-rows]} :edn-params}
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on]
all-vendors (reduce
(fn [x y]
(assoc x (:name y) y))
{}
(vendors/get-all))
all-companies (reduce
(fn [x y]
(-> x
(assoc (:code y) y)
(assoc (:name y) y)))
{}
(companies/get-all))
all-vendors (by :name (vendors/get-all))
all-companies (companies/get-all)
all-companies (merge (by :code all-companies) (by :name all-companies))
rows (->> (str/split excel-rows #"\n" )
(map #(str/split % #"\t"))

View File

@@ -27,7 +27,7 @@
(assoc-in [:status :loading] true)
(assoc-in [::params] params))
:graphql {:token (-> cofx :db :user)
:query-obj (invoice-table/query (assoc params :imported true))
:query-obj (invoice-table/query (assoc params :imported true :status "unpaid"))
:on-success [::received]}}))
(re-frame/reg-event-db