Use the :let and :when modifiers for for

This commit is contained in:
Ben Sturmfels 2025-09-27 22:27:20 +10:00
parent d0565f794f
commit 2cb7c3a3ad
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0

View file

@ -101,11 +101,11 @@
(for [[k v] meta]
(format " %s: \"%s\"\n" (name k) v))
;; Postings and posting metadata
(for [{:keys [amount account currency meta]} postings]
(when-not (zero? amount)
(for [{:keys [amount account currency meta]} postings
: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)))))))))
(format " %s: \"%s\"\n" (name k) v))))))))
;; Each of the below functions returns one of the five payroll
;; transaction "sections" (one or more Beancount transaction entry):
@ -144,10 +144,10 @@
:payroll-type "US:General"}
:postings []}
postings (flatten
(for [[name employee-records] (group-by :name records)]
(let [total-net-pay (total-by-type "Net Pay" employee-records)
(for [[name employee-records] (group-by :name records)
:let [total-net-pay (total-by-type "Net Pay" employee-records)
total-reimbursement (total-by-type "Reimbursement" employee-records)
actual-total-net-pay (- total-net-pay total-reimbursement)]
actual-total-net-pay (- total-net-pay total-reimbursement)]]
[{:account "Expenses:Payroll:Salary"
:amount actual-total-net-pay :currency "USD"
:meta (assoc-project projects name {:entity name})}
@ -159,14 +159,14 @@
:meta (assoc-project projects name {:entity name :payroll-type "US:Reimbursement"})}
{:account "Liabilities:Payable:Accounts"
:amount (- total-reimbursement) :currency "USD"
:meta (assoc-project projects name {:entity name :tax-implication "Reimbursement"})}])))]
:meta (assoc-project projects name {:entity name :tax-implication "Reimbursement"})}]))]
[(assoc template :postings postings)]))
(defn individual-taxes
"Return a transaction of expenses/witholding for each employee."
[date period pay-invoice-no retirement-invoice-no projects records]
(for [[name employee-records] (group-by :name records)]
(let [template {:date date :desc (format "Monthly Payroll - %s - TAXES - %s" period name)
(for [[name employee-records] (group-by :name records)
:let [template {:date date :desc (format "Monthly Payroll - %s - TAXES - %s" period name)
:meta (assoc-project
projects name
{:project "Conservancy"
@ -215,8 +215,8 @@
withholding-postings
withholding-asset-postings
insurance-postings
insurance-asset-postings)]
(assoc template :postings all-postings))))
insurance-asset-postings)]]
(assoc template :postings all-postings)))
(defn employer-taxes
"Return an employer taxes transaction."
@ -295,11 +295,11 @@
:tax-implication "Retirement-Pretax"
:invoice invoice-no}
:postings []}
liability-postings (for [[name employee-records] (group-by :name records)]
(let [total-retirement (total-by-type "Retirement" employee-records)]
liability-postings (for [[name employee-records] (group-by :name records)
:let [total-retirement (total-by-type "Retirement" employee-records)]]
{:account "Liabilities:Payable:Accounts"
:amount total-retirement :currency "USD"
:meta {:entity name}}))
:meta {:entity name}})
total-liabilities (total liability-postings)
asset-postings [{:account "Assets:Citizens:Check1273"
:amount (- total-liabilities) :currency "USD"}]
@ -319,15 +319,15 @@
:payroll-type "US:General"}
:postings []}
employee-postings (flatten
(for [[name employee-records] (group-by :name records)]
(let [net-pay (total-by-type "Net Pay" employee-records)
reimbursements (total-by-type "Reimbursement" employee-records)]
(for [[name employee-records] (group-by :name records)
:let [net-pay (total-by-type "Net Pay" employee-records)
reimbursements (total-by-type "Reimbursement" employee-records)]]
[{:account "Liabilities:Payable:Accounts"
:amount (- net-pay reimbursements) :currency "USD"
:meta (assoc-project projects name {:entity name})}
{:account "Liabilities:Payable:Accounts"
:amount reimbursements :currency "USD"
:meta (assoc-project projects name {:entity name})}])))
:meta (assoc-project projects name {:entity name})}]))
total-net-pay (total-by-type "Net Pay" records)
total-reimbursements (total-by-type "Reimbursement" records)
total-net-pay-posting {:account "Assets:Citizens:Check1273"
@ -361,18 +361,18 @@
:amount (+ total-liabilities total-unemploy) :currency "USD"
:meta {:entity "Paychex"}}]
withholding-liability-postings (flatten
(for [[name employee-records] (group-by :name records)]
(let [witholding-records (filter #(= (:type %) "Withholding") employee-records)
(for [[name employee-records] (group-by :name records)
:let [witholding-records (filter #(= (:type %) "Withholding") employee-records)
insurance-records (filter (fn [{:keys [category type]}]
(and (= type "Withholding")
(str/starts-with? category "NY Disability")))
employee-records)]
employee-records)]]
[{:account "Liabilities:Payable:Accounts"
:amount (total witholding-records) :currency "USD"
:meta (assoc-project projects name {:entity name})}
{:account "Liabilities:Payable:Accounts"
:amount (- (total insurance-records)) :currency "USD"
:meta (assoc-project projects name {:entity name})}])))
:meta (assoc-project projects name {:entity name})}]))
asset-postings [{:account "Assets:Citizens:Check1273"
:amount (- (+ total-liabilities total-unemploy (total withholding-liability-postings))) :currency "USD"
:meta {:entity "Paychex" :tax-implication "Tax-Payment"}}]