Sysco can now import for a line item

This commit is contained in:
2024-05-02 23:08:18 -07:00
parent c50b706de9
commit 52a23209d7
5 changed files with 1869 additions and 26 deletions

View File

@@ -1,29 +1,50 @@
(ns auto-ap.jobs.sysco
(:require
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [conn]]
[auto-ap.jobs.core :refer [execute]]
[auto-ap.datomic :refer [audit-transact random-tempid]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :refer [code-invoice]]
[auto-ap.parse :as parse]
[auto-ap.time :as t]
[clj-time.coerce :as coerce]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[com.brunobonacci.mulog :as mu]
[auto-ap.logging :as alog]
[clojure.string :as str]
[com.unbounce.dogstatsd.core :as statsd]
[config.core :refer [env]]
[datomic.api :as dc]
[auto-ap.datomic.vendors :as d-vendors])
(:import
(java.util UUID)))
(:require [amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [conn]]
[auto-ap.datomic :refer [audit-transact pull-attr random-tempid]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :refer [code-invoice]]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.jobs.core :refer [execute]]
[auto-ap.logging :as alog]
[auto-ap.parse :as parse]
[auto-ap.time :as t]
[auto-ap.utils :refer [dollars=]]
[clj-time.coerce :as coerce]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.string :as str]
[config.core :refer [env]]
[datomic.api :as dc])
(:import (java.util UUID)))
(def bucket-name (:data-bucket env))
(def sysco-name->line
(with-open [data (io/reader (io/resource "sysco_line_item_mapping.csv"))]
(let [data (csv/read-csv data)]
(->> data
(drop 1)
(map (fn [ [_ _ name _ account-number]]
[name (ffirst (dc/q '[:find ?a
:in $ ?an
:where [?a :account/numeric-code ?an]]
(dc/db conn)
(Long/parseLong account-number)))]))
(into {})))))
(defn get-line-account [item-name]
(get sysco-name->line item-name
(ffirst (dc/q '[:find ?a
:in $ ?an
:where [?a :account/numeric-code ?an]]
(dc/db conn)
50000))))
(def ^:dynamic 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"])
(def item-price-index 15)
(def item-name-index 29)
(def summary-keys ["TranCode" "GroupID" "Company" "CustomerNumber" "InvoiceNumber" "RecordType" "Item" "InvoiceDocument" "TotalLines" "TotalQtyInvoice" "TotalQty" "TotalQtySplit" "TotalQtyPounds" "TotalExtendedPrice" "TotalTaxAmount" "TotalInvoiceAmount" "AccountDate"])
@@ -46,6 +67,29 @@
io/reader
csv/read-csv))
(defn code-individual-items [invoice csv-rows tax]
(let [items (->> csv-rows
butlast
(reduce
(fn [acc row]
(update acc (get-line-account (nth row item-name-index))
(fnil + 0.0)
(Double/parseDouble (nth row item-price-index))
)
)
{})
)
items-with-tax (update items (get-line-account "TAX")
(fnil + 0.0)
tax)]
(assoc invoice :invoice/expense-accounts
(for [[account amount] items-with-tax]
#:invoice-expense-account {:db/id (random-tempid)
:account account
:location (:invoice/location invoice)
:amount amount}))))
(defn extract-invoice-details [csv-rows sysco-vendor]
(let [[header-row & csv-rows] csv-rows
header-row (into {} (map vector header-keys header-row))
@@ -64,14 +108,17 @@
(header-row "AddressLine2")
(header-row "City1")
(header-row "City2")])
account-number (some-> account-number Long/parseLong str)
matching-client (and account-number
(d-clients/exact-match account-number))
(d-clients/exact-match account-number))
_ (when-not matching-client
(throw (ex-info "cannot find matching client"
{:account-number account-number
:name customer-identifier})))
code-items (get (into #{} (pull-attr (dc/db conn) :client/feature-flags (:db/id matching-client)))
"code-sysco-items")
total (Double/parseDouble (summary-row "TotalExtendedPrice"))
tax (Double/parseDouble (summary-row "TotalTaxAmount"))
date (t/parse
@@ -98,7 +145,8 @@
:import-status :import-status/completed
:status :invoice-status/unpaid
:client-identifier customer-identifier}
true (code-invoice))))
true (code-invoice)
code-items (code-individual-items csv-rows tax))))
(defn mark-key [k]
(s3/copy-object {:source-bucket-name bucket-name
@@ -117,6 +165,36 @@
(s3/delete-object {:bucket-name bucket-name
:key k}))
(defn get-test-invoice-file
([] (get-test-invoice-file 999))
( [i]
(nth (->> (s3/list-objects-v2 {:bucket-name "data.prod.app.integreatconsult.com"
:prefix "sysco/imported"})
:object-summaries
(map :key)
)
i)))
(defn check-okay-amount? [i]
(dollars=
(:invoice/total i)
(reduce + 0.0 (map :invoice-expense-account/amount (:invoice/expense-accounts i)))))
(comment
(with-bindings { #'bucket-name "data.prod.app.integreatconsult.com"}
(doall
(for [n (range 800 940 )
:let [result (-> (get-test-invoice-file n)
read-sysco-csv
(extract-invoice-details (get-sysco-vendor))
)]
:when (not (check-okay-amount? result))]
result)))
)
(defn import-sysco []
(let [sysco-vendor (get-sysco-vendor)

View File

@@ -498,7 +498,8 @@
:options [["new-square" "New Square+Ezcater (no effect)"]
["manually-pay-cintas" "Manually Pay Cintas"]
["include-in-ntg-corp-reports" "Include in NTG Corporate reports"]
["import-custom-amount" "Import Custom Amount Line Items from Square"]]})))
["import-custom-amount" "Import Custom Amount Line Items from Square"]
["code-sysco-items" "Code individual sysco line items"]]})))
(com/data-grid-cell {:class "align-top"}
(com/a-icon-button {"@click.prevent.stop" "$refs.p.remove()"} svg/x))))