diff --git a/migrator/migrations/1523595957-DOWN-switch-date-format.sql b/migrator/migrations/1523595957-DOWN-switch-date-format.sql new file mode 100644 index 00000000..c19f06b6 --- /dev/null +++ b/migrator/migrations/1523595957-DOWN-switch-date-format.sql @@ -0,0 +1,3 @@ +-- 1523595957 DOWN switch-date-format +alter table invoices drop column date; +alter table invoices add column date varchar(255); diff --git a/migrator/migrations/1523595957-UP-switch-date-format.sql b/migrator/migrations/1523595957-UP-switch-date-format.sql new file mode 100644 index 00000000..e173c4a4 --- /dev/null +++ b/migrator/migrations/1523595957-UP-switch-date-format.sql @@ -0,0 +1,3 @@ +-- 1523595957 UP switch-date-format +alter table invoices drop column date; +alter table invoices add column date timestamp with time zone; diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index e35027c0..e6d5aae2 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -35,7 +35,9 @@ (if company (-> base-query (helpers/merge-where [:= :imported true]) - (helpers/merge-where [:= :company-id company])) + (helpers/merge-where [:= :company-id (if (int? company) + company + (Integer/parseInt company))])) (-> base-query (helpers/merge-where [:= :imported true]))))) @@ -44,7 +46,9 @@ (if company (-> base-query (helpers/merge-where [:= :imported false]) - (helpers/merge-where [:= :company-id company])) + (helpers/merge-where [:= :company-id (if (int? company) + company + (Integer/parseInt company))])) (-> base-query (helpers/merge-where [:= :imported false]))))) diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index 3b35b619..421784c0 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -3,9 +3,23 @@ [auto-ap.parse.templates :as t] [clj-fuzzy.metrics :as m] [clojure.java.shell :as sh] - [clojure.string :as str])) + [clojure.string :as str] + [clj-time.format :as f] + [clj-time.core :as time])) +(defmulti parse-value (fn [method _ _] + method)) + +(defmethod parse-value :clj-time + [_ format value] + (time/from-time-zone (f/parse (f/formatter format) value) + (time/time-zone-for-id "America/Los_Angeles"))) + +(defmethod parse-value nil + [_ _ value] + value) + (defn template-applies? [text {:keys [keywords]}] (every? #(re-find % text) keywords)) @@ -20,8 +34,10 @@ :extract (reduce-kv (fn [result k v] - (assoc result k (some-> (first (map second (re-seq v text))) - str/trim ))) + (let [value (some-> (first (map second (re-seq v text))) + str/trim ) + [value-parser parser-params] (-> template :parser k)] + (assoc result k (parse-value value-parser parser-params value)))) {:vendor-code (:vendor template)}))])) (defn parse [text] diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index ad681b34..eb204e11 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -1,19 +1,22 @@ (ns auto-ap.parse.templates) + (def pdf-templates [{:vendor "CHFW" :keywords [#"CHEF'S WAREHOUSE"] :extract {:total #"2 WKS C\.C\.\s+([\d.,]+)" :customer-identifier #"\n([A-Z][A-Z ]+)\s{2,}" :date #"\s+([0-9]+/[0-9]+/[0-9]+)" - :invoice-number #"\s+[0-9]+/[0-9]+/[0-9]+\s+([0-9]+)"}} + :invoice-number #"\s+[0-9]+/[0-9]+/[0-9]+\s+([0-9]+)"} + :parser {:date [:clj-time "MM/dd/yyyy"]}} {:vendor "GGM" :keywords [#"Golden Gate Meat"] :extract {:total #"Invoice Total\:\s+\$([\d.,]+)" :customer-identifier #"Bill To\s*:\s*([\w ]+)\s{2,}" :date #"Printed:\s+([0-9]+/[0-9]+/[0-9]+)" - :invoice-number #"Invoice\s+[^\n]+\n[^\n]+\n\s+([0-9]+)"}} + :invoice-number #"Invoice\s+[^\n]+\n[^\n]+\n\s+([0-9]+)"} + :parser {:date [:clj-time "MM/dd/yyyy"]}} {:vendor "CINTAS" :keywords [#"CINTAS CORPORATION"] @@ -21,6 +24,7 @@ :customer-identifier #"BILL TO\s*:\s{2,}([\w ]+)\s{2,}" :date #"INVOICE DATE\s*\n.*\s+([0-9]+/[0-9]+/[0-9]+)" :total #"INVOICE TOTAL\s+([0-9.]+)"} + :parser {:date [:clj-time "MM/dd/yy"]} :multi #"\f\f"}]) (def excel-templates diff --git a/src/cljs/auto_ap/effects.cljs b/src/cljs/auto_ap/effects.cljs index 99e3c4db..fe436ca1 100644 --- a/src/cljs/auto_ap/effects.cljs +++ b/src/cljs/auto_ap/effects.cljs @@ -4,6 +4,7 @@ [cljs-http.client :as http] [cljs-time.coerce :as c] [cljs-time.core :as time] + [cljs-time.format :as format] [cljs.core.async :refer [date-times [x] - (cond (map? x) - (into {} (map (fn [[k v]] - [k (if (instance? js/Date v) - (time/to-default-time-zone (c/from-date v)) - v)]) - x)) - (list? x) - (map dates->date-times x))) + (walk/postwalk + (fn [node] + (cond + + (and (string? node) + (re-matches is-8601 node)) + (do + (format/parse (format/formatters :date-time) node)) + + (instance? js/Date node) + (time/to-default-time-zone (c/from-date node)) + + + :else + node)) + x)) (re-frame/reg-fx :http @@ -74,6 +84,7 @@ (keyword? node) (snake node) + :else node)) m)) diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index f371fc20..5693a3f1 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -1,7 +1,9 @@ (ns auto-ap.views.components.invoice-table (:require [re-frame.core :as re-frame] [auto-ap.subs :as subs] - [reagent.core :as reagent])) + [auto-ap.views.utils :refer [date->str]] + [reagent.core :as reagent] + [cljs-time.format :as format])) (defn toggle-sort-by [params key] @@ -58,5 +60,5 @@ [:td (:name vendor)] [:td (:name company)] [:td invoice-number] - [:td date] + [:td (date->str date) ] [:td total]]))]])))) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 0e4df3cb..e91b0a56 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -25,10 +25,12 @@ (def pretty (format/formatter "MM/dd/yyyy")) (defn date->str [d] - (format/unparse pretty d)) + (when d + (format/unparse pretty d))) (defn date-time->str [d] - (format/unparse pretty-long d)) + (when d + (format/unparse pretty-long d)))