-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cashier.py
479 lines (388 loc) · 13.3 KB
/
Cashier.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#################################################
# Property of EZQ
# LAST MODIFIED: 220419
#################################################
#################################################
"""
This Cashier UI class is written to modularise the
project
This script runs in the RPI client and does the
following:
- Connect to firebase using Firebase class
- Collects Customer Contact Details
- Updates relevant Firebase data
"""
#################################################
#------------------IMPORTS----------------------#
import os
import time
import json
from time import sleep
from firebase import Firebase
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, StringProperty
#=================+CONSTANTS====================#
os.environ["KIVY_GL_BACKEND"] = "gl"
STORE_NAME = 'Western Food'
#===============================================#
# Setup Firebase instance
FIREBASE = Firebase()
FIREBASE.connect()
class MenuScreen(Screen):
def __init__(self, **kwargs):
"""
Initialization of MenuScene as a Screen widget
Attributes
----------
- self.order : str
Input order
- self.state : str
State of toggle button
- self.additional_info : str
Input additional info for order
- self.popup : None
Class attribute to store popup object
- self.btns : list
List to store all buttons
- self.firebase : object
Firebase object
"""
super(Screen, self).__init__()
# Set up Variables
self.order = StringProperty(None)
self.state = StringProperty(None)
self.additional_info = StringProperty(None)
self.popup = None
self.btns = []
# Create a firebase object
self.firebase = FIREBASE
# Create ToggleButtons for each dish on the menu, store in the list self.btns and add to layout
menu_list = ["Chicken Chop Spaghetti", "Grilled Fish Spaghetti", "Chicken Cubes with Rice", "Aglio Olio", "Mexican Chicken Chop", "Chicken Chop with Fries"]
for i in menu_list:
btn = ToggleButton(text=i, group="menu", font_size = 20, background_normal='', background_color=[0.96, 0.54, 0.41, 1], markup = True)
btn.bind(on_press = self.get_order)
self.btns.append(btn)
self.grid.add_widget(btn)
def get_order(self, togglebutton):
"""
Method to save the order of the customer to send to firebase
Parameters
----------
togglebutton : Widget
ToggleButton containing choice of order
Function Calls
--------------
None
Returns
-------
None
"""
tb = togglebutton
self.order = str(tb.text)
self.state = str(tb.state)
def get_additional_info(self):
"""
Method to save the additional information for customer's order
Parameters
----------
None
Function Calls
--------------
None
Returns
-------
None
"""
self.additional_info = str(self.ids.additional_info_input.text)
def set_pop_up(self):
"""
Method to conduct sanity check
Parameters
----------
None
Function Calls
--------------
self.popup_layout : Method
Generate the popup layout
self.popup.open : Method
Opens a popup
Returns
-------
None
"""
content = self.popup_layout()
if not self.popup:
self.popup = Popup(title="Warning", content=content, auto_dismiss=True, size_hint=(0.5, 0.5))
self.popup.open()
def popup_layout(self):
"""
Method to add neccessary warning and widget to popup
Parameters
----------
None
Function Calls
--------------
None
Returns
-------
layout : BoxLayout
Layout of text and button
"""
layout = BoxLayout(orientation="vertical")
text = Label(text="Please select an order")
btn = Button(text="OKAY!")
btn.bind(on_release=self.close_popup)
layout.add_widget(text)
layout.add_widget(btn)
return layout
def close_popup(self, *args):
self.popup.dismiss()
def check_order(self):
"""
Method to confirm your order and transit to Submit Screen
Parameters
----------
None
Function Calls
--------------
self.set_pop_up : Method
Method to conduct sanity check
self.manager.ids.submit_screen.update_nums_layout : Method
List out available phone numbers that has not been assigned orders
Returns
-------
None
"""
if self.state != "down":
self.set_pop_up()
else:
self.reset()
self.manager.current = "SubmitScreen"
self.manager.ids.submit_screen.update_nums_layout()
self.manager.ids.submit_screen.order = self.order
self.manager.ids.submit_screen.additional_info = self.additional_info
def reset(self):
"""
Method to reset the state of all Toggle Buttons and Text Input
"""
for i in self.btns:
if i.state == "down":
i.state = "normal"
self.ids.additional_info_input.text = ""
class SubmitScreen(Screen):
order = StringProperty()
additional_info = StringProperty()
def __init__(self, **kwargs):
"""
Initialization of SubmitScreen as a Screen widget
Attributes
----------
- self.firebase : object
Firebase object
- self.nums : array
Array of phone numbers
- self.num : str
Phone number of customer
- self.state : str
State of toggle button
- self.popup : None
Class attribute to store popup object
- self.nums_btns : list
List to store all buttons
"""
super(Screen, self).__init__()
self.firebase = FIREBASE
self.nums = None
self.num = StringProperty(None)
self.state = StringProperty(None)
self.popup = None
self.nums_btns = []
def get_nums(self):
"""
Method to get the phone numbers that has not been served
Parameters
----------
None
Function Calls
--------------
self.firebase.get_data : Method
Obtain customers data
Returns
-------
self.nums : array
Array containing phone numbers
"""
# Clear pre-existing widgets
if self.nums_btns:
for x in self.nums_btns:
self.ids.boxy.remove_widget(x)
# Get data from database
data = self.firebase.get_data("customers")
#print(data)
# Get keys - order nums
key = []
for customer_id,customer in data.items():
if customer['served'] == False:
key.append(customer_id)
# key = [x for x in data]
# Sort the keys list
sorted(key, reverse=True)
# Get the top 3 numbers based on top 3 keys
self.nums = []
for k in key:
d = data[k]
self.nums.append(k + ':' + d['mobile'])
# self.nums = [x["mobile"] for x in [data[y] for y in key[:3]]]
return self.nums
def bind_nums(self, togglebutton):
"""
Method to save the selected number and customer id
"""
tb = togglebutton
self.num = str(tb.text)
self.state = str(tb.state)
self.customer_id = tb.text.split(':')[0]
def update_nums_layout(self):
"""
Method to update the available numbers on the screen
Parameters
----------
None
Function Calls
--------------
self.get_nums : Method
Get the phone numbers that has not been served
self.bind_nums : Method
Save the selected number and customer id
"""
self.nums = self.get_nums()
for num in self.nums:
btn = ToggleButton(text=num, group="nums")
btn.bind(on_press=self.bind_nums)
self.nums_btns.append(btn)
self.ids.boxy.add_widget(btn)
def set_pop_up(self):
"""
Method to conduct sanity check
Parameters
----------
None
Function Calls
--------------
self.popup_layout : Method
Generate the popup layout
self.popup.open : Method
Opens a popup
Returns
-------
None
"""
content = self.popup_layout()
if not self.popup:
self.popup = Popup(title="Warning", content=content, auto_dismiss=True, size_hint=(0.5, 0.5))
self.popup.open()
def popup_layout(self):
"""
Method to add neccessary warning and widget to popup
Parameters
----------
None
Function Calls
--------------
None
Returns
-------
layout : BoxLayout
Layout of text and button
"""
layout = BoxLayout(orientation="vertical")
text = Label(text="Please select a number")
btn = Button(text="OKAY!")
btn.bind(on_release=self.close_popup)
layout.add_widget(text)
layout.add_widget(btn)
return layout
def update(self):
"""
Method to send order data to firebase
Parameters
----------
None
Function Calls
--------------
self.firebase.get_data : Method
Get order data from firebase
self.firebase.update : Method
Update information on firebase
Returns
-------
None
"""
# Get the current order ids
order_data = self.firebase.get_data("orders")
store_order_data = order_data[STORE_NAME]
# Used ids
used_ids = []
for order_id,item in store_order_data.items():
used_ids.append(order_id)
new_id = None
for i in range(1,999):
i = '{:0>3}'.format(str(i))
if i not in used_ids:
new_id = i
break
new_order_dict = {}
new_order_dict['food'] = self.order
new_order_dict['ready'] = False
new_order_dict['addit_info'] = self.additional_info
new_order_dict['time_waited'] = 0
new_order_dict['id'] = self.customer_id
new_order_dict["order_time"] = int(time.time())
print('adding new id ' + str(new_id) + ' and data ' + str(new_order_dict))
self.firebase.update(["orders", STORE_NAME], {new_id:new_order_dict})
#update that the customer is served
self.firebase.update(["customers",self.customer_id],{'served':True})
def check_nums(self):
"""
Method to confirm your order and transit to Menu Screen
Parameters
----------
None
Function Calls
--------------
self.set_pop_up : Method
Method to conduct sanity check
self.update : Method
Send order data to firebase
Returns
-------
None
"""
if self.state != "down":
self.set_pop_up()
else:
self.update()
self.manager.current = "MenuScreen"
#self.manager.ids.submit_screen.order = self.order
#self.manager.ids.submit_screen.additional_info = self.additional_info
def close_popup(self, *args):
self.popup.dismiss()
class CashierApp(App):
def __init__(self):
super().__init__()
self.firebase = FIREBASE
def build(self):
pass
if __name__ == "__main__":
a = CashierApp()
a.run()