File size: 6,053 Bytes
6aa11d0
 
 
aeea10a
d35d415
6aa11d0
 
d35d415
 
6aa11d0
 
 
 
 
d35d415
6aa11d0
d35d415
6aa11d0
 
 
 
 
 
 
d35d415
 
 
 
26ebcc4
d35d415
 
 
6aa11d0
 
 
 
d35d415
 
 
6aa11d0
d35d415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aa11d0
d35d415
 
6aa11d0
d35d415
6aa11d0
d35d415
 
 
 
 
 
6aa11d0
d35d415
 
6aa11d0
3c64d21
d35d415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc59e1
745e7f6
d35d415
29780da
d35d415
 
6aa11d0
d35d415
4153de7
6aa11d0
d35d415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c30aa
d35d415
 
 
291b184
 
29780da
d35d415
 
4153de7
d35d415
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
import streamlit as st
import cv2
from PIL import Image
import numpy as np  # <-- Add this line
import io
import tempfile
import os
import pickle
import requests
from datetime import datetime
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.auth.transport.requests import Request
from simple_salesforce import Salesforce
import pytz

# === SALESFORCE SETUP ===
sf = Salesforce(
    username='suriraja822@agentforce.com',
    password='Sati@1010',
    security_token='B9nS0HEyBE7YmWllqXCyiOJpY',
    domain='login'
)

# === CONSTANTS ===
UPLOAD_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF'

def get_vacant_rooms():
    rooms = sf.query("SELECT Id, Name FROM Hotel_Room__c WHERE Room_Status__c = 'Vacant Clean'")
    return {room['Name']: room['Id'] for room in rooms['records']}

def upload_image_to_drive(image_np, filename):
    creds = None
    if os.path.exists("token.pickle"):
        with open("token.pickle", "rb") as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
    service = build('drive', 'v3', credentials=creds)
    image = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
    buf = io.BytesIO()
    image.save(buf, format="JPEG")
    buf.seek(0)

    with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
        tmp.write(buf.read())
        tmp.flush()
        file_metadata = {'name': filename, 'parents': [UPLOAD_FOLDER_ID]}
        media = MediaFileUpload(tmp.name, mimetype='image/jpeg')
        uploaded_file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
        file_id = uploaded_file.get('id')
        os.remove(tmp.name)
        return f"https://drive.google.com/uc?id={file_id}"

def save_guest_to_sf(data):
    try:
        sf.Hotel_Guest__c.create(data)
        st.success("βœ… Guest registered successfully.")
    except Exception as e:
        st.error(f"❌ Salesforce Guest Error: {e}")

def save_staff_to_sf(data):
    try:
        sf.Hotel_Staff__c.create(data)
        st.success("βœ… Staff registered successfully.")
    except Exception as e:
        st.error(f"❌ Salesforce Staff Error: {e}")

st.title("🏨 Hotel Guest and Staff Registration")
tab1, tab2 = st.tabs(["πŸ‘€ Guest", "πŸ‘¨β€πŸ’Ό Staff"])

with tab1:
    st.header("Register a Guest")
    fname = st.text_input("First Name")
    lname = st.text_input("Last Name")
    email = st.text_input("Email")
    phone = st.text_input("Phone")
    id_number = st.text_input("ID Number")
    special_requests = st.text_area("Special Requests")
    emergency_contact = st.text_input("Emergency Contact")
    vacant_rooms = get_vacant_rooms()
    room_name = st.selectbox("Assign Room", list(vacant_rooms.keys()))
    capture = st.camera_input("Capture Guest Photo")

    if st.button("πŸ“ Register Guest"):
        if capture:
            timestamp = datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y%m%d%H%M%S")
            image = Image.open(capture)
            image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
            file_name = f"G_{timestamp}.jpg"
            photo_url = upload_image_to_drive(image_np, file_name)

            guest_data = {
                'First_Name__c': fname,
                'Last_Name__c': lname,
                'Email__c': email,
                'Phone__c': phone,
                'ID_Number__c': id_number,
                'Special_Requests__c': special_requests,
                'Emergency_Contact__c': emergency_contact,
                'Room_Number__c': vacant_rooms[room_name],
                'Check_In_Date__c': datetime.utcnow().isoformat(),
                'Photo_Uploaded__c': True,
                 "Photo_URL__c":photo_url,
                'HF_Registered__c': True,
                'Guest_Status__c': 'Checked In'
            }
            save_guest_to_sf(guest_data)
        else:
            st.warning("πŸ“Έ Please capture a photo before registering.")

with tab2:
    st.header("Register a Staff Member")
    fname = st.text_input("Staff First Name")
    lname = st.text_input("Staff Last Name")
    email = st.text_input("Staff Email")
    phone = st.text_input("Staff Phone")
    emp_number = st.text_input("Employee Number")
    department = st.text_input("Department")
    position = st.text_input("Position")
    shift = st.selectbox("Shift", ["Morning", "Evening", "Night"])
    status = st.selectbox("Status", ["Active", "Inactive"])
    access = st.selectbox("Access Level", ["Low", "Medium", "High"])
    badge = st.text_input("Badge Number")
    supervisor = st.text_input("Supervisor")
    hire_date = st.date_input("Hire Date")
    capture = st.camera_input("Capture Staff Photo")

    if st.button("πŸ“ Register Staff"):
        if capture:
            timestamp = datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y%m%d%H%M%S")
            image = Image.open(capture)
            image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
            file_name = f"S_{timestamp}.jpg"
            photo_url = upload_image_to_drive(image_np, file_name)

            staff_data = {
                'First_Name__c': fname,
                'Last_Name__c': lname,
                'Email__c': email,
                'Phone__c': phone,
                'Employee_Number__c': emp_number,
                'Department__c': department,
                'Position__c': position,
                'Shift__c': shift,
                'Employment_Status__c': status,
                'Access_Level__c': 'Basic',
                'Badge_Number__c': badge,
                'Supervisor__c': supervisor,
                'Hire_Date__c': hire_date.isoformat(),
                    'Photo_Uploaded__c': True,
                'photo_url__c':photo_url,
                'HF_Registered__c': True
            }
            save_staff_to_sf(staff_data)
        else:
            st.warning("πŸ“Έ Please capture a photo before registering.")