File size: 10,381 Bytes
95bad29
 
 
 
 
b398e78
 
 
 
 
 
95bad29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b398e78
 
 
 
 
 
 
 
 
 
95bad29
b398e78
9490fc6
95bad29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b398e78
95bad29
 
 
 
 
b398e78
95bad29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b398e78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9490fc6
b398e78
 
 
 
 
 
 
 
 
 
95bad29
 
b398e78
95bad29
 
 
9490fc6
95bad29
b398e78
 
95bad29
 
 
 
 
 
 
 
 
 
 
9490fc6
95bad29
b398e78
95bad29
 
 
 
 
 
 
 
 
 
 
 
 
9490fc6
95bad29
b398e78
95bad29
 
 
 
 
 
 
 
 
 
 
 
9490fc6
95bad29
b398e78
95bad29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b398e78
 
 
 
95bad29
 
 
 
 
 
 
 
 
b398e78
 
 
 
95bad29
 
 
 
 
 
 
 
 
 
 
 
b398e78
 
 
 
95bad29
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from constants import Styles, Constants
import streamlit as st


class Functions:
    @staticmethod
    def calculated_current_salary_after_tax(current_salary, tax_brackets):
        return current_salary - Functions.calculate_monthly_tax(
            current_salary, tax_brackets
        )

    @staticmethod
    def calculate_monthly_tax(monthly_income, tax_brackets):
        annual_income = monthly_income * 12
        total_tax = 0
        remaining_income = annual_income
        for lower, upper, rate in tax_brackets:
            if remaining_income <= 0:
                break
            taxable_amount = min(remaining_income, upper - lower)
            tax = taxable_amount * rate
            total_tax += tax
            remaining_income -= taxable_amount
        monthly_tax = total_tax / 12
        return round(monthly_tax, 2)

    @staticmethod
    def calculate_net_salary(gross_salary, tax_brackets):
        return gross_salary - Functions.calculate_monthly_tax(
            gross_salary, tax_brackets
        )

    @staticmethod
    def calculated_initial_desired_net(
        current_salary, desired_increment, daily_cost_of_travel, physical_days_per_week
    ):
        return (current_salary + current_salary * desired_increment) + (
            daily_cost_of_travel * physical_days_per_week * 4.5
        )

    @staticmethod
    def calculate_additional_amount(initial_desired_net, tax_brackets):
        gross_salary = initial_desired_net
        max_iterations = 100
        for _ in range(max_iterations):
            net_salary = Functions.calculate_net_salary(gross_salary, tax_brackets)
            if abs(net_salary - initial_desired_net) < 0.01:
                break
            gross_salary += initial_desired_net - net_salary
        additional_amount = gross_salary - initial_desired_net

        return {
            "initial_desired_net": round(initial_desired_net, 2),
            "gross_salary_needed": round(gross_salary, 2),
            "additional_amount": round(additional_amount, 2),
            "tax": round(
                Functions.calculate_monthly_tax(gross_salary, tax_brackets), 2
            ),
            "final_net_salary": round(
                Functions.calculate_net_salary(gross_salary, tax_brackets), 2
            ),
        }


import pandas as pd


