Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tax Bracket calculator #492

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ The initial price of the product or service. |
| | | - `operating_expenses` (int): The amount of operating expenses. |
| | | - `interest_expense` (int): The cost incurred by an entity for borrowed funds. |
|-------------------------------|----------------------------------------|---------------------------------------------------------|
| POST /tax_bracket_calculator | Calculate Tax Bracket Calculator | - `income` (float): The total income earned by the individual. |
| | | - `filing_status` (String): The tax filing status of the individual.|
|-------------------------------|----------------------------------------|---------------------------------------------------------|
| POST /margin_of_safety | Calculate margin of safety | - `current_sales` (float): The amount of current sales. |
| | | - `break_even_point` (float): The break_even_point amount. |
|--------------------------- ---|----------------------------------------|---------------------------------------------------------|
|--------------------------- ---|----------------------------------------|---------------------------------------------------------|
17 changes: 17 additions & 0 deletions ENDPOINTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,23 @@ Sample Output
"Interest Coverage Ratio": "3.75%",
}
```

**POST** `/tax_bracket_calculator`

- Request body : `{
"income": 50000,
"filing_status": single,
}`
- Sample output

```py
{
"Tag": "Tax Bracket Calculator"
"applicable_tax_bracket": 12.00%,
"tax_liability": 6000.00,
}
```

**POST** `/margin_of_safety`

- Request body : `{
Expand Down
32 changes: 31 additions & 1 deletion helpers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,8 +2077,38 @@ def interest_coverage_ratio(revenue:float, cost_of_goods_services:float, operati
ratio = EBIT / interest_expense
return ratio


# Function to Calculate Tax Bracket Calculator

def tax_bracket_calculator(income:float, filing_status:str):
tax_brackets = {
'single': {0: 0.10, 9875: 0.12, 40125: 0.22, 85525: 0.24, 163300: 0.32, 207350: 0.35, 518400: 0.37},
'married_joint': {0: 0.10, 19750: 0.12, 80250: 0.22, 171050: 0.24, 326600: 0.32, 414700: 0.35, 622050: 0.37},
'head_of_household': {0: 0.10, 14100: 0.12, 53700: 0.22, 85500: 0.24, 163300: 0.32, 207350: 0.35, 518400: 0.37}
}

if filing_status not in tax_brackets:
raise ValueError("Invalid filing status.")

applicable_brackets = tax_brackets[filing_status]
tax_liability = 0
remaining_income = income

for bracket, tax_rate in applicable_brackets.items():
if remaining_income <= bracket:
tax_liability += remaining_income * tax_rate
break
else:
taxable_income_in_bracket = bracket - max(0, income - remaining_income)
tax_liability += taxable_income_in_bracket * tax_rate
remaining_income -= taxable_income_in_bracket

tax_ratio = tax_liability / income

return {"Tax Ratio": "{:.2%}".format(tax_ratio)}

# Function to Calculate Margin of Safety

def margin_of_safety(current_sales:float, break_even_point: float):
margin = ((current_sales - break_even_point) / current_sales) * 100
return margin
return margin
14 changes: 12 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@
from tasks.cash_conversion_cycle import cash_conversion_cycle_task
from tasks.financialAssestRatio import financial_assest_ratio
from tasks.PolicyPremium import calculate_policy_premium
from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity, PolicyPremium, AveragePaymentPeriod, ModifiedInternalRateOfReturn, SavingGoal, InterestCoverageRatio, MarginOfSafety
from validators.request_validators import SimpleInterestRateRequest, calculatePension, compoundInterest, futureSip, paybackPeriod, capmRequest, DebtServiceCoverageRatio, futureValueOfAnnuity, futureValueOfAnnuityDue, ProfitPercentage, LossPercentage, DefensiveIntervalRatio, CashConversionCycle, RateofReturn, financialAssestRatio, PriceElasticity, PolicyPremium, AveragePaymentPeriod, ModifiedInternalRateOfReturn, SavingGoal, InterestCoverageRatio, MarginOfSafety, TaxBracketCalculator
from tasks.financialAssestRatio import financial_assest_ratio
from tasks.PriceElasticity import calculate_price_elasticity
from tasks.average_payment_period import average_payment_period_task
from tasks.Saving_Goal import saving_goal
from tasks.modified_internal_rate_of_return import calculate_modified_internal_rate_of_return_task
from tasks.interest_coverage_ratio import interest_coverage_ratio_task
from tasks.tax_bracket_calculator import tax_bracket_calculator
from tasks.margin_of_safety import margin_of_safety_task


# Creating the app
app = FastAPI(
title="FinTech API",
Expand Down Expand Up @@ -2014,6 +2014,16 @@ def interest_coverage_ratio(request: InterestCoverageRatio):
return interest_coverage_ratio_task(request.revenue, request.cost_of_goods_services,
request.operating_expenses, request.interest_expense)

# Endpoint to calculate Tax Bracket Calculator

@app.post(
"/tax_bracket_calculator",
tags=["tax_bracket_calculator"],
description="Calculates Tax Bracket Calculator",
)
def tax_bracket_calculator(request: TaxBracketCalculator):
return interest_coverage_ratio_task(request.income, request.filing_status)

# Endpoint to calculate Margin of Safety

@app.post(
Expand Down
48 changes: 48 additions & 0 deletions tasks/tax_bracket_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from helpers import functions
from fastapi import HTTPException, status

def tax_bracket_calculator(income: float, filing_status: str,):
try:
"""
Calculate the applicable tax bracket and tax liability based on a user's income and tax filing status (United States, 2021 tax brackets for single filers).

Parameters:
income (float): The user's total income.
filing_status (str): The user's tax filing status ('single', 'married_joint', 'married_separate', 'head_of_household').

Returns:
dict: A dictionary containing the applicable tax bracket and tax liability.
- 'Tax Bracket': The user's applicable tax bracket as a percentage.
- 'Tax Liability': The calculated tax liability amount.
"""
# Tax brackets and their corresponding tax rates (United States, 2021 tax brackets for single filers)
tax_brackets = {
0: 0.10,
9875: 0.12,
40125: 0.22,
85525: 0.24,
163300: 0.32,
207350: 0.35,
518400: 0.37,
}

# Find the applicable tax bracket and calculate the tax liability
tax_liability = 0
remaining_income = income

for bracket, tax_rate in tax_brackets.items():
if remaining_income <= bracket:
tax_liability += remaining_income * tax_rate
break
else:
taxable_income_in_bracket = bracket - max(0, income - remaining_income)
tax_liability += taxable_income_in_bracket * tax_rate
remaining_income -= taxable_income_in_bracket

return {
"Tag": "Accrued Interest",
'Tax Bracket': "{:.2%}".format(tax_rate),
'Tax Liability': tax_liability,
}
except:
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
4 changes: 4 additions & 0 deletions validators/request_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,10 @@ class InterestCoverageRatio(BaseModel):
operating_expenses:float
interest_expense:float

class TaxBracketCalculator(BaseModel):
income:float
filing_status:str

class MarginOfSafety(BaseModel):
current_sales:float
break_even_point: float