File size: 4,447 Bytes
f63d435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
from dotenv import load_dotenv
import os
from datetime import datetime

# Load environment variables
load_dotenv()

# Microsoft Graph API settings
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
FILE_ID = "A57BDBC1CDEC2768!282563"
WORKSHEET_ID = "{00000000-0001-0000-0000-000000000000}"

def update_sum_insured(sum_insured):
    """Update Sum Insured cell value"""
    if not ACCESS_TOKEN:
        st.error("Access token not found. Please check your .env file.")
        return None
    
    headers = {
        'Authorization': f'Bearer {ACCESS_TOKEN}',
        'Content-Type': 'application/json',
    }
    
    url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='SUM_INSURED')"
    
    data = {
        "values": [[sum_insured]]
    }
    
    try:
        response = requests.patch(url, headers=headers, json=data)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error updating Sum Insured: {str(e)}")
        return None

def update_year_built(year_built):
    """Update Year Built cell value"""
    if not ACCESS_TOKEN:
        st.error("Access token not found. Please check your .env file.")
        return None
    
    headers = {
        'Authorization': f'Bearer {ACCESS_TOKEN}',
        'Content-Type': 'application/json',
    }
    
    url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='YEAR_BUILT')"
    
    data = {
        "values": [[year_built]]
    }
    
    try:
        response = requests.patch(url, headers=headers, json=data)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error updating Year Built: {str(e)}")
        return None

def get_insurance_premium():
    """Get insurance premium value from Excel"""
    if not ACCESS_TOKEN:
        st.error("Access token not found. Please check your .env file.")
        return None
    
    headers = {
        'Authorization': f'Bearer {ACCESS_TOKEN}',
        'Content-Type': 'application/json',
    }
    
    url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='INSURANCE_PREMIUM')"
    
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error getting insurance premium: {str(e)}")
        return None

def main():
    st.title("Insurance Calculator")
    st.write("Update Excel values and calculate insurance premium")
    
    # Input form
    with st.form("excel_update_form"):
        sum_insured = st.number_input("Sum Insured", min_value=0, value=1000000)
        year_built = st.number_input("Year Built", min_value=1900, max_value=datetime.now().year, value=2000)
        submitted = st.form_submit_button("Calculate Premium")
        
        if submitted:
            # Update Sum Insured
            sum_insured_result = update_sum_insured(sum_insured)
            if sum_insured_result:
                st.success("Sum Insured updated successfully!")
            else:
                st.error("Failed to update Sum Insured")
                return
            
            # Update Year Built
            year_built_result = update_year_built(year_built)
            if year_built_result:
                st.success("Year Built updated successfully!")
            else:
                st.error("Failed to update Year Built")
                return
            
            # Get calculated premium
            premium_result = get_insurance_premium()
            
            if premium_result and 'values' in premium_result:
                premium = premium_result['values'][0][0]
                st.header("Calculation Result")
                st.write(f"**Insurance Premium:** {premium:,.2f}")
                
                # Display summary
                st.subheader("Summary")
                st.write(f"**Sum Insured:** {sum_insured:,.2f}")
                st.write(f"**Year Built:** {year_built}")
                st.write(f"**Annual Premium:** {premium:,.2f}")
            else:
                st.error("Failed to get insurance premium")

if __name__ == "__main__":
    main()