File size: 11,274 Bytes
740b75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01d5640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b75a
01d5640
 
740b75a
01d5640
 
740b75a
01d5640
 
 
740b75a
01d5640
740b75a
01d5640
 
 
740b75a
01d5640
740b75a
01d5640
740b75a
01d5640
 
 
 
 
740b75a
01d5640
 
 
 
 
 
 
 
 
 
 
 
740b75a
01d5640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b75a
 
01d5640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b75a
 
01d5640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b75a
 
 
01d5640
740b75a
 
 
01d5640
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from datetime import datetime

from utilities import (
    initialize_session_state, save_user_data, generate_rule_id, generate_plan_id,
    display_saved_records, load_theme_config, store_and_fetch_company_info,
    get_feature_user_data, default_button_click, custom_button_click, uploader,
    render_selectboxes, slab_data, insert_into_bq
)
from html_templates import (
    app_title, plan_info_header, button_styles, rule_info_header, 
    expected_billing_header, final_billing_header, side_logo
)
from placeholders import (
    fee_type_options, fee_nature_options, variable_options, chargeable_on_options,
    plan_validity_options, payment_method_options, fee_reversal_options, threshold_options
)
from mapping import new_mapping


initialize_session_state()
theme_settings = load_theme_config()
user_input_1 = st.sidebar.number_input("Enter Commercial value:")
user_input_2 = float(user_input_1)
st.session_state['user_input_3'] = "{:.2f}".format(user_input_2)

plan_name = st.sidebar.text_input("Plan Name", help="Enter the name of the plan")
plan_description = st.sidebar.text_area("Plan Description", max_chars=250, help="Describe the plan")
plan_start_date = st.sidebar.date_input("Plan Start Date", value=datetime.now(), help="Select the start date of the plan")


# Initialize session state variables if they don't exist
if 'new_mapping' not in st.session_state:
    st.session_state['new_mapping'] = {}
if 'file_data' not in st.session_state:
    st.session_state['file_data'] = None
if 'mapping_mode' not in st.session_state:
    st.session_state['mapping_mode'] = None  # None, 'default', 'custom'
if 'button_clicked' not in st.session_state:
    st.session_state['button_clicked'] = None

# Function to handle custom button click
def custom_button_click():
    st.session_state.button_clicked = 'custom'

# Function to handle default button click
def default_button_click():
    st.session_state.button_clicked = 'default'

# Display buttons without immediate execution on click
col1, col2 = st.sidebar.columns([0.25, 0.45])
with col1:
    default_clicked = st.button("Default mapping")
with col2:
    custom_clicked = st.button("Custom mapping")

# Determine action based on button clicked
if default_clicked:
    default_button_click()
elif custom_clicked:
    custom_button_click()

st.markdown(button_styles, unsafe_allow_html=True)

# Default button mapping
if st.session_state.button_clicked == 'default':
    st.session_state['new_mapping'] = new_mapping
    st.session_state['mapping_mode'] = 'default'
    st.sidebar.markdown(rule_info_header, unsafe_allow_html=True)

# Handle Custom Mapping
elif st.session_state.button_clicked == 'custom':
    st.markdown(button_styles, unsafe_allow_html=True)
    uploaded_file = st.sidebar.file_uploader("Upload a file:", type=['csv', 'xlsx'], key='file_uploader')
    st.sidebar.markdown(rule_info_header, unsafe_allow_html=True)

    if uploaded_file is not None:
            uploader(uploaded_file)

    
