Tons of small fixes

This commit is contained in:
Bryce Covert
2020-07-27 21:28:02 -07:00
parent 3737cfa628
commit fdc1d3e9e4
26 changed files with 428 additions and 178 deletions

View File

@@ -11,7 +11,7 @@
(update-in [:query :where] conj where))]
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
(def default-read '(pull ?e [*]))
(def default-read '(pull ?e [* {:client/_bank-accounts [:db/id]}]))
(defn <-datomic [x]
(->> x

View File

@@ -72,6 +72,12 @@
:where ['[?e :payment/check-number ?check-number]]}
:args [(:check-number args)]})
(not-empty (:invoice-number args))
(merge-query {:query {:in ['?invoice-number]
:where ['[?e :payment/invoices ?i]
'[?i :invoice/invoice-number ?invoice-number]]}
:args [(:invoice-number args)]})
(:bank-account-id args)
(merge-query {:query {:in ['?bank-account-id]
:where ['[?e :payment/bank-account ?bank-account-id]]}

View File

@@ -115,11 +115,13 @@
(group-by :db/id))]
(->> ids
(map results)
(filter identity)
(map first))))
(defn get-graphql [args]
(let [db (d/db (d/connect uri))
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))

View File

@@ -299,7 +299,16 @@
{:db/ident :client/week-b-credits
:db/doc "How much money gets credited each week"
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one}]]}}
:db/cardinality :db.cardinality/one}]]}
:auto-ap/auto-pay {:txes [[{:db/ident :vendor/automatically-paid-when-due
:db/doc "The clients for which invoices will automatically be paid."
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
{:db/ident :invoice/automatically-paid-when-due
:db/doc "Whether this invoice should be marked as paid when it's due"
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one}]]}}
]
(println "Conforming database...")
(c/ensure-conforms conn norms-map)

View File

@@ -15,7 +15,8 @@
(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 :db/id]}]}])
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :client/code :db/id]}]
:vendor/automatically-paid-when-due [:db/id :client/name]}])
(defn get-graphql [args]
(->> (cond-> {:query {:find [(list 'pull '?e default-read)]
@@ -32,7 +33,8 @@
{:default-account [:account/name :db/id :account/location]
: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 :db/id]}]}])
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]
:vendor/automatically-paid-when-due [:db/id :client/name]}])
:in $ ?e
:where [?e]]
(d/db (d/connect uri))

View File

@@ -145,6 +145,7 @@
:code {:type 'String}
:terms {:type 'Int}
:hidden {:type 'Boolean}
:automatically_paid_when_due {:type '(list :client)}
:terms_overrides {:type '(list :terms_override)}
:account_overrides {:type '(list :vendor_account_override)}
@@ -305,7 +306,8 @@
:client_id {:type 'Int}
:payments {:type '(list :invoice_payment)}
:vendor {:type :vendor}
:client {:type :client}}}
:client {:type :client}
:automatically_paid_when_due {:type 'Boolean}}}
@@ -496,6 +498,7 @@
:amount_lte {:type :money}
:amount_gte {:type :money}
:check_number_like {:type 'String}
:invoice_number {:type 'String}
:start {:type 'Int}
:sort {:type '(list :sort_item)}}
@@ -608,6 +611,7 @@
:terms {:type 'Int}
:terms_overrides {:type '(list :add_terms_override)}
:code {:type 'String}
:automatically_paid_when_due {:type '(list :id)}
:hidden {:type 'Boolean}
:print_as {:type 'String}
@@ -630,6 +634,7 @@
:invoice_number {:type 'String}
:expense_accounts {:type '(list :edit_expense_account)}
:location {:type :iso_date}
:automatically_paid_when_due {:type 'Boolean}
:date {:type :iso_date}
:due {:type :iso_date}
:client_id {:type :id}
@@ -642,6 +647,7 @@
:invoice_number {:type 'String}
:expense_accounts {:type '(list :edit_expense_account)}
:date {:type :iso_date}
:automatically_paid_when_due {:type 'Boolean}
:due {:type :iso_date}
:total {:type 'Float}}}
:edit_transaction
@@ -971,7 +977,7 @@
'[?i :invoice/due ?due]
'[(<= ?due ?due-before)]
'[?i :invoice/outstanding-balance ?outstanding]]}
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 31)))]})
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 180)))]})
outstanding-checks (reduce
+
0.0
@@ -983,7 +989,7 @@
'(or
[?p :payment/type :payment-type/debit]
[?p :payment/type :payment-type/check])]}
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 31)))]})))
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/days 180)))]})))
recent-fulfillments (d/query {:query {:find '[?f ?d]
:in '[$ ?client ?min-date]
:where ['[?t :transaction/forecast-match ?f]
@@ -993,7 +999,7 @@
:args [(d/db (d/connect uri)) client_id (coerce/to-date (t/plus (time/local-now) (t/months -2)))]})
forecasted-transactions (for [{:forecasted-transaction/keys [amount identifier day-of-month]
:db/keys [id]} forecasted-transactions
month (range -1 2)
month (range -1 7)
:let [next (t/plus (t/local-date (t/year (time/local-now))
(t/month (time/local-now))
day-of-month )
@@ -1018,7 +1024,7 @@
(or week-a-credits 0)
(or week-b-credits 0))
:date (coerce/to-date-time date)})
(take 5 (time/day-of-week-seq 1)))
(take (* 7 4) (time/day-of-week-seq 1)))
(filter #(>= (:amount %) 0) forecasted-transactions))
:upcoming_debits (into (mapv
(fn [date]
@@ -1026,7 +1032,7 @@
(or week-a-debits 0)
(or week-b-debits 0)))
:date (coerce/to-date-time date)})
(take 5 (time/day-of-week-seq 1)))
(take (* 7 4) (time/day-of-week-seq 1)))
(filter #(< (:amount %) 0) forecasted-transactions))
})))

