making invoices that are paid create a corresponding check
This commit is contained in:
@@ -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.*
|
||||
|
||||
Reference in New Issue
Block a user