Viewing File: /home/ubuntu/codegamaai-test/taxtrax_calculators/help_calc.py

import pandas as pd

df = pd.read_csv("apy_calculator.csv")
df_final = df[["age", "period", "1000_monthly", "2000_monthly", "3000_monthly", "4000_monthly", "5000_monthly"]]
print("Loading help_calc.py...")
print("APY Calculator loaded successfully!")
import math
import numpy as np
import numpy_financial as npf


# 1. SIP Calculator
def sip_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of SIP
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the monthly interest rate
    monthly_interest_rate = rate_of_interest / 12
    # Calculate the number of months
    months = time_period * 12
    # Calculate the maturity amount
    maturity_amount = principal * ((1 + monthly_interest_rate)**months - 1) * ((1 + monthly_interest_rate) / monthly_interest_rate)
    invested_amount = principal * months
    estimated_return = maturity_amount - invested_amount

    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)
    
    return maturity_amount, invested_amount, estimated_return

# 2. Lumpsum Calculator
def lumpsum_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of Lumpsum
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    maturity_amount = principal * ((1 + rate_of_interest)**time_period)
    invested_amount = principal
    estimated_return = maturity_amount - invested_amount

    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    return maturity_amount, invested_amount, estimated_return


# 3. SWP (Systematic Withdrawal Plan) Calculator

# SWP Helper Calc
def lum_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of SIP
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the monthly interest rate
    monthly_interest_rate = rate_of_interest / 12
    # Calculate the number of months
    months = time_period * 12
    # Calculate the maturity amount
    maturity_amount = principal * ((1 + monthly_interest_rate)**months - 1) * ((1 + monthly_interest_rate) / monthly_interest_rate)
    invested_amount = principal * months
    estimated_return = maturity_amount - invested_amount
    return maturity_amount, invested_amount, estimated_return

def swp_calculator(total_investment, withdrawl_per_month, rate_of_interest, time_period, compounding_frequency):
    """Calculate the maturity amount of SWP considering withdrawal per month, rate of interest, time period and compounding frequency"""

    total_withdrawl = withdrawl_per_month * time_period * 12
    total_months = time_period * 12
    closing_balance_prev = total_investment
    for month in range(1, total_months+1):
        starting_balance_curr = closing_balance_prev - withdrawl_per_month
        current_maturity_amount, current_invested_amount, current_estimated_return = lum_calculator(starting_balance_curr, rate_of_interest, 1/12)
        closing_balance_prev = current_maturity_amount
        # print(f"Month: {month}, Closing Balance: {closing_balance_prev}")

    final_value = closing_balance_prev

    # Change all values to nearest integer
    final_value = round(final_value)
    total_withdrawl = round(total_withdrawl)
    total_investment = round(total_investment)
    
    return total_investment, total_withdrawl, final_value

# 4. MF Calculator( Mutual Fund Investments)
def mf_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of Mutual Fund
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    maturity_amount = principal * ((1 + rate_of_interest)**time_period)
    invested_amount = principal
    estimated_return = maturity_amount - invested_amount

    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    return maturity_amount, invested_amount, estimated_return

# 5. SSY Calculator( Sukanya Samriddhi Yojana)
def ssy_calculator(yearly_investment, girl_age, start_period, ssy_interest_rate, compounding_frequency=1):
    
    investment_period = 15
    total_period = 21
    total_investment = yearly_investment * investment_period
    maturity_year = start_period + total_period
    expected_rate_of_return = ssy_interest_rate
    
    primary_maturity_amount = 0
    for i in range(0, investment_period):
        total_corpus_for_year = (primary_maturity_amount + yearly_investment) * ((1 + expected_rate_of_return / 100))
        primary_maturity_amount = total_corpus_for_year
        # print("end of year", i, "total_corpus_for_year", total_corpus_for_year)
    
    print("primary_maturity_amount", primary_maturity_amount)
    # Simple Interest on primary_matuirty_amount for 6 years
    for j in range(0, 6):
        primary_maturity_amount = primary_maturity_amount + (primary_maturity_amount * (expected_rate_of_return / 100))
        # print("end of year", j, "primary_maturity_amount", primary_maturity_amount)
    # secondary_maturity_amount = primary_maturity_amount + (primary_maturity_amount * (expected_rate_of_return / 100) * 6)
    total_interest = primary_maturity_amount - total_investment

    total_investment = round(total_investment)
    primary_maturity_amount = round(primary_maturity_amount)
    total_interest = round(total_interest)

    return total_investment, primary_maturity_amount, total_interest, maturity_year


