making invoices that are paid create a corresponding check

This commit is contained in:
Bryce Covert
2018-07-29 16:32:13 -07:00
parent 4069f731e9
commit 5cac08437f
3 changed files with 56 additions and 26 deletions

View File

@@ -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)})