Accounts don't need to be loaded

This commit is contained in:
2022-04-19 13:27:47 -07:00
parent f73b406abd
commit f51f3450d6
22 changed files with 398 additions and 420 deletions

View File

@@ -11,6 +11,16 @@
:account-client-override/name
{:account-client-override/client [:db/id :client/name]}]}])
(defn clientize [a client]
(if-let [override-name (->> a
:account/client-overrides
(filter #(= client
(:db/id (:account-client-override/client %))))
first
:account-client-override/name)]
(assoc a :account/name override-name)
a))
(defn get-accounts
([]
(get-accounts {}))

View File

@@ -6,13 +6,18 @@
[clj-time.coerce :as coerce]
[auto-ap.time-utils :refer [next-dom]]
[clj-time.core :as time]
[auto-ap.datomic.vendors :as d-vendors]))
[auto-ap.datomic.vendors :as d-vendors]
[clojure.tools.logging :as log]
[auto-ap.datomic.accounts :as d-accounts]))
(def default-read '[*
{:invoice/client [:client/name :db/id :client/locations :client/code]}
{:invoice/vendor [* {:vendor/address [*]}]}
{:invoice/status [:db/ident]}
{:invoice/expense-accounts [* {:invoice-expense-account/account [*]}]}
{:invoice/expense-accounts [* {:invoice-expense-account/account [:account/name :db/id
:account/location
{:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}]}]}
{:invoice-payment/_invoice [* {:invoice-payment/payment [* {:payment/status [*]}
{:payment/bank-account [*]}
{:transaction/_payment [*]}]}]}])
@@ -23,6 +28,10 @@
(update :invoice/due coerce/from-date)
(update :invoice/scheduled-payment coerce/from-date)
(update :invoice/status :db/ident)
(update :invoice/expense-accounts (fn [eas]
(map
#(update % :invoice-expense-account/account d-accounts/clientize (:db/id (:invoice/client x)))
eas)))
(rename-keys {:invoice-payment/_invoice :invoice/payments})))
(defn raw-graphql-ids

View File

@@ -1,13 +1,9 @@
(ns auto-ap.datomic.ledger
(:require [datomic.api :as d]
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
[auto-ap.utils :refer [dollars-0?]]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.datomic :refer [merge-query apply-sort-3 apply-pagination add-sorter-fields conn]]
[auto-ap.datomic :refer [uri]]
[clojure.tools.logging :as log]
[clj-time.coerce :as c]
[clj-time.core :as time]))
[auto-ap.datomic.accounts :as d-accounts]))
(def export-read
[:journal-entry/external-id
@@ -34,12 +30,12 @@
:account/name
:account/numeric-code
:account/location
{:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}
{:account/type [:db/ident :db/id]}
{:bank-account/type [:db/ident :db/id]}]}]}])
(defn raw-graphql-ids [db args]
(log/info "ARGS" args)
(let [query (cond-> {:query {:find []
:in ['$ ]
:where []}
@@ -161,20 +157,27 @@
true
(merge-query {:query {:find ['?sort-default '?e] :where ['[?e :journal-entry/date ?sort-default]]}}))]
(->> (doto query log/info)
(->> query
(d/query)
(apply-sort-3 (update args :sort conj {:sort-key "default-2" :asc true}))
(apply-pagination args))))
(defn graphql-results [ids db args]
(defn graphql-results [ids db _]
(let [results (->> (d/pull-many db '[* {:journal-entry/client [:client/name :client/code :db/id]
:journal-entry/vendor [:vendor/name :db/id]
:journal-entry/line-items [* {:journal-entry-line/account [*
{:account/type [*]}
{:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}
{:bank-account/type [*]}]}]}]
ids)
(map #(update % :journal-entry/date c/from-date))
(map (fn [je]
(update je :journal-entry/line-items
(fn [jels]
(map
#(update % :journal-entry-line/account d-accounts/clientize (:db/id (:journal-entry/client je)))
jels)))))
(filter (fn [je]
(every?
(fn [jel]
@@ -191,12 +194,9 @@
(defn get-graphql [args]
(let [db (d/db conn)
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))
(defn filter-ids [ids]
(if ids
(->> {:query {:find ['?e]
@@ -207,8 +207,3 @@
(map first)
vec)
[]))

View File

@@ -142,6 +142,22 @@
{:db/id accounts-payable
:db/ident :account/accounts-payable}]]))
(defn add-account-search-terms [conn]
[(->> (d/q '[:find ?i ?n
:in $
:where [?i :account/name ?n]]
(d/db conn))
(map (fn [[i n]]
{:db/id i
:account/search-terms n})))
(->> (d/q '[:find ?i ?n
:in $
:where [?i :account-client-override/name ?n]]
(d/db conn))
(map (fn [[i n]]
{:db/id i
:account-client-override/search-terms n})))])
(defn migrate [conn]
(let [
norms-map (merge {:auto-ap/base-schema {:txes auto-ap.datomic/base-schema}
@@ -491,7 +507,20 @@
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one}]]}
:auto-ap/add-payment-type-credit {:txes [[{:db/ident :payment-type/credit
:db/doc "Credit for negative invoices"}]]}}
:db/doc "Credit for negative invoices"}]]}
:auto-ap/fulltext-accounts {:txes [[{:db/ident :account/search-terms
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "a name search for accounts"
:db/fulltext true}
{:db/ident :account-client-override/search-terms
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "a name search for accounts"
:db/fulltext true}]]
:requires [:auto-ap/add-account-overrides]}
:auto-ap/add-search-terms-accounts {:txes-fn `add-account-search-terms
:requires [:auto-ap/fulltext-accounts]}}

View File

@@ -5,7 +5,8 @@
[clj-time.coerce :as c]
[clj-time.coerce :as coerce]
[clojure.tools.logging :as log]
[clojure.string :as str]))
[clojure.string :as str]
[auto-ap.datomic.accounts :as d-accounts]))
(defn sort-fn [sort-by]
(cond
@@ -194,11 +195,20 @@
:transaction/accounts [:transaction-account/amount
:db/id
:transaction-account/location
{:transaction-account/account [:db/id :account/name :account/numeric-code]}]
{:transaction-account/account [:account/name :db/id
:account/location
{:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}]}]
:transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]}]
ids)
(map #(update % :transaction/date c/from-date))
(map #(update % :transaction/post-date c/from-date))
(map #(update % :transaction/accounts
(fn [tas]
(map
(fn [ta]
(update ta :transaction-account/account d-accounts/clientize (:db/id (:transaction/client %))))
tas))))
(map (fn [transaction]
(cond-> transaction
(:transaction/payment transaction) (update-in [:transaction/payment :payment/date] c/from-date)
@@ -240,7 +250,10 @@
:transaction/accounts [:transaction-account/amount
:db/id
:transaction-account/location
{ :transaction-account/account [:db/id :account/name :account/numeric-code]}]
{ :transaction-account/account [:account/name :db/id
:account/location
{:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}]}]
:transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]}]
id)
(update :transaction/date c/from-date)

View File

@@ -3,7 +3,8 @@
[auto-ap.datomic :refer [conn merge-query uri add-sorter-fields apply-pagination merge-query apply-sort-3]]
[auto-ap.graphql.utils :refer [limited-clients]]
[clojure.string :as str]
[datomic.api :as d]))
[datomic.api :as d]
[auto-ap.datomic.accounts :as d-accounts]))
(defn <-datomic [a]
(cond-> a
@@ -18,21 +19,28 @@
nil)]
(if clients
(-> vendor
(update :vendor/account-overrides (fn [ao] (filter #(clients (:db/id (:vendor-account-override/client %))) ao)))
(update :vendor/account-overrides (fn [aos]
(->> aos
(filter #(clients (:db/id (:vendor-account-override/client %))))
(map #(update % :vendor-account-override/account d-accounts/clientize (:db/id (:vendor-account-override/client %)))))))
(update :vendor/terms-overrides (fn [to] (filter #(clients (:db/id (:vendor-terms-override/client %))) to)))
(update :vendor/schedule-payment-dom (fn [to] (filter #(clients (:db/id (:vendor-schedule-payment-dom/client %))) to))))
vendor)))
(-> vendor
(update :vendor/account-overrides (fn [aos]
(->> aos
(map #(update % :vendor-account-override/account d-accounts/clientize (:db/id (:vendor-account-override/client %)))))))))))
(def default-read
'[* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :client/code :db/id]}]
:vendor/schedule-payment-dom [* {:vendor-schedule-payment-dom/client [:client/name :client/code :db/id]}]
'[* {:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id
{:account/client-overrides [:account-client-override/client :account-client-override/name]}]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :client/code :db/id]}]
:vendor/schedule-payment-dom [* {:vendor-schedule-payment-dom/client [:client/name :client/code :db/id]}]
:vendor/automatically-paid-when-due [:db/id :client/name]
:vendor/legal-entity-tin-type [:db/ident :db/id]
:vendor/legal-entity-1099-type [:db/ident :db/id]
:vendor/default-account [:db/id :account/numeric-code :account/name]
:vendor-usage/_vendor [:vendor-usage/client :vendor-usage/count]}])
:vendor/legal-entity-tin-type [:db/ident :db/id]
:vendor/legal-entity-1099-type [:db/ident :db/id]
:vendor/default-account [:db/id :account/numeric-code :account/name]
:vendor-usage/_vendor [:vendor-usage/client :vendor-usage/count]}])
(defn get-usages [args]
(->> (cond-> {:query {:find ['?v '?c '(count ?e)]