lots of QOL improvements.
This commit is contained in:
29
src/clj/auto_ap/background/invoices.clj
Normal file
29
src/clj/auto_ap/background/invoices.clj
Normal file
@@ -0,0 +1,29 @@
|
||||
(ns auto-ap.background.invoices
|
||||
(:require [auto-ap.datomic.invoices :as d-invoices]
|
||||
[auto-ap.datomic :refer [uri]]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.time :as time]
|
||||
[clj-time.coerce :as coerce]))
|
||||
|
||||
(def break (atom false))
|
||||
|
||||
(defn close-auto-invoices []
|
||||
(while (and (not @break)
|
||||
(not (Thread/interrupted)))
|
||||
(println "Clearing automatic invoices")
|
||||
(try
|
||||
(->> (d/query {:query {:find ['?e]
|
||||
:in ['$ '?today]
|
||||
:where ['[?e :invoice/automatically-paid-when-due true]
|
||||
'[?e :invoice/status :invoice-status/unpaid]
|
||||
'[?e :invoice/due ?d]
|
||||
'[(<= ?d ?today)]]}
|
||||
:args [(d/db (d/connect uri)) (coerce/to-date (time/local-now))]})
|
||||
(mapv (fn [[i]] {:db/id i
|
||||
:invoice/outstanding-balance 0.0
|
||||
:invoice/status :invoice-status/paid}))
|
||||
(d/transact (d/connect uri))
|
||||
deref)
|
||||
(Thread/sleep (* 1000 60 60))
|
||||
(catch Exception e
|
||||
(println (.toString e))))))
|
||||
@@ -194,15 +194,27 @@
|
||||
|
||||
|
||||
(defn get-existing-set []
|
||||
(d/query
|
||||
(cond-> {:query {:find ['?vendor '?client '?invoice-number]
|
||||
:in ['$]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
|
||||
:args [(d/db (d/connect uri))]})))
|
||||
(let [vendored-results (set (d/query {:query {:find ['?vendor '?client '?invoice-number]
|
||||
:in ['$]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
|
||||
:args [(d/db (d/connect uri))]}))
|
||||
vendorless-results (->> (d/query {:query {:find ['?client '?invoice-number]
|
||||
:in ['$]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
(not [?e :invoice/vendor])
|
||||
[?e :invoice/client ?client]
|
||||
(not [?e :invoice/status :invoice-status/voided])
|
||||
]}
|
||||
|
||||
:args [(d/db (d/connect uri))]})
|
||||
(mapv (fn [[client invoice-number]]
|
||||
[nil client invoice-number]) )
|
||||
set)]
|
||||
(into vendored-results vendorless-results)))
|
||||
|
||||
|
||||
|
||||
@@ -107,9 +107,12 @@
|
||||
:extract {:date #"INVOICE DATE(?s:.*)(?= (?:[0-9]+/[0-9]+/[0-9]+)\s+([0-9]+/[0-9]+/[0-9]+)) "
|
||||
:customer-identifier #"SOLD TO:(?:.*)(?=\n)\n(.*)(?=\s{2,})" ;; ([\S ]+)\s{2,}
|
||||
:invoice-number #"INVOICE\n(?:.*?)(?=\d{4,})(\d+)"
|
||||
:total #"PAY THIS AMOUNT(?s:.*)(?= ([0-9,]+\.[0-9]{2}))"}
|
||||
:total #"PAY THIS AMOUNT(?s:.*)(?= ([0-9,]+\.[0-9]{2}))"
|
||||
:account-number #"ACCOUNT #.*\n.*\n\s+(\d+)"}
|
||||
:parser {:date [:clj-time "MM/dd/yy"]
|
||||
:total [:trim-commas nil]}}
|
||||
:total [:trim-commas nil]}
|
||||
:multi #"\f"
|
||||
:multi-match? #"PAY THIS AMOUNT"}
|
||||
|
||||
;; GOLDEN BRANDS
|
||||
{:vendor "Golden Brands San Jose"
|
||||
|
||||
@@ -64,6 +64,9 @@
|
||||
(defn parse-vendor-id [{:keys [vendor]}]
|
||||
(:db/id vendor))
|
||||
|
||||
(defn parse-automatically-paid-when-due [{:keys [vendor client-id]}]
|
||||
(boolean ((set (map :db/id (:vendor/automatically-paid-when-due vendor))) client-id)))
|
||||
|
||||
(defn parse-amount [i]
|
||||
(try
|
||||
(Double/parseDouble (str/replace (or (second
|
||||
@@ -121,6 +124,7 @@
|
||||
(map (parse-or-error :client-id #(parse-client % all-clients)))
|
||||
(map (parse-or-error :vendor #(parse-vendor % all-vendors)))
|
||||
(map (parse-or-error :vendor-id #(parse-vendor-id %)))
|
||||
(map (parse-or-error :automatically-paid-when-due #(parse-automatically-paid-when-due %)))
|
||||
(map (parse-or-error :account-id parse-account-numeric-code))
|
||||
(map (parse-or-error :invoice-number parse-invoice-number))
|
||||
(map (parse-or-error :total parse-amount))
|
||||
@@ -131,13 +135,14 @@
|
||||
|
||||
(defn invoice-rows->transaction [rows]
|
||||
(->> rows
|
||||
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor]}]
|
||||
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location account-id check vendor automatically-paid-when-due]}]
|
||||
(let [invoice (cond->
|
||||
#:invoice {:db/id (.toString (java.util.UUID/randomUUID))
|
||||
:vendor vendor-id
|
||||
:client client-id
|
||||
:default-location default-location
|
||||
:import-status :import-status/imported
|
||||
:automatically-paid-when-due automatically-paid-when-due
|
||||
#_#_:default-expense-account default-expense-account
|
||||
:total total
|
||||
:outstanding-balance (if (= "Cash" check)
|
||||
@@ -245,7 +250,8 @@
|
||||
". "
|
||||
(.toString e))
|
||||
{:args [ invoice-number matching-vendor (:db/id matching-client)]})))
|
||||
))]
|
||||
))
|
||||
automatically-paid-for (set (map :db/id (:vendor/automatically-paid-when-due matching-vendor)))]
|
||||
|
||||
(cond
|
||||
(not (and matching-location matching-client))
|
||||
@@ -259,6 +265,7 @@
|
||||
:invoice/client-identifier customer-identifier
|
||||
:invoice/vendor (:db/id matching-vendor)
|
||||
:invoice/invoice-number invoice-number
|
||||
:invoice/automatically-paid-when-due (boolean (automatically-paid-for (:db/id matching-client)))
|
||||
:invoice/total (Double/parseDouble total)
|
||||
:invoice/date (to-date date)
|
||||
:invoice/import-status :import-status/pending
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[auto-ap.handler :refer [app]]
|
||||
[auto-ap.ledger :refer [process-all keep-up-to-date]]
|
||||
[auto-ap.yodlee.core :refer [load-in-memory-cache]]
|
||||
[auto-ap.background.invoices :refer [close-auto-invoices]]
|
||||
[nrepl.server :refer [start-server stop-server]]
|
||||
[config.core :refer [env]]
|
||||
[ring.adapter.jetty :refer [run-jetty]])
|
||||
@@ -18,5 +19,6 @@
|
||||
(future (process-all))
|
||||
(future (load-in-memory-cache))
|
||||
(future (keep-up-to-date))
|
||||
(future (close-auto-invoices))
|
||||
#_(future (always-process-sqs))
|
||||
(run-jetty app {:port port :join? false})))
|
||||
|
||||
Reference in New Issue
Block a user