# 6. PPF Calculator( Public Provident Fund)
def ppf_calculator(yearly_investment, time_period, ppf_interest_rate):
    """
    Calculate the maturity amount of Public Provident Fund 
    F = P [({(1+i) ^n}-1)/i]
    """
    rate_of_interest = ppf_interest_rate / 100

    # maturity_amount = yearly_investment * ((((1 + rate_of_interest)**time_period) - 1) / rate_of_interest) - yearly_investment

    maturity_amount = yearly_investment * ((1 + rate_of_interest)**time_period - 1) * ((1 + rate_of_interest) / rate_of_interest)

    invested_amount = yearly_investment * (time_period)
    estimated_return = maturity_amount - invested_amount

    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)
    
    
    return maturity_amount, invested_amount, estimated_return

# 7. EPF Calculator( Employee Provident Fund)
def epf_calculator(monthly_salary, age, employee_contribution_rate, rate_of_interest, annual_increase):
    """
    Calculate the maturity amount of Employee Provident Fund(Compound Interest(Monthly))
    """
    retirement_age = 58
    rate_of_interest = rate_of_interest / 100
    annual_increase = annual_increase / 100

    employer_contribution_rate = 3.67

    yearly_income = monthly_salary * 12
    total_contribution = 0
    epf_closing_balance = 0
    
    for i in range(age, retirement_age):
        if i == age:
            yearly_income = yearly_income
        else:
            yearly_income = yearly_income * (1 + annual_increase)
        
        employee_epf_contribution = yearly_income * (employee_contribution_rate / 100)
        employer_epf_contribution = yearly_income * (employer_contribution_rate / 100)
        year_epf_contribution = employee_epf_contribution + employer_epf_contribution
        epf_closing_balance += year_epf_contribution

        epf_closing_balance = epf_closing_balance * (1 + rate_of_interest)
    epf_closing_balance = round(epf_closing_balance)
        

    return epf_closing_balance
# 8. FD Calculator( Fixed Deposit)
def fd_calculator_compound(principal, rate_of_interest, time_period, compounding_frequency):
    """
    Calculate the maturity amount of Fixed Deposit
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    maturity_amount = principal * ((1 + rate_of_interest/compounding_frequency)**(time_period*compounding_frequency))
    # maturity_amount = principal + principal * ((1 + rate_of_interest )** time_period - 1)
    invested_amount = principal
    estimated_return = maturity_amount - invested_amount
    
    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    return maturity_amount, invested_amount, estimated_return

# 9. RD Calculator( Recurring Deposit)  
def rd_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of Recurring Deposit (Monthly Deposit)
    M = R[(1+i)^n-1]/(1-(1+i)^(-1/3) )

    M = Maturity Value
    R = Monthly Installment
    n = Number of quarters
    I = Rate of interest / 400
    """
    n = 4 * time_period
    i = rate_of_interest / 400
    maturity_amount = principal * (((1 + i)**n - 1) / (1 - (1 + i)**(-1/3)))
    invested_amount = principal * time_period * 12
    estimated_return = maturity_amount - invested_amount

    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    return maturity_amount, invested_amount, estimated_return

# 10. NPS Calculator( National Pension Scheme)
def nps_calculator(monthly_contribution, expected_rate_of_return, current_age, compounding_frequency):
    maturity_age = 60
    yearly_investment = monthly_contribution * 12
    time_period = maturity_age - current_age
    existing_investment = 0
    for i in range(0, time_period+1):
        total_corpus_for_year = (existing_investment + yearly_investment) * ((1 + expected_rate_of_return / 100))
        existing_investment = total_corpus_for_year
    total_investment = time_period * yearly_investment
    interest_earned = existing_investment - total_investment
    min_annuity_amount = existing_investment * 0.40

    # Change all values to nearest integer
    maturity_amount = round(existing_investment)
    invested_amount = round(total_investment)
    estimated_return = round(interest_earned)
    min_annuity_amount = round(min_annuity_amount)

    return maturity_amount, invested_amount, estimated_return, min_annuity_amount


