making invoices that are paid create a corresponding check
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
-- 1532704782 UP fix-iguanas-locations
|
||||
update companies set data = '{:locations ["DT" "EG" "SC" "SG" "CB" "BH"] :bank-accounts [{:id 0 :type "cash" :name "Cash"} {:number "000158443280" :id 1 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1120 BofA Exp and Main - 3280" :yodlee-account-id 16279663} {:number "" :id 2 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1122 BofA Squirrel - 5255" :yodlee-account-id 16279666} {:number "000158743279" :id 3 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1121 BofA Tax & SLO - 3279" :yodlee-account-id 16279664} {:number "" :id 4 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1123 BofA Paychecks - 5250" :yodlee-account-id 16279665} {:number "" :id 5 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1125 BofA EG Exp - 5598" :yodlee-account-id 16279667} {:number "" :id 6 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1126 BofA SC Exp - 5318" :yodlee-account-id 16279668} {:number "" :id 7 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1127 BofA SG Exp - 8407" :yodlee-account-id 16279669} {:number "" :id 8 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1124 BofA DT Exp - 8279" :yodlee-account-id 16279670} {:number "" :id 9 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1128 BofA CB Exp - 2495" :yodlee-account-id 16279671} {:number "" :id 10 :check-number 10000 :bank-name "Bank of America" :bank-code "11-35/1210" :routing "121000358" :name "A1129 BofA BH Exp - 1665" :yodlee-account-id 16279672}]}' where id = 39;
|
||||
|
||||
|
||||
|
||||
@@ -24,26 +24,36 @@
|
||||
column-names (str/join "," (map name k))]
|
||||
(reduce
|
||||
(fn [affected rows]
|
||||
(+ affected
|
||||
(first (j/db-do-prepared (get-conn)
|
||||
(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 :v.default-expense-account]
|
||||
: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] }] })))))
|
||||
0
|
||||
(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 :v.default-expense-account]
|
||||
: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.*
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
(:require [auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.db.invoices-checks :as invoices-checks]
|
||||
[auto-ap.db.checks :as checks]
|
||||
[auto-ap.db.utils :refer [query]]
|
||||
[auto-ap.yodlee.import :refer [manual-import]]
|
||||
[auto-ap.utils :refer [by]]
|
||||
@@ -159,26 +161,42 @@
|
||||
(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 default-expense-account]}]
|
||||
(map (fn [{:keys [vendor-id total company-id amount date invoice-number default-location default-expense-account check]}]
|
||||
{:vendor-id vendor-id
|
||||
:company-id company-id
|
||||
:default-location default-location
|
||||
:default-expense-account default-expense-account
|
||||
:total total
|
||||
:outstanding-balance total
|
||||
:outstanding-balance (if (= "Cash" check)
|
||||
0
|
||||
total)
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:status (if (= "Cash" check)
|
||||
"paid"
|
||||
"unpaid")
|
||||
:invoice-number invoice-number
|
||||
:date date}))))
|
||||
|
||||
inserted-row-count (invoices/upsert-multi! insert-rows)
|
||||
already-imported-count (- (count insert-rows) inserted-row-count)]
|
||||
inserted-rows (invoices/upsert-multi! insert-rows)
|
||||
already-imported-count (- (count insert-rows) (count inserted-rows))]
|
||||
(doseq [inserted-row inserted-rows
|
||||
:when (= "paid" (:status inserted-row))]
|
||||
(let [inserted-check (checks/insert! {:vendor-id (:vendor-id inserted-row)
|
||||
:company-id (:company-id inserted-row)
|
||||
:bank-account-id 0
|
||||
:type "cash"
|
||||
:amount (:total inserted-row)
|
||||
:status "cleared"
|
||||
:date (:date inserted-row)})]
|
||||
(invoices-checks/insert-multi! [{:amount (:total inserted-row)
|
||||
:invoice-id (:id inserted-row)
|
||||
:check-id (:id inserted-check)}])))
|
||||
(expense-accounts/assign-defaults!)
|
||||
|
||||
|
||||
|
||||
{:status 200
|
||||
:body (pr-str {:imported inserted-row-count
|
||||
:body (pr-str {:imported (count inserted-rows)
|
||||
:already-imported already-imported-count
|
||||
:vendors-not-found vendors-not-found
|
||||
:errors (map #(dissoc % :date) error-rows)})
|
||||
|
||||
Reference in New Issue
Block a user