marks as paid.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
(:require [auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]
|
(:require [auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]
|
||||||
[auto-ap.parse :as parse]
|
[auto-ap.parse :as parse]
|
||||||
[auto-ap.db.companies :as companies]
|
[auto-ap.db.companies :as companies]
|
||||||
|
[auto-ap.db.invoices-checks :as invoices-checks]
|
||||||
[auto-ap.db.vendors :as vendors]
|
[auto-ap.db.vendors :as vendors]
|
||||||
[auto-ap.entities.companies :as company]
|
[auto-ap.entities.companies :as company]
|
||||||
[auto-ap.entities.vendors :as vendor]
|
[auto-ap.entities.vendors :as vendor]
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn upsert-multi! [rows]
|
(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))]
|
column-names (str/join "," (map name k))]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [affected rows]
|
(fn [affected rows]
|
||||||
@@ -30,7 +31,7 @@
|
|||||||
(map clj->db )
|
(map clj->db )
|
||||||
(map (apply juxt k))))]]
|
(map (apply juxt k))))]]
|
||||||
:insert-into [[:invoices 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]
|
:from [:v]
|
||||||
:left-join [[:invoices :exist]
|
:left-join [[:invoices :exist]
|
||||||
[:and
|
[:and
|
||||||
@@ -55,6 +56,10 @@
|
|||||||
(query (-> base-query
|
(query (-> base-query
|
||||||
(helpers/merge-where [:in :id ids]))))
|
(helpers/merge-where [:in :id ids]))))
|
||||||
|
|
||||||
|
(defn get-by-id [id]
|
||||||
|
(first (query (-> base-query
|
||||||
|
(helpers/merge-where [:= :id id])))))
|
||||||
|
|
||||||
(defn approve []
|
(defn approve []
|
||||||
(j/update! (get-conn) :invoices {:imported true} [] ))
|
(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
|
(cond-> base-query
|
||||||
(not (nil? imported)) (helpers/merge-where [:= :imported imported])
|
(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])))
|
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])))
|
||||||
|
|
||||||
(defn get-graphql [{:keys [start sort-by asc] :as args}]
|
(defn get-graphql [{:keys [start sort-by asc] :as args}]
|
||||||
@@ -128,4 +134,17 @@
|
|||||||
:vendor-id (:id (first (filter #(= (:code %) vendor-code) vendors)))
|
:vendor-id (:id (first (filter #(= (:code %) vendor-code) vendors)))
|
||||||
:imported false
|
:imported false
|
||||||
:potential-duplicate 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))))))
|
||||||
|
|||||||
@@ -19,9 +19,15 @@
|
|||||||
(query (-> base-query
|
(query (-> base-query
|
||||||
(helpers/merge-where [:= :invoice-id id]))))
|
(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]
|
(defn get-by-id [id]
|
||||||
(first (query (-> base-query
|
(first (query (-> base-query
|
||||||
(helpers/merge-where [:= :id id])))))
|
(helpers/merge-where [:= :id id])))))
|
||||||
|
|
||||||
(defn insert-multi! [rows]
|
(defn insert-multi! [rows]
|
||||||
(->> (j/insert-multi! (get-conn)
|
(->> (j/insert-multi! (get-conn)
|
||||||
|
|||||||
@@ -100,6 +100,7 @@
|
|||||||
:queries
|
:queries
|
||||||
{:invoice_page {:type '(list :invoice_page)
|
{:invoice_page {:type '(list :invoice_page)
|
||||||
:args {:imported {:type 'Boolean}
|
:args {:imported {:type 'Boolean}
|
||||||
|
:status {:type 'String}
|
||||||
:company_id {:type 'Int}
|
:company_id {:type 'Int}
|
||||||
:start {:type 'Int}
|
:start {:type 'Int}
|
||||||
:sort_by {:type 'String}
|
:sort_by {:type 'String}
|
||||||
|
|||||||
@@ -216,6 +216,8 @@
|
|||||||
|
|
||||||
(make-pdfs (map second checks))
|
(make-pdfs (map second checks))
|
||||||
(companies/upsert company-id updated-company)
|
(companies/upsert company-id updated-company)
|
||||||
|
(doseq [{:keys [invoice-id]} invoice-payments]
|
||||||
|
(invoices/mark-finished-if-complete invoice-id))
|
||||||
{:invoices (mapcat first checks)
|
{:invoices (mapcat first checks)
|
||||||
:pdf-url (merge-pdfs (map (comp :s3-key second) checks))}))
|
:pdf-url (merge-pdfs (map (comp :s3-key second) checks))}))
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
[auto-ap.db.vendors :as vendors]
|
[auto-ap.db.vendors :as vendors]
|
||||||
[auto-ap.db.invoices :as invoices]
|
[auto-ap.db.invoices :as invoices]
|
||||||
[auto-ap.db.utils :refer [query]]
|
[auto-ap.db.utils :refer [query]]
|
||||||
|
[auto-ap.utils :refer [by]]
|
||||||
[auto-ap.parse :as parse]
|
[auto-ap.parse :as parse]
|
||||||
[auto-ap.routes.utils :refer [wrap-secure]]
|
[auto-ap.routes.utils :refer [wrap-secure]]
|
||||||
[compojure.core :refer [GET POST context defroutes
|
[compojure.core :refer [GET POST context defroutes
|
||||||
@@ -107,20 +108,10 @@
|
|||||||
{{:keys [excel-rows]} :edn-params}
|
{{: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]
|
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on]
|
||||||
|
|
||||||
all-vendors (reduce
|
all-vendors (by :name (vendors/get-all))
|
||||||
(fn [x y]
|
|
||||||
(assoc x (:name y) y))
|
|
||||||
{}
|
|
||||||
(vendors/get-all))
|
|
||||||
|
|
||||||
all-companies (reduce
|
all-companies (companies/get-all)
|
||||||
(fn [x y]
|
all-companies (merge (by :code all-companies) (by :name all-companies))
|
||||||
(-> x
|
|
||||||
(assoc (:code y) y)
|
|
||||||
(assoc (:name y) y)))
|
|
||||||
{}
|
|
||||||
(companies/get-all))
|
|
||||||
|
|
||||||
|
|
||||||
rows (->> (str/split excel-rows #"\n" )
|
rows (->> (str/split excel-rows #"\n" )
|
||||||
(map #(str/split % #"\t"))
|
(map #(str/split % #"\t"))
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
(assoc-in [:status :loading] true)
|
(assoc-in [:status :loading] true)
|
||||||
(assoc-in [::params] params))
|
(assoc-in [::params] params))
|
||||||
:graphql {:token (-> cofx :db :user)
|
: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]}}))
|
:on-success [::received]}}))
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
|
|||||||
Reference in New Issue
Block a user