This commit is contained in:
2023-04-27 11:33:54 -07:00
7 changed files with 246 additions and 91 deletions

View File

@@ -65,15 +65,17 @@
({:bank-account-type/check :account-type/asset
:bank-account-type/cash :account-type/asset
:bank-account-type/credit :account-type/liability}
(:db/ident (:bank-account/type (:account li)))))]
(:db/ident (:bank-account/type (:journal-entry-line/account li)))))]
[(-> j :journal-entry/client :client/code)
(-> j :journal-entry/vendor :vendor/name)
(atime/unparse (coerce/to-date-time (-> j :journal-entry/date))
atime/normal-date)
(-> j :db/id)
(-> li :db/id)
(-> li :journal-entry-line/account :account/numeric-code)
(-> li :journal-entry-line/account :account/name)
(or (-> li :journal-entry-line/account :account/numeric-code)
(-> li :journal-entry-line/account :bank-account/numeric-code))
(or (-> li :journal-entry-line/account :account/name)
(-> li :journal-entry-line/account :bank-account/name))
(some-> account-type name )
(-> li :journal-entry-line/debit)
(-> li :journal-entry-line/credit)

View File

@@ -43,14 +43,16 @@
:key k}))
(defn is-csv-file? [x]
(= "csv" (last (str/split x #"[\\.]"))))
(= "dat" (last (str/split x #"[\\.]"))))
(defn decipher-source [k]
(cond
(str/includes? k "Cintas")
(and (str/includes? k "Cintas")
(str/includes? k "zcic"))
:cintas
(and (str/includes? k "GeneralProduce")
(str/includes? k "FRANCHISEE")
(is-csv-file? k))
:general-produce
@@ -63,25 +65,34 @@
(defmethod extract-invoice-details :general-produce
[k input-stream clients]
(log/info ::parsing-general-produce :key k)
(->> (read-csv input-stream)
(drop 1)
(filter (fn [[_ _ _ _ _ _ _ _ _ _ _ break-flag]]
(= "Y" break-flag)))
(map (fn [[vendor location-hint invoice-number ship-date invoice-total ]]
(let [matching-client (and location-hint
(parse/exact-match clients location-hint))]
{:invoice/vendor vendor
:invoice/location (parse/best-location-match matching-client location-hint location-hint )
:invoice/date (coerce/to-date (atime/parse ship-date atime/normal-date))
:invoice/invoice-number invoice-number
:invoice/total (Double/parseDouble invoice-total)
:invoice/outstanding-balance (Double/parseDouble invoice-total)
:invoice/client matching-client
:invoice/import-status :import-status/imported
:invoice/status :invoice-status/unpaid
:invoice/client-identifier location-hint
})))
(filter :invoice/client)))
(try
(->> (read-csv input-stream)
(drop 1)
(filter (fn [[_ _ _ _ _ _ _ _ _ _ _ break-flag]]
(= "Y" break-flag)))
(map (fn [[vendor location-hint invoice-number ship-date invoice-total ]]
(let [matching-client (and location-hint
(parse/exact-match clients location-hint))]
(when-not matching-client
(log/warn ::missing-client
:client-hint location-hint))
{:invoice/location (parse/best-location-match matching-client location-hint location-hint )
:invoice/date (coerce/to-date (atime/parse ship-date atime/normal-date))
:invoice/invoice-number invoice-number
:invoice/total (Double/parseDouble invoice-total)
:invoice/vendor :vendor/general-produce
:invoice/outstanding-balance (Double/parseDouble invoice-total)
:invoice/client matching-client
:invoice/import-status :import-status/imported
:invoice/status :invoice-status/unpaid
:invoice/client-identifier location-hint
})))
(filter :invoice/client)
(into []))
(catch Exception e
(log/error ::cant-import-general-produce
:error e)
[])))
(defmethod extract-invoice-details :unknown
[k input-stream clients]
@@ -115,9 +126,7 @@
first
:content
first)
matching-client (and location-hint (parse/exact-match clients location-hint))
]
matching-client (and location-hint (parse/exact-match clients location-hint))]
(if matching-client
(let [invoice-date (->> node-seq
(map zip/node)
@@ -182,7 +191,7 @@
:invoice invoice)
[invoice])
(do
(log/info ::missing-client
(log/warn ::missing-client
:client-hint location-hint)
[]))))
@@ -204,55 +213,56 @@
:destination-key invoice-key })
invoice-key))
(defn import-ntg-invoices []
(let [clients (map first (d/q '[:find (pull ?c [:client/code
:db/id
:client/feature-flags
{:client/location-matches [:location-match/matches :location-match/location]}
:client/name
:client/matches
:client/locations])
:where [?c :client/code]]
(d/db conn)))
keys (->> (s3/list-objects-v2 {:bucket-name bucket-name
:prefix "ntg-invoices/pending"})
:object-summaries
(map :key))]
(defn import-ntg-invoices
([] (import-ntg-invoices (->> (s3/list-objects-v2 {:bucket-name bucket-name
:prefix "ntg-invoices/pending"})
:object-summaries
(map :key))))
([keys]
(let [clients (map first (d/q '[:find (pull ?c [:client/code
:db/id
:client/feature-flags
{:client/location-matches [:location-match/matches :location-match/location]}
:client/name
:client/matches
:client/locations])
:where [?c :client/code]]
(d/db conn)))]
(log/info ::found-invoice-keys
:keys keys )
(let [transaction (->> keys
(mapcat (fn [k]
(try
(let [invoice-key (copy-readable-version k)
invoice-url (str "http://" bucket-name ".s3-website-us-east-1.amazonaws.com/" invoice-key)]
(with-open [is (-> (s3/get-object {:bucket-name bucket-name
:key k})
:input-stream)]
(->> (extract-invoice-details k
is
clients)
(map (fn [i]
(log/info ::importing-invoice
:invoice i)
i))
(mapv (fn [i]
`(propose-invoice ~(assoc i :invoice/source-url invoice-url))))))) (catch Exception e
(log/error ::cant-load-file
:key k
:exception e)
(mark-error k)
[]))))
(into []))]
(audit-transact transaction {:user/name "sysco importer" :user/role "admin"})
(log/info ::success
:count (count transaction)
:sample (take 3 transaction)))
(doseq [k keys]
(mark-key k))))
(log/info ::found-invoice-keys
:keys keys )
(let [transaction (->> keys
(mapcat (fn [k]
(try
(let [invoice-key (copy-readable-version k)
invoice-url (str "http://" bucket-name ".s3-website-us-east-1.amazonaws.com/" invoice-key)]
(with-open [is (-> (s3/get-object {:bucket-name bucket-name
:key k})
:input-stream)]
(->> (extract-invoice-details k
is
clients)
(map (fn [i]
(log/info ::importing-invoice
:invoice i)
i))
(mapv (fn [i]
`(propose-invoice ~(assoc i :invoice/source-url invoice-url))))))) (catch Exception e
(log/error ::cant-load-file
:key k
:exception e)
(mark-error k)
[]))))
(into []))]
(audit-transact transaction {:user/name "sysco importer" :user/role "admin"})
(log/info ::success
:count (count transaction)
:sample (take 3 transaction)))
(doseq [k keys]
(mark-key k)))))
(defn -main [& _]
(execute "ntg-invoices" import-ntg-invoices))
(execute "ntg" import-ntg-invoices))

View File

@@ -9,6 +9,7 @@
[auto-ap.jobs.ezcater-upsert :as job-ezcater-upsert]
[auto-ap.jobs.import-uploaded-invoices :as job-import-uploaded-invoices]
[auto-ap.jobs.intuit :as job-intuit]
[auto-ap.jobs.ntg :as job-ntg]
#_[auto-ap.backup :as backup]
[auto-ap.jobs.ledger-reconcile :as job-reconcile-ledger]
[auto-ap.jobs.load-historical-sales :as job-load-historical-sales]
@@ -145,6 +146,9 @@
;; (= job "export-backup")
;; (backup/-main)
(= job "ntg")
(job-ntg/-main)
:else
(do
(add-shutdown-hook! shutdown-mount)