2024-02-20 03:19:23 +00:00
|
|
|
(ns import
|
|
|
|
(:require [clojure.data.csv :as csv]
|
|
|
|
[clojure.java.io :as io]
|
|
|
|
[clojure.string :as str]))
|
|
|
|
|
|
|
|
(defn kebab-kw [x]
|
|
|
|
(-> x
|
|
|
|
str/lower-case
|
|
|
|
(str/replace " " "-")
|
|
|
|
keyword))
|
|
|
|
|
|
|
|
(defn format-name [name]
|
|
|
|
(case name
|
|
|
|
"Sharp, Sage A" "Sharp-Sage-A"
|
|
|
|
(-> name
|
|
|
|
(str/replace " Jr." "")
|
|
|
|
(str/replace ", " "-")
|
|
|
|
(str/replace #" \w$" ""))))
|
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn read-grouped-csv [filename]
|
|
|
|
(with-open [reader (io/reader filename)]
|
|
|
|
(doall
|
|
|
|
(group-by
|
|
|
|
:name
|
|
|
|
(for [[_ name _ category type & totals] (csv/read-csv reader)]
|
|
|
|
{:name (import/format-name name)
|
|
|
|
:category category
|
|
|
|
:type type
|
|
|
|
:amount (apply max (map bigdec (remove str/blank? totals)))})))))
|
|
|
|
|
|
|
|
(defn render-transaction [{:keys [date payee desc meta postings]}]
|
|
|
|
(str/join (concat [(if payee
|
|
|
|
(format "%s txn \"%s\" \"%s\"\n" date payee desc)
|
|
|
|
(format "%s txn \"%s\"\n" date desc))]
|
|
|
|
(for [[k v] meta]
|
2024-02-20 03:19:23 +00:00
|
|
|
(format " %s: \"%s\"\n" (name k) v))
|
2024-02-21 14:02:38 +00:00
|
|
|
(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]
|
2024-02-20 03:19:23 +00:00
|
|
|
(format " %s: \"%s\"\n" (name k) v)))))))))
|
|
|
|
|
|
|
|
(defn cat->acct [cat]
|
|
|
|
;; TODO: Can we make this more general for other states?
|
|
|
|
(case cat
|
|
|
|
"Fed Income Tax" "US:Tax:Income"
|
|
|
|
"Medicare" "US:Tax:Medicare"
|
|
|
|
"IL Income Tax" "US:IL:Tax:Income"
|
|
|
|
"NY Income Tax" "US:NY:Tax:Income"
|
|
|
|
"NYC Income Tax" "US:NY:Tax:NYC"
|
|
|
|
"OR Income Tax" "US:OR:Tax:Income"
|
|
|
|
"OH Income Tax" "US:OH:Tax:Income"
|
|
|
|
"PNTSD Income Tax" "US:OH:Tax:PNTSD"
|
|
|
|
"COLMB Income Tax" "US:OH:Tax:COLUMB"
|
|
|
|
"Social Security" "US:Tax:SocialSecurity"
|
|
|
|
"NY Disability" "US:NY:Disability"
|
|
|
|
"OR Disability PFL" "US:OR:Disability:PFL"
|
|
|
|
"NY Disability PFL" "US:NY:Disability:PFL"
|
|
|
|
"OR TRANS STT" "US:OR:Tax:STT"
|
2024-02-20 07:26:45 +00:00
|
|
|
"Fed Unemploy" "US:Unemployment"
|
|
|
|
"IL Unemploy" "IL:Unemployment"
|
|
|
|
"NY Unemploy" "NY:Unemployment"
|
|
|
|
"OR Unemploy" "OR:Unemployment"
|
2024-02-21 14:02:38 +00:00
|
|
|
"NY Re-empl Svc" "US:NY:Reempt"
|
2024-02-20 03:19:23 +00:00
|
|
|
cat))
|
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn employee-entity-meta [name]
|
|
|
|
(if (= name "Sharp-Sage-A")
|
|
|
|
{:entity name
|
|
|
|
:project "Outreachy"}
|
|
|
|
{:entity name}))
|
|
|
|
|
|
|
|
(defn project [name]
|
|
|
|
(if (= name "Sharp-Sage-A")
|
|
|
|
"Outreachy"
|
|
|
|
"Conservancy"))
|
|
|
|
|
|
|
|
(defn split-fee
|
|
|
|
"Share a total fee into n groups allocating the remainder as evenly as possible."
|
|
|
|
[total n]
|
|
|
|
(let [total (bigdec total)
|
|
|
|
fee-share (.setScale (bigdec (/ (double total) n)) 2 java.math.RoundingMode/FLOOR)
|
|
|
|
extra-cents (* 100M (- total (* fee-share n)))
|
|
|
|
base-fee-allocation (repeat fee-share)
|
|
|
|
cents-allocation (take n (concat (repeat extra-cents 0.01M) (repeat 0)))]
|
|
|
|
(map + base-fee-allocation cents-allocation)))
|
2024-02-20 03:19:23 +00:00
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn payroll
|
|
|
|
"Return a net pay transaction."
|
|
|
|
[date period receipt-no groups]
|
|
|
|
(let [postings (for [[name records] groups]
|
|
|
|
(let [total-net-pay (->> records
|
2024-02-20 07:26:45 +00:00
|
|
|
(filter #(= (:type %) "Net Pay"))
|
2024-02-21 14:02:38 +00:00
|
|
|
(map :amount)
|
|
|
|
(apply +))
|
2024-02-20 07:26:45 +00:00
|
|
|
total-reimbursement (->> records
|
|
|
|
(filter #(= (:type %) "Reimbursement"))
|
2024-02-21 14:02:38 +00:00
|
|
|
(map :amount)
|
|
|
|
(apply +))
|
|
|
|
actual-total-net-pay (- total-net-pay total-reimbursement)]
|
|
|
|
[{:account "Expenses:Payroll:Salary"
|
|
|
|
:amount actual-total-net-pay
|
|
|
|
:currency "USD"
|
|
|
|
:meta (employee-entity-meta name)}
|
|
|
|
{:account "Assets:FR:Check2721"
|
|
|
|
:amount (- actual-total-net-pay)
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:entity name}}
|
|
|
|
{:account "Expenses:Hosting"
|
|
|
|
:amount total-reimbursement
|
|
|
|
:currency "USD"
|
|
|
|
:meta (merge (employee-entity-meta name) {:payroll-type "US:Reimbursement"})}
|
|
|
|
{:account "Assets:FR:Check2721"
|
|
|
|
:amount (- total-reimbursement)
|
|
|
|
:currency "USD"
|
|
|
|
:meta (merge (employee-entity-meta name) {:tax-implication "Reimbursement"})}]))]
|
|
|
|
[{:date date :desc (format "Monthly Payroll - %s - Net Pay" period)
|
|
|
|
:meta {:program "Conservancy:Payroll"
|
|
|
|
:project "Conservancy"
|
|
|
|
:receipt receipt-no
|
|
|
|
:approval "Financial/Employment-Records/memo-re-board-approval-of-payroll.txt"
|
|
|
|
:tax-implication "W2"
|
|
|
|
:payroll-type "US:General"}
|
|
|
|
:postings (apply concat postings)}]))
|
2024-02-20 03:19:23 +00:00
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn individual-taxes
|
|
|
|
"Return a transaction of expenses/witholding for each employee."
|
|
|
|
[date period receipt-no invoice-no groups]
|
2024-02-20 03:19:23 +00:00
|
|
|
(for [[name records] groups]
|
2024-02-21 14:02:38 +00:00
|
|
|
(let [super-lines (filter #(str/starts-with? (:category %) "403b") records)
|
|
|
|
;; TODO: Have I got the liability/witholding right? Which is used in which report.
|
|
|
|
witholding-lines (filter #(= (:type %) "Withholding") records)
|
|
|
|
;; TODO: We seem to add these extra insurance lines for Karen (NY) only. Confirm with Rosanne.
|
|
|
|
insurance-lines (filter #(and (= (:type %) "Withholding")
|
|
|
|
(str/includes? (:category %) "NY Disability")) records)
|
|
|
|
total-super (->> super-lines
|
|
|
|
(map :amount)
|
|
|
|
(apply +))
|
|
|
|
super-postings (for [{:keys [category amount]} super-lines]
|
|
|
|
(if (= category "403b ER match")
|
|
|
|
{:account "Expenses:Payroll:Salary"
|
|
|
|
:amount amount
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:payroll-type "US:403b:Match"
|
|
|
|
:invoice invoice-no}}
|
|
|
|
{:account "Expenses:Payroll:Salary"
|
|
|
|
:amount amount
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:payroll-type "US:403b:Employee"
|
|
|
|
:invoice invoice-no}}))
|
|
|
|
liability-postings [{:account "Liabilities:Payable:Accounts"
|
|
|
|
:amount (- total-super)
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:invoice invoice-no}}]
|
|
|
|
withholding-postings (for [{:keys [category amount]} witholding-lines]
|
|
|
|
{:account "Expenses:Payroll:Salary"
|
|
|
|
:amount amount
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:payroll-type (cat->acct category)}})
|
|
|
|
withholding-asset-postings [{:account "Assets:FR:Check2721"
|
|
|
|
:amount (- (reduce + (map :amount witholding-lines)))
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:tax-implication "W2"}}]
|
|
|
|
insurance-postings (for [{:keys [category amount]} insurance-lines]
|
|
|
|
{:account "Expenses:Insurance"
|
|
|
|
:amount (- amount)
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:payroll-type (cat->acct category)}})
|
|
|
|
insurance-asset-postings [{:account "Assets:FR:Check2721"
|
|
|
|
:amount (reduce + (map :amount insurance-lines))
|
|
|
|
:currency "USD"}]]
|
|
|
|
{:date date :desc (format "Monthly Payroll - %s - TAXES - %s" period name)
|
|
|
|
:meta {:project (project name)
|
|
|
|
:program "Conservancy:Payroll"
|
|
|
|
:entity name
|
|
|
|
:receipt receipt-no
|
|
|
|
:approval "Financial/Employment-Records/memo-re-board-approval-of-payroll.txt"}
|
|
|
|
:postings (concat
|
|
|
|
super-postings
|
|
|
|
liability-postings
|
|
|
|
withholding-postings
|
|
|
|
withholding-asset-postings
|
|
|
|
insurance-postings
|
|
|
|
insurance-asset-postings)})))
|
2024-02-20 07:26:45 +00:00
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn employer-taxes
|
|
|
|
"Return an employer taxes transaction."
|
|
|
|
[date period receipt-no groups]
|
|
|
|
(let [liability-postings (apply concat
|
|
|
|
(for [[name records] groups]
|
|
|
|
(let [liability-lines (filter #(= (:type %) "Liability") records)]
|
|
|
|
(for [{:keys [category amount]} liability-lines]
|
|
|
|
{:account "Expenses:Payroll:Tax"
|
|
|
|
:amount amount
|
|
|
|
:currency "USD"
|
|
|
|
:meta (merge
|
|
|
|
(employee-entity-meta name)
|
|
|
|
;; TODO: Check lack of ":Tax:" with Rosanne.
|
|
|
|
{:payroll-type (str/replace (cat->acct category) "Tax:" "")})}))))
|
|
|
|
total-liabilities (->> liability-postings (map :amount) (reduce +))
|
|
|
|
asset-postings [{:account "Assets:FR:Check2721"
|
|
|
|
:amount (- total-liabilities)
|
|
|
|
:currency "USD"
|
|
|
|
:meta {:entity "Paychex"
|
|
|
|
:tax-implication "Tax-Payment"}}]]
|
|
|
|
[{:date date :desc (format "Monthly Payroll - %s - TAXES - Employer" period)
|
|
|
|
:meta {:program "Conservancy:Payroll"
|
|
|
|
:project "Conservancy"
|
|
|
|
:receipt receipt-no
|
|
|
|
:approval "Financial/Employment-Records/memo-re-board-approval-of-payroll.txt"}
|
|
|
|
:postings (concat liability-postings asset-postings)}]))
|
2024-02-20 03:19:23 +00:00
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn payroll-fees
|
|
|
|
"Return a payroll fees transaction."
|
|
|
|
[date period receipt-no invoice-no total-fees groups]
|
|
|
|
(let [employees (keys groups)
|
|
|
|
exact-fee-allocation (split-fee total-fees (count employees))
|
|
|
|
employee-fees (map vector employees exact-fee-allocation)
|
|
|
|
expense-postings (for [[name fee] employee-fees]
|
2024-02-21 07:08:49 +00:00
|
|
|
{:account "Expenses:Payroll:Fees"
|
|
|
|
:amount fee
|
|
|
|
:currency "USD"
|
2024-02-21 14:02:38 +00:00
|
|
|
:meta (employee-entity-meta name)})
|
|
|
|
asset-postings [{:account "Assets:FR:Check2721"
|
|
|
|
:amount (- total-fees)
|
|
|
|
:currency "USD"}]]
|
|
|
|
[{:date date :payee "Paychex" :desc (format "Monthly Payroll - %s - Fee" period)
|
|
|
|
:meta {:program "Conservancy:Payroll"
|
|
|
|
:project "Conservancy"
|
|
|
|
:receipt receipt-no
|
|
|
|
:invoice invoice-no
|
|
|
|
:approval "Financial/Employment-Records/memo-re-board-approval-of-payroll.txt"
|
|
|
|
:tax-implication "USA-Corporation"}
|
|
|
|
:postings (concat expense-postings asset-postings)}]))
|
2024-02-20 03:19:23 +00:00
|
|
|
|
2024-02-21 14:02:38 +00:00
|
|
|
(defn retirement
|
|
|
|
"Return a retirement transaction."
|
|
|
|
[date period receipt-no invoice-no groups]
|
|
|
|
(let [liability-postings (for [[name records] groups]
|
|
|
|
(let [total-retirement (->> records
|
|
|
|
(filter #(= (:type %) "Retirement"))
|
|
|
|
(map :amount)
|
|
|
|
(reduce +))]
|
|
|
|
{:account "Liabilities:Payable:Accounts",
|
|
|
|
:amount total-retirement,
|
|
|
|
:currency "USD",
|
|
|
|
:meta {:entity name}}))
|
|
|
|
total-liabilities (->> liability-postings (map :amount) (reduce +))
|
|
|
|
asset-postings [{:account "Assets:FR:Check1345"
|
|
|
|
:amount (- total-liabilities)
|
|
|
|
:currency "USD"}]]
|
|
|
|
[{:date date :desc (format "ASCENSUS TRUST RET PLAN - ACH DEBIT - Vanguard 403(b) - %s" period)
|
|
|
|
:meta {:program "Conservancy:Payroll"
|
|
|
|
:project "Conservancy"
|
|
|
|
:receipt receipt-no
|
|
|
|
:approval "Financial/Employment-Records/memo-re-board-approval-of-payroll.txt"
|
|
|
|
:tax-implication "Retirement-Pretax"
|
|
|
|
:invoice invoice-no}
|
|
|
|
:postings (concat liability-postings asset-postings)}]))
|