Move examples out into a separate file

This commit is contained in:
Ben Sturmfels 2024-02-20 14:55:14 +11:00
parent 5421ae8adc
commit 76520640d1
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0
3 changed files with 23 additions and 19 deletions

View file

@ -1,30 +1,30 @@
(ns core (ns core
(:require [clojure.data.csv :as csv] (:require [import :as import]
[clojure.java.io :as io]
[import :as import]
[parse :refer [parse]] [parse :refer [parse]]
[lambdaisland.deep-diff2 :as dd])) [lambdaisland.deep-diff2 :as dd]))
;; TODO: Need some tests now it's working.
;; TODO: Where do the $25.81 fees come from?
;; TODO: Need a CLI to run it on a CSV file.
(defn sort-postings [transactions] (defn sort-postings [transactions]
(for [t transactions] (for [t transactions]
(update t :postings (update t :postings
(fn [ps] (sort-by (juxt #(get-in % [:meta :entity]) :account :amount) (filter #(not (zero? (:amount %))) ps)))))) (fn [ps] (sort-by (juxt #(get-in % [:meta :entity]) :account :amount) (filter #(not (zero? (:amount %))) ps))))))
(comment (comment
(def data (require '[examples :as examples])
(with-open [reader (io/reader "/home/ben/Downloads/2023-12-27_Pay-Item-Details_2023-12-2.csv")] (def data (import/read-grouped-csv "/home/ben/Downloads/2023-12-27_Pay-Item-Details_2023-12-2.csv"))
(doall
(import/prep-csv-data (csv/read-csv reader)))))
(def groups (group-by :name data))
(def imported (def imported
(concat [(import/import-monthly-payroll groups)] (concat [(import/import-monthly-payroll data)]
(import/import-individual-taxes groups))) (import/import-individual-taxes data)))
(dd/pretty-print (dd/diff (dd/pretty-print
(sort-postings human) (dd/diff
(sort-postings imported) (sort-postings (parse examples/human))
)) (sort-postings imported)))
(doseq [i imported] (doseq [i imported]
(println (import/render-transaction i))) (println (import/render-transaction i)))

View file

@ -15,6 +15,11 @@
(defn bigdec-or-zero [s] (defn bigdec-or-zero [s]
(if (str/blank? s) (bigdec 0) (bigdec s))) (if (str/blank? s) (bigdec 0) (bigdec s)))
(defn read-grouped-csv [filename]
(with-open [reader (io/reader filename)]
(doall
(group-by :name (import/prep-csv-data (csv/read-csv reader))))))
(defn prep-csv-data [records] (defn prep-csv-data [records]
(let [headers [:org :name :id :category :type :t-earnings :_ :t-earnings-match :_ :t-reimbursement :t-retirement :_ :t-withholding :t-liability :_ :t-net-pay] (let [headers [:org :name :id :category :type :t-earnings :_ :t-earnings-match :_ :t-reimbursement :t-retirement :_ :t-withholding :t-liability :_ :t-net-pay]
map-records (map zipmap (repeat headers) records) map-records (map zipmap (repeat headers) records)

View file

@ -41,11 +41,10 @@
:postings (s/* ::posting) :postings (s/* ::posting)
:_ (s/* #{\newline}))) :_ (s/* #{\newline})))
(s/def ::transactions (s/def ::transactions (s/+ ::transaction))
(s/+ ::transaction))
(defn kv->map (defn kv->map
"Convert map {:key \"x\" :value \"y\"} to {:x y}" "Convert vector [{:key \"x\" :value \"y\"}] to {:x y}"
[vec] [vec]
(into {} (for [{:keys [key value]} vec] (into {} (for [{:keys [key value]} vec]
[(keyword key) value]))) [(keyword key) value])))
@ -65,7 +64,7 @@
(contains? % :token)) (:token %) (contains? % :token)) (:token %)
;; drop :_ keys ;; drop :_ keys
(= (type %) clojure.lang.PersistentArrayMap) (dissoc % :_) (= (type %) clojure.lang.PersistentArrayMap) (dissoc % :_)
;; convert {:key a :value b} to {:a b} ;; convert vector of :key/:value maps to a map
(and (vector? %) (and (vector? %)
(= (type (first %)) clojure.lang.PersistentArrayMap) (= (type (first %)) clojure.lang.PersistentArrayMap)
(contains? (first %) :key)) (kv->map %) (contains? (first %) :key)) (kv->map %)