# 11. HRA Calculator( House Rent Allowance)
import math
def hra_calculator(location, basic_salary, dearness_allownces, hra, hra_actual):
    
    if location == "Metro":
        hra_final = max(0,math.ceil(min(0.5 * (basic_salary+dearness_allownces), hra ,(hra_actual - 0.1*(basic_salary+dearness_allownces)))))
    else:
        hra_final = max(0,math.ceil(min(0.4*( basic_salary+dearness_allownces), hra , (hra_actual - 0.1*(basic_salary+dearness_allownces)))))

    return hra_final

# 12. Retirement Calculator

def annual_income_requirement_calc(current_expanses, inflation_rate, retirement_age, current_age):
    future_expanses = current_expanses * ((1 + inflation_rate) ** (retirement_age - current_age))
    yearly_req = future_expanses * 12
    yearly_req = round(yearly_req)
    return yearly_req



def retirement_calculator(current_expanses, rate_of_return, current_age, retirement_age, retirement_type):

    inflation_rate = 0.06
    rate_of_return = rate_of_return / 100
    # Constants
    MONTHS_IN_YEAR = 12
    retirement_years = 20
    annual_income_requirement = annual_income_requirement_calc(current_expanses, inflation_rate, retirement_age, current_age)
    print(f"The yearly requirement is: {annual_income_requirement} INR")

    # Inflation adjusted rate of return
    inflation_adjusted_return = (1 + rate_of_return) / (1 + inflation_rate) - 1
    monthly_inflation_adjusted_return = inflation_adjusted_return / MONTHS_IN_YEAR
    
    # Retirement period in months
    retirement_period_months = retirement_years * MONTHS_IN_YEAR
    
    # Monthly income requirement at retirement
    monthly_income_requirement = annual_income_requirement / MONTHS_IN_YEAR

    if retirement_type == "normal":
        pass
    elif retirement_type == "elevated":
        monthly_income_requirement = monthly_income_requirement * 1.25
    elif retirement_type == "lower":
        monthly_income_requirement = monthly_income_requirement * 0.75
    
    # Calculate retirement corpus using PV function
    retirement_corpus = npf.pv(rate=monthly_inflation_adjusted_return, nper=retirement_period_months, pmt=-monthly_income_requirement, fv=0, when='begin')
    
    # Years until retirement
    years_until_retirement = retirement_age - current_age
    
    # Calculate monthly savings required using PMT function
    monthly_savings_required = npf.pmt(rate=rate_of_return / MONTHS_IN_YEAR, nper=years_until_retirement * MONTHS_IN_YEAR, pv=0, fv=-retirement_corpus, when='begin')
    
    # Change all values to nearest integer
    retirement_corpus = round(retirement_corpus)
    monthly_savings_required = round(monthly_savings_required)

    return retirement_corpus, monthly_savings_required

# 13. EMI Calculator (For Home Loan, Car Loan, Personal Loan, Education Loan, etc.)
def emi_calculator(loan_amount, rate_of_interest, loan_tenure, loan_type):
    """
    Calculate the EMI of Loan
    EMI = [P x R x (1+R) ^N]/ [(1+R) ^ (N-1)], where –
    P is the principal amount
    R is the rate of interest
    N is the loan tenure
    """
    rate_of_interest = rate_of_interest / 100
    monthly_rate_of_interest = rate_of_interest / 12
    total_months = loan_tenure * 12

    monthly_emi = (loan_amount * monthly_rate_of_interest * (1 + monthly_rate_of_interest)**total_months) / ((1 + monthly_rate_of_interest)**(total_months) - 1)
    total_payment = monthly_emi * total_months
    total_interest = total_payment - loan_amount
    
    principal_amount = loan_amount

    # Change all values to nearest integer
    monthly_emi = round(monthly_emi)
    principal_amount = round(principal_amount)
    total_interest = round(total_interest)
    total_payment = round(total_payment)

    return monthly_emi, principal_amount, total_interest, total_payment

