Amazing-Python-Scripts
355 строк · 16.6 Кб
1import sqlite3
2from tkinter import ttk
3from tkinter import *
4from tkinter import messagebox
5from Addtional_features import mycombobox, myentry
6import datetime
7import os
8
9# USER MENU
10
11
12class User:
13def __init__(self, mainw):
14self.mainw = mainw
15
16def user_mainmenu(self, a, b):
17self.mainframe = LabelFrame(
18self.mainw, width=800, height=140, bg="#f7f7f7")
19self.mainframe.place(x=330, y=120)
20mi = PhotoImage(file="images/items.png")
21mi = mi.subsample(a, b)
22self.aitems = Button(self.mainframe, text="Items", bd=5, font="roboto 11 bold",
23bg="cyan", image=mi, compound=TOP, command=self.builditemtable)
24self.aitems.image = mi
25self.aitems.place(x=260, y=17)
26mi = PhotoImage(file="images/invoice2.png")
27mi = mi.subsample(a, b)
28self.aitems = Button(self.mainframe, text="Invoice", bd=5, bg="cyan",
29font="roboto 11 bold", image=mi, compound=TOP, command=self.make_invoice)
30self.aitems.image = mi
31self.aitems.place(x=62, y=17)
32mi = PhotoImage(file="images/change1.png")
33mi = mi.subsample(a, b)
34self.changeuser = Button(self.mainframe, text="Sign out", bd=5,
35bg="cyan", font="roboto 11 bold", image=mi, compound=TOP)
36self.changeuser.image = mi
37self.changeuser.place(x=460, y=17)
38mi = PhotoImage(file="images/Door_Out-512.png")
39mi = mi.subsample(a, b)
40self.logout = Button(self.mainframe, text="Quit", bd=5,
41bg="cyan", font="roboto 11 bold", image=mi, compound=TOP)
42self.logout.image = mi
43self.logout.place(x=670, y=17)
44self.tableframe1 = Frame(
45self.mainw, width=150, height=400, bg="#FFFFFF")
46self.tableframe1.place(x=1230, y=270, anchor=NE)
47self.tableframe1info = self.tableframe1.place_info()
48self.tableframe = Frame(self.mainw, width=350,
49height=700, bg="#FFFFFF")
50self.tableframe.place(x=1110, y=300, anchor=NE)
51self.tableframeinfo = self.tableframe.place_info()
52self.entryframe = Frame(self.mainw, width=800,
53height=350, bg="#FFFFFF")
54self.entryframe.place(x=810, y=460+20)
55self.entryframeinfo = self.entryframe.place_info()
56self.entryframe1 = Frame(
57self.mainw, width=500, height=350, bg="#FFFFFF")
58self.entryframe1.place(x=230, y=470+20)
59self.entryframe1info = self.entryframe1.place_info()
60
61def builditemtable(self):
62self.entryframe.place_forget()
63self.entryframe1.place_forget()
64self.tableframe.place(self.tableframeinfo)
65self.tableframe1.place_forget()
66scrollbarx = Scrollbar(self.tableframe, orient=HORIZONTAL)
67scrollbary = Scrollbar(self.tableframe, orient=VERTICAL)
68self.tree = ttk.Treeview(self.tableframe, columns=("Product ID", "Product Name", "Description", "Category",
69'Price', 'Stocks'), selectmode="extended", height=18,
70yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
71self.tree.column('#0', stretch=NO, minwidth=0, width=0)
72self.tree.column('#1', stretch=NO, minwidth=0, width=100)
73self.tree.column('#2', stretch=NO, minwidth=0, width=150)
74self.tree.column('#3', stretch=NO, minwidth=0, width=150)
75self.tree.column('#4', stretch=NO, minwidth=0, width=100)
76self.tree.column('#5', stretch=NO, minwidth=0, width=100)
77self.tree.column('#6', stretch=NO, minwidth=0, width=100)
78self.tree.heading('Product ID', text="Product ID", anchor=W)
79self.tree.heading('Product Name', text="Product Name", anchor=W)
80self.tree.heading('Description', text="Description", anchor=W)
81self.tree.heading('Category', text="Category", anchor=W)
82self.tree.heading('Price', text="Price", anchor=W)
83self.tree.heading('Stocks', text="Stocks", anchor=W)
84self.tree.grid(row=1, column=0, sticky="W")
85scrollbary.config(command=self.tree.yview)
86scrollbarx.grid(row=2, column=0, sticky="we")
87scrollbarx.config(command=self.tree.xview)
88scrollbary.grid(row=1, column=1, sticky="ns", pady=30)
89self.getproducts()
90
91def getproducts(self):
92self.cur.execute("select * from products")
93productlist = self.cur.fetchall()
94for i in productlist:
95self.tree.insert('', 'end', values=(i))
96
97def make_invoice(self):
98self.tableframe.place_forget()
99self.entryframe.place(self.entryframeinfo)
100self.entryframe1.place(self.entryframe1info)
101self.tableframe1.place(self.tableframe1info)
102scrollbarx = Scrollbar(self.tableframe1, orient=HORIZONTAL)
103scrollbary = Scrollbar(self.tableframe1, orient=VERTICAL)
104self.tree = ttk.Treeview(self.tableframe1, columns=("Transaction ID", "Product ID", "Product Name",
105'Quantity', 'Price', 'Date', 'Time'), selectmode="browse", height=6, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
106self.tree.column('#0', stretch=NO, minwidth=0, width=0)
107self.tree.column('#1', stretch=NO, minwidth=0, width=140)
108self.tree.column('#2', stretch=NO, minwidth=0, width=150)
109self.tree.column('#3', stretch=NO, minwidth=0, width=170)
110self.tree.column('#4', stretch=NO, minwidth=0, width=130)
111self.tree.column('#5', stretch=NO, minwidth=0, width=130)
112self.tree.column('#6', stretch=NO, minwidth=0, width=130)
113self.tree.column('#7', stretch=NO, minwidth=0, width=130)
114self.tree.heading('Transaction ID', text="Transaction ID", anchor=W)
115self.tree.heading('Product ID', text="Product ID", anchor=W)
116self.tree.heading('Product Name', text="Product Name", anchor=W)
117self.tree.heading('Quantity', text="Quantity", anchor=W)
118self.tree.heading('Price', text="Price", anchor=W)
119self.tree.heading('Date', text="Date", anchor=W)
120self.tree.heading('Time', text="Time", anchor=W)
121self.tree.grid(row=1, column=0, sticky="W")
122scrollbary.config(command=self.tree.yview)
123scrollbarx.grid(row=2, column=0, sticky="we")
124scrollbarx.config(command=self.tree.xview)
125scrollbary.grid(row=1, column=1, sticky="ns", pady=30)
126self.tree.bind("<<TreeviewSelect>>", self.clicktranstable)
127self.user_input()
128
129def user_input(self):
130self.cur.execute('select max(trans_id) from sales')
131li = self.cur.fetchall()
132if (li[0][0] != None):
133self.transid = li[0][0] + 1
134else:
135self.transid = 100
136self.qty = StringVar(value=1)
137self.additem = StringVar()
138self.total = IntVar(value=0)
139Button(self.entryframe, text="Proceed", command=self.transtableadd, bd=10,
140width=8, height=7, bg="#FFFFFF", font="roboto 10").place(x=0, y=30)
141Button(self.entryframe, text="Add to cart", command=self.addtotrans, bd=10,
142width=10, height=3, bg="#FFFFFF", font="roboto 10").place(x=100, y=80)
143Button(self.entryframe, text="Remove", command=self.removecart, bd=10,
144width=10, height=3, bg="#FFFFFF", font="roboto 10").place(x=210, y=80)
145entercart = mycombobox(self.entryframe, width=20,
146textvariable=self.additem, font="roboto 12")
147entercart.place(x=100, y=30, height=30)
148cartqty = Entry(self.entryframe, textvariable=self.qty,
149width=9, bg="#ffffff", font="roboto 12")
150cartqty.place(x=320, y=30, height=30)
151carttotal = Entry(self.entryframe, textvariable=self.total,
152width=20, state='readonly', bg="#ffffff", font="roboto 12")
153carttotal.place(x=130, y=185, height=60)
154Label(self.entryframe, text="Quantity",
155font="roboto 12 bold", bg="#ffffff").place(x=318, y=0)
156Label(self.entryframe, text="Search",
157font="roboto 12 bold", bg="#ffffff").place(x=100, y=0)
158Label(self.entryframe, text="Amount Due",
159font="roboto 14 bold", bg="#ffffff").place(x=0, y=205)
160self.cur.execute("select max(invoice) from sales")
161self.invoice = self.cur.fetchall()
162self.invoice = self.invoice[0][0] + 1
163Label(self.tableframe1, text="Invoice No. "+str(self.invoice),
164font="roboto 14 bold", bg="#ffffff").grid(row=0, column=0)
165self.cur.execute("select product_desc,product_price from products")
166li = self.cur.fetchall()
167self.inventory = []
168self.desc_price = dict()
169for i in range(0, len(li)):
170if (self.inventory.count(li[i][0]) == 0):
171self.inventory.append(li[i][0])
172self.desc_price[li[i][0]] = li[i][1]
173entercart.set_completion_list(self.inventory)
174li = ['Product Id', 'Product Name', 'Price', 'Left Stock']
175va = 0
176for i in range(0, 4):
177Label(self.entryframe1, text=li[i], font="roboto 14 bold", bg="#FFFFFF").place(
178x=0, y=va)
179va += 65
180self.cartitemid = StringVar()
181self.cartitem = StringVar()
182self.cartitemprice = StringVar()
183self.cartitemstock = StringVar()
184Entry(self.entryframe1, textvariable=self.cartitemid, font="roboto 14",
185bg="#FFFFFF", width=25, state='readonly').place(x=162, y=0, height=40)
186Entry(self.entryframe1, textvariable=self.cartitem, font="roboto 14",
187bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65, height=40)
188Entry(self.entryframe1, textvariable=self.cartitemprice, font="roboto 14",
189bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65*2, height=40)
190Entry(self.entryframe1, textvariable=self.cartitemstock, font="roboto 14",
191bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65*3, height=40)
192self.id_qty = dict()
193self.cur.execute("select product_id from products")
194l = self.cur.fetchall()
195for i in range(0, len(l)):
196self.id_qty[l[i][0]] = 0
197
198def addtotrans(self):
199if (len(self.additem.get()) == 0 or self.inventory.count(self.additem.get()) == 0):
200messagebox.showerror("Error", "Product Not Found!")
201return
202else:
203if (not self.qty.get().isdigit()):
204messagebox.showerror('Error', 'Invalid quantity!')
205return
206if (int(self.qty.get()) <= 0):
207messagebox.showerror('Error', 'Invalid quantity!')
208return
209self.cur.execute(
210"select product_id,product_desc from products where product_desc = ? ", (self.additem.get(),))
211row = self.cur.fetchall()
212row = [list(row[0])]
213row[0].insert(0, self.transid)
214self.transid += 1
215row[0].append(int(self.qty.get()))
216row[0].append(
217(int(self.qty.get())*self.desc_price[self.additem.get()]))
218x = str(datetime.datetime.now().strftime("%d-%m-%y"))
219row[0].append(x)
220x = datetime.datetime.now()
221x = str(x.hour)+' : '+str(x.minute)+' : '+str(x.second)
222row[0].append(x)
223row = [tuple(row[0])]
224self.cartitemid.set(row[0][1])
225self.cartitemprice.set(self.desc_price[self.additem.get()])
226self.cartitem.set(row[0][2])
227self.cur.execute(
228"select stocks from products where product_id=?", (row[0][1],))
229li = self.cur.fetchall()
230if ((li[0][0]-self.id_qty[row[0][1]])-int(self.qty.get()) < 0):
231if (li[0][0] != 0):
232messagebox.showerror(
233'Error', 'Product with this quantity not available!')
234else:
235messagebox.showerror('Error', 'Product out of stock!')
236return
237self.id_qty[row[0][1]] += int(self.qty.get())
238self.cartitemstock.set(li[0][0]-self.id_qty[row[0][1]])
239for data in row:
240self.tree.insert('', 'end', values=(data))
241self.total.set(self.total.get() + (int(self.qty.get())
242* self.desc_price[self.additem.get()]))
243self.qty.set('1')
244self.additem.set('')
245
246def transtableadd(self):
247x = self.tree.get_children()
248if (len(x) == 0):
249messagebox.showerror('Error', 'Empty cart!')
250return
251if (messagebox.askyesno('Alert!', 'Do you want to proceed?') == False):
252return
253a = []
254self.cur.execute("select max(invoice) from sales")
255self.invoice = self.cur.fetchall()
256self.invoice = self.invoice[0][0]+1
257for i in x:
258l = self.tree.item(i)
259a.append(l['values'])
260for i in a:
261s = (str(i[5])).split('-')
262i[5] = s[2] + "-" + s[1] + "-" + s[0]
263self.cur.execute("insert into sales values (?,?,?,?,?,?)", (int(
264i[0]), int(self.invoice), int(i[1]), int(i[3]), i[5], i[6]))
265self.cur.execute(
266"select stocks from products where product_id=?", (int(i[1]),))
267l = self.cur.fetchall()
268self.cur.execute("update products set stocks=? where product_id=?",
269(l[0][0]-self.id_qty[str(i[1])], int(i[1])))
270self.base.commit()
271messagebox.showinfo('Success', 'Transaction Successful!')
272self.makeprint()
273self.tree.delete(*self.tree.get_children())
274self.cartitemstock.set('')
275self.cartitem.set('')
276self.cartitemid.set('')
277self.cartitemprice.set('')
278self.total.set(0)
279self.additem.set('')
280self.qty.set('1')
281self.cur.execute("select product_id from products")
282l = self.cur.fetchall()
283for i in range(0, len(l)):
284self.id_qty[l[i][0]] = 0
285self.make_invoice()
286
287def removecart(self):
288re = self.tree.selection()
289if (len(re) == 0):
290messagebox.showerror('Error', 'No cart selected')
291return
292if (messagebox.askyesno('Alert!', 'Remove cart?') == True):
293x = self.tree.get_children()
294re = re[0]
295l = []
296fi = []
297for i in x:
298if (i != re):
299l.append(tuple((self.tree.item(i))['values']))
300else:
301fi = ((self.tree.item(i))['values'])
302self.tree.delete(*self.tree.get_children())
303for i in l:
304self.tree.insert('', 'end', values=(i))
305self.cartitemstock.set('')
306self.cartitem.set('')
307self.cartitemid.set('')
308self.cartitemprice.set('')
309self.additem.set('')
310self.qty.set('1')
311self.id_qty[str(fi[1])] -= fi[3]
312self.total.set(self.total.get()-fi[4])
313return
314
315def makeprint(self):
316if messagebox.askyesno("Alert!", "Print this transaction?") == True:
317pass
318''''
319x = datetime.datetime.now()
320x = str(x.hour) + ' - ' + str(x.minute) + ' - ' + str(x.second)
321dir="E:\GUI\docs\ "
322if not os.path.exists(dir):
323os.makedirs(dir)
324shop="\t\t\t\t\t The Bake Shop\n"
325address="\t\t\t\t Dwarka Sec-12, New Delhi\n"
326phone="\t\t\t\t Phone-:1122334455\n"
327sample="\t\t\t\t\t Invoice No.-:"+str(self.invoice)+"\n"
328dt="\t\t\t\t "+x
329head="\n\n\t\t------------------------------------------------------\n"
330row="\t Sno.\t\tProduct Name\t\tQuantity\tTotal\n"
331final=shop+address+phone+sample+dt+head+row
332file_name=str(dir)+str(x)+".rtf"
333f=open(file_name,"x")
334f.write(final)
335x=self.tree.get_children()
336for i in range(0,len(x)):
337f.write("\t "+str(i+1)+"\t\t")
338for j in range(2,len(self.tree.item(x[i])['values'])-2):
339f.write(str(self.tree.item(x[i])['values'][j])+"\t\t")
340f.write("\n")
341f.close()
342'''
343
344def clicktranstable(self, event):
345cur = self.tree.selection()
346cur = self.tree.item(cur)
347li = cur['values']
348if (len(li) == 7):
349self.cartitemid.set((li[1]))
350self.cartitem.set((li[2]))
351self.cur.execute(
352"select product_price,stocks from products where product_id=?", (li[1],))
353li = self.cur.fetchall()
354self.cartitemprice.set(li[0][0])
355self.cartitemstock.set(li[0][1]-self.id_qty[self.cartitemid.get()])
356