-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpaystub
More file actions
executable file
·121 lines (104 loc) · 3.69 KB
/
paystub
File metadata and controls
executable file
·121 lines (104 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash -e
# Get your latest paystub as PDF from Check's API.
usage() {
(
echo "usage: ${0##*/} [-h | --help | -n N | -t]"
echo "Get your latest paystub as PDF from Check's API."
echo
echo "options:"
(
echo " -h, --help@ show usage help"
echo " -n N@ generate the last N paysubs (default: 1)"
echo " -t@ show historical tax information"
) | column -ts@
) >&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
usage
fi
if [[ -z "$(command -v jq)" ]]; then
echo "error: jq not installed; please install jq" >&2
exit 1
fi
USER="$(whoami)"
RENDER_COUNT=1
SHOW_TAXES=0
while getopts "n:t" opt; do
case "$opt" in
n) RENDER_COUNT="$OPTARG" ;;
t) SHOW_TAXES=1 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
# In Keychain Access create a "New Password Item" with the following details.
# - name: "check_api_key_prod"
# - account: Your macOS user name
# - password: Your Check API key
CHECK_API_KEY_PROD="$(security find-generic-password -a "$USER" -s check_api_key_prod -w 2>/dev/null || true)"
# Do the same as above for your Check employee id.
CHECK_API_EMPLOYEE_ID_PROD="$(security find-generic-password -a "$USER" -s check_api_employee_id_prod -w 2>/dev/null || true)"
if [[ -z $CHECK_API_KEY_PROD ]]; then
echo "error: no password item found for check_api_key_prod in Keychain Access" >&2
exit 1
fi
if [[ -z $CHECK_API_EMPLOYEE_ID_PROD ]]; then
echo "error: no password item found for check_api_employee_id_prod in Keychain Access" >&2
exit 1
fi
OUTPUT="$(mktemp -t tmp.XXXXXXXXXX)"
trap 'rm -f $OUTPUT' EXIT
echo -n $'\e[1;34m>\e[0m\e[31m '
echo -n "fetching employee information... "
curl -sS --location "https://api.checkhq.com/employees/$CHECK_API_EMPLOYEE_ID_PROD/paystubs" \
--header "Authorization: Bearer $CHECK_API_KEY_PROD" \
--output "$OUTPUT"
echo "done"
echo -n $'\e[0m'
for ((i = 0; i < RENDER_COUNT; i++)); do
PAYDAY="$(jq -r ".results[$i].payday" <"$OUTPUT")"
PAYROLL_ID="$(jq -r ".results[$i].payroll" <"$OUTPUT")"
PAYSTUB="paystub-$PAYDAY.pdf"
echo -n $'\e[1;34m>\e[0m\e[31m '
echo -n "rendering $PAYSTUB (payroll: $PAYROLL_ID)... "
curl -sS --location \
"https://api.checkhq.com/employees/$CHECK_API_EMPLOYEE_ID_PROD/paystubs/$PAYROLL_ID" \
--header "Authorization: Bearer $CHECK_API_KEY_PROD" \
--header "Accept: application/pdf" \
--output "$PAYSTUB"
echo "done"
echo -n $'\e[0m'
done
if [[ $RENDER_COUNT == "1" ]]; then
open "$PAYSTUB"
fi
[[ $SHOW_TAXES == "1" ]] || exit 0
# show historical tax information including federal and state taxes
TAXES=("Federal Income Tax" "State Tax")
N_PAYSTUBS="$(jq -r '.results | length' <"$OUTPUT")"
(
# data rows
for ((i = 0; i < N_PAYSTUBS; i++)); do
PAYROLL="$(jq -r ".results[$i].payroll" <"$OUTPUT")"
PAYDAY="$(jq -r ".results[$i].payday" <"$OUTPUT")"
EARNINGS="$(jq -r ".results[$i].summary.earnings" <"$OUTPUT")"
echo -n "$PAYROLL|$PAYDAY|$EARNINGS"
for t in "${TAXES[@]}"; do
AMOUNT="$(jq -r ".results[$i].employee_taxes[] | select(.description | contains(\"$t\")) | .amount" <"$OUTPUT")"
echo -n "|$AMOUNT"
done
echo
done
# header row
echo -n "Payroll|Payday|Earnings"
for t in "${TAXES[@]}"; do
DESC="$(jq -r ".results[0].employee_taxes[] | select(.description | contains(\"$t\")) | .description" <"$OUTPUT")"
echo -n "|$DESC"
done
echo
) | column -t -s'|' | nl | sort -nr | cut -f 2-
if [[ "$(jq -r '.next' <"$OUTPUT")" != "null" ]]; then
echo "$(tput bold)warning: older paystubs are truncated$(tput sgr0)" >&2
exit 1
fi