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

View file

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