forked from aidayg24/Shop-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
394 lines (345 loc) · 24.2 KB
/
admin.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
import csv # importing csv
from tkinter import ttk # importing ttk module
from tkinter import * # importing tkinter library
from tkinter import messagebox # importing messagebox module
import hashlib # importing hashlib module
import logging # importing tkinter library for log information
# Define the data logging format:
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="logfile.log",
level=logging.DEBUG,
format=LOG_FORMAT,
filemode='a')
logger = logging.getLogger()
class Admin: # Define the admin class to organize the admin's tasks and authority
def __init__(self):
file = open("admin info.txt", "r")
password = file.readline().split(",")
self.password = str(password[1]) # the password by default is 0000
self.hash_entry_password = None
def about(self): # This function displays information about the application
messagebox.showinfo("about maktab store",
"Project 4: Store Accounting\nprogramming by:\ntahere zare(raha),aida rostami,malihe mirzaii")
def login(self):
# A window will open so that the admin can enter her password
getpass = Toplevel() # making a top window
getpass.menubar = Menu(getpass)
getpass.helpmenu = Menu(getpass.menubar, tearoff=0)
getpass.helpmenu.add_command(label="About", command=self.about)
getpass.menubar.add_cascade(label="Help", menu=getpass.helpmenu)
getpass.config(menu=getpass.menubar) # display the menu
# Configure window rows and column:
getpass.rowconfigure(0, minsize=800, weight=1)
getpass.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(getpass, relief=RAISED, bd=2, bg='grey')
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=getpass.quit).grid(row=10, column=0,
sticky="ew", padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Define the main frame:
fr_main = Frame(getpass, relief=RAISED, bd=1)
# get password:
pas = Entry(fr_main, width=30)
# An asterisk is displayed when entering a password for security issues
pas.config(show='*')
lbl_pas = ttk.Label(fr_main, text="Your Password : ").grid(row=2, column=0)
pas.grid(row=2, column=1, sticky=W)
entry_password = pas.get().lower().strip().encode()
self.hash_entry_password = hashlib.md5(entry_password).hexdigest()
log_btn = Button(fr_main, text='Login', command=self.chekpassword).grid(row=3, column=1, padx=100, pady=6)
fr_main.grid(row=0, column=1, sticky="nsew")
getpass.mainloop() # creating a loop for the main window to store the changes
def chekpassword(self):
entrypass = self.hash_entry_password
if entrypass == self.password: # If the password entered is correct, the login window will open
logger.info("admin log in!")
loggingadmin = Toplevel() # making a top window
loggingadmin.menubar = Menu(loggingadmin)
loggingadmin.helpmenu = Menu(loggingadmin.menubar, tearoff=0)
loggingadmin.helpmenu.add_command(label="About", command=self.about)
loggingadmin.menubar.add_cascade(label="Help", menu=loggingadmin.helpmenu)
loggingadmin.config(menu=loggingadmin.menubar) # display the menu
# Configure window rows and column:
loggingadmin.rowconfigure(0, minsize=800, weight=1)
loggingadmin.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(loggingadmin, relief=RAISED, bd=2, bg='grey')
btn_add = Button(taskbar_frame, text='New Product', bg='VioletRed4', command=self.add_new_product).grid(
row=0, column=0, sticky="ew", padx=5, pady=5)
btn_invoice = Button(taskbar_frame, text='Invoices', bg='VioletRed4', command=self.show_invoices).grid(
row=1, column=0, sticky="ew", padx=5, pady=5)
btn_charge = Button(taskbar_frame, text='Charge Product', bg='VioletRed4',
command=self.charge_stock_by_admin).grid(row=3, column=0, sticky="ew", padx=5, pady=5)
btn_storeroom = Button(taskbar_frame, text='storeroom', bg='VioletRed4', command=self.seeproduct).grid(
row=4, column=0, sticky="ew", padx=5, pady=5)
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=loggingadmin.quit).grid(row=10, column=0,
sticky="ew",
padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Warns the admin if the stock is out of stock:
file = open("product.csv", 'r')
line_counter = 0
for line in file.readlines():
data = line.strip().split(",")
if line_counter > 0:
if int(data[4]) == 0:
messagebox.showerror('storeroom', 'Inventory with {} barcode is exhausted'.format(data[2]))
logger.warning('Inventory with {} barcode is exhausted'.format(data[2]))
line_counter += 1
loggingadmin.mainloop() # creating a loop for the main window to store the changes
else:
# If the password is not correct, it gives an error:
messagebox.showerror("Failed login", "Wrong password!\nTry again")
logger.error("login failed")
def add_new_product(self):
"""admin can add new product to the list of products and updates the entrepot"""
addproduct = Toplevel() # making a top window
addproduct.menubar = Menu(addproduct)
addproduct.helpmenu = Menu(addproduct.menubar, tearoff=0)
addproduct.helpmenu.add_command(label="About", command=self.about)
addproduct.menubar.add_cascade(label="Help", menu=addproduct.helpmenu)
addproduct.config(menu=addproduct.menubar) # display the menu
# Configure window rows and column:
addproduct.rowconfigure(0, minsize=800, weight=1)
addproduct.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(addproduct, relief=RAISED, bd=2, bg='grey')
btn_add = Button(taskbar_frame, text='New Product', bg='VioletRed4', command=self.add_new_product).grid(row=0,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_invoice = Button(taskbar_frame, text='Invoices', bg='VioletRed4', command=self.show_invoices).grid(row=1,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_charge = Button(taskbar_frame, text='Charge Product', bg='VioletRed4',
command=self.charge_stock_by_admin).grid(row=3, column=0, sticky="ew", padx=5, pady=5)
btn_storeroom = Button(taskbar_frame, text='storeroom', bg='VioletRed4', command=self.seeproduct).grid(row=4,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=addproduct.quit).grid(row=10, column=0,
sticky="ew", padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Define the main frame:
fr_main = Frame(addproduct, relief=RAISED, bd=1)
# Receives product information for registration from the admin
self.product_name = Entry(fr_main, width=30)
self.brand = Entry(fr_main, width=30)
self.barcode = Entry(fr_main, width=30)
self.price = Entry(fr_main, width=30)
self.stock = Entry(fr_main, width=30)
add_btn = Button(fr_main, text='Add', command=self.adding).grid(row=7, column=1, padx=100, pady=6)
lbl_product_name = ttk.Label(fr_main, text="product name : ").grid(row=2, column=0)
lbl_brand = ttk.Label(fr_main, text="brand : ").grid(row=3, column=0)
lbl_barcode = ttk.Label(fr_main, text="barcode : ").grid(row=4, column=0)
lbl_price = ttk.Label(fr_main, text="price : ").grid(row=5, column=0)
lbl_stock = ttk.Label(fr_main, text="stock : ").grid(row=6, column=0)
self.product_name.grid(row=2, column=1, sticky=W)
self.brand.grid(row=3, column=1, sticky=W)
self.barcode.grid(row=4, column=1, sticky=W)
self.price.grid(row=5, column=1, sticky=W)
self.stock.grid(row=6, column=1, sticky=W)
fr_main.grid(row=0, column=1, sticky="nsew")
addproduct.mainloop() # creating a loop for the main window to store the changes
def adding(self):
# In order to have less problems in the future, we will use .lower().strip()
productname = self.product_name.get().lower().strip()
brand = self.brand.get().lower().strip()
barcode = self.barcode.get().lower().strip()
price = self.price.get().lower().strip()
stock = self.stock.get().lower().strip()
# We save the entered information in the warehouse Excel file
with open('product.csv', 'a', newline='') as csvpr:
fieldnames = ['product name', 'brand', 'barcode', 'price', 'stock']
writer = csv.DictWriter(csvpr, fieldnames=fieldnames)
writer.writerow({'product name': productname,
'brand': brand,
'barcode': barcode,
'price': price,
'stock': stock})
logger.info("new product added")
messagebox.showinfo('Add product', 'new product added')
def show_invoices(self):
# A new window will open to display all registered invoices
seeinvokes = Toplevel() # making a top window
seeinvokes.menubar = Menu(seeinvokes)
seeinvokes.helpmenu = Menu(seeinvokes.menubar, tearoff=0)
seeinvokes.helpmenu.add_command(label="About", command=self.about)
seeinvokes.menubar.add_cascade(label="Help", menu=seeinvokes.helpmenu)
seeinvokes.config(menu=seeinvokes.menubar) # display the menu
# Configure window rows and column:
seeinvokes.rowconfigure(0, minsize=800, weight=1)
seeinvokes.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(seeinvokes, relief=RAISED, bd=2, bg='grey')
btn_add = Button(taskbar_frame, text='New Product', bg='VioletRed4', command=self.add_new_product).grid(row=0,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_invoice = Button(taskbar_frame, text='Invoices', bg='VioletRed4', command=self.show_invoices).grid(row=1,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_charge = Button(taskbar_frame, text='Charge Product', bg='VioletRed4',
command=self.charge_stock_by_admin).grid(row=3, column=0, sticky="ew", padx=5, pady=5)
btn_storeroom = Button(taskbar_frame, text='storeroom', bg='VioletRed4', command=self.seeproduct).grid(row=4,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=seeinvokes.quit).grid(row=10, column=0,
sticky="ew", padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Define the main frame:
fr_main = Frame(seeinvokes, relief=RAISED, bd=1)
# The invoices table is shown to her
file = open("invoice.csv", "r")
row = 1
for line in file.readlines():
data = line.strip().split(",")
productname = Label(fr_main, text=data[0].strip() + ' , ' + data[1].strip())
barcode = Label(fr_main, text=data[2].strip())
number = Label(fr_main, text=data[3].strip())
price = Label(fr_main, text=data[4].strip())
sum = Label(fr_main, text=data[5].strip())
totallsum = Label(fr_main, text=data[6].strip())
productname.grid(row=row, column=0, sticky="w", padx=5, pady=5)
barcode.grid(row=row, column=1, sticky="w", padx=5, pady=5)
number.grid(row=row, column=2, sticky="w", padx=5, pady=5)
price.grid(row=row, column=3, sticky="w", padx=5, pady=5)
sum.grid(row=row, column=4, sticky="w", padx=5, pady=5)
totallsum.grid(row=row, column=5, sticky="w", padx=5, pady=5)
row += 1
fr_main.grid(row=0, column=1, sticky="nsew")
seeinvokes.mainloop() # creating a loop for the main window to store the changes
def seeproduct(self):
# A window will open so that the admin can see a view of the warehouse
loggingadmin = Toplevel() # making a top window
loggingadmin.menubar = Menu(loggingadmin)
loggingadmin.helpmenu = Menu(loggingadmin.menubar, tearoff=0)
loggingadmin.helpmenu.add_command(label="About", command=self.about)
loggingadmin.menubar.add_cascade(label="Help", menu=loggingadmin.helpmenu)
loggingadmin.config(menu=loggingadmin.menubar) # display the menu
# Configure window rows and column:
loggingadmin.rowconfigure(0, minsize=800, weight=1)
loggingadmin.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(loggingadmin, relief=RAISED, bd=2, bg='grey')
btn_add = Button(taskbar_frame, text='New Product', bg='VioletRed4', command=self.add_new_product).grid(row=0,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_invoice = Button(taskbar_frame, text='Invoices', bg='VioletRed4', command=self.show_invoices).grid(row=1,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_charge = Button(taskbar_frame, text='Charge Product', bg='VioletRed4',
command=self.charge_stock_by_admin).grid(row=3, column=0, sticky="ew", padx=5, pady=5)
btn_storeroom = Button(taskbar_frame, text='storeroom', bg='VioletRed4', command=self.seeproduct).grid(row=4,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=loggingadmin.quit).grid(row=10, column=0,
sticky="ew", padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Define the main frame:
fr_main = Frame(loggingadmin, relief=RAISED, bd=1)
# Reads and displays inventory information from the file
file = open("product.csv", 'r')
row = 0
for line in file.readlines():
data = line.strip().split(",")
category = Label(fr_main, text=data[0])
brand = Label(fr_main, text=data[1])
barcode = Label(fr_main, text=data[2])
price = Label(fr_main, text=data[3])
stock = Label(fr_main, text=data[4])
category.grid(row=row, column=0, sticky="w", padx=5, pady=5)
brand.grid(row=row, column=1, sticky="w", padx=5, pady=5)
barcode.grid(row=row, column=2, sticky="w", padx=5, pady=5)
price.grid(row=row, column=3, sticky="w", padx=5, pady=5)
stock.grid(row=row, column=4, sticky="w", padx=5, pady=5)
row += 1
file.close()
fr_main.grid(row=0, column=1, sticky="nsew")
loggingadmin.mainloop() # creating a loop for the main window to store the changes
def charge_stock_by_admin(self):
# A window will open so that the admin can increase the inventory of the desired product by entering the information
charge = Toplevel() # making a top window
charge.menubar = Menu(charge)
charge.helpmenu = Menu(charge.menubar, tearoff=0)
charge.helpmenu.add_command(label="About", command=self.about)
charge.menubar.add_cascade(label="Help", menu=charge.helpmenu)
charge.config(menu=charge.menubar) # display the menu
# Configure window rows and column:
charge.rowconfigure(0, minsize=800, weight=1)
charge.columnconfigure(1, minsize=800, weight=1)
# Definition of taskbar:
taskbar_frame = Frame(charge, relief=RAISED, bd=2, bg='grey')
btn_add = Button(taskbar_frame, text='New Product', bg='VioletRed4', command=self.add_new_product).grid(row=0,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_invoice = Button(taskbar_frame, text='Invoices', bg='VioletRed4', command=self.show_invoices).grid(row=1,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_charge = Button(taskbar_frame, text='Charge Product', bg='VioletRed4',
command=self.charge_stock_by_admin).grid(row=3, column=0, sticky="ew", padx=5, pady=5)
btn_storeroom = Button(taskbar_frame, text='storeroom', bg='VioletRed4', command=self.seeproduct).grid(row=4,
column=0,
sticky="ew",
padx=5,
pady=5)
btn_exit = Button(taskbar_frame, text="Exit", bg='red4', command=charge.quit).grid(row=10, column=0,
sticky="ew", padx=5)
taskbar_frame.grid(row=0, column=0, sticky="ns")
# Define the main frame:
fr_main = Frame(charge, relief=RAISED, bd=1)
self.barcode = Entry(fr_main, width=30)
# The admin determines the desired charge amount in the allowable range by dragging the scale:
self.num = Scale(fr_main, from_=0, to=100, orient=HORIZONTAL)
charge_btn = Button(fr_main, text='Charge', command=self.charged).grid(row=4, column=1, padx=100, pady=6)
lbl_barcode = ttk.Label(fr_main, text="barcode : ").grid(row=2, column=0)
lbl_num = ttk.Label(fr_main, text="How many? ").grid(row=3, column=0)
self.barcode.grid(row=2, column=1, sticky=W)
self.num.grid(row=3, column=1, sticky=W)
fr_main.grid(row=0, column=1, sticky="nsew")
charge.mainloop() # creating a loop for the main window to store the changes
def charged(self):
""" the function opens the csv file that contains the list of products
and updates the stock(number of a product) after admin charge it."""
file = open("product.csv", 'r')
file_data = file.readlines()
file.close()
file_overwrite = open("product.csv", 'w')
line_counter = 1
for line in file_data:
if line_counter > 1:
data = line.strip().split(",")
if data[2].strip() == self.barcode.get().lower().strip():
stock = int(str(data[4]))
stock += int(self.num.get())
data[4] = str(stock)
new_data = ",".join(data)
file_overwrite.write(new_data + "\n")
else:
new_data = ",".join(data)
file_overwrite.write(new_data + "\n")
else:
file_overwrite.write(line)
line_counter+=1
file_overwrite.close()
messagebox.showinfo('charge product', 'inventory increased')
logger.info('The inventory was charged')