# 14. Simple Interest Calculator
def simple_interest_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the Simple Interest
    """
    simple_interest = (principal * rate_of_interest * time_period) / 100
    total_amount = principal + simple_interest

    # Change all values to nearest integer
    simple_interest = round(simple_interest)
    principal = round(principal)
    total_amount = round(total_amount)

    return simple_interest, principal, total_amount

# 16. Compound Interest Calculator
def compound_interest_calculator(principal, rate_of_interest, time_period, compounding_frequency):
    """
    Calculate the Compound Interest
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    total_amount = principal * ((1 + rate_of_interest/compounding_frequency)**(time_period*compounding_frequency))
    
    total_interest = total_amount - principal

    # Change all values to nearest integer
    total_amount = round(total_amount)
    principal = round(principal)
    total_interest = round(total_interest)

    return total_amount, principal, total_interest

# 17. National Savings Certificate (NSC) Calculator

def nsc_calculator(principal, rate_of_interest, time_period, compounding_frequency):
    """
    Calculate the maturity amount of National Savings Certificate
    """
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    # maturity_amount = principal * ((1 + rate_of_interest)**time_period)

    # Add compounding frequency in maturity amount
    maturity_amount = principal * ((1 + rate_of_interest/compounding_frequency)**(time_period*compounding_frequency))
    invested_amount = principal
    estimated_return = maturity_amount - invested_amount
    # Change all values to nearest integer
    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    return maturity_amount, invested_amount, estimated_return

# 18. Step up SIP Calculator

def yearly_sip_calculator(monthly_investment, rate_of_interest, time_period):
    """
    Calculate the maturity amount of SIP
    """
    compounding_frequency = 12
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the monthly interest rate
    monthly_interest_rate = rate_of_interest / 12
    # Calculate the number of months
    months = time_period * 12
    # Calculate the maturity amount
    maturity_amount = monthly_investment * ((1 + monthly_interest_rate)**months - 1) * ((1 + monthly_interest_rate) / monthly_interest_rate)

    # Add compounding frequency in maturity amount

    invested_amount = monthly_investment * months
    estimated_return = maturity_amount - invested_amount

    return maturity_amount, invested_amount, estimated_return


def lumsum_calculator(principal, rate_of_interest, time_period):
    """
    Calculate the maturity amount of Lumpsum
    """
    compounding_frequency = 12
    # Convert rate of interest to decimal
    rate_of_interest = rate_of_interest / 100
    # Calculate the maturity amount
    # maturity_amount = principal * ((1 + rate_of_interest)**time_period)
    maturity_amount = principal * ((1 + rate_of_interest/compounding_frequency)**(time_period*compounding_frequency))
    invested_amount = principal
    estimated_return = maturity_amount - invested_amount
    return maturity_amount, invested_amount, estimated_return


def step_up_sip_calculator(principal_monthly, rate_of_interest, time_period, step_up_percentage, step_up_frequency):
    """
    Calculate the maturity amount of Step Up SIP
    """
    maturity_amount = 0
    invested_amount = 0
    estimated_return = 0
    
    for i in range(1, time_period+1):
        
        # Reinvest the maturity amount of the previous year
        lumsum_maturity_amount, lumsum_invested_amount, lumsum_estimated_return = lumsum_calculator(maturity_amount, rate_of_interest, 1)
        yearly_maturity, yearly_investment, yearly_return = yearly_sip_calculator(principal_monthly, rate_of_interest, 1)
        maturity_amount += yearly_maturity
        maturity_amount += lumsum_estimated_return
        invested_amount += yearly_investment

        estimated_return += yearly_return
        estimated_return += lumsum_estimated_return
        principal_monthly = principal_monthly * (1 + step_up_percentage / 100)
        # print("principal_monthly_next_year", principal_monthly)

    maturity_amount = round(maturity_amount)
    invested_amount = round(invested_amount)
    estimated_return = round(estimated_return)

    
    return maturity_amount, invested_amount, estimated_return


