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
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
[import :as import]
(:require [import :as import]
[parse :refer [parse]]
[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]
(for [t transactions]
(update t :postings
(fn [ps] (sort-by (juxt #(get-in % [:meta :entity]) :account :amount) (filter #(not (zero? (:amount %))) ps))))))
(comment
(def data
(with-open [reader (io/reader "/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))
(require '[examples :as examples])
(def data (import/read-grouped-csv "/home/ben/Downloads/2023-12-27_Pay-Item-Details_2023-12-2.csv"))
(def imported
(concat [(import/import-monthly-payroll groups)]
(import/import-individual-taxes groups)))
(concat [(import/import-monthly-payroll data)]
(import/import-individual-taxes data)))
(dd/pretty-print (dd/diff
(sort-postings human)
(sort-postings imported)
))
(dd/pretty-print
(dd/diff
(sort-postings (parse examples/human))
(sort-postings imported)))
(doseq [i imported]
(println (import/render-transaction i)))

View file

@ -15,6 +15,11 @@
(defn bigdec-or-zero [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]
(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)

View file

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