adding invoices works correctly again.
This commit is contained in:
@@ -11,18 +11,26 @@
|
||||
(update-in [:query :where] conj where))]
|
||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
||||
|
||||
(def default-read '(pull ?e [*
|
||||
{:invoice/client [:client/name :db/id]}
|
||||
{:invoice/vendor [:vendor/name :db/id]}
|
||||
{:invoice/status [:db/ident]}
|
||||
{:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}]))
|
||||
|
||||
(defn <-datomic [x]
|
||||
(->> x
|
||||
(map first)
|
||||
|
||||
(map #(update % :invoice/date c/from-date))
|
||||
(map #(update % :invoice/status :db/ident))
|
||||
(map #(rename-keys % {:invoice-payment/_invoice :invoice/payments}))))
|
||||
|
||||
(defn raw-graphql [args]
|
||||
(->> (d/query
|
||||
(cond-> (doto {:query {:find ['(pull ?e [*
|
||||
{:invoice/client [:client/name :db/id]}
|
||||
{:invoice/vendor [:vendor/name :db/id]}
|
||||
{:invoice/status [:db/ident]}
|
||||
{:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}])]
|
||||
:in ['$]
|
||||
:where ['[?e :invoice/original-id]
|
||||
]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
println)
|
||||
(cond-> {:query {:find [default-read]
|
||||
:in ['$]
|
||||
:where ['[?e :invoice/invoice-number]]}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
|
||||
(:client-id args) (add-arg '?client-id (cond-> (:client-id args)
|
||||
(string? (:client-id args))
|
||||
@@ -34,13 +42,7 @@
|
||||
'[?c :client/original-id ?original-id])
|
||||
(:status args) (add-arg '?status (keyword "invoice-status" (:status args))
|
||||
'[?e :invoice/status ?status])))
|
||||
(map first)
|
||||
|
||||
(map #(update % :invoice/date c/from-date))
|
||||
(map #(update % :invoice/status :db/ident))
|
||||
(map #(rename-keys % {:invoice-payment/_invoice :invoice/payments}))
|
||||
|
||||
#_(map #(update % :transaction/post-date c/from-date))))
|
||||
(<-datomic)))
|
||||
|
||||
(defn sort-fn [args]
|
||||
(cond
|
||||
@@ -65,3 +67,23 @@
|
||||
|
||||
(->> (raw-graphql args)
|
||||
(count)))
|
||||
|
||||
(defn get-by-id [id]
|
||||
(->>
|
||||
(d/query (-> {:query {:find [default-read]
|
||||
:in ['$]
|
||||
:where []}
|
||||
:args [(d/db (d/connect uri))]}
|
||||
(add-arg '?e (cond-> id (string? id) Long/parseLong) ['?e])))
|
||||
(<-datomic)
|
||||
(first)))
|
||||
|
||||
(defn find-conflicting [{:keys [:invoice/invoice-number :invoice/vendor :invoice/client]}]
|
||||
(->> (d/query
|
||||
(cond-> {:query {:find [default-read]
|
||||
:in ['$ '?invoice-number '?vendor '?client]
|
||||
:where ['[?e :invoice/invoice-number ?invoice-number]
|
||||
'[?e :invoice/vendor ?vendor]
|
||||
'[?e :invoice/client ?client]]}
|
||||
:args [(d/db (d/connect uri)) invoice-number (Long/parseLong vendor) (Long/parseLong client)]}))
|
||||
(<-datomic)))
|
||||
|
||||
@@ -14,3 +14,19 @@
|
||||
(map (fn [ba]
|
||||
(update ba :bank-account/type :db/ident ))
|
||||
bas)))))))
|
||||
|
||||
(defn get-by-id [id]
|
||||
|
||||
(->> (d/q '[:find (pull ?e [*])
|
||||
:in $ ?e
|
||||
:where [?e]]
|
||||
(d/db (d/connect uri))
|
||||
(Long/parseLong id))
|
||||
(map first)
|
||||
(first)
|
||||
#_(map (fn [c]
|
||||
(update c :client/bank-accounts
|
||||
(fn [bas]
|
||||
(map (fn [ba]
|
||||
(update ba :bank-account/type :db/ident ))
|
||||
bas)))))))
|
||||
|
||||
@@ -273,12 +273,12 @@
|
||||
:amount {:type 'String}}}
|
||||
|
||||
:add_invoice
|
||||
{:fields {:id {:type 'Int}
|
||||
{:fields {:id {:type 'String}
|
||||
:invoice_number {:type 'String}
|
||||
:location {:type 'String}
|
||||
:date {:type 'String}
|
||||
:company_id {:type 'Int}
|
||||
:vendor_id {:type 'Int}
|
||||
:client_id {:type 'String}
|
||||
:vendor_id {:type 'String}
|
||||
:vendor_name {:type 'String}
|
||||
:total {:type 'Float}}}
|
||||
|
||||
@@ -316,9 +316,9 @@
|
||||
:void_invoice {:type :invoice
|
||||
:args {:invoice_id {:type 'Int}}
|
||||
:resolve :mutation/void-invoice}
|
||||
:void_check {:type :payment
|
||||
:void_payment {:type :payment
|
||||
:args {:payment_id {:type 'String}}
|
||||
:resolve :mutation/void-check}
|
||||
:resolve :mutation/void-payment}
|
||||
:edit_expense_accounts {:type :invoice
|
||||
:args {:invoice_id {:type 'Int}
|
||||
:expense_accounts {:type '(list :edit_expense_account)}}
|
||||
@@ -483,7 +483,7 @@
|
||||
:mutation/add-invoice gq-invoices/add-invoice
|
||||
:mutation/edit-invoice gq-invoices/edit-invoice
|
||||
:mutation/void-invoice gq-invoices/void-invoice
|
||||
:mutation/void-check gq-checks/void-check
|
||||
:mutation/void-payment gq-checks/void-check
|
||||
:mutation/edit-expense-accounts gq-invoices/edit-expense-accounts
|
||||
:get-vendor get-vendor
|
||||
:get-expense-account expense-accounts/get-expense-account
|
||||
|
||||
@@ -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