-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
391 lines (330 loc) · 12.5 KB
/
app.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
import requests
import cv2
import numpy as np
from pyzbar import pyzbar
import argparse
import os
import re
import glob
import tkinter as tk
from tkinter import *
import smtplib
from prettytable import PrettyTable
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import csv
from PIL import ImageGrab
import time
import multiprocessing
from tabulate import tabulate
import pyrebase
import firebase_admin as firebase
from firebase_admin import firestore, storage
import json
from datetime import date
import pyqrcode
import png
from pyqrcode import QRCode
from PIL import ImageTk, Image
today = date.today()
date=str(today)
print(date)
items=[]
itemScanned=[]
quantity=[]
prices=[]
fruitsAndVegetables=['Apple','Mango','Banana','Litchi','Orange','Pomogranate','Cheeku','Muskmelon'
,'Watermelon','Beetroot','Beans','Carrot','Radish']
liquids=['Pepsi','Coca-Cola','Tropicana','Harpic','Maaza']
url="http://192.168.0.104:8080/shot.jpg"
def showThankYouPage():
s = QRCodeData
url = pyqrcode.create(s)
# url.svg("myqr.svg", scale = 8)
url.png('myqr.png', scale = 6)
thankYouPage=tk.Tk()
thankYouPage.title("ABC Mart")
thankYouPage.geometry("500x500+300-100")
title=Label(thankYouPage,text="BILL DETAILS", font=('comicsana',18)).place(x=170,y=10)
path = "myqr.png"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(thankYouPage, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
content=Label(thankYouPage,text="Thank you for shopping with us! \n We have mailed you the E-Bill to the entered email!").place(x=110,y=50)
content=Label(thankYouPage,text="Please scan the QR Code below if you are logging in as GUEST!").place(x=110,y=70)
button=tk.Button(thankYouPage,text='Close',fg="white",width=30,bg="black",command=thankYouPage.destroy)
button.place(x=150,y=400)
thankYouPage.mainloop()
def sendEBill():
senderAddress = '[email protected]'
senderPassword = '@Sample123'
server = 'smtp.gmail.com:587'
recieverAddress = email
with open('totalPrice.txt','r') as f:
totalPrice=f.read()
f.close()
text = """
Dear Customer,
Thank you for shopping with us!
Please find your E-Bill down below :
{table}
Regards,
ABC Mart
"""
html = """
<html>
<head>
<style>
table, th, td, tr {{ border: 1px solid red; border-collapse: collapse; }}
th, td{{ border-left:1px solid red; border-right: 1px solid red;}}
</style>
</head>
<body>
<p>Dear Customer,</p>
<p>Thank you for shopping with us! <br/>
Please find your E-Bill down below : </p><br/>
{table}
<br/>
<p>Total Price : Rs. %s /-</p><br/><br/>
<p>Regards,</p><br/>
<p>ABC Mart</p>
</body>
</html>
""" %totalPrice
data=[]
with open('data.csv') as f:
data=[line.split() for line in f]
text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))
message = MIMEMultipart("alternative", None, [MIMEText(text), MIMEText(html,'html')])
message['Subject'] = "ABC Mart E-Bill"
message['From'] = senderAddress
message['To'] = recieverAddress
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(senderAddress, senderPassword)
server.sendmail(senderAddress, recieverAddress, message.as_string())
print('Email Sent')
server.quit()
def navigateToPageFour():
if emailExists:
userDetails = db.collection('users').document(email).get().to_dict()
currentBillNumber = userDetails['billQuantity']
nextBillNumber = currentBillNumber + 1
userDetails['billQuantity']=nextBillNumber
db.collection('users').document(email).set(userDetails)
documentID = email+str(nextBillNumber)
print(documentID)
totalPrice=0
slNo=int(1)
for i in range(len(items)):
index1='item'+str(slNo)
index2='quantity'+str(slNo)
index3='price'+str(slNo)
if i==0:
existing={}
else:
existing = db.collection('bills').document(documentID).get().to_dict()
print('**************************************************************')
print(existing)
existing['date']=date
existing[index1]=itemScanned[i]
existing[index2]=quantity[i]
existing[index3]=prices[i]
totalPrice+=int(prices[i])
existing['totalPrice'] = totalPrice
db.collection(u'bills').document(documentID).set(existing)
slNo += 1
else:
global QRCodeData
userDetails = db.collection('guests').document('guestNumber').get().to_dict()
currentBillNumber = userDetails['number']
nextBillNumber = currentBillNumber + 1
userDetails['number']=nextBillNumber
db.collection('guests').document('guestNumber').set(userDetails)
documentID = 'guest'+str(nextBillNumber)
QRCodeData = documentID
print("QRCODEDAT = ",QRCodeData)
print(documentID)
totalPrice=0
slNo=int(1)
for i in range(len(items)):
index1='item'+str(slNo)
index2='quantity'+str(slNo)
index3='price'+str(slNo)
if i==0:
existing={}
else:
existing = db.collection('guests').document(documentID).get().to_dict()
print('**************************************************************')
print(existing)
existing['date']=date
existing[index1]=itemScanned[i]
existing[index2]=quantity[i]
existing[index3]=prices[i]
totalPrice+=int(prices[i])
existing['totalPrice'] = totalPrice
db.collection(u'guests').document(documentID).set(existing)
slNo += 1
sendEBill()
billPage.destroy()
showThankYouPage()
def writeToFile():
pointerLocal=0
with open('data.csv','w',newline='') as f:
f.write('SlNo Item Quantity Price')
for item in itemScanned:
f.write('\n')
temp=str(pointerLocal+1)+' '+itemScanned[pointerLocal]+' '+quantity[pointerLocal]+' '+prices[pointerLocal]
f.write(temp)
pointerLocal+=1
def showBillPage():
writeToFile()
global billPage
billPage=tk.Tk()
billPage.title("ABC Mart")
billPage.geometry("500x500+300-100")
title=Label(billPage,text="BILL DETAILS", font=('comicsana',18)).place(x=170,y=10)
slNo=1
slNox=10
slNoy=50
itemNamex=70
itemNamey=50
quantityx=250
quantityy=50
pricex=350
pricey=50
discountx=420
discounty=50
pointer=0
totalPrice=0
title=Label(billPage,text="SL NO").place(x=slNox,y=slNoy)
title=Label(billPage,text="ITEM").place(x=itemNamex,y=itemNamey)
title=Label(billPage,text="QUANTITY").place(x=quantityx,y=quantityy)
title=Label(billPage,text="PRICE").place(x=pricex,y=pricey)
title=Label(billPage,text="DISCOUNT").place(x=discountx,y=discounty)
slNoy += 20
itemNamey += 20
quantityy += 20
pricey += 20
discounty += 20
name=''
for item in itemScanned:
if item in fruitsAndVegetables:
name='Kgs'
elif item in liquids:
name='Lts'
else:
name='Units'
title=Label(billPage,text=slNo).place(x=slNox,y=slNoy)
title=Label(billPage,text=item).place(x=itemNamex,y=itemNamey)
title=Label(billPage,text=quantity[pointer]+" "+name).place(x=quantityx,y=quantityy)
title=Label(billPage,text=prices[pointer]+'/-').place(x=pricex,y=pricey)
title=Label(billPage,text="None").place(x=discountx,y=discounty)
totalPrice += int(prices[pointer])
slNo += 1
slNoy += 20
itemNamey += 20
quantityy += 20
pricey += 20
discounty += 20
pointer+=1
with open('totalPrice.txt','w') as f:
f.write(str(totalPrice))
f.close()
totalPriceLabel=Label(billPage,text="TOTAL PRICE : "+str(totalPrice)).place(x=itemNamex,y=discounty+20)
button=tk.Button(billPage,text='Pay '+str(totalPrice),fg="white",width=30,bg="black",command=navigateToPageFour)
button.place(x=130,y=200)
billPage.mainloop()
def getBarCodes():
while True:
imageResponse=requests.get(url)
imageArray=np.array(bytearray(imageResponse.content),dtype=np.uint8)
image=cv2.imdecode(imageArray,-1)
imS=cv2.resize(image,(900,500))
cv2.imshow("AndroidCam",imS)
bars=pyzbar.decode(image)
if bars:
for bar in bars:
(x,y,w,h)=bar.rect
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
barcodeData=bar.data.decode("utf-8")
barcodeType=bar.type
text="{}({})".format(barcodeData,barcodeType)
splStr='('
res=text.partition(splStr)[0]
print(res)
if res not in items:
items.append(res)
itemName,quant,pri,itemID=map(str,res.split())
itemScanned.append(itemName)
quantity.append(quant)
prices.append(pri)
cv2.waitKey(100)
if cv2.waitKey(1)==27:
cv2.destroyAllWindows()
showBillPage()
break
def navigateToPageThree():
statusPage.destroy()
getBarCodes()
return
def showStatus():
userDetails = db.collection('users').document(email).get().to_dict()
global statusPage
global emailExists
statusPage=tk.Tk()
statusPage.title("ABC Mart")
statusPage.geometry("500x500+300-100")
title=Label(statusPage,text="ACCOUNT STATUS", font=('comicsana',18)).place(x=150,y=10)
if(userDetails==None):
title=Label(statusPage,text="Account associated with the entered Email ID NOT FOUND!").place(x=100,y=100)
title=Label(statusPage, text="Please use GUEST LOGIN method to pay").place(x=100,y=120)
emailExists=False
else:
title=Label(statusPage,text="An account associated with the entered Email ID exists!").place(x=100,y=100)
title=Label(statusPage, text="Please login on your mobile to pay").place(x=100,y=120)
emailExists=True
button = tk.Button(statusPage,text='Proceed',fg="white",width=30,bg="black",command=navigateToPageThree)
button.place(x=140,y=200)
statusPage.mainloop()
def navigateToPageTwo():
global email
email = entry1.get()
phoneNumber = entry2.get()
print(email, phoneNumber)
mainWindow.destroy()
showStatus()
#--------------------------------FOR FIRESTORE----------------------------
cred = firebase.credentials.Certificate("firebaseKey.json")
keyFile = open('pyrebaseKey.json','r')
keyJson = keyFile.read()
key = json.loads(keyJson)
firebase.initialize_app(cred,{'storageBucket':'selfcheckout-8b125.appspot.com'})
db = firestore.client() #For database
bucket = storage.bucket()
auth = pyrebase.initialize_app(key).auth() #For authentication
#-------------------------------------FIRESTORE ENDS--------------------------
#-------------------------------------MAIN WINDOW-----------------------------
mainWindow = tk.Tk()
mainWindow.title('ABC Mart')
mainWindow.geometry("500x500+300-100")
frame = tk.Frame(mainWindow)
#----------------------------------MAIN WINDOW ENDS---------------------------
#-----------------------------------PAGE1------------------------------------------------------
title = Label(mainWindow,text="ENTER DETAILS", font=('comicsana',18)).place(x=150,y=10)
tk.Label(mainWindow, text="Email-ID", font=('comicsana',13)).place(x=90,y=100)
tk.Label(mainWindow, text="Phone Number", font=('comicsana',13)).place(x=90,y=150)
entry1 = tk.Entry(mainWindow, width=30)
entry1.place(x=240,y=100)
entry2 = tk.Entry(mainWindow, width=30)
entry2.place(x=240,y=150)
button = tk.Button(mainWindow,text='Proceed',fg="white",width=30,bg="black",command=navigateToPageTwo)
button.place(x=130,y=200)
#------------------------------------PAGE 1 ENDS------------------------------------------------
mainWindow.mainloop()