Created
April 19, 2020 13:22
-
-
Save spanthetree/c3bd5ee694ae766b80fc3ad3656e70eb to your computer and use it in GitHub Desktop.
parse paystubs and optionally convert tax/gross pay to another currency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import os | |
import argparse | |
import json | |
from exchangeratesapi import Api | |
def convert_pay(src_list): | |
date = '' | |
target_list = [] | |
for item in src_list: | |
for k,v in item.items(): | |
date = k | |
target_dict = {date: {}} | |
target_gross = v['grossAmount'] * v['dailyRate'] | |
target_tax = v['fedTax'] * v['dailyRate'] | |
target_dict[date]['grossAmount'] = round(target_gross, 3) | |
target_dict[date]['fedTax'] = round(target_tax, 3) | |
target_dict[date]['dailyRate'] = v['dailyRate'] | |
target_list.append(target_dict) | |
return target_list | |
def get_pay(year, file_list, args, api): | |
date = '' | |
src_list = [] | |
for f in file_list: | |
path = year + '/' + f | |
with open(path) as json_file: | |
data = json.load(json_file) | |
statement = data['payStatement'] | |
date = statement['payDate'] | |
src_dict = {date: {}} | |
src_dict[date]['netAmount'] = statement['netPayAmount']['amountValue'] | |
src_dict[date]['grossAmount'] = statement['grossPayAmount']['amountValue'] | |
dailyRate = api.get_rate('USD', target=args.target, start_date=date) | |
# dailyRate = 107.999 | |
src_dict[date]['dailyRate'] = round(dailyRate, 3) | |
for item in statement['deductionCategoryDetails']: | |
if "Taxes" in item['deductionCategoryCodeName']: | |
src_dict[date]['fedTax'] = item['deductionCategoryTotalPeriodAmount']['amountValue'] * -1 | |
src_list.append(src_dict) | |
return src_list | |
def get_file_list(year): | |
file_list = [] | |
for root, dirs, files in os.walk(year): | |
for f in files: | |
if f.endswith("json"): | |
file_list.append(f) | |
return file_list | |
def main(): | |
parser = argparse.ArgumentParser(description="parse json paystubs") | |
parser.add_argument('-y', '--year', help='Year to parse') | |
parser.add_argument('-s', '--source', help='Source Currency') | |
parser.add_argument('-t', '--target', help='Target Currency') | |
args = parser.parse_args() | |
api = Api() | |
year = args.year | |
file_list = get_file_list(year) | |
src_list = get_pay(year, file_list, args, api) | |
target_pay = convert_pay(src_list) | |
if args.target == args.source: | |
for item in src_list: | |
print(item) | |
else: | |
for item in target_pay: | |
print(item) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment