File size: 5,764 Bytes
90537f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Unified Database Schema Design

## Overview
Refactor from multiple hotel-specific databases to a single unified `Tabble.db` with hotel_id discrimination.

## Current vs Target Architecture

### Current Architecture
- **Multiple Databases:** Each hotel has separate .db file
- **Authentication:** database_name + password
- **Data Isolation:** Separate database files
- **Connection Management:** DatabaseManager switches between databases per session

### Target Architecture
- **Single Database:** Tabble.db
- **Authentication:** hotel_name + password (from hotels.csv)
- **Data Isolation:** hotel_id foreign key filtering
- **Connection Management:** Single connection with hotel_id context

## Hotels Registry Table

```sql
CREATE TABLE hotels (
    id INTEGER PRIMARY KEY,
    hotel_name VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
```

**Data Migration from hotels.csv:**
```csv
hotel_name,password,hotel_id
tabble_new,myhotel,1
anifa,anifa123,2
hotelgood,hotelgood123,3
hotelmoon,moon123,4
shine,shine123,5
```

## Updated Table Schemas

### 1. Dishes Table
```sql
CREATE TABLE dishes (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    name VARCHAR,
    description TEXT,
    category VARCHAR,
    price FLOAT,
    quantity INTEGER DEFAULT 0,
    image_path VARCHAR,
    discount FLOAT DEFAULT 0,
    is_offer INTEGER DEFAULT 0,
    is_special INTEGER DEFAULT 0,
    visibility INTEGER DEFAULT 1,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id)
);
```

### 2. Persons Table
```sql
CREATE TABLE persons (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    username VARCHAR,
    password VARCHAR,
    phone_number VARCHAR,
    visit_count INTEGER DEFAULT 0,
    last_visit DATETIME,
    created_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    UNIQUE(hotel_id, username),
    UNIQUE(hotel_id, phone_number)
);
```

### 3. Orders Table
```sql
CREATE TABLE orders (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    person_id INTEGER,
    table_number INTEGER,
    total_amount FLOAT,
    status VARCHAR,
    unique_id VARCHAR,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    FOREIGN KEY (person_id) REFERENCES persons (id)
);
```

### 4. Order Items Table
```sql
CREATE TABLE order_items (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    order_id INTEGER,
    dish_id INTEGER,
    quantity INTEGER,
    price FLOAT,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    FOREIGN KEY (order_id) REFERENCES orders (id),
    FOREIGN KEY (dish_id) REFERENCES dishes (id)
);
```

### 5. Tables Table
```sql
CREATE TABLE tables (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    table_number INTEGER,
    is_occupied BOOLEAN DEFAULT FALSE,
    current_order_id INTEGER,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    FOREIGN KEY (current_order_id) REFERENCES orders (id),
    UNIQUE(hotel_id, table_number)
);
```

### 6. Settings Table
```sql
CREATE TABLE settings (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    hotel_name VARCHAR NOT NULL,
    address VARCHAR,
    contact_number VARCHAR,
    email VARCHAR,
    tax_id VARCHAR,
    logo_path VARCHAR,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    UNIQUE(hotel_id)
);
```

### 7. Feedback Table
```sql
CREATE TABLE feedback (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    order_id INTEGER,
    person_id INTEGER,
    rating INTEGER,
    comment TEXT,
    created_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id),
    FOREIGN KEY (order_id) REFERENCES orders (id),
    FOREIGN KEY (person_id) REFERENCES persons (id)
);
```

### 8. Loyalty Program Table
```sql
CREATE TABLE loyalty_tiers (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    visit_count INTEGER,
    discount_percentage FLOAT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id)
);
```

### 9. Selection Offers Table
```sql
CREATE TABLE selection_offers (
    id INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    min_amount FLOAT,
    discount_amount FLOAT,
    is_active BOOLEAN DEFAULT TRUE,
    description VARCHAR,
    created_at DATETIME,
    updated_at DATETIME,
    FOREIGN KEY (hotel_id) REFERENCES hotels (id)
);
```

## Key Changes Required

### 1. Database Models (SQLAlchemy)
- Add `hotel_id` column to all models
- Add foreign key relationships to hotels table
- Update unique constraints to include hotel_id

### 2. Authentication System
- Change from `database_name + password` to `hotel_name + password`
- Update middleware to validate against hotels table
- Modify frontend to use hotel names instead of database names

### 3. Database Manager
- Remove database switching logic
- Use single connection to Tabble.db
- Add hotel_id context to session management

### 4. Query Filtering
- Add `filter(Model.hotel_id == current_hotel_id)` to all queries
- Update all CRUD operations to include hotel_id
- Ensure data isolation through query filtering

### 5. Migration Strategy
- Create migration script to:
  1. Create new Tabble.db with unified schema
  2. Migrate data from existing hotel databases
  3. Populate hotels table from hotels.csv
  4. Add hotel_id to all migrated records

## Data Isolation Verification
- All queries must include hotel_id filtering
- No cross-hotel data access possible
- Maintain same security level as separate databases