renamed company to client.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
(ns auto-ap.db.checks
|
||||
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn execute!] :as u]
|
||||
[auto-ap.entities.companies :as entity]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.java.jdbc :as j]
|
||||
[honeysql.core :as sql]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(ns auto-ap.db.companies
|
||||
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn query execute!]]
|
||||
[auto-ap.entities.companies :as entity]
|
||||
|
||||
[clojure.edn :as edn]
|
||||
[clojure.java.jdbc :as j]
|
||||
[honeysql.core :as sql]
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
[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]
|
||||
[clojure.java.jdbc :as j]
|
||||
[clojure.string :as str]
|
||||
[honeysql.core :as sql]
|
||||
@@ -13,47 +11,6 @@
|
||||
|
||||
(def all-keys #{:company-id :vendor-id :imported :potential-duplicate :total :invoice-number :date :outstanding-balance :default-location :default-expense-account})
|
||||
|
||||
(defn insert-multi! [rows]
|
||||
(j/insert-multi! (get-conn)
|
||||
:invoices
|
||||
(map clj->db rows)))
|
||||
|
||||
|
||||
(defn upsert-multi! [rows]
|
||||
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported :status :outstanding-balance :default-location :default-expense-account]))
|
||||
column-names (str/join "," (map name k))]
|
||||
(reduce
|
||||
(fn [affected rows]
|
||||
(concat affected
|
||||
(let [[query & params] (sql/format {:with [[[:v (str "(" column-names ")")]
|
||||
(helpers/values (->> rows
|
||||
(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 :v.outstanding-balance :v.default-location (sql/raw "cast(v.default_expense_account as INT)")]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
[:= :exist.invoice-number :v.invoice-number]
|
||||
[:not= :exist.status "voided"]
|
||||
[:= :exist.company-id :v.company-id]
|
||||
[:or [:= :exist.vendor-id :v.vendor-id]
|
||||
[:and
|
||||
[:= :exist.vendor-id nil]
|
||||
[:= :v.vendor-id nil]]]]]
|
||||
:where [:= :exist.id nil] }]})
|
||||
statement (j/prepare-statement
|
||||
(j/get-connection (get-conn))
|
||||
query
|
||||
{:return-keys true})]
|
||||
(j/execute!
|
||||
(get-conn)
|
||||
(concat [statement] params))
|
||||
(->> (j/result-set-seq (.getGeneratedKeys statement))
|
||||
(map db->clj)
|
||||
doall))))
|
||||
[]
|
||||
(partition-all 2000 rows))))
|
||||
|
||||
(def base-query (sql/build :select :invoices.*
|
||||
:from :invoices))
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
(ns auto-ap.db.invoices-checks
|
||||
(:require [auto-ap.db.utils :refer [clj->db db->clj get-conn execute!] :as u]
|
||||
[auto-ap.entities.companies :as entity]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.java.jdbc :as j]
|
||||
[honeysql.core :as sql]
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[buddy.auth :refer [throw-unauthorized]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-company? assert-can-see-company]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin can-see-client? assert-can-see-client]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.users :as d-users]
|
||||
@@ -32,13 +32,6 @@
|
||||
:serialize (schema/as-conformer #(.toString %))}}
|
||||
:objects
|
||||
{
|
||||
:company
|
||||
{:fields {:id {:type 'String}
|
||||
:name {:type 'String}
|
||||
:email {:type 'String}
|
||||
:address {:type :address}
|
||||
:locations {:type '(list String)}
|
||||
:bank_accounts {:type '(list :bank_account)}}}
|
||||
:client
|
||||
{:fields {:id {:type :id}
|
||||
:name {:type 'String}
|
||||
@@ -96,7 +89,6 @@
|
||||
:type {:type 'String}
|
||||
:amount {:type 'String}
|
||||
:vendor {:type :vendor}
|
||||
:company {:type :company}
|
||||
:date {:type 'String}
|
||||
:bank_account {:type :bank_account}
|
||||
:memo {:type 'String}
|
||||
@@ -168,7 +160,7 @@
|
||||
:status {:type 'String}
|
||||
:expense_accounts {:type '(list :invoices_expense_accounts)}
|
||||
:date {:type 'String}
|
||||
:company_id {:type 'Int}
|
||||
:client_id {:type 'Int}
|
||||
:payments {:type '(list :invoice_payment)}
|
||||
:vendor {:type :vendor}
|
||||
:client {:type :client}}}
|
||||
@@ -241,10 +233,8 @@
|
||||
|
||||
:resolve :get-payment-page}
|
||||
|
||||
:company {:type '(list :company)
|
||||
:resolve :get-company}
|
||||
:client {:type '(list :company)
|
||||
:resolve :get-company}
|
||||
:client {:type '(list :client)
|
||||
:resolve :get-client}
|
||||
:vendor {:type '(list :vendor)
|
||||
:resolve :get-vendor}
|
||||
:user {:type '(list :user)
|
||||
@@ -402,9 +392,9 @@
|
||||
:limit Integer/MAX_VALUE))))
|
||||
|
||||
|
||||
(defn get-company [context args value]
|
||||
(defn get-client [context args value]
|
||||
(->graphql
|
||||
(filter #(can-see-company? (:id context) %)
|
||||
(filter #(can-see-client? (:id context) %)
|
||||
(d-clients/get-all))))
|
||||
|
||||
(defn get-user [context args value]
|
||||
@@ -420,7 +410,7 @@
|
||||
|
||||
(defn print-checks [context args value]
|
||||
|
||||
(assert-can-see-company (:id context) (:client_id args))
|
||||
(assert-can-see-client (:id context) (:client_id args))
|
||||
(->graphql
|
||||
(gq-checks/print-checks (map (fn [i] {:invoice-id (:invoice_id i)
|
||||
:amount (:amount i)})
|
||||
@@ -439,7 +429,7 @@
|
||||
:get-payment-page gq-checks/get-payment-page
|
||||
:get-transaction-page gq-transactions/get-transaction-page
|
||||
|
||||
:get-company get-company
|
||||
:get-client get-client
|
||||
:get-user get-user
|
||||
:mutation/add-handwritten-check gq-checks/add-handwritten-check
|
||||
:mutation/print-checks print-checks
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(ns auto-ap.graphql.checks
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]]
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client]]
|
||||
[datomic.api :as d]
|
||||
[clojure.edn :as edn]
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
@@ -316,7 +316,7 @@
|
||||
(let [invoice (d-invoices/get-by-id (:invoice_id args))
|
||||
bank-account-id (:bank_account_id args)
|
||||
bank-account (d-bank-accounts/get-by-id bank-account-id)
|
||||
_ (assert-can-see-company (:id context) (:invoice/client invoice))
|
||||
_ (assert-can-see-client (:id context) (:invoice/client invoice))
|
||||
base-payment (base-payment [invoice] (:invoice/vendor invoice)
|
||||
(:invoice/client invoice)
|
||||
bank-account :payment-type/check 0 {(:invoice_id args) (:amount args)})]
|
||||
@@ -339,7 +339,7 @@
|
||||
(let [check (d-checks/get-by-id id)]
|
||||
(assert (or (= :payment-status/pending (:payment/status check))
|
||||
(#{:payment-type/cash :payment-type/debit} (:payment/type check))))
|
||||
(assert-can-see-company (:id context) (:client-id check))
|
||||
(assert-can-see-client (:id context) (:client-id check))
|
||||
(let [removing-payments (mapcat (fn [x]
|
||||
(let [invoice (:invoice-payment/invoice x)
|
||||
new-balance (+ (:invoice/outstanding-balance invoice)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(ns auto-ap.graphql.invoices
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company assert-admin]]
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client assert-admin]]
|
||||
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
@@ -34,7 +34,7 @@
|
||||
:invoice/vendor vendor_id
|
||||
:invoice/client client_id}))
|
||||
(throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number})))
|
||||
(let [_ (assert-can-see-company (:id context) client_id)
|
||||
(let [_ (assert-can-see-client (:id context) client_id)
|
||||
vendor (d-vendors/get-by-id vendor_id)
|
||||
|
||||
expense-account-id (:vendor/default-expense-account vendor)
|
||||
@@ -59,7 +59,7 @@
|
||||
(->graphql))))
|
||||
|
||||
|
||||
(defn edit-invoice [context {{:keys [id invoice_number total vendor_id date company_id] :as in} :invoice} value]
|
||||
(defn edit-invoice [context {{:keys [id invoice_number total vendor_id date client_id] :as in} :invoice} value]
|
||||
|
||||
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
@@ -71,7 +71,7 @@
|
||||
println))
|
||||
(throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number})))
|
||||
paid-amount (- (:invoice/total invoice) (:invoice/outstanding-balance invoice))
|
||||
_ (assert-can-see-company (:id context) (:db/id (:client invoice)))
|
||||
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
|
||||
updated-invoice (d-invoices/update {:db/id id
|
||||
:invoice/invoice-number invoice_number
|
||||
:invoice/date (coerce/to-date (parse date iso-date))
|
||||
@@ -82,7 +82,7 @@
|
||||
|
||||
(defn void-invoice [context {id :invoice_id} value]
|
||||
(let [invoice (d-invoices/get-by-id id)
|
||||
_ (assert-can-see-company (:id context) (:company-id invoice))
|
||||
_ (assert-can-see-client (:id context) (:client-id invoice))
|
||||
updated-invoice (d-invoices/update {:db/id id
|
||||
:invoice/total 0.0
|
||||
:invoice/outstanding-balance 0.0
|
||||
@@ -98,7 +98,7 @@
|
||||
(defn edit-expense-accounts [context args value]
|
||||
;; TODO - Can expense account id be used as a unique field? It may compose with component, meaning
|
||||
;; that you don't have to figure out which ones to delete and which ones to add. Just set to 0.
|
||||
(assert-can-see-company (:id context) (:db/id (:client (d-invoices/get-by-id (:invoice_id args)))))
|
||||
(assert-can-see-client (:id context) (:db/id (:invoice/client (d-invoices/get-by-id (:invoice_id args)))))
|
||||
(let [current-expense-accounts (:invoice/expense-accounts (d-invoices/get-by-id (:invoice_id args)))
|
||||
invoice-id (:invoice_id args)
|
||||
|
||||
|
||||
@@ -47,14 +47,14 @@
|
||||
(when-not (= "admin" (:user/role id))
|
||||
(throw-unauthorized)))
|
||||
|
||||
(defn can-see-company? [identity company]
|
||||
(defn can-see-client? [identity client]
|
||||
(or (= "admin" (:user/role identity))
|
||||
((set (map :db/id (:user/clients identity))) (:db/id company))
|
||||
((set (map :db/id (:user/clients identity))) company)))
|
||||
((set (map :db/id (:user/clients identity))) (:db/id client))
|
||||
((set (map :db/id (:user/clients identity))) client)))
|
||||
|
||||
(defn assert-can-see-company [identity company]
|
||||
(when-not (can-see-company? identity company)
|
||||
(println "IDENTITY " identity " can not see company " company)
|
||||
(defn assert-can-see-client [identity client]
|
||||
(when-not (can-see-client? identity client)
|
||||
(println "IDENTITY " identity " can not see company " client)
|
||||
(throw-unauthorized)))
|
||||
|
||||
(defn limited-clients [id]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(ns auto-ap.graphql.vendors
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-company]]
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-client]]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.time :refer [parse iso-date]]
|
||||
[datomic.api :as d]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(ns auto-ap.handler
|
||||
(:require [amazonica.core :refer [defcredential]]
|
||||
[auto-ap.routes.auth :as auth]
|
||||
[auto-ap.routes.companies :as companies]
|
||||
[auto-ap.routes.clients :as clients]
|
||||
[auto-ap.routes.invoices :as invoices]
|
||||
[auto-ap.routes.reminders :as reminders]
|
||||
[auto-ap.routes.graphql :as graphql]
|
||||
@@ -34,7 +34,7 @@
|
||||
exports/routes
|
||||
yodlee/routes
|
||||
invoices/routes
|
||||
companies/routes
|
||||
clients/routes
|
||||
reminders/routes
|
||||
checks/routes
|
||||
graphql/routes
|
||||
|
||||
@@ -67,13 +67,12 @@
|
||||
[file filename]
|
||||
(excel/parse-file file filename))
|
||||
|
||||
(defn best-match [companies company-identifier]
|
||||
(println companies)
|
||||
(->> companies
|
||||
(map (fn [company]
|
||||
(if-let [matches (:matches company)]
|
||||
[company (apply min (map #(m/jaccard (.toLowerCase company-identifier) %) matches))]
|
||||
[company 1])))
|
||||
(defn best-match [clients client-identifier]
|
||||
(->> clients
|
||||
(map (fn [client]
|
||||
(if-let [matches (:client/matches client)]
|
||||
[client (apply min (map #(m/jaccard (.toLowerCase client-identifier) %) matches))]
|
||||
[client 1])))
|
||||
(filter #(< (second %) 0.25))
|
||||
(sort-by second)
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
(ns auto-ap.routes.companies
|
||||
(ns auto-ap.routes.clients
|
||||
(:require [auto-ap.datomic.clients :as clients]
|
||||
[auto-ap.graphql.utils :refer [can-see-company? assert-can-see-company]]
|
||||
[auto-ap.graphql.utils :refer [can-see-client? assert-can-see-client]]
|
||||
[auto-ap.routes.utils :refer [wrap-secure wrap-spec]]
|
||||
[auto-ap.entities.companies :as entity]
|
||||
[auto-ap.entities.clients :as entity]
|
||||
[compojure.core :refer [GET PUT context defroutes
|
||||
wrap-routes]]))
|
||||
|
||||
|
||||
(defroutes routes
|
||||
(wrap-routes
|
||||
(context "/companies" []
|
||||
(context "/clients" []
|
||||
#_(wrap-spec
|
||||
(PUT "/:id" {{:keys [address email locations new-bank-accounts]} :edn-params :keys [edn-params] {:keys [id ]} :route-params :as r}
|
||||
(assert-can-see-company (:identity r) id)
|
||||
(assert-can-see-client (:identity r) id)
|
||||
(let [id (Integer/parseInt id)
|
||||
company (companies/get-by-id id)
|
||||
company (d-clients/get-by-id id)
|
||||
updated-company (merge company {:address address
|
||||
:email email
|
||||
:locations locations})]
|
||||
@@ -30,7 +30,7 @@
|
||||
(if-let [id (:db/id (or (clients client-code)
|
||||
(clients client)))]
|
||||
id
|
||||
(throw (Exception. (str "Company code '" client-code "' and company named '" client "' not found.")))))
|
||||
(throw (Exception. (str "Client code '" client-code "' and client named '" client "' not found.")))))
|
||||
|
||||
(defn parse-invoice-number [{:keys [invoice-number]}]
|
||||
(or invoice-number ""))
|
||||
@@ -94,7 +94,7 @@
|
||||
:details (str e)})))))
|
||||
|
||||
(defn parse-invoice-rows [excel-rows]
|
||||
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on :default-expense-account]
|
||||
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :client :bill-entered :bill-rejected :added-on :exported-on :default-expense-account]
|
||||
all-vendors (by :vendor/name (d-vendors/get-graphql {}))
|
||||
all-clients (d-clients/get-all)
|
||||
all-clients (merge (by :client/code all-clients) (by :client/name all-clients))
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
nil))
|
||||
nil))
|
||||
|
||||
(defn import-transactions [transactions transaction->company transaction->bank-account-id]
|
||||
(defn import-transactions [transactions transaction->client transaction->bank-account-id]
|
||||
(doseq [transaction transactions
|
||||
:let [{post-date :postDate
|
||||
account-id :accountId
|
||||
@@ -62,7 +62,7 @@
|
||||
(- amount)
|
||||
amount)
|
||||
check-number (extract-check-number transaction)
|
||||
client-id (transaction->company transaction)
|
||||
client-id (transaction->client transaction)
|
||||
bank-account-id (transaction->bank-account-id transaction)
|
||||
check-id (transaction->check-id transaction check-number client-id bank-account-id amount)]]
|
||||
(try
|
||||
@@ -125,7 +125,7 @@
|
||||
:yodlee-account-id yodlee-account-id}))
|
||||
(:client/bank-accounts c)))
|
||||
(d-clients/get-all))
|
||||
transaction->company (comp (by :yodlee-account-id :client-id all-bank-accounts) :accountId)
|
||||
transaction->client (comp (by :yodlee-account-id :client-id all-bank-accounts) :accountId)
|
||||
transaction->bank-account-id (comp (by :yodlee-account-id :bank-account-id all-bank-accounts) :accountId)]
|
||||
(import-transactions transactions transaction->company transaction->bank-account-id)))
|
||||
(import-transactions transactions transaction->client transaction->bank-account-id)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user