class StreamlitFunctions:
    @staticmethod
    def update_initial_salary_parameter():
        # if (
        #     st.session_state.user_initial_desired_net_state
        #     < st.session_state.current_salary
        # ):
        #     st.session_state.user_initial_desired_net = (
        #         st.session_state.current_salary
        #         + st.session_state.user_initial_desired_net_state
        #     )
        # else:
        st.session_state.user_initial_desired_net = (
            st.session_state.user_initial_desired_net_state
        )
        st.session_state.type_to_calculate = "desired_salary"

    @staticmethod
    def reset_initial_salary_parameter():
        def reset_values():
            st.session_state.user_initial_desired_net = Constants.DEFAULT_INITIAL_NET
            st.session_state.valid_input = True

        st.button(
            "Reset Final Desired Net Salary Value",
            on_click=reset_values,
            use_container_width=True,
        )

    @staticmethod
    def initial_salary_parameter():
        st.header("Final Desired Net Salary")
        if st.session_state.user_initial_desired_net >= st.session_state.current_salary:
            minimum_value = st.session_state.current_salary
        else:
            minimum_value = 0.0
        st.number_input(
            "Final Desired Net Salary (PKR)",
            min_value=minimum_value,
            value=st.session_state.user_initial_desired_net,
            step=1000.0,
            key="user_initial_desired_net_state",
            on_change=StreamlitFunctions.update_initial_salary_parameter,
        )

    @staticmethod
    def reset_tax_brackets():
        def reset_values():
            st.session_state.tax_brackets = Constants.DEFAULT_TAX_BRACKETS

        st.button(
            "Reset Tax Brackets",
            use_container_width=True,
            on_click=reset_values,
        )

    @staticmethod
    def reset_salary_parameters():
        def reset_values():
            st.session_state.current_salary = Constants.DEFAULT_CURRENT_SALARY
            st.session_state.desired_increment_percentage = (
                Constants.DEFAULT_INCREMENT_PERCENTAGE
            )
            st.session_state.daily_cost_of_travel = Constants.DEFAULT_DAILY_TRAVEL_COST
            st.session_state.physical_days_per_week = Constants.DEFAULT_PHYSICAL_DAYS

        st.button(
            "Reset Salary Parameters Values",
            use_container_width=True,
            on_click=reset_values,
        )

    @staticmethod
    def reset_tax_on_current_salary():
        def reset_values():
            st.session_state.tax_on_current_salary = (
                Constants.DEFAULT_CURRENT_SALARY_WITH_TAX
            )

        st.button(
            "Reset Current Salary",
            use_container_width=True,
            on_click=reset_values,
        )

    @staticmethod
    def print_tax_on_current_salary():
        st.header("Tax on Current Salary")

        def update_tax_on_current_salary_parameter():
            st.session_state.tax_on_current_salary = (
                st.session_state.tax_on_current_salary_state
            )
            st.session_state.type_to_calculate = "tax_on_current_salary"

        st.number_input(
            "Current monthly Salary (PKR)",
            min_value=0.0,
            step=1000.0,
            value=st.session_state.tax_on_current_salary,
            key="tax_on_current_salary_state",
            on_change=update_tax_on_current_salary_parameter,
        )

    @staticmethod
    def print_salary_parameters():
        st.header("Salary Parameters")

        def update_current_salary_parameter():
            st.session_state.current_salary = st.session_state.current_salary_state
            st.session_state.type_to_calculate = "salary_parameters"

        st.number_input(
            "Current monthly salary after Tax (PKR)",
            min_value=0.0,
            step=1000.0,
            value=st.session_state.current_salary,
            key="current_salary_state",
            on_change=update_current_salary_parameter,
        )

        def update_desired_increment_percentage_parameter():
            st.session_state.desired_increment_percentage = (
                st.session_state.desired_increment_percentage_state
            )
            st.session_state.type_to_calculate = "salary_parameters"

        st.number_input(
            "Desired salary increment (as a decimal)",
            min_value=0.0,
            step=0.05,
            value=st.session_state.desired_increment_percentage,
            format="%.2f",
            key="desired_increment_percentage_state",
            on_change=update_desired_increment_percentage_parameter,
        )

        def update_daily_cost_of_travel_parameter():
            st.session_state.daily_cost_of_travel = (
                st.session_state.daily_cost_of_travel_state
            )
            st.session_state.type_to_calculate = "salary_parameters"

        st.number_input(
            "Daily cost of travel (PKR)",
            min_value=0,
            step=100,
            value=st.session_state.daily_cost_of_travel,
            key="daily_cost_of_travel_state",
            on_change=update_daily_cost_of_travel_parameter,
        )

        def update_physical_days_per_week_parameter():
            st.session_state.physical_days_per_week = (
                st.session_state.physical_days_per_week_state
            )
            st.session_state.type_to_calculate = "salary_parameters"

        st.number_input(
            "Number of On-Site days per week",
            min_value=0,
            max_value=7,
            step=1,
            value=st.session_state.physical_days_per_week,
            key="physical_days_per_week_state",
            on_change=update_physical_days_per_week_parameter,
        )

    @staticmethod
    def print_tax_brackets():
        st.header("Tax Brackets")
        tax_brackets_df = pd.DataFrame(
            st.session_state.tax_brackets,
            columns=["Lower Limit", "Upper Limit", "Tax Rate"],
        )

        edited_tax_brackets = st.data_editor(
            tax_brackets_df, num_rows="dynamic", use_container_width=True
        )
        st.session_state.tax_brackets = list(
            edited_tax_brackets.itertuples(index=False, name=None)
        )

    @staticmethod
    def initialize_session_values():
        st.title("Net Salary Calculator")
        if "tax_brackets" not in st.session_state:
            st.session_state.tax_brackets = Constants.DEFAULT_TAX_BRACKETS
        if "tax_on_current_salary" not in st.session_state:
            st.session_state.tax_on_current_salary = (
                Constants.DEFAULT_CURRENT_SALARY_WITH_TAX
            )
        if "current_salary" not in st.session_state:
            st.session_state.current_salary = Constants.DEFAULT_CURRENT_SALARY
        if "desired_increment_percentage" not in st.session_state:
            st.session_state.desired_increment_percentage = (
                Constants.DEFAULT_INCREMENT_PERCENTAGE
            )
        if "daily_cost_of_travel" not in st.session_state:
            st.session_state.daily_cost_of_travel = Constants.DEFAULT_DAILY_TRAVEL_COST
        if "physical_days_per_week" not in st.session_state:
            st.session_state.physical_days_per_week = Constants.DEFAULT_PHYSICAL_DAYS
        if "user_initial_desired_net" not in st.session_state:
            st.session_state.user_initial_desired_net = Constants.DEFAULT_INITIAL_NET
            
        if "type_to_calculate" not in st.session_state:
            st.session_state.type_to_calculate = None
        
    @staticmethod
    def custom_metric(label, value):
        st.markdown(Styles.METRIC_STYLE, unsafe_allow_html=True)
        st.markdown(
            f"""
        <div class="metric-container">
            <div class="metric-label">{label}</div>
            <div class="metric-value">PKR {value:,.2f}</div>
        </div>
        """,
            unsafe_allow_html=True,
        )