# 19. Gratuity Calculator
def gratuity_calculator(basic_salary, dearness_allownces, gratuity_service_period):
    """
    Calculate the Gratuity Amount
    """
    gratuity_amount = (basic_salary + dearness_allownces) * gratuity_service_period * 15 / 26
    
    # Change all values to nearest integer
    gratuity_amount = round(gratuity_amount)
    return gratuity_amount

# 20. Atal Pension Yojana (APY) Calculator
def apy_calculator(joining_age, desired_monthly_pension, df_final=df_final):
    """
    Calculate the monthly contribution required for Atal Pension Yojana
    """
    column_name = str(desired_monthly_pension) + "_monthly"
    row = df_final[df_final["age"] == joining_age]
    monthly_contribution = row[column_name].values[0]
    # Change Data Type of Monthly Contribution to float
    monthly_contribution = float(monthly_contribution)

    invested_amount = monthly_contribution * 12 * (60 - joining_age)
    investment_duration = 60 - joining_age
    
    return monthly_contribution, invested_amount, investment_duration


# 21. CAGR (Compound Annual Growth Rate) Calculator
def cagr_calculator(present_value, future_value, time_period):
    """
    Calculate the Compound Annual Growth Rate
    """
    cagr = ((future_value / present_value)**(1/time_period)) - 1

    # Change all values to nearest integer
    cagr = round(cagr, 2)

    return cagr * 100

# 22. GST Calculator
def calculate_from_excluding_gst(amount_excluding_gst, gst_rate):
    gst_amount = (amount_excluding_gst * gst_rate) / 100
    amount_including_gst = amount_excluding_gst + gst_amount
    return gst_amount, amount_including_gst
 
def calculate_from_including_gst(amount_including_gst, gst_rate):
    amount_excluding_gst = amount_including_gst / (1 + gst_rate / 100)
    gst_amount = amount_including_gst - amount_excluding_gst
    return amount_excluding_gst, gst_amount
 
def calculate_gst_components(gst_amount):
    cgst = sgst_ugst = gst_amount / 2
    return cgst, sgst_ugst

def gst_calculator(amount, gst_rate, is_including_gst):
    if is_including_gst == "True":
        amount_excluding_gst, gst_amount = calculate_from_including_gst(amount, gst_rate)
        # In case of CGST and SGST/UTGST:
        cgst, sgst_ugst = calculate_gst_components(gst_amount)

        # Change all values to nearest integer
        amount_excluding_gst = round(amount_excluding_gst)
        gst_amount = round(gst_amount)
        cgst = round(cgst)
        sgst_ugst = round(sgst_ugst)

        return amount_excluding_gst, gst_amount, cgst, sgst_ugst
    else:
        gst_amount, amount_including_gst = calculate_from_excluding_gst(amount, gst_rate)
        # In case of CGST and SGST/UTGST:
        cgst, sgst_ugst = calculate_gst_components(gst_amount)

        # Change all values to nearest integer
        amount_including_gst = round(amount_including_gst)
        gst_amount = round(gst_amount)
        cgst = round(cgst)
        sgst_ugst = round(sgst_ugst)
        return amount_including_gst, gst_amount, cgst, sgst_ugst
    

# 23. Flat vs Reducing Interest Rate EMI Calculator
def calculate_flat_rate_interest(principal, annual_interest_rate, tenure_years):
    total_interest = (principal * annual_interest_rate * tenure_years) / 100
    total_amount = principal + total_interest
    monthly_emi = total_amount / (tenure_years * 12)
    return monthly_emi, total_interest, total_amount
 
def calculate_reducing_rate_emi(principal, annual_interest_rate, tenure_years):
    monthly_interest_rate = annual_interest_rate / (100 * 12)
    total_payments = tenure_years * 12
    emi = (principal * monthly_interest_rate * (1 + monthly_interest_rate) ** total_payments) / (((1 + monthly_interest_rate) ** total_payments) - 1)
    total_amount = emi * total_payments
    total_interest = total_amount - principal
    return emi, total_interest, total_amount

