diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index 10c3c0f5..cb2c72ef 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -1,7 +1,7 @@ (ns auto-ap.graphql.checks (:require [amazonica.aws.s3 :as s3] - [auto-ap.datomic :refer [audit-transact remove-nils conn]] + [auto-ap.datomic :refer [conn remove-nils]] [auto-ap.datomic.accounts :as a] [auto-ap.datomic.bank-accounts :as d-bank-accounts] [auto-ap.datomic.checks :as d-checks] @@ -10,7 +10,14 @@ [auto-ap.datomic.transactions :as d-transactions] [auto-ap.datomic.vendors :as d-vendors] [auto-ap.graphql.utils - :refer [->graphql <-graphql assert-admin assert-failure assert-can-see-client enum->keyword assert-not-locked]] + :refer [->graphql + <-graphql + assert-admin + assert-can-see-client + assert-failure + assert-not-locked + enum->keyword]] + [auto-ap.ledger :refer [transact-with-ledger]] [auto-ap.numeric :refer [num->words]] [auto-ap.time :refer [iso-date local-now parse]] [auto-ap.utils :refer [by dollars-0?]] @@ -405,7 +412,7 @@ (make-pdfs (filter #(and (= :payment-type/check (:payment/type %)) (> (:payment/amount %) 0.0)) checks))) - (audit-transact checks id) + (transact-with-ledger checks id) {:invoices (d-invoices/get-multi (map :invoice-id invoice-payments)) @@ -461,7 +468,7 @@ 0 invoice-payment-lookup)] - (audit-transact + (transact-with-ledger (into [(assoc base-payment :payment/type :payment-type/check :payment/status :payment-status/pending @@ -494,7 +501,7 @@ updated-payment {:db/id id :payment/amount 0.0 :payment/status :payment-status/voided}] - (audit-transact (conj removing-payments updated-payment) + (transact-with-ledger (conj removing-payments updated-payment) (:id context))) (-> (d-checks/get-by-id id) (->graphql)))) @@ -514,7 +521,7 @@ specific-ids (d-checks/filter-ids (:ids args)) all-ids (into (set ids) specific-ids)] (log/info "Voiding " (count all-ids) args) - (audit-transact (->> all-ids + (transact-with-ledger (->> all-ids (d/q '[:find [(pull ?p [:db/id {:invoice-payment/_payment [:invoice-payment/amount :db/id @@ -630,7 +637,7 @@ - (audit-transact (-> [] + (transact-with-ledger (-> [] (conj payment) (into (invoice-payments invoices invoice-amounts))) (:id context)) (->graphql {:invoices (d-invoices/get-multi (map :db/id invoices))}))) diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 5937a050..a292c4df 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -1,7 +1,8 @@ (ns auto-ap.graphql.invoices (:require [auto-ap.datomic - :refer [audit-transact conn remove-nils uri]] + :refer [conn remove-nils uri]] + [auto-ap.ledger :refer [transact-with-ledger]] [auto-ap.datomic.clients :as d-clients] [auto-ap.datomic.invoices :as d-invoices] [auto-ap.datomic.vendors :as d-vendors] @@ -59,7 +60,7 @@ (doseq [i invoices] (assert-can-see-client (:id context) (:db/id (:invoice/client (d/entity (d/db conn) i))))) (let [transactions (map (fn [i] [:db/retractEntity i]) invoices)] - (audit-transact transactions (:id context)) + (transact-with-ledger transactions (:id context)) invoices)) (defn approve-invoices [context {:keys [invoices]} _] @@ -69,7 +70,7 @@ (assert-can-see-client (:id context) (-> invoice :invoice/client :db/id)) (assert-not-locked (-> invoice :invoice/client :db/id) (-> invoice :invoice/date))) (let [transactions (map (fn [i] {:db/id i :invoice/import-status :import-status/imported}) invoices)] - (audit-transact transactions (:id context)) + (transact-with-ledger transactions (:id context)) invoices)) (defn assert-no-conflicting [{:keys [invoice_number client_id vendor_id]}] @@ -166,7 +167,7 @@ (assert-valid-expense-accounts expense_accounts) (assert-invoice-amounts-add-up in) - (let [transaction-result (audit-transact [(add-invoice-transaction in)] (:id context))] + (let [transaction-result (transact-with-ledger [(add-invoice-transaction in)] (:id context))] (-> (d-invoices/get-by-id (get-in transaction-result [:tempids "invoice"])) (->graphql (:id context))))) @@ -183,7 +184,7 @@ (assert-not-locked client_id (:date in)) (assert-valid-expense-accounts (:expense_accounts in)) (assert-invoice-amounts-add-up in) - (let [transaction-result (audit-transact [(add-invoice-transaction in)] (:id context))] + (let [transaction-result (transact-with-ledger [(add-invoice-transaction in)] (:id context))] (-> (gq-checks/print-checks-internal [{:invoice-id (get-in transaction-result [:tempids "invoice"]) :amount total}] client_id @@ -217,7 +218,7 @@ expense_accounts)} due (assoc :invoice/due (coerce/to-date due)) scheduled_payment (assoc :invoice/scheduled-payment (coerce/to-date scheduled_payment)))] - (audit-transact (concat [updated-invoice] + (transact-with-ledger (concat [updated-invoice] (map (fn [d] [:db/retract id :invoice/expense-accounts d]) deleted)) (:id context)) (-> (d-invoices/get-by-id id) @@ -227,7 +228,7 @@ (let [invoice (d-invoices/get-by-id id)] (assert-can-see-client (:id context) (:db/id (:invoice/client invoice))) (assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice)) - (audit-transact [{:db/id id + (transact-with-ledger [{:db/id id :invoice/total 0.0 :invoice/outstanding-balance 0.0 :invoice/status :invoice-status/voided @@ -253,7 +254,7 @@ all-ids (into (set ids) specific-ids)] (log/info "Voiding " (count all-ids) args) - (audit-transact + (transact-with-ledger (->> all-ids (d/q '[:find [(pull ?i [:db/id :invoice/date {:invoice/expense-accounts [:db/id]}]) ...] :in $ [?i ...] @@ -293,7 +294,7 @@ :in ['$ '?e]} :args [history id]}) [last-transaction] (->> txs (sort-by first) (last))] - (audit-transact [(->> txs + (transact-with-ledger [(->> txs (filter (fn [[tx]] (= tx last-transaction))) (reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]] (-> new-transaction @@ -313,7 +314,7 @@ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice))) (assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice)) (assert (not (seq (:invoice-payment/_invoice invoice)))) - (audit-transact [[:db/add id :invoice/status :invoice-status/unpaid] + (transact-with-ledger [[:db/add id :invoice/status :invoice-status/unpaid] [:db/add id :invoice/outstanding-balance (:invoice/total invoice)] [:db/retract id :invoice/scheduled-payment (:invoice/scheduled-payment invoice)]] (:id context)) @@ -333,7 +334,7 @@ expense-account->entity (:expense_accounts args))}] - (audit-transact (concat [updated] + (transact-with-ledger (concat [updated] (map (fn [d] [:db/retract invoice-id :invoice/expense-accounts d]) deleted)) (:id context)) (->graphql diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index b3ed6259..8ed8f8d1 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -1,7 +1,6 @@ (ns auto-ap.graphql.transactions (:require - [auto-ap.datomic - :refer [audit-transact audit-transact-batch conn remove-nils]] + [auto-ap.datomic :refer [conn remove-nils]] [auto-ap.datomic.accounts :as a] [auto-ap.datomic.checks :as d-checks] [auto-ap.datomic.invoices :as d-invoices] @@ -19,6 +18,8 @@ ident->enum-f snake->kebab]] [auto-ap.import.transactions :as i-transactions] + [auto-ap.ledger + :refer [transact-batch-with-ledger transact-with-ledger]] [auto-ap.rule-matching :as rm] [auto-ap.utils :refer [dollars=]] [clj-time.coerce :as coerce] @@ -87,7 +88,7 @@ all-ids-not-locked)] (log/info "Unapproving " (count all-ids) args) - (audit-transact-batch + (transact-batch-with-ledger (->> all-ids (mapv (fn [t] {:db/id t @@ -166,7 +167,7 @@ (throw (ex-info err {:validation-error err}) )))) (log/info "Bulk coding " (count all-ids) args) - (audit-transact-batch + (transact-batch-with-ledger (mapcat (fn [i] (cond-> [(cond-> i (:approval_status args) (assoc :transaction/approval-status (enum->keyword (:approval_status args) "transaction-approval-status")) @@ -185,7 +186,7 @@ db (d/db conn)] (log/info "Deleting " (count all-ids) args) - (audit-transact-batch + (transact-batch-with-ledger (mapcat (fn [i] (let [transaction (d/entity db i) payment-id (-> transaction :transaction/payment :db/id) @@ -265,7 +266,7 @@ (when (not= :payment-status/cleared (-> payment :payment/status :db/ident)) (throw (ex-info "Payment can't be undone because it isn't cleared." {:validation-error "Payment can't be undone because it isn't cleared."}))) (if is-autopay-payment? - (audit-transact + (transact-with-ledger (cond-> [{:db/id (:db/id payment) :payment/status :payment-status/pending} {:db/id transaction-id @@ -291,7 +292,7 @@ :where ['[?ip :invoice-payment/payment ?p]]} :args [(d/db conn) (:db/id payment)]} )))) (:id context)) - (audit-transact + (transact-with-ledger (into (cond-> [{:db/id (:db/id payment) :payment/status :payment-status/pending} {:db/id transaction-id @@ -374,7 +375,7 @@ (when missing-locations (throw (ex-info (str "Location '" (str/join ", " missing-locations) "' not found on client.") {})) ) - (audit-transact (concat [(remove-nils {:db/id id + (transact-with-ledger (concat [(remove-nils {:db/id id :transaction/vendor vendor_id :transaction/approval-status (some->> approval_status name @@ -413,7 +414,7 @@ (when-not (dollars= (- (:transaction/amount transaction)) (:payment/amount payment)) (throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"}))) - (audit-transact (into + (transact-with-ledger (into [{:db/id (:db/id payment) :payment/status :payment-status/cleared :payment/date (coerce/to-date (first (sort [(:payment/date payment) @@ -469,7 +470,7 @@ (:db/id (:transaction/bank-account transaction)) (:db/id (:transaction/client transaction)))] (log/info "Adding a new payment" payment-tx) - @(d/transact conn payment-tx)) + (transact-with-ledger payment-tx (:id context))) (-> (d-transactions/get-by-id transaction_id) approval-status->graphql @@ -506,7 +507,7 @@ (:db/id (:transaction/bank-account transaction)) (:db/id (:transaction/client transaction)))] (log/info "Adding a new payment" payment-tx) - @(d/transact conn payment-tx)) + (transact-with-ledger payment-tx (:id context))) (-> (d-transactions/get-by-id transaction_id) approval-status->graphql @@ -541,7 +542,7 @@ (when (:transaction/payment transaction) (throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"})))) - (audit-transact (transduce + (transact-with-ledger (transduce (map #(into [(remove-nils (rm/apply-rule {:db/id (:db/id %) :transaction/amount (:transaction/amount %)} diff --git a/src/clj/auto_ap/import/transactions.clj b/src/clj/auto_ap/import/transactions.clj index 5d231984..06b3521c 100644 --- a/src/clj/auto_ap/import/transactions.clj +++ b/src/clj/auto_ap/import/transactions.clj @@ -1,10 +1,11 @@ (ns auto-ap.import.transactions (:require - [auto-ap.datomic :refer [audit-transact conn remove-nils uri]] + [auto-ap.datomic :refer [conn remove-nils uri]] [auto-ap.datomic.accounts :as a] [auto-ap.datomic.checks :as d-checks] [auto-ap.datomic.transaction-rules :as tr] [auto-ap.datomic.transactions :as d-transactions] + [auto-ap.ledger :refer [transact-with-ledger]] [auto-ap.rule-matching :as rm] [auto-ap.time :as atime] [auto-ap.utils :refer [dollars=]] @@ -316,9 +317,9 @@ :error :import-batch/error :not-ready :import-batch/not-ready) inc)) (when (= :import action) - (audit-transact (transaction->txs transaction bank-account rule-applying-function) - {:user/name user - :user/role ":admin"})))) + (transact-with-ledger (transaction->txs transaction bank-account rule-applying-function) + {:user/name user + :user/role ":admin"})))) (get-stats [_] @stats) diff --git a/src/clj/auto_ap/jobs/sysco.clj b/src/clj/auto_ap/jobs/sysco.clj index eab524d9..1e752357 100644 --- a/src/clj/auto_ap/jobs/sysco.clj +++ b/src/clj/auto_ap/jobs/sysco.clj @@ -4,6 +4,8 @@ [auto-ap.datomic :refer [conn]] [auto-ap.datomic.clients :as d-clients] [auto-ap.datomic.invoices :refer [code-invoice]] + [auto-ap.jobs.core :refer [execute]] + [auto-ap.ledger :refer [transact-with-ledger]] [auto-ap.parse :as parse] [auto-ap.time :as t] [clj-time.coerce :as coerce] @@ -13,12 +15,10 @@ [clojure.tools.logging :as log] [com.unbounce.dogstatsd.core :as statsd] [config.core :refer [env]] - [datomic.api :as d] - [auto-ap.jobs.core :refer [execute]]) + [datomic.api :as d]) (:import (java.util UUID))) - (def bucket-name (:data-bucket env)) (def header-keys ["TransCode" "GroupID" "Company" "CustomerNumber" "InvoiceNumber" "RecordType" "Item" "InvoiceDocument" "AccountName" "AccountDunsNo" "InvoiceDate" "AccountDate" "CustomerPONo" "PaymentTerms" "TermsDescription" "StoreNumber" "CustomerName" "AddressLine1" "AddressLine2" "City1" "State1" "Zip1" "Phone1" "Duns1" "Hin1" "Dea1" "TIDCustomer" "ChainNumber" "BidNumber" "ContractNumber" "CompanyNumber" "BriefName" "Address" "Address2" "City2" "State2" "Zip2" "Phone2" "Duns2" "Hin2" "Dea2" "Tid_OPCO" "ObligationIndicator" "Manifest" "Route" "Stop" "TermsDiscountPercent" "TermsDiscountDueDate" "TermsNetDueDate" "TermsDiscountAmount" "TermsDiscountCode" "OrderDate" "DepartmentCode"]) @@ -148,7 +148,7 @@ (.getName (io/file k))) println)})) []))))) - result @(d/transact conn transaction)] + result (transact-with-ledger transaction {:user/name "sysco importer" :user/role "admin"})] (log/infof "Imported %d invoices" (/ (count (:tempids result)) 2))) (doseq [k keys] (mark-key k)) diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index b4428001..9c105700 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -1,6 +1,6 @@ (ns auto-ap.ledger (:require - [auto-ap.datomic :refer [conn remove-nils]] + [auto-ap.datomic :refer [audit-transact conn remove-nils]] [auto-ap.logging :refer [info-event]] [auto-ap.utils :refer [dollars-0? dollars=]] [clj-time.coerce :as c] @@ -42,6 +42,7 @@ (= :import-status/pending (:db/ident (:invoice/import-status entity))) (= :invoice-status/voided (:db/ident (:invoice/status entity))) (dollars-0? (:invoice/total entity))) + (remove-nils {:journal-entry/source "invoice" :journal-entry/client (:db/id (:invoice/client entity)) @@ -76,42 +77,43 @@ :transaction/bank-account '[* {:bank-account/type [:db/ident]}] :transaction/accounts '[* {:transaction-account/account [*]}] }] id) - decreasing? (< (:transaction/amount entity) 0.0) + decreasing? (< (or (:transaction/amount entity) 0.0) 0.0) credit-from-bank? decreasing? debit-from-bank? (not decreasing?)] - (when-not (or (= :transaction-approval-status/excluded (:db/ident (:transaction/approval-status entity))) - (= :transaction-approval-status/suppressed (:db/ident (:transaction/approval-status entity))) - (dollars-0? (:transaction/amount entity))) + (when (and (not (= :transaction-approval-status/excluded (:db/ident (:transaction/approval-status entity)))) + (not (= :transaction-approval-status/suppressed (:db/ident (:transaction/approval-status entity)))) + (:transaction/amount entity) + (not (dollars-0? (:transaction/amount entity)))) (remove-nils - {:journal-entry/source "transaction" - :journal-entry/client (:db/id (:transaction/client entity)) - :journal-entry/date (:transaction/date entity) - :journal-entry/original-entity (:db/id entity) - :journal-entry/alternate-description (:transaction/description-original entity) - :journal-entry/vendor (:db/id (:transaction/vendor entity)) - :journal-entry/amount (Math/abs (:transaction/amount entity)) - :journal-entry/cleared-against (:transaction/cleared-against entity) + {:journal-entry/source "transaction" + :journal-entry/client (:db/id (:transaction/client entity)) + :journal-entry/date (:transaction/date entity) + :journal-entry/original-entity (:db/id entity) + :journal-entry/alternate-description (:transaction/description-original entity) + :journal-entry/vendor (:db/id (:transaction/vendor entity)) + :journal-entry/amount (Math/abs (:transaction/amount entity)) + :journal-entry/cleared-against (:transaction/cleared-against entity) - :journal-entry/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity)) - :journal-entry-line/location "A" - :journal-entry-line/credit (when credit-from-bank? - (Math/abs (:transaction/amount entity))) - :journal-entry-line/debit (when debit-from-bank? - (Math/abs (:transaction/amount entity)))}) - ] - (map - (fn [a] - (remove-nils{:journal-entry-line/account (:db/id (:transaction-account/account a)) - :journal-entry-line/location (:transaction-account/location a) - :journal-entry-line/debit (when credit-from-bank? - (Math/abs (:transaction-account/amount a))) - :journal-entry-line/credit (when debit-from-bank? - (Math/abs (:transaction-account/amount a)))})) - (if (seq (:transaction/accounts entity)) - (:transaction/accounts entity) - [{:transaction-account/amount (:transaction/amount entity)}]))) - - :journal-entry/cleared true}))))) + :journal-entry/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity)) + :journal-entry-line/location "A" + :journal-entry-line/credit (when credit-from-bank? + (Math/abs (:transaction/amount entity))) + :journal-entry-line/debit (when debit-from-bank? + (Math/abs (:transaction/amount entity)))}) + ] + (map + (fn [a] + (remove-nils{:journal-entry-line/account (:db/id (:transaction-account/account a)) + :journal-entry-line/location (:transaction-account/location a) + :journal-entry-line/debit (when credit-from-bank? + (Math/abs (:transaction-account/amount a))) + :journal-entry-line/credit (when debit-from-bank? + (Math/abs (:transaction-account/amount a)))})) + (if (seq (:transaction/accounts entity)) + (:transaction/accounts entity) + [{:transaction-account/amount (:transaction/amount entity)}]))) + + :journal-entry/cleared true}))))) #_(defmethod entity-change->ledger :expected-deposit [db [type id]] @@ -142,45 +144,6 @@ [_ _] nil) -(mount/defstate tx-report-queue - :start (d/tx-report-queue conn) - :stop (d/remove-tx-report-queue conn)) - - -(defn process-one [] - (lc/with-context {:source "process-txes"} - (try - (let [transaction (.take tx-report-queue) - _ (log/info "Converting tranasction to ledger") - db (:db-after transaction) - affected-entities (->> (:tx-data transaction) - (map (fn [^datomic.db.Datum x] - {:e (:e x) - :a (d/ident db (:a x)) - :v (:v x) - :added (:added x)})) - (group-by :e) - (mapcat #(datums->impacted-entity db %)) - (set)) - _ (info-event (str "Found " (count affected-entities) " affected entities") - {:affected-entities (count affected-entities)}) - d-txs (->> affected-entities - (map #(entity-change->ledger db %)) - (filter seq)) - retractions (map (fn [[_ e]] [:db/retractEntity [:journal-entry/original-entity e]]) affected-entities)] - - (when (seq retractions) - @(d/transact conn retractions)) - - (doseq [d-tx d-txs] - @(d/transact conn [d-tx])) - (log/info "Succesfully process transaction")) - (catch Exception e - (log/error e))))) - -(mount/defstate process-txes-worker - :start (scheduler/run-fun process-one 1) - :stop (-> process-txes-worker :running? (reset! false))) (defn reconcile-ledger ([] (reconcile-ledger (-> (t/now) @@ -459,3 +422,43 @@ :text "This process looks for unbalance ledger entries, or missing ledger entries" :priority :low} nil)) + +(defn transact-with-ledger [transaction id] + (let [db (d/db conn) + tx (d/with db transaction) + affected-entities (->> (:tx-data tx) + (map (fn [^datomic.db.Datum x] + {:e (:e x) + :a (d/ident db (:a x)) + :v (:v x) + :added (:added x)})) + (group-by :e) + (mapcat #(datums->impacted-entity db %)) + (set)) + ledger-txs (->> affected-entities + (map #(entity-change->ledger (:db-after tx) %)) + (filter seq)) + retractions (map (fn [[_ e]] [:db/retractEntity [:journal-entry/original-entity e]]) affected-entities)] + (audit-transact retractions id) + (audit-transact (into transaction ledger-txs) id))) + + +(defn transact-batch-with-ledger [txes id] + (let [batch-id (.toString (java.util.UUID/randomUUID))] + (reduce + (fn [full-tx batch] + (let [batch (conj (vec batch) {:db/id "datomic.tx" + :audit/batch batch-id}) + _ (log/info "transacting batch " batch-id " " (count batch)) + tx-result (transact-with-ledger batch id) + _ (Thread/sleep 1000)] + + (cond-> full-tx + (:tx-data full-tx) (update :tx-data #(into % (:tx-data tx-result))) + (not (:tx-data full-tx)) (assoc :tx-data (vec (:tx-data tx-result))) + (not (:db-before full-tx)) (assoc :db-before (:db-before tx-result)) + true (assoc :db-after (:db-after tx-result)) + true (update :tempids merge (:tempids tx-result))))) + + {} + (partition-all 50 txes)))) diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 6204d61c..540b1f9a 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -1,28 +1,31 @@ (ns auto-ap.routes.invoices - (:require [auto-ap.datomic :refer [remove-nils uri conn]] - [auto-ap.datomic.clients :as d-clients] - [auto-ap.datomic.invoices :as d-invoices] - [auto-ap.datomic.accounts :as a] - [auto-ap.datomic.vendors :as d-vendors] - [auto-ap.graphql.utils :refer [assert-admin assert-can-see-client]] - [auto-ap.import.manual :as manual] - [auto-ap.import.manual.common :as c] - [auto-ap.parse :as parse] - [auto-ap.routes.utils :refer [wrap-secure]] - [auto-ap.utils :refer [by]] - [clj-time.coerce :as coerce :refer [to-date]] - [clojure.data.csv :as csv] - [clojure.java.io :as io] - [clojure.string :as str] - [clojure.tools.logging :as log] - [compojure.core :refer [context defroutes POST wrap-routes]] - [datomic.api :as d] - [ring.middleware.json :refer [wrap-json-response]] - [unilog.context :as lc] - [amazonica.aws.s3 :as s3] - [config.core :refer [env]] - [digest]) - (:import java.util.UUID)) + (:require + [amazonica.aws.s3 :as s3] + [auto-ap.datomic :refer [conn remove-nils uri]] + [auto-ap.datomic.accounts :as a] + [auto-ap.datomic.clients :as d-clients] + [auto-ap.datomic.invoices :as d-invoices] + [auto-ap.datomic.vendors :as d-vendors] + [auto-ap.graphql.utils :refer [assert-admin assert-can-see-client]] + [auto-ap.import.manual :as manual] + [auto-ap.import.manual.common :as c] + [auto-ap.ledger :refer [transact-with-ledger]] + [auto-ap.parse :as parse] + [auto-ap.routes.utils :refer [wrap-secure]] + [auto-ap.utils :refer [by]] + [clj-time.coerce :as coerce :refer [to-date]] + [clojure.data.csv :as csv] + [clojure.java.io :as io] + [clojure.string :as str] + [clojure.tools.logging :as log] + [compojure.core :refer [context defroutes POST wrap-routes]] + [config.core :refer [env]] + [datomic.api :as d] + [digest] + [ring.middleware.json :refer [wrap-json-response]] + [unilog.context :as lc]) + (:import + (java.util UUID))) (defn reset-id [i] (update i :invoice-number @@ -270,7 +273,7 @@ (throw (ex-info "No new invoices found." {}))) (log/info "creating invoice" potential-invoices) - @(d/transact (d/connect uri) potential-invoices)))) + (transact-with-ledger potential-invoices user)))) (defn validate-account-rows [rows code->existing-account] (when-let [bad-types (seq (->> rows @@ -380,7 +383,7 @@ conj [] rows)] - @(d/transact (d/connect uri) txes))) + (transact-with-ledger txes nil))) (defroutes routes (wrap-routes @@ -472,8 +475,9 @@ (not= "Cash" (:check %)))) (map :vendor-name) set) - _ @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows) - user))] + _ (transact-with-ledger (invoice-rows->transaction (:new grouped-rows) + user) + user)] {:status 200 :body (pr-str {:imported (count (:new grouped-rows)) :already-imported (count (:exists grouped-rows)) diff --git a/src/clj/auto_ap/server.clj b/src/clj/auto_ap/server.clj index 24b8a143..be595a19 100644 --- a/src/clj/auto_ap/server.clj +++ b/src/clj/auto_ap/server.clj @@ -14,7 +14,6 @@ [auto-ap.jobs.sysco :as job-sysco] [auto-ap.jobs.vendor-usages :as job-vendor-usages] [auto-ap.jobs.yodlee2 :as job-yodlee2] - [auto-ap.ledger :as ledger] [clojure.tools.logging :as log] [com.unbounce.dogstatsd.core :as statsd] [config.core :refer [env]] @@ -122,8 +121,7 @@ (let [without (cond-> [] (not (env :run-web? )) (into [#'jetty #'jetty-stats]) - (not (env :run-background?)) (into [#'ledger/process-txes-worker - #'migrate/migrate-start]))] + (not (env :run-background?)) (into [#'migrate/migrate-start]))] (log/info "starting without " without) (add-shutdown-hook! shutdown-mount) diff --git a/src/clj/user.clj b/src/clj/user.clj index e47305b0..52a0e94a 100644 --- a/src/clj/user.clj +++ b/src/clj/user.clj @@ -2,8 +2,8 @@ (:require [amazonica.aws.s3 :as s3] [auto-ap.datomic :refer [uri]] - [auto-ap.ledger :as l] - [auto-ap.server ] + [auto-ap.ledger :as l :refer [transact-with-ledger]] + [auto-ap.server] [auto-ap.square.core :as square] [auto-ap.time :as atime] [auto-ap.utils :refer [by]] @@ -13,15 +13,15 @@ [clojure.core.async :as async] [clojure.data.csv :as csv] [clojure.java.io :as io] - clojure.pprint + [clojure.pprint] [clojure.string :as str] [config.core :refer [env]] [datomic.api :as d] [mount.core :as mount] - nrepl.middleware.print + [nrepl.middleware.print] [unilog.context :as lc]) (:import - [org.apache.commons.io.input BOMInputStream])) + (org.apache.commons.io.input BOMInputStream))) #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn mark-until-date [client end] @@ -43,7 +43,7 @@ :invoice/exclude-from-ledger true})) (partition-all 100))] - @(d/transact conn p) + (transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"}) (println "process 100")) (doseq [p (->> @@ -63,24 +63,7 @@ :transaction/approval-status :transaction-approval-status/excluded})) (partition-all 100))] - @(d/transact conn p) (println "process 100")))) - -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} -(defn unapprove-all [] - (let [conn (d/connect uri)] - (doseq [p (->> - (d/query {:query {:find '[?e] - :in '[$ ] - :where ['[?e :transaction/date ?d ]]} - :args [(d/db conn)]}) - (mapv first) - (mapv (fn [i] - {:db/id i - :transaction/approval-status :transaction-approval-status/unapproved})) - (partition-all 100))] - - @(d/transact conn p) - (println "process 100")))) + (transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"}) (println "process 100")))) #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn load-accounts [conn] @@ -414,42 +397,6 @@ (defn start-db [] (mount.core/start (mount.core/only #{#'auto-ap.datomic/conn}))) -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} -(defn touch-transaction-ledger [e] - @(d/transact auto-ap.datomic/conn [[:db/retractEntity [:journal-entry/original-entity e]]]) - @(d/transact auto-ap.datomic/conn [(l/entity-change->ledger (d/db auto-ap.datomic/conn) - [:transaction e])])) - -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} -(defn mismatched-transactions [] - (let [jel-accounts (reduce - (fn [acc [e lia]] - (update acc e (fnil conj #{} ) lia)) - {} - (d/query {:query {:find ['?e '?lia] - :in ['$] - :where ['[?je :journal-entry/line-items ?li] - '[?je :journal-entry/original-entity ?e] - '[?li :journal-entry-line/account ?lia] - '[?lia :account/name]]} - :args [(d/db auto-ap.datomic/conn)]})) - transaction-accounts (reduce - (fn [acc [e lia]] - (update acc e (fnil conj #{} ) lia)) - {} - (d/query {:query {:find ['?e '?lia] - :in ['$] - :where ['[?e :transaction/accounts ?li] - '(not [?e :transaction/approval-status :transaction-approval-status/excluded]) - '[?li :transaction-account/account ?lia] - - '[?lia :account/name]]} - :args [(d/db auto-ap.datomic/conn)]})) - ] - (filter - (fn [[e accounts]] (not= accounts (get jel-accounts e))) - transaction-accounts))) - #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}