-
-
Save brownnrl/27b04ac4c40d5b3fe190 to your computer and use it in GitHub Desktop.
MoneyGuru Salary Import Plugin
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
# See youtube video at http://youtu.be/Bhv6yL40quA | |
from core.plugin import ImportActionPlugin | |
from core.model.account import AccountType | |
from core.model.amount import Amount | |
from core.model.transaction import Split | |
class SalaryFixerPlugin(ImportActionPlugin): | |
NAME = "Salary Fixing Import Plugin" | |
ACTION_NAME = "Salary fiximafication" | |
def perform_action(self, import_document, transactions, panes, selected_rows=None): | |
gross_income = import_document.accounts.find('gross income', AccountType.Income) | |
employer_matching = import_document.accounts.find('employer matching', AccountType.Income) | |
federal_tax = import_document.accounts.find('federal tax', AccountType.Expense) | |
state_tax = import_document.accounts.find('state tax', AccountType.Expense) | |
retirement_fund = import_document.accounts.find('retirement', AccountType.Asset) | |
health_fund = import_document.accounts.find('health fund', AccountType.Asset) | |
fed_tax_rate = 0.3 | |
state_tax_rate = 0.07 | |
retirement_percentage = 0.05 | |
health_fund_withdraw = 250 | |
for transaction in transactions: | |
if 'salary' in transaction.description.lower() and len(transaction.splits) == 2: | |
txn_copy = transaction.replicate() | |
froms, tos = txn_copy.splitted_splits() | |
salary = froms[0] | |
net = tos[0] | |
if salary.amount == 0: | |
continue | |
amount_value = abs(salary.amount.value) | |
amount_currency = salary.amount.currency | |
after_benefits_value = 1/(1 - fed_tax_rate - state_tax_rate) * amount_value | |
federal_split = Split(txn_copy, | |
federal_tax, | |
Amount(fed_tax_rate * after_benefits_value, amount_currency)) | |
state_split = Split(txn_copy, | |
state_tax, | |
Amount(state_tax_rate * after_benefits_value, amount_currency)) | |
gross_pay = 1/(1-retirement_percentage) * (after_benefits_value + health_fund_withdraw) | |
retirement_split = Split(txn_copy, | |
retirement_fund, | |
Amount(retirement_percentage * gross_pay, amount_currency)) | |
matching_income_split = Split(txn_copy, | |
employer_matching, | |
Amount(-retirement_split.amount.value, amount_currency)) | |
retirement_matching_split = Split(txn_copy, | |
retirement_fund, | |
Amount(retirement_split.amount.value, amount_currency)) | |
health_split = Split(txn_copy, | |
health_fund, | |
Amount(health_fund_withdraw, amount_currency)) | |
salary_split = Split(txn_copy, | |
salary.account if salary.account else gross_income, | |
Amount(-gross_pay, amount_currency)) | |
net.amount = Amount(net.amount.value, net.amount.currency) | |
txn_copy.set_splits([salary_split, | |
retirement_split, | |
matching_income_split, | |
retirement_matching_split, | |
health_split, | |
federal_split, | |
state_split, | |
net]) | |
import_document.change_transaction(transaction, txn_copy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment