From 7b7275f7a459ded750b704e33ca11cc5159e5a6f Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 15 May 2018 09:01:02 -0700 Subject: [PATCH] marks as paid. --- src/clj/auto_ap/db/invoices.clj | 27 ++++++++++++++++--- src/clj/auto_ap/db/invoices_checks.clj | 10 +++++-- src/clj/auto_ap/graphql.clj | 1 + src/clj/auto_ap/routes/checks.clj | 2 ++ src/clj/auto_ap/routes/invoices.clj | 17 +++--------- .../auto_ap/views/pages/unpaid_invoices.cljs | 2 +- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index 20515223..3a943684 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -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}] @@ -128,4 +134,17 @@ :vendor-id (:id (first (filter #(= (:code %) vendor-code) vendors))) :imported false :potential-duplicate false) - :vendor-code))))) + :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)))))) diff --git a/src/clj/auto_ap/db/invoices_checks.clj b/src/clj/auto_ap/db/invoices_checks.clj index 143b78c7..e91e50a4 100644 --- a/src/clj/auto_ap/db/invoices_checks.clj +++ b/src/clj/auto_ap/db/invoices_checks.clj @@ -19,9 +19,15 @@ (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]))))) + (first (query (-> base-query + (helpers/merge-where [:= :id id]))))) (defn insert-multi! [rows] (->> (j/insert-multi! (get-conn) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index a052e6d2..6af8d817 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -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} diff --git a/src/clj/auto_ap/routes/checks.clj b/src/clj/auto_ap/routes/checks.clj index e1463c2b..74489f15 100644 --- a/src/clj/auto_ap/routes/checks.clj +++ b/src/clj/auto_ap/routes/checks.clj @@ -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))})) diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 50aaaf75..9d1934bd 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -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-vendors (by :name (vendors/get-all)) - all-companies (reduce - (fn [x y] - (-> x - (assoc (:code y) y) - (assoc (:name y) y))) - {} - (companies/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")) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 0bc9b24d..4e676d88 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -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