Improve readability of some inline functions

This commit is contained in:
Ben Sturmfels 2024-02-27 08:27:48 +11:00
parent fb0204d76c
commit f9d8ea9e45
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0
2 changed files with 18 additions and 11 deletions

View file

@ -89,13 +89,13 @@
(format " %s: \"%s\"\n" (name k) v))
;; Postings and posting metadata
(for [{:keys [amount account currency meta]} postings]
(when (not (zero? amount))
(when-not (zero? amount)
(format " %-40s %10.2f %s\n%s" account amount currency
(str/join (for [[k v] meta]
(format " %s: \"%s\"\n" (name k) v)))))))))
;; Each of the below functions returns one of the five sections of the required
;; bookkeeping data:
;; Each of the below functions returns one of the five payroll
;; transaction "sections" (one or more Beancount transaction entry):
;;
;; * net pay (single transaction)
;; * individual taxes (one transaction for each employee)
@ -103,6 +103,11 @@
;; * fees (single transaction)
;; * retirement (single trasaction)
;;
;; These transaction sections are described in detail in
;; beancount/doc/Payroll.md (though in truth the initial importer was written
;; without realising that file existed and instead by precisely matching the
;; recent manually created payroll transactions).
;;
;; These functions take the input CSV data, pre-formatted and grouped by
;; employee.
;;
@ -160,8 +165,9 @@
witholding-lines (filter #(= (:type %) "Withholding") records)
;; We add these extra disability insurance/asset postings for NY only
;; as discussed in beancount/doc/Payroll.md.
insurance-lines (filter #(and (= (:type %) "Withholding")
(str/starts-with? (:category %) "NY Disability")) records)
insurance-lines (filter (fn [{:keys [category type]}]
(and (= type "Withholding")
(str/starts-with? category "NY Disability"))) records)
total-retirement (->> retirement-lines
(map :amount)
(reduce + 0M))
@ -215,8 +221,9 @@
(defn employer-taxes
"Return an employer taxes transaction."
[date period receipt-no data]
(let [liability-lines (filter #(and (= (:type %) "Liability")
(not (str/includes? (:category %) "Unemploy"))) data)
(let [liability-lines (filter (fn [{:keys [category type]}]
(and (= type "Liability")
(not (str/includes? category "Unemploy")))) data)
liability-postings (for [{:keys [amount name category]} liability-lines]
{:account "Expenses:Payroll:Tax"
:amount amount
@ -228,8 +235,9 @@
{:entity name
:payroll-type (str/replace (cat->payroll-type category) "Tax:" "")})})
total-liabilities (->> liability-postings (map :amount) (reduce + 0M))
unemploy-lines (filter #(and (= (:type %) "Liability")
(str/includes? (:category %) "Unemploy")) data)
unemploy-lines (filter (fn [{:keys [category type]}]
(and (= type "Liability")
(str/includes? category "Unemploy"))) data)
unemploy-postings (for [{:keys [amount name category]} unemploy-lines]
{:account "Expenses:Payroll:Tax"
:amount amount
@ -256,7 +264,6 @@
"Return a payroll fees transaction."
[date period receipt-no invoice-no total-fees data]
(let [employees (distinct (map :name data))
_ (println employees)
exact-fee-allocation (split-fee total-fees (count employees))
employee-fees (map vector employees exact-fee-allocation)
expense-postings (for [[name fee] employee-fees]

View file

@ -94,5 +94,5 @@
(for [t transactions]
(update t :postings
(fn [ps] (->> ps
(filter #(not (zero? (:amount %))))
(remove #(zero? (:amount %)))
(sort-by (juxt #(get-in % [:meta :entity]) :account :amount)))))))