View File

@@ -293,6 +293,19 @@
(conj payment)
(into (invoice-payments invoices invoice-amounts)))))
(defn validate-belonging [client-id invoices bank-account]
(when-not (apply = client-id (map (comp :db/id :invoice/client) invoices ))
(throw (ex-info "You can't pay for that invoice from this bank account."
{:validation-error "You can't pay for that invoice from this bank account."
:client-id client-id
:invoices (map :invoice/invoice-number invoices)}))
)
(when-not (= client-id (:db/id (:client/_bank-accounts bank-account)))
(throw (ex-info "The selected bank doesn't belong to this client"
{:validation-error "The selected bank doesn't belong to this client"
:client-id client-id
:invoices (map :invoice/invoice-number invoices)}))))
(defn print-checks [invoice-payments client-id bank-account-id type]
(let [type (keyword "payment-type" (name type))
invoices (d-invoices/get-multi (map :invoice-id invoice-payments))
@@ -303,6 +316,8 @@
invoices-grouped-by-vendor (group-by (comp :db/id :invoice/vendor) invoices)
bank-account (d-bank-accounts/get-by-id bank-account-id)
_ (validate-belonging client-id invoices bank-account)
checks (->> (for [[[vendor-id invoices] index] (map vector invoices-grouped-by-vendor (range))]
(invoices->entities invoices (vendors vendor-id) client bank-account type index invoice-amounts))
(reduce into [])
@@ -347,6 +362,7 @@
bank-account (d-bank-accounts/get-by-id bank-account-id)
_ (doseq [invoice invoices]
(assert-can-see-client (:id context) (:invoice/client invoice)))
_ (validate-belonging (:db/id (:client/_bank-accounts bank-account)) invoices bank-account)
invoice-payment-lookup (by :invoice_id :amount (:invoice_payments args))
base-payment (base-payment invoices
(:invoice/vendor (first invoices))

View File

@@ -57,7 +57,7 @@
:account account_id
:location location}))
(defn add-invoice-transaction [{:keys [total invoice_number location client_id vendor_id vendor_name date due expense_accounts] :as in}]
(defn add-invoice-transaction [{:keys [total invoice_number location automatically_paid_when_due client_id vendor_id vendor_name date due expense_accounts] :as in}]
(println date)
(let [vendor (d-vendors/get-by-id vendor_id)
account (:vendor/default-account vendor)
@@ -77,7 +77,8 @@
expense_accounts)}
(:vendor/terms vendor) (assoc :invoice/due (coerce/to-date
(time/plus date (time/days (d-vendors/terms-for-client-id vendor client_id)))))
due (assoc :invoice/due (coerce/to-date due)))))
due (assoc :invoice/due (coerce/to-date due))
(boolean? automatically_paid_when_due) (assoc :invoice/automatically-paid-when-due automatically_paid_when_due))))
(defn deleted-expense-accounts [invoice expense-accounts]
@@ -116,7 +117,7 @@
->graphql)))
(defn edit-invoice [context {{:keys [id due invoice_number total vendor_id date client_id expense_accounts] :as in} :invoice} value]
(defn edit-invoice [context {{:keys [id due invoice_number total vendor_id date client_id expense_accounts automatically_paid_when_due] :as in} :invoice} value]
(let [invoice (d-invoices/get-by-id id)
_ (when (seq (doto (d-invoices/find-conflicting {:db/id id
:invoice/invoice-number invoice_number
@@ -136,11 +137,13 @@
updated-invoice (cond-> {:db/id id
:invoice/invoice-number invoice_number
:invoice/date (coerce/to-date date)
:invoice/total total
:invoice/outstanding-balance (- total paid-amount)
:invoice/expense-accounts (map expense-account->entity
expense_accounts)}
due (assoc :invoice/due (coerce/to-date due)))]
due (assoc :invoice/due (coerce/to-date due))
(boolean? automatically_paid_when_due) (assoc :invoice/automatically-paid-when-due automatically_paid_when_due))]
@(d/transact (d/connect uri) (concat [updated-invoice]
(map (fn [d] [:db/retract id :invoice/expense-accounts d]) deleted)))
(-> (d-invoices/get-by-id id)

View File

@@ -70,11 +70,21 @@
:email (:email secondary_contact)})
)})]
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/account-overrides account-overrides])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides]))
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/terms-overrides terms-overrides])
(is-admin? (:id context)) (conj [:reset (if id id "vendor") :vendor/automatically-paid-when-due
(doto (mapv
(fn [apwd]
{:db/id apwd})
(:automatically_paid_when_due in))
println)]))
_ (println transaction)
transaction-result @(d/transact (d/connect uri) transaction)]
(-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor"))
id))
(doto println)
(->graphql))))
(defn merge-vendors [context {:keys [from to]} value]

View File

@@ -70,24 +70,22 @@
[rows]
(println "Importing Sysco-styled 1")
(let [header (first rows)]
(doto
(transduce
(comp (drop 1)
(map (fn [row]
(into {} (map vector header row))))
(map (fn [row]
{:vendor-code nil
:customer-identifier (str (get row "Ship-To Name") " " (or (get row "Ship-To Number")
(get row "\"Ship-To Number\"")))
:invoice-number (str/trim (get row "Invoice Number"))
:date (parse-date-fallover (get row "Invoice Date") ["yyyy-MM-dd"])
:total (str/replace (get row "Original Amount") #"[,\$]" "")
:text (str/join " " (vals row))
:full-text (str/join " " (vals row))})))
conj
[]
rows)
println)))
(transduce
(comp (drop 1)
(map (fn [row]
(into {} (map vector header row))))
(map (fn [row]
{:vendor-code nil
:customer-identifier (str (get row "Ship-To Name") " " (or (get row "Ship-To Number")
(get row "\"Ship-To Number\"")))
:invoice-number (str/trim (get row "Invoice Number"))
:date (parse-date-fallover (get row "Invoice Date") ["yyyy-MM-dd" "M/d/yyyy"])
:total (str/replace (get row "Original Amount") #"[,\$]" "")
:text (str/join " " (vals row))
:full-text (str/join " " (vals row))})))
conj
[]
rows)))
(defmethod parse-csv :mama-lus
[rows]

View File

@@ -175,6 +175,17 @@
:multi #"\n"
:multi-match? #"^\s+.*?\d{6,}.*?\$"}
;; C & L
{:vendor "C&L Produce"
:keywords [#"440 Franklin Street"]
:extract {:date #"([0-9]+/[0-9]+/[0-9]+)"
:customer-identifier #"Bill To.*\n.*\n\s+(.*?)\s{2,}"
:invoice-number #"Invoice #.*\n.*\n.*?(\d{5,})\n"
:total #"Total\s+\$([0-9\.,]+)"}
:parser {:date [:clj-time "MM/dd/yy"]
:total [:trim-commas nil]}}
{:vendor "General Produce Company"
:keywords [#"generalproduce.com"]
:extract {:date #"INVOICE DATE.*\n.*?([0-9]+/[0-9]+/[0-9]+)"

View File

@@ -39,7 +39,7 @@
(reduced (time/from-time-zone (f/parse (f/formatter format) value)
(time/time-zone-for-id "America/Los_Angeles")))
(catch Exception e
(println e)
(println (.getMessage e))
nil)))
nil
format)