def flat_vs_reducing_rate(principal, annual_interest_rate, tenure_years):
    flat_emi, flat_total_interest, flat_total_amount = calculate_flat_rate_interest(principal, annual_interest_rate, tenure_years)
    reducing_emi, reducing_total_interest, reducing_total_amount = calculate_reducing_rate_emi(principal, annual_interest_rate, tenure_years)

    # Change all values to nearest integer
    flat_emi = round(flat_emi)
    flat_total_interest = round(flat_total_interest)
    flat_total_amount = round(flat_total_amount)
    reducing_emi = round(reducing_emi)
    reducing_total_interest = round(reducing_total_interest)
    reducing_total_amount = round(reducing_total_amount)

    return flat_emi, flat_total_interest, flat_total_amount, reducing_emi, reducing_total_interest, reducing_total_amount

# 24. Inflation Calculator
def inflation_calculator(current_cost, rate_of_inflation, time_period):
    """
    Calculate the Inflation Rate
    """
    future_cost = current_cost * ((1 + rate_of_inflation / 100)**time_period)
    cost_increase = future_cost - current_cost

    # Change all values to nearest integer
    cost_increase = round(cost_increase)
    future_cost = round(future_cost)

    return cost_increase, future_cost

# 25. Winning Prize, Lottery, Jackpot, etc.
def winning_prize_calculator(winning_amount, tax_deduction):
    """
    Calculate the Winning Prize after Tax Deduction
    """
    winning_prize = winning_amount * (1 - tax_deduction / 100)
    # Change all values to nearest integer
    winning_prize = round(winning_prize)
    return winning_prize

# 26. Bitcoin Calculator
def bitcoin_tax_calculator(purchase_amount, sale_amount, tax_rate):
    """
    Calculate the Bitcoin Amount in INR
    """
    bitcoin_inr = sale_amount - purchase_amount
    tax_amount = bitcoin_inr * tax_rate / 100
    bitcoin_inr = bitcoin_inr - tax_amount

    # Change all values to nearest integer
    bitcoin_inr = round(bitcoin_inr)
    tax_amount = round(tax_amount)

    return bitcoin_inr, tax_amount

# 27 Take Home Salary Calculator
def take_home_salary_calculator(cost_to_company, bonus, professional_tax, employer_pf, employee_pf, additional_deductions):
    """
    Calculate the Take Home Salary
    """
    gross_salary = cost_to_company - bonus

    yearly_employee_pf = employee_pf * 12
    yearly_employer_pf = employer_pf * 12
    yearly_professional_tax = professional_tax * 12
    
    total_deductions = yearly_employee_pf + yearly_professional_tax + yearly_employer_pf + additional_deductions
    nett_salary = gross_salary - total_deductions

    take_home_salary = nett_salary / 12
    final_deducations = total_deductions + bonus
    monthly_deducted_salary = final_deducations / 12

    # Change all values to nearest integer
    take_home_salary = round(take_home_salary)
    monthly_deducted_salary = round(monthly_deducted_salary)
    final_deducations = round(final_deducations)
    nett_salary = round(nett_salary)
    
    return take_home_salary, monthly_deducted_salary, final_deducations, nett_salary

# 28. TDS (Tax Deducted at Source) Calculator
def calculate_tax_old_regime(income):
    if income <= 250000:
        return 0
    elif income <= 500000:
        return (income - 250000) * 0.1
    elif income <= 1000000:
        return (income - 500000) * 0.2 + 25000
    else:
        return (income - 1000000) * 0.3 + 125000
 
def calculate_tax_new_regime(income):
    if income <= 300000:
        return 0
    elif income <= 600000:
        return (income - 300000) * 0.05
    elif income <= 900000:
        return (income - 600000) * 0.1 + 15000
    elif income <= 1200000:
        return (income - 900000) * 0.15 + 45000
    elif income <= 1500000:
        return (income - 1200000) * 0.2 + 90000
    else:
        return (income - 1500000) * 0.3 + 150000
 