if st.session_state.get('mapping_mode'):
    render_selectboxes(st.session_state, st.session_state.get('new_mapping'))

    selected_variable_type = st.session_state['selected_variable_type']

    usage_1 = st.number_input(f"Usage limit for {selected_variable_type}", min_value=0.0, help="Enter the usage limit")
    usage_2 = float(usage_1)
    usage_3 = "{:.2f}".format(usage_2)

    expected_billing = user_input_1

    st.markdown(expected_billing_header, unsafe_allow_html= True)

    selected_plan_validity = st.selectbox("Plan Validity", [""] + plan_validity_options, help="Plan validity defines the periodicity of calculation for the above billing", key="plan_vd_2")
    selected_payment_method = st.selectbox("Payment Method", [""] + payment_method_options, help="Payment method defines the mode of payment of the plan", key="py_vd_2")
    
    initial_net_billing = 0
    user_input_3 = st.session_state['user_input_3']

    if selected_plan_validity == 'One time':
        final_billing = 1 * expected_billing
    elif selected_plan_validity == 'Monthly':
        final_billing = 1 * expected_billing
    elif selected_plan_validity == 'Quarterly':
        final_billing = 3 * expected_billing
    elif selected_plan_validity == 'Bi-Annually':
        final_billing = 6 * expected_billing
    elif selected_plan_validity == 'Annually':
        final_billing = 12 * expected_billing
    else:
        final_billing = initial_net_billing


    st.markdown(final_billing_header, unsafe_allow_html= True)

    st.markdown(button_styles, unsafe_allow_html=True)

    rule_id = None
    plan_id = None
    user_data = None
    project_id = "fynd-db"
    dataset_id = "finance_dwh"
    table_id = "plan_v2"
    service_account_info = {
        "type": "service_account",
        "project_id": "fynd-db",
        "private_key_id": "48954327ef172acc3091a91881840dc353ff0413",
        "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmIs0vgXflZmHD\n0Ie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wKBgF6ZvqCcom0HPU5I7f+wu+vdVfA6yy45lHmLqAamSFw7cLBPI8Jh\nbI7jA9/Rgn9oipUmxcX34M/Eiq4u8Xp1qC4tP/16YMpaVU8qjY7ZdfB2b75lgdaT\npZLOxZsq9X8Xauso8uxv+nDCG/8YtmEV9d61u0acE+t+mA3PVxqkZ0m/AoGBAI5c\nY76AqeN+JVxaEm/0tIsj9Om46hR2URJ2lzB6YCuzINUqy9GjHJBWj9oITzD2FmNV\n/yCGIeW3CClOyCtzJyNLhYf5Sr+XjoKRQVN/+7+C/l2YL6Sg4Ok/PRMm6iH+u2QB\nJTY1d0pOdfUPqR8gKsVJgBGE04iwE/RmLpp9/XZRAoGAFtaa9JeHk9dmZiST3lev\ns5syeOK1cCj1+nJer/GPEjqWMKxbCULvoEYBp7RaoEXjfGi7P4Mc6YHnbljHc+sL\niJzHhxrv/HYZdlCdTU3IRWT0sNHzqZemfYnsVTSp+4y61sGDxioDYL4CLnMq5T2F\nqzfc2qp3s8ApVloY3giuqnI=\n-----END PRIVATE KEY-----\n",
        "client_email": "plan-maker@fynd-db.iam.gserviceaccount.com",
        "client_id": "114883060650442183907",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/plan-maker%40fynd-db.iam.gserviceaccount.com",
        "universe_domain": "googleapis.com"
    }


    # Submit button
    if st.button("Submit"):
    # Save user data
        user_data = {
        "Business_Head": selected_business_head,
        "Company_ID": company_id.strip() if company_id else None,
        "Company_Name": st.session_state.get('company_name', ''),
        "Currency": currency,
        "Bundle_by": bundle_by,
        "Plan_Name": plan_name,
        "Plan_Description": plan_description,
        "Product_lines": st.session_state.get('ordering_channel', ''),
        "Fulfilling_Location": st.session_state.get('fulfilling_location', ''),
        "Application_ID": st.session_state.get('application_id', ''),
        "Fee_Type": st.session_state.get('selected_fee_type', ''),
        "Variable_Type": selected_variable_type,
        "Chargeable_on": st.session_state.get('selected_chargeable_on', ''),
        "Fee_Nature": 0,
        "Commercial_value": user_input_3,
        "Fee_reversal": 0,
        "Reversal_%": 0,
        "Usage_limit": usage_3,
        "Threshold_value": 0,
        "Threshold_option": 0,
        "Expected_Billing": user_input_3,
        "Plan_Validity": selected_plan_validity,
        "Payment_Method": selected_payment_method,
        "Net_total_billing":int(final_billing)
        }

        
    # Save and display user data
        save_user_data(user_data)
        st.session_state.user_data = user_data
        st.session_state.df = pd.DataFrame([user_data])

        st.write("User Data Submitted")
        st.write(st.session_state.df)

        # Display saved records
        # display_saved_records()

    # Insert into BigQuery
    if st.button("Export to BQ"):

        if project_id and dataset_id and table_id and service_account_info:
            user_data = st.session_state.user_data 

            # Generate rule_id and plan_id
            rule_id = generate_rule_id(user_data)
            plan_id = generate_plan_id(user_data)

            # Update user_data with generated IDs
            user_data['rule_id'] = rule_id
            user_data['plan_id'] = plan_id
            st.session_state['created_at'] = datetime.now().isoformat()

            user_data = {
            "Business_Head": st.session_state.get('selected_business_head'),
            "Company_ID": st.session_state.get('company_id'.strip()) if st.session_state.get('company_id') else None,
            "Company_Name": st.session_state.get('company_name', ''),
            "Currency": st.session_state.get('currency'),
            "Bundle_by": st.session_state.get('bundle_by'),
            "Plan_Name": plan_name,
            "Plan_Description": plan_description,
            "Product_lines": st.session_state.get('ordering_channel', ''),
            "Fulfilling_Location": st.session_state.get('fulfilling_location', ''),
            "Application_ID": st.session_state.get('application_id', ''),
            "Fee_Type": st.session_state.get('selected_fee_type', ''),
            "Variable_Type": selected_variable_type,
            "Chargeable_on": st.session_state.get('selected_chargeable_on', ''),
            "Fee_Nature": 0,
            "Commercial_value": user_input_3,
            "Fee_reversal": 0,
            "Reversal_%": 0,
            "Usage_limit": usage_3,
            "Threshold_value": 0,
            "Threshold_option": 0,
            "Expected_Billing": user_input_3,
            "Plan_Validity": selected_plan_validity,
            "Payment_Method": selected_payment_method,
            "Net_total_billing":int(final_billing)
            }
            
            # Save the user data locally and to session state
            save_user_data(user_data)

            # Display saved records
            display_saved_records()

            # Store created_at, rule_id, and plan_id in session state
            st.session_state['created_at'] = datetime.now().isoformat()
            st.session_state['rule_id'] = rule_id
            st.session_state['plan_id'] = plan_id

            insert_into_bq(user_data, project_id, dataset_id, table_id, service_account_info)
        else:
            st.error("One or more required variables are not defined.")