Commit 425b0853 authored by phil's avatar phil

multiple changes...

* debug log events in a nice fashion
* assert-exactly-one for resolving invoices on settlements
* fix bug in calculating taxes on settlements
* on subcommand transform allow for filtering by properties
parent e8e62ca1
......@@ -11,10 +11,12 @@
[easy.common :as common]
[easy.log :as log]
[easy.common.invoice-no :as invoice-no]
[clojure.tools.cli :refer [parse-opts]]
[cljs.pprint :refer [pprint]]
[cljs.spec.alpha :as s]
[clojure.string :refer [join]]
[clojure.tools.cli :refer [parse-opts]]
[clojure.string :refer [join split replace]]
[clojure.walk :refer [keywordize-keys]]
[clojure.data :refer [diff]]
;; NOTE: Even though we don't use any of the remaining
;; namespaces in this list, we nevertheless have to
;; require them here, otherwise they won't get loaded at
......@@ -71,7 +73,12 @@
(let [context (util/bin-by (comp keyword :type) events)]
(->> events
(map (partial safe-transform context))
pprint)))
;; This filters events to the ones where the filter matches,
;; as this is applied after transformation, you can filter
;; for anything.
(filter #(nil? (second (diff % (:filter options)))))
util/write-yaml
println)))
(defn noop!
......@@ -143,11 +150,20 @@
(process.exit 0)))
(defn- parse-filter [arg]
(->> (split arg #",")
(map #(split % #"="))
flatten
(apply hash-map)
keywordize-keys))
(def cli-options
[["-d" "--debug" "Debug output"]
["-i" "--input INPUT" "Input file"]
["-y" "--year NUMBER" "Year"]
["-n" "--no NUMBER" "Invoice No"]])
["-y" "--year NUMBER" "Year (only applies to the subcommand: ledger)"]
["-n" "--no NUMBER" "Invoice No (only applies to the subcommand: invoice)"]
["-f" "--filter FILTER" "Filter (only applies to the subcommand: transform)" :parse-fn parse-filter]])
;; TODO: get rid of the warning by using reader conditionals in
......
......@@ -18,6 +18,11 @@
(first args))
(defn debug-evt [evt & args]
(if (-> @config :options :options :debug)
(println (str "[" (:invoice-no evt) "]") (apply str args))))
;; (defn spy [& args]
;; (apply pprint args)
;; (last args))
......@@ -98,12 +98,19 @@
(assoc* evt :customer)))
(defn assert-exactly-one [x]
(case (count x)
0 (util/die "Expected exactly one, but got zero.")
1 (first x) ;; unpack
(util/die "More than one is too many.")))
(defn resolve-invoice [{:keys [invoice-no] :as evt} context]
(if (nil? context)
evt
(->> context
(filter #(= invoice-no (:invoice-no %)))
first ;; TODO: assert-exactly-one
assert-exactly-one
(safe-transform nil)
(assoc* evt :invoice))))
......@@ -125,6 +132,7 @@
(defn add-tax-in [evt]
(log/debug-evt evt "tax-in = " (:net-total evt) " x " (-> evt :invoice :tax-rate-in))
(->> (:net-total evt)
(* (-> evt :invoice :tax-rate-in))
util/round-currency
......@@ -132,6 +140,7 @@
(defn add-tax-out [evt]
(log/debug-evt evt "tax-out = " (:net-total evt) " x " (-> evt :invoice :tax-rate-out))
(->> (:net-total evt)
(* (-> evt :invoice :tax-rate-out))
util/round-currency
......@@ -139,6 +148,7 @@
(defn add-tax-win [evt]
(log/debug-evt evt "tax-win = " (:tax-in evt) " - " (:tax-out evt))
(->> (:tax-out evt)
(- (:tax-in evt))
util/round-currency
......@@ -152,7 +162,7 @@
(defn add-net-total
"Calculates the net-total based on the gross-total (here `amount`)."
[evt]
(->> (/ (:amount evt) (inc (:tax-rate-in evt)))
(->> (/ (:amount evt) (inc (-> evt :invoice :tax-rate-in)))
util/round-currency
(assoc* evt :net-total)))
......@@ -274,6 +284,9 @@
;; `context` can also be nil, this is the case if the event is
;; transformed while being resolved for another event
(defmethod transform :settlement [context evt]
(if context
(log/debug-evt evt "TRANSFORM WITH CONTEXT")
(log/debug-evt evt "TRANSFORM WITHOUT CONTEXT"))
(-> evt
(common/validate! ::event)
merge-defaults
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment