File size: 10,504 Bytes
80feb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from app.database import (
    create_tables,
    SessionLocal,
    Dish,
    Person,
    Base,
    LoyaltyProgram,
    SelectionOffer,
    Table,
)
from sqlalchemy import create_engine
from datetime import datetime, timezone
import os
import sys


def init_db(force_reset=False):
    # Check if force_reset is enabled
    if force_reset:
        # Drop all tables and recreate them
        print("Forcing database reset...")
        Base.metadata.drop_all(
            bind=create_engine(
                "sqlite:///./tabble_new.db", connect_args={"check_same_thread": False}
            )
        )

    # Create tables
    create_tables()

    # Create a database session
    db = SessionLocal()

    # Check if dishes already exist
    existing_dishes = db.query(Dish).count()
    if existing_dishes > 0:
        print("Database already contains data. Skipping initialization.")
        return

    # Add sample dishes
    sample_dishes = [
        # Regular dishes
        Dish(
            name="Margherita Pizza",
            description="Classic pizza with tomato sauce, mozzarella, and basil",
            category="Main Course",
            price=12.99,
            quantity=20,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=0,
        ),
        Dish(
            name="Caesar Salad",
            description="Fresh romaine lettuce with Caesar dressing, croutons, and parmesan",
            category="Appetizer",
            price=8.99,
            quantity=15,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=0,
        ),
        Dish(
            name="Chocolate Cake",
            description="Rich chocolate cake with ganache frosting",
            category="Dessert",
            price=6.99,
            quantity=10,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=0,
        ),
        Dish(
            name="Iced Tea",
            description="Refreshing iced tea with lemon",
            category="Beverage",
            price=3.99,
            quantity=30,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=0,
        ),
        Dish(
            name="Chicken Alfredo",
            description="Fettuccine pasta with creamy Alfredo sauce and grilled chicken",
            category="Main Course",
            price=15.99,
            quantity=12,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
        ),
        Dish(
            name="Garlic Bread",
            description="Toasted bread with garlic butter and herbs",
            category="Appetizer",
            price=4.99,
            quantity=25,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=0,
        ),
        # Special offer dishes
        Dish(
            name="Weekend Special Pizza",
            description="Deluxe pizza with premium toppings and extra cheese",
            category="Main Course",
            price=18.99,
            quantity=15,
            image_path="/static/images/default-dish.jpg",
            discount=20,
            is_offer=1,
            is_special=0,
        ),
        Dish(
            name="Seafood Pasta",
            description="Fresh pasta with mixed seafood in a creamy sauce",
            category="Main Course",
            price=22.99,
            quantity=10,
            image_path="/static/images/default-dish.jpg",
            discount=15,
            is_offer=1,
            is_special=0,
        ),
        Dish(
            name="Tiramisu",
            description="Classic Italian dessert with coffee-soaked ladyfingers and mascarpone cream",
            category="Dessert",
            price=9.99,
            quantity=8,
            image_path="/static/images/default-dish.jpg",
            discount=25,
            is_offer=1,
            is_special=0,
        ),
        # Today's special dishes
        Dish(
            name="Chef's Special Steak",
            description="Prime cut steak cooked to perfection with special house seasoning",
            category="Main Course",
            price=24.99,
            quantity=12,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=1,
        ),
        Dish(
            name="Truffle Mushroom Risotto",
            description="Creamy risotto with wild mushrooms and truffle oil",
            category="Main Course",
            price=16.99,
            quantity=10,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=1,
        ),
        Dish(
            name="Chocolate Lava Cake",
            description="Warm chocolate cake with a molten center, served with vanilla ice cream",
            category="Dessert",
            price=8.99,
            quantity=15,
            image_path="/static/images/default-dish.jpg",
            discount=0,
            is_offer=0,
            is_special=1,
        ),
    ]

    # Add dishes to database
    for dish in sample_dishes:
        db.add(dish)

    # Add sample users
    sample_users = [
        Person(
            username="john_doe",
            password="password123",
            visit_count=1,
            last_visit=datetime.now(timezone.utc),
        ),
        Person(
            username="jane_smith",
            password="password456",
            visit_count=3,
            last_visit=datetime.now(timezone.utc),
        ),
        Person(
            username="guest",
            password="guest",
            visit_count=5,
            last_visit=datetime.now(timezone.utc),
        ),
    ]

    # Add users to database
    for user in sample_users:
        db.add(user)

    # Add sample loyalty program tiers
    sample_loyalty_tiers = [
        LoyaltyProgram(
            visit_count=3,
            discount_percentage=5.0,
            is_active=True,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        LoyaltyProgram(
            visit_count=5,
            discount_percentage=10.0,
            is_active=True,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        LoyaltyProgram(
            visit_count=10,
            discount_percentage=15.0,
            is_active=True,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        LoyaltyProgram(
            visit_count=20,
            discount_percentage=20.0,
            is_active=True,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
    ]

    # Add loyalty tiers to database
    for tier in sample_loyalty_tiers:
        db.add(tier)

    # Add sample selection offers
    sample_selection_offers = [
        SelectionOffer(
            min_amount=50.0,
            discount_amount=5.0,
            is_active=True,
            description="Spend $50, get $5 off",
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        SelectionOffer(
            min_amount=100.0,
            discount_amount=15.0,
            is_active=True,
            description="Spend $100, get $15 off",
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        SelectionOffer(
            min_amount=150.0,
            discount_amount=25.0,
            is_active=True,
            description="Spend $150, get $25 off",
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
    ]

    # Add selection offers to database
    for offer in sample_selection_offers:
        db.add(offer)

    # Add sample tables
    sample_tables = [
        Table(
            table_number=1,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=2,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=3,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=4,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=5,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=6,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=7,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
        Table(
            table_number=8,
            is_occupied=False,
            created_at=datetime.now(timezone.utc),
            updated_at=datetime.now(timezone.utc),
        ),
    ]

    # Add tables to database
    for table in sample_tables:
        db.add(table)

    # Commit changes
    db.commit()

    print("Database initialized with sample data:")
    print("- Added", len(sample_dishes), "sample dishes")
    print("- Added", len(sample_users), "sample users")
    print("- Added", len(sample_loyalty_tiers), "loyalty program tiers")
    print("- Added", len(sample_selection_offers), "selection offers")
    print("- Added", len(sample_tables), "tables")

    # Close session
    db.close()


if __name__ == "__main__":
    # Create static/images directory if it doesn't exist
    os.makedirs("app/static/images", exist_ok=True)

    # Check for force reset flag
    force_reset = "--force-reset" in sys.argv

    # Initialize database
    init_db(force_reset)