adding default expense account to export
This commit is contained in:
@@ -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 :outstanding-balance :default-location})
|
||||
(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)
|
||||
@@ -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 :outstanding-balance :default-location]))
|
||||
(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]
|
||||
@@ -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 :v.outstanding-balance :v.default-location]
|
||||
{: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 :v.default-expense-account]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
(defn assign-defaults! []
|
||||
(j/db-do-prepared (get-conn)
|
||||
(sql/format {:insert-into [[:invoices-expense-accounts [:invoice-id :expense-account-id :amount :location]]
|
||||
{:select [:i.id :v.default-expense-account :i.total :i.default_location]
|
||||
{:select [:i.id (sql/raw "COALESCE (i.default_expense_account, v.default_expense_account)") :i.total :i.default_location]
|
||||
:from [[:invoices :i]]
|
||||
:join [[:vendors :v]
|
||||
[:= :v.id :i.vendor-id]]
|
||||
|
||||
@@ -55,6 +55,13 @@
|
||||
(catch Exception e
|
||||
(throw (Exception. (str "Could not parse total from value '" (:amount i) "'") e)))))
|
||||
|
||||
(defn parse-default-expense-account [i]
|
||||
(try
|
||||
(when-let [default-expense-account (:default-expense-account i)]
|
||||
(Integer/parseInt default-expense-account))
|
||||
(catch Exception e
|
||||
(throw (Exception. (str "Could not parse expense account from value '" (:default-expense-account i) "'") e)))))
|
||||
|
||||
(defn parse-account-id [i]
|
||||
(try
|
||||
(Integer/parseInt (second
|
||||
@@ -125,7 +132,7 @@
|
||||
(POST "/upload-integreat"
|
||||
{{:keys [excel-rows]} :edn-params user :identity}
|
||||
(assert-admin user)
|
||||
(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 :default-expense-account]
|
||||
|
||||
all-vendors (by :name (vendors/get-all))
|
||||
|
||||
@@ -141,6 +148,7 @@
|
||||
|
||||
(map (parse-or-error :company-id #(parse-company % all-companies)))
|
||||
(map (parse-or-error :vendor-id #(parse-vendor % all-vendors)))
|
||||
(map (parse-or-error :default-expense-account parse-default-expense-account))
|
||||
(map (parse-or-error :invoice-number parse-invoice-number))
|
||||
(map (parse-or-error :total parse-amount))
|
||||
(map (parse-or-error :date parse-date)))
|
||||
@@ -151,10 +159,11 @@
|
||||
(map :vendor-name)
|
||||
set)
|
||||
insert-rows (vec (->> (filter #(not (seq (:errors %))) rows)
|
||||
(map (fn [{:keys [vendor-id total company-id amount date invoice-number default-location]}]
|
||||
(map (fn [{:keys [vendor-id total company-id amount date invoice-number default-location default-expense-account]}]
|
||||
{:vendor-id vendor-id
|
||||
:company-id company-id
|
||||
:default-location default-location
|
||||
:default-expense-account default-expense-account
|
||||
:total total
|
||||
:outstanding-balance total
|
||||
:imported true
|
||||
|
||||
Reference in New Issue
Block a user