adding invoices works correctly again.
This commit is contained in:
@@ -2,40 +2,48 @@
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-company]]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.invoices-expense-accounts :as invoices-expense-accounts]
|
||||
[auto-ap.expense-accounts :as expense-accounts]
|
||||
[auto-ap.time :refer [parse iso-date]]))
|
||||
[auto-ap.time :refer [parse iso-date]]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[clj-time.coerce :as coerce]))
|
||||
|
||||
(defn -create-or-get-vendor [vendor-id vendor-name]
|
||||
(if vendor-id
|
||||
(vendors/get-by-id vendor-id)
|
||||
(vendors/insert {:name vendor-name :default-expense-account 0})))
|
||||
|
||||
(defn add-invoice [context {{:keys [total invoice_number location company_id vendor_id vendor_name date] :as in} :invoice} value]
|
||||
(when (seq (invoices/find-conflicting {:invoice-number invoice_number
|
||||
:vendor-id vendor_id
|
||||
:company-id company_id}))
|
||||
(defn add-invoice [context {{:keys [total invoice_number location client_id vendor_id vendor_name date] :as in} :invoice} value]
|
||||
(when (seq (d-invoices/find-conflicting {:invoice/invoice-number invoice_number
|
||||
:invoice/vendor vendor_id
|
||||
:invoice/client client_id}))
|
||||
(throw (ex-info (str "Invoice '" invoice_number "' already exists.") {:invoice-number invoice_number})))
|
||||
(let [vendor (-create-or-get-vendor vendor_id vendor_name)
|
||||
_ (assert-can-see-company (:id context) company_id)
|
||||
_ (when-not (:default-expense-account vendor)
|
||||
(throw (ex-info (str "Vendor '" (:name vendor) "' does not have a default expense acount.") {:vendor-id vendor_id} )))
|
||||
company (companies/get-by-id company_id)
|
||||
|
||||
[invoice] (invoices/insert-multi! [{:invoice-number invoice_number
|
||||
:company-id company_id
|
||||
:vendor-id (:id vendor)
|
||||
:total total
|
||||
:outstanding-balance total
|
||||
:status "unpaid"
|
||||
:imported true
|
||||
:date (parse date iso-date)}])]
|
||||
(invoices-expense-accounts/replace-for-invoice
|
||||
(:id invoice) [{:expense-account-id (:default-expense-account vendor)
|
||||
:location (get-in expense-accounts/expense-accounts [(:default-expense-account vendor) :location] location)
|
||||
:amount total}] )
|
||||
(-> invoice
|
||||
(let [_ (assert-can-see-company (:id context) client_id)
|
||||
vendor (d-vendors/get-by-id vendor_id)
|
||||
|
||||
expense-account-id (:vendor/default-expense-account vendor)
|
||||
_ (when-not expense-account-id
|
||||
(throw (ex-info (str "Vendor '" (:vendor/name vendor) "' does not have a default expense acount.") {:vendor-id vendor_id} )))
|
||||
transaction [{:db/id "invoice"
|
||||
:invoice/invoice-number invoice_number
|
||||
:invoice/client (Long/parseLong client_id)
|
||||
:invoice/vendor (Long/parseLong vendor_id)
|
||||
:invoice/total total
|
||||
:invoice/outstanding-balance total
|
||||
:invoice/status :invoice-status/unpaid
|
||||
:invoice/date (coerce/to-date date)
|
||||
:invoice/expense-accounts [{:invoice-expense-account/expense-account-id expense-account-id
|
||||
:invoice-expense-account/location (get-in expense-accounts/expense-accounts [expense-account-id :location] location)
|
||||
:invoice-expense-account/amount total}]}]
|
||||
transaction-result @(d/transact (d/connect uri) transaction)
|
||||
]
|
||||
|
||||
|
||||
(-> (d-invoices/get-by-id (get-in transaction-result [:tempids "invoice"]))
|
||||
(->graphql))))
|
||||
|
||||
|
||||
@@ -66,9 +74,7 @@
|
||||
(->graphql))))
|
||||
|
||||
|
||||
(defn get-invoices-expense-accounts [context args value]
|
||||
(->graphql
|
||||
(invoices-expense-accounts/get-for-invoice (:id value))))
|
||||
|
||||
|
||||
(defn edit-expense-accounts [context args value]
|
||||
(assert-can-see-company (:id context) (:company-id (invoices/get-by-id (:invoice_id args))))
|
||||
|
||||
Reference in New Issue
Block a user