def tds_calculator(section, amount, income=0, tax_regime='old', recipient_type='individual'):
    # Define TDS rates
    tds_rates = {
        '192A': 10, # Payment of salary
        '193': 10, # Premature withdrawal from EPF
        '194': 10, # Dividends Income
        '194A': 10, # Interest on securities
        '194B': 30, # Winnings from lotteries, crossword puzzles, etc.
        '194BB': 30, # Winnings from Horse Races
        '194C': {'Individuals/HUF': 1, 'Others': 2}, # Payment to contractors/sub-contractors
        '194D': 5, # Insurance commission
        '194H': 5, # Commission or brokerage
        '194I_A': 2, # Rent of plant and machinery
        '194I_B': 10, # Rent of land, building, furniture or fittings
        '194IA': 1, # Transfer of immovable property
        '194IB': 5, # Rent by individual or HUF
        '194IC': 10, # Payment under specified agreement
        '194J_professional': 2, # Professional fees
        '194J_technical': 2, # Other fees
        '194K': 10, # Income in respect of units
        '194I': {'Plant & Machinery': 2, 'Land or building or furniture or fitting': 10},
        '194J': {'Technical services': 2, 'Other services': 10},
        '194LA': 10, # Payment of compensation on acquisition of certain immovable property
        '194LBA': 10, # Business trust 
        '194LBB': 10, # Investment fund paying an income to a unit holder
        '194LBC': 25, # Income in respect of investment in securitization trust
        '194M': 5, # Commission, brokerage, etc.
        '194N': {'exceeds_20lakhs': 2, 'exceeds_1crore': 5} # Cash withdrawal
    }
 
    if section == '192':
        if tax_regime == 'old':
            return calculate_tax_old_regime(income)
        elif tax_regime == 'new':
            return calculate_tax_new_regime(income)
        else:
            print("Invalid tax regime specified.")
            return 0
    elif section in tds_rates:
        rate = tds_rates[section]
        if isinstance(rate, dict):
            # Placeholder for specific conditions, e.g., section 194C
            if section == '194N' and income > 0:
                if amount > 10000000:
                    rate = rate['exceeds_1crore']
                elif amount > 2000000:
                    rate = rate['exceeds_20lakhs']
                else:
                    return 0
            elif section == '194C':
                if recipient_type == 'others':
                    rate = rate['Others']
                else:
                    rate = rate['Individuals/HUF']

        tds_amount = amount * rate / 100
        return tds_amount
    else:
        print("Section not found in the TDS rates list.")
        return 0   


# 2. Brokerage Calculator
def brokerage_delivery(buy_price, sell_price, quantity, broker_charges):
    """
    Calculate the brokerage for delivery trades
    """
    turnover = buy_price * quantity + sell_price * quantity
    profit_and_loss = (sell_price - buy_price) * quantity

    brokerage = broker_charges
    stt = 0.001 * turnover
    stt = round(stt, 2)

    exchange_transaction_charges = 0.0000325 * (buy_price * quantity + sell_price * quantity)
    exchange_transaction_charges = round(exchange_transaction_charges, 2)

    sebi_charges = 0.000001 * (buy_price * quantity + sell_price * quantity)
    sebi_charges = round(sebi_charges, 2)

    gst = 0.18 * (brokerage + exchange_transaction_charges + sebi_charges)
    gst = round(gst, 2)

    stamp_duty = 0.00015 * buy_price * quantity
    stamp_duty = round(stamp_duty, 2)

    total_charges = brokerage + stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    non_brorkerage_charges = stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    total_profit_loss = profit_and_loss - total_charges

    # Apply rounding to the charges for 2 decimal places
    total_charges = round(total_charges, 2)
    non_brorkerage_charges = round(non_brorkerage_charges, 2)
    total_profit_loss = round(total_profit_loss, 2)
    
    return  turnover, profit_and_loss, total_charges, non_brorkerage_charges, total_profit_loss, brokerage, stt, exchange_transaction_charges, gst, sebi_charges, stamp_duty

