-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
615 lines (483 loc) · 19.9 KB
/
main.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import pymysql
window = Tk()
window.geometry("900x600")
window.title("Billing")
#======================== field listner ================
def quantityFieldListener(a,b,c):
global quantityVar
global costVar
global itemRate
quantity = quantityVar.get()
if quantity != "":
try:
quantity = float(quantity)
cost = quantity*itemRate
quantityVar.set("%.2f"%quantity)
costVar.set("%.2f"%cost)
except ValueError:
quantity = quantity[:-1]
quantityVar.set(quantity)
else:
quantity = 0
quantityVar.set("%.2f"%quantity)
def costFieldListener(a,b,c):
global quantityVar
global costVar
global itemRate
cost = costVar.get()
if cost !="":
try:
cost = float(cost)
quantity = cost/itemRate
costVar.set("%.2f"%cost)
quantityVar.set("%.2f"%quantity)
except ValueError:
cost = cost[:-1]
costVar.set(cost)
else:
cost = 0
costVar.set(cost)
#===============================global variable ==========================
#========== login variable ==============
usernameVar = StringVar()
passwordVar = StringVar()
mobileNoVar=StringVar()
emailVar=StringVar()
#========== main window variable ========
options=[]
rateDict={}
itemVariable=StringVar()
quantityVar = StringVar()
quantityVar.trace('w',quantityFieldListener)
itemRate=2
rateVar = StringVar()
rateVar.set("%.2f"%itemRate)
costVar= StringVar()
costVar.trace('w',costFieldListener)
#========== main Tree View =============
billsTV = ttk.Treeview(height = 15, columns=('Rate', 'Quantity', 'Cost'))
#================ update tree view =================
updateTV = ttk.Treeview(height=15, columns=('Name','Rate','Type','Store_Type'))
#========== add item variable ===========
storeOptions=['Frozen','Fresh']
addItemNameVar = StringVar()
addItemRateVar = StringVar()
addItemTypeVar = StringVar()
addStoredVar = StringVar()
addStoredVar.set(storeOptions[0])
itemLists = list()
totalCost = 0.0
totalCostVar = StringVar()
totalCostVar.set("Total Cost = {}".format(totalCost))
updateItemId = ""
#========================= function to generate bill ==============
def generate_bill():
global itemVariable
global quantityVar
global itemRate
global costVar
global itemLists
global totalCost
global totalCostVar
itemName = itemVariable.get()
quantity = quantityVar.get()
cost = costVar.get()
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor()
query = "insert into bill (name,quantity,rate,cost) values('{}','{}','{}','{}')".format(itemName,quantity,itemRate,cost)
cursor.execute(query)
conn.commit()
conn.close()
listDict = {"name":itemName, "rate":itemRate, "quantity":quantity, "cost":cost}
itemLists.append(listDict)
totalCost+=float(cost)
quantityVar.set("0")
costVar.set("0")
updateListView()
totalCostVar.set("Total Cost = {}".format(totalCost))
def OnDoubleClick(event):
global addItemNameVar
global addItemRateVar
global addItemTypeVar
global addStoredVar
global updateItemId
item = updateTV.selection()
updateItemId = updateTV.item(item,"text")
item_detail = updateTV.item(item,"values")
item_index = storeOptions.index(item_detail[3])
addItemTypeVar.set(item_detail[2])
addItemRateVar.set(item_detail[1])
addItemNameVar.set(item_detail[0])
addStoredVar.set(storeOptions[item_index])
def updateListView():
records = billsTV.get_children()
for element in records:
billsTV.delete(element)
for row in itemLists:
billsTV.insert('','end',text=row['name'],values=(row["rate"],row["quantity"],row["cost"]))
def getItemLists():
records = updateTV.get_children()
for element in records:
updateTV.delete(element)
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor(pymysql.cursors.DictCursor)
query = "select * from itemlist"
cursor.execute(query)
data = cursor.fetchall()
for row in data:
updateTV.insert('','end',text=row['nameid'],values=(row['name'],row['rate'],row['type'],row['storetype']))
updateTV.bind("<Double-1>",OnDoubleClick)
conn.close()
def print_bill():
global itemLists
global totalCost
billString=""
billString+="=======================Receipt================\n\n"
billString+="==============================================\n"
billString+="{:<20}{:<10}{:<15}{:<10}\n".format("Name","Rate","Quantity","Cost")
billString+="==============================================\n"
for item in itemLists:
billString+="{:<20}{:<10}{:<15}{:<10}\n".format(item["name"],item["rate"],item["quantity"],item["cost"])
billString+="==============================================\n"
billString+="{:<20}{:<10}{:<15}{:<10}\n".format("Total Cost"," "," ",totalCost)
billFile = filedialog.asksaveasfile(mode='w',defaultextension=".txt")
if billFile is None:
messagebox.showerror("Invalid File Name","Please Enter Valid Name")
else:
billFile.write(billString)
billFile.close()
print(billString)
itemLists = []
totalCost = 0.0
updateListView()
totalCostVar.set("Total Cost = {}".format(totalCost))
#================ function to logout ==============
def LogOut():
remove_all_widgets()
loginWindow()
def moveToUpdate():
remove_all_widgets()
updateItemWindow()
#=============== function to move to view bills window ==========
def movetoBills():
remove_all_widgets()
viewAllBills()
#=========== function to read data from list of item ======
def readAllData():
global options
global rateDict
global itemVariable
global itemRate
global rateVar
options=[]
rateDict={}
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor(pymysql.cursors.DictCursor)
query = "select * from itemlist"
cursor.execute(query)
data = cursor.fetchall()
count=0
for row in data:
count+=1
options.append(row['nameid'])
rateDict[row['nameid']]=row['rate']
itemVariable.set(options[0])
itemRate = int(rateDict[options[0]])
conn.close()
rateVar.set("%.2f"%itemRate)
if count == 0:
remove_all_widgets()
itemAddWindow()
else:
remove_all_widgets()
mainWindow()
def optionMenuListener(event):
global itemVariable
global rateDict
global itemRate
item = itemVariable.get()
itemRate = int(rateDict[item])
rateVar.set("%.2f"%itemRate)
#============ function to remove all widget ==========
def remove_all_widgets():
global window
for widget in window.winfo_children():
widget.grid_remove()
#============ update bill data =============
def updateBillsData():
records = billsTV.get_children()
for element in records:
billsTV.delete(element)
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor(pymysql.cursors.DictCursor)
query = "select * from bill"
cursor.execute(query)
data = cursor.fetchall()
for row in data:
billsTV.insert('','end',text=row['name'],values=(row["rate"],row["quantity"],row["cost"]))
conn.close()
#============== admin login functiom ==============
def adminLogin():
global usernameVar
global passwordVar
username = usernameVar.get()
password = passwordVar.get()
if username == "" or password == "":
messagebox.showerror("Login","Kindly fill all Credentials")
loginWindow()
else:
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor()
query = "select * from users where username='{}'and password='{}'".format(username, password)
cursor.execute(query)
data = cursor.fetchall()
admin = False
for row in data:
admin = True
conn.close()
if admin:
readAllData()
else:
messagebox.showerror("Invalid user", "Credentials enters are Invalid")
def adminSignUp():
a=usernameVar.get()
b=passwordVar.get()
c=mobileNoVar.get()
d=emailVar.get()
conn=pymysql.connect(host="localhost",user="root",passwd="",db="billservice")
cursor=conn.cursor()
query="select username from users "
cursor.execute(query)
data=cursor.fetchall()
list(data)
f=0
for i in data:
p=list(i)
if a==i[0]:
f+=1
if f==0:
query="insert into users(username,password,mobileNo,emailId) value('{}','{}','{}','{}')".format(a,b,c,d)
cursor.execute(query)
conn.commit()
conn.close()
messagebox.showinfo("SignUp","Username and Password created")
loginWindow()
else:
messagebox.showerror("Signup","Username already in use Try another one")
signupWindow()
def addItemListener():
remove_all_widgets()
itemAddWindow()
def addItem():
global addItemNameVar
global addItemRateVar
global addItemTypeVar
global addStoredVar
name = addItemNameVar.get()
rate = addItemRateVar.get()
Type = addItemTypeVar.get()
storeType = addStoredVar.get()
nameId = name.replace(" ","_")
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor()
query = "insert into itemlist (name,nameid,rate,type,storetype) value('{}','{}','{}','{}','{}')".format(name, nameId, rate, Type,storeType)
cursor.execute(query)
conn.commit()
conn.close()
addItemNameVar.set("")
addItemRateVar.set("")
addItemTypeVar.set("")
def updateIem():
global addItemNameVar
global addItemRateVar
global addItemTypeVar
global addStoredVar
global updateItemId
name = addItemNameVar.get()
rate = addItemRateVar.get()
Type = addItemTypeVar.get()
storeType = addStoredVar.get()
conn = pymysql.connect(host="localhost", user="root", passwd="", db="billservice")
cursor = conn.cursor()
query = "update itemlist set name='{}',rate='{}',type='{}',storetype='{}' where nameid='{}'".format(name,rate,Type,storeType,updateItemId)
cursor.execute(query)
conn.commit()
conn.close()
addItemNameVar.set("")
addItemRateVar.set("")
addItemTypeVar.set("")
getItemLists()
def signupWindow():
remove_all_widgets()
usernameVar.set("")
passwordVar.set("")
mobileNoVar.set("")
emailVar.set("")
titleLabel = Label (window,text = "Billing System", font = "Arial 40", fg="green")
titleLabel.grid(row=0,column=0,columnspan=4,padx=(40,0),pady=(10,0))
loginLabel = Label(window,text="Admin Login", font = "Arial 30")
loginLabel.grid(row = 1, column=2,columnspan=2,padx=(50,0),pady=10)
usernameLabel = Label(window, text="Username",width=15,height=2)
usernameLabel.grid(row=2, column=2)
passwordLabel = Label(window, text="Password",width=15,height=2)
passwordLabel.grid(row=3, column=2)
EmailLabel = Label(window, text="Email",width=15,height=2)
EmailLabel.grid(row=4, column=2)
mobileNoLabel = Label(window, text="Mobile No.",width=15,height=2)
mobileNoLabel.grid(row=5, column=2)
usernameEntry= Entry(window, textvariable=usernameVar)
usernameEntry.grid(row=2, column=3)
passwordEntry = Entry (window, textvariable=passwordVar,show="*")
passwordEntry.grid(row=3,column=3)
mobileNoEntry= Entry(window, textvariable=mobileNoVar)
mobileNoEntry.grid(row=5, column=3)
EmailEntry = Entry (window, textvariable = emailVar)
EmailEntry.grid(row=4,column=3)
loginButton=Button(window,text="Login",width=20, height=2,command=lambda:loginWindow())
loginButton.grid(row=6,column=2)
signUpButton=Button(window,text="Sign Up",width=20, height=2,command=lambda:adminSignUp())
signUpButton.grid(row=6,column=3)
def loginWindow():
remove_all_widgets()
titleLabel = Label (window,text = "Billing System", font = "Arial 40", fg="green")
titleLabel.grid(row=0,column=0,columnspan=4,padx=(40,0),pady=(10,0))
loginLabel = Label(window,text="Admin Login", font = "Arial 30")
loginLabel.grid(row = 1, column=2,columnspan=2,padx=(50,0),pady=10)
usernameLabel = Label(window, text = "Username")
usernameLabel.grid(row=2, column=2,padx=20,pady=5)
passwordLabel = Label(window, text = "Password")
passwordLabel.grid(row=3, column=2,padx=20,pady=5)
usernameEntry = Entry(window, textvariable = usernameVar)
usernameEntry.grid(row=2,column=3,padx=20,pady=5)
passwordEntry = Entry(window, textvariable = passwordVar, show="*")
passwordEntry.grid(row=3,column=3,padx=20,pady=5)
loginButton = Button(window, text="Login", width = 20, height=2, command = lambda:adminLogin())
loginButton.grid(row=4,column=2)
signUpButton = Button(window, text="Sign Up", width = 20, height=2, command = lambda:signupWindow())
signUpButton.grid(row=4,column=3)
def mainWindow():
remove_all_widgets()
titleLabel = Label (window,text = "Billing System", font = "Arial 30", fg="green")
titleLabel.grid(row=0,column=1,columnspan=3,pady=(10,0))
addNewItem = Button(window, text = "Add Item", width=15, height=2, command= lambda:addItemListener())
addNewItem.grid(row=1, column=0,padx=(10,0),pady=(10,0))
updateItem = Button(window, text="Update Item", width=15, height=2, command=lambda:moveToUpdate())
updateItem.grid(row=1, column=1,padx=(10,0),pady=(10,0))
showallEntry = Button(window, text="Show Bills", width=15, height=2, command=lambda:movetoBills())
showallEntry.grid(row=1, column=2,padx=(10,0),pady=(10,0))
logoutBtn = Button(window, text = "Log out", width=15, height=2, command= lambda:LogOut())
logoutBtn.grid(row=1, column=4,pady=(10,0))
itemLabel = Label(window, text = "Select Item")
itemLabel.grid(row=2, column=0, padx=(5,0),pady=(10,0))
itemDropDown = OptionMenu(window,itemVariable,*options, command = optionMenuListener)
itemDropDown.grid(row=2, column=1, padx=(10,0), pady=(10,0))
rateLabel = Label(window, text = "Rate")
rateLabel.grid(row=2, column=2,padx=(10,0), pady=(10,0))
rateValue = Label(window, textvariable = rateVar)
rateValue.grid(row = 2, column = 3,padx=(10,0), pady=(10,0))
quantityLabel = Label(window, text = "Quantity")
quantityLabel.grid(row=3, column=0, padx= (5,0),pady=(10,0))
quantityEntry = Entry(window, textvariable=quantityVar)
quantityEntry.grid(row=3, column=1, padx= (5,0),pady=(10,0))
costLabel = Label(window, text = "Cost")
costLabel.grid(row=3, column=2, padx= (5,0),pady=(10,0))
costEntry = Entry(window, textvariable = costVar)
costEntry.grid(row=3, column=3, padx= (5,0),pady=(10,0))
buttonBill = Button(window,text="Add to List", width =10, command = lambda:generate_bill())
buttonBill.grid(row=3, column=4, padx=(5,0),pady=(10,0))
billLabel = Label(window, text = "Bills", font = "Arial 25")
billLabel.grid(row = 4, column = 2)
billsTV.grid(row = 5, column = 0, columnspan = 5)
scrollBar = Scrollbar(window, orient = "vertical", command = billsTV.yview)
scrollBar.grid(row = 5, column=4, sticky = "NSE")
billsTV.configure(yscrollcommand = scrollBar.set)
billsTV.heading('#0', text = "Item Name")
billsTV.heading('#1', text = "Rate")
billsTV.heading('#2', text = "Quantity")
billsTV.heading('#3', text = "Cost")
totalCostLabel = Label(window, textvariable = totalCostVar)
totalCostLabel.grid(row=6,column=1)
generateBill = Button(window, text="Generate Bills", width=15, command = lambda:print_bill())
generateBill.grid(row=6, column=4)
updateListView()
def itemAddWindow():
backButton = Button(window, text = "Back", command = lambda:readAllData())
backButton.grid(row=0,column=0)
titleLabel = Label (window,text = "Billing System",width=40, font = "Arial 30", fg="green")
titleLabel.grid(row=0,column=1,columnspan=5,pady=(10,0))
itemNameLabel = Label(window, text = "Name")
itemNameLabel.grid(row=1, column=1, pady=(10,0))
itemNameEntry = Entry(window, textvariable = addItemNameVar)
itemNameEntry.grid(row=1, column=2, pady=(10,0))
itemRateLabel = Label(window, text = "Rate")
itemRateLabel.grid(row=1, column=3, pady=(10,0))
itemRateEntry = Entry(window, textvariable = addItemRateVar)
itemRateEntry.grid(row=1, column=4, pady=(10,0))
itemTypeLabel = Label(window, text = "Type")
itemTypeLabel.grid(row=2, column=1, pady=(10,0))
itemTypeEntry = Entry(window, textvariable = addItemTypeVar)
itemTypeEntry.grid(row=2, column=2, pady=(10,0))
storeTypeLabel = Label(window, text = "Store Type")
storeTypeLabel.grid(row=2, column=3, pady=(10,0))
storeTypeEntry = OptionMenu(window, addStoredVar, *storeOptions)
storeTypeEntry.grid(row=2, column=4, pady=(10,0))
AddItemButton = Button(window, text = "Add Item", width=20, height=2, command = lambda:addItem())
AddItemButton.grid(row=3, column=3, pady=(10,0))
def updateItemWindow():
backButton = Button(window, text = "Back", command = lambda:readAllData())
backButton.grid(row=0,column=0)
titleLabel = Label (window,text = "Billing System",width=40, font = "Arial 30", fg="green")
titleLabel.grid(row=0,column=1,columnspan=5,pady=(10,0))
itemNameLabel = Label(window, text = "Name")
itemNameLabel.grid(row=1, column=1, pady=(10,0))
itemNameEntry = Entry(window, textvariable = addItemNameVar)
itemNameEntry.grid(row=1, column=2, pady=(10,0))
itemRateLabel = Label(window, text = "Rate")
itemRateLabel.grid(row=1, column=3, pady=(10,0))
itemRateEntry = Entry(window, textvariable = addItemRateVar)
itemRateEntry.grid(row=1, column=4, pady=(10,0))
itemTypeLabel = Label(window, text = "Type")
itemTypeLabel.grid(row=2, column=1, pady=(10,0))
itemTypeEntry = Entry(window, textvariable = addItemTypeVar)
itemTypeEntry.grid(row=2, column=2, pady=(10,0))
storeTypeLabel = Label(window, text = "Store Type")
storeTypeLabel.grid(row=2, column=3, pady=(10,0))
storeTypeEntry = OptionMenu(window, addStoredVar, *storeOptions)
storeTypeEntry.grid(row=2, column=4, pady=(10,0))
AddItemButton = Button(window, text = "Update Item", width=20, height=2, command = lambda:updateIem())
AddItemButton.grid(row=3, column=3, pady=(10,0))
updateTV.grid(row=4, column=0, columnspan=5)
scrollBar = Scrollbar(window, orient = "vertical", command = billsTV.yview)
scrollBar.grid(row = 4, column=4, sticky = "NSE")
updateTV.configure(yscrollcommand = scrollBar.set)
updateTV.heading('#0', text = "Item ID")
updateTV.column('#0',minwidth=0, width=150)
updateTV.heading('#1', text = "Item Name")
updateTV.column('#1',minwidth=0, width=170)
updateTV.heading('#2', text = "Rate")
updateTV.column('#2',minwidth=0, width=150)
updateTV.heading('#3', text = "Type")
updateTV.column('#3',minwidth=0, width=150)
updateTV.heading('#4', text = "Store Type")
updateTV.column('#4',minwidth=0, width=150)
getItemLists()
def viewAllBills():
backButton = Button(window, text = "Back", command = lambda:readAllData())
backButton.grid(row=0,column=0)
titleLabel = Label (window,text = "Billing System",width=40, font = "Arial 30", fg="green")
titleLabel.grid(row=0,column=1,columnspan=5,pady=(10,0))
billsTV.grid(row = 1, column = 0, columnspan = 5)
scrollBar = Scrollbar(window, orient = "vertical", command = billsTV.yview)
scrollBar.grid(row = 1, column=4, sticky = "NSE")
billsTV.configure(yscrollcommand = scrollBar.set)
billsTV.heading('#0', text = "Item Name")
billsTV.heading('#1', text = "Rate")
billsTV.heading('#2', text = "Quantity")
billsTV.heading('#3', text = "Cost")
updateBillsData()
loginWindow()
window.mainloop()