def brokerage_intraday(buy_price, sell_price, quantity, broker_charges):
    """
    Calculate the brokerage for intraday trades
    """
    turnover = buy_price * quantity + sell_price * quantity
    profit_and_loss = (sell_price - buy_price) * quantity

    brokerage = broker_charges
    stt = 0.00025 * (sell_price * quantity)
    stt = round(stt)

    exchange_transaction_charges = 0.0000325 * (buy_price * quantity + sell_price * quantity)
    exchange_transaction_charges = round(exchange_transaction_charges, 2)

    gst = 0.18 * (brokerage + exchange_transaction_charges)
    gst = round(gst, 2)

    sebi_charges = 0.000001 * (buy_price * quantity + sell_price * quantity)
    sebi_charges = round(sebi_charges, 2)

    stamp_duty = 0.00002 * sell_price * quantity
    stamp_duty = round(stamp_duty, 2)

    total_charges = brokerage + stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    non_brorkerage_charges = stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    total_profit_loss = profit_and_loss - total_charges

    # Apply rounding to the charges for 2 decimal places
    total_charges = round(total_charges, 2)
    non_brorkerage_charges = round(non_brorkerage_charges, 2)
    total_profit_loss = round(total_profit_loss, 2)
    
    return  turnover, profit_and_loss, total_charges, non_brorkerage_charges, total_profit_loss, brokerage, stt, exchange_transaction_charges, gst, sebi_charges, stamp_duty

def future_trading(buy_price, sell_price, quantity, broker_charges):
    
    turnover = buy_price * quantity + sell_price * quantity
    profit_and_loss = (sell_price - buy_price) * quantity

    brokerage = broker_charges
    stt = 0.000125 * (sell_price * quantity)
    stt = round(stt)

    exchange_transaction_charges = 0.000019 * (buy_price * quantity + sell_price * quantity)
    exchange_transaction_charges = round(exchange_transaction_charges, 2)

    gst = 0.18 * (brokerage + exchange_transaction_charges)
    gst = round(gst, 2)

    sebi_charges = 0.000001 * (buy_price * quantity + sell_price * quantity)
    sebi_charges = round(sebi_charges, 2)

    stamp_duty = 0.00002 * buy_price * quantity
    stamp_duty = round(stamp_duty, 2)

    total_charges = brokerage + stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    non_brorkerage_charges = stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    total_profit_loss = profit_and_loss - total_charges

    # Apply rounding to the charges for 2 decimal places
    total_charges = round(total_charges, 2)
    non_brorkerage_charges = round(non_brorkerage_charges, 2)
    total_profit_loss = round(total_profit_loss, 2)
    
    return  turnover, profit_and_loss, total_charges, non_brorkerage_charges, total_profit_loss, brokerage, stt, exchange_transaction_charges, gst, sebi_charges, stamp_duty


def option_trading(buy_price, sell_price, quantity, broker_charges):
    
    turnover = buy_price * quantity + sell_price * quantity
    profit_and_loss = (sell_price - buy_price) * quantity

    brokerage = broker_charges
    stt = 0.000625 * (sell_price * quantity)
    stt = round(stt)

    exchange_transaction_charges = 0.0005 * (buy_price * quantity + sell_price * quantity)
    exchange_transaction_charges = round(exchange_transaction_charges, 2)

    gst = 0.18 * (brokerage + exchange_transaction_charges)
    gst = round(gst, 2)

    sebi_charges = 0.000001 * (buy_price * quantity + sell_price * quantity)
    sebi_charges = round(sebi_charges, 2)

    stamp_duty = 0.00003 * buy_price * quantity
    stamp_duty = round(stamp_duty, 2)

    total_charges = brokerage + stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    non_brorkerage_charges = stt + exchange_transaction_charges + gst + sebi_charges + stamp_duty
    total_profit_loss = profit_and_loss - total_charges

    # Apply rounding to the charges for 2 decimal places
    total_charges = round(total_charges, 2)
    non_brorkerage_charges = round(non_brorkerage_charges, 2)
    total_profit_loss = round(total_profit_loss, 2)
    
    return  turnover, profit_and_loss, total_charges, non_brorkerage_charges, total_profit_loss, brokerage, stt, exchange_transaction_charges, gst, sebi_charges, stamp_duty
Back to Directory File Manager