Amazing-Python-Scripts

Форк
0
355 строк · 16.6 Кб
1
import sqlite3
2
from tkinter import ttk
3
from tkinter import *
4
from tkinter import messagebox
5
from Addtional_features import mycombobox, myentry
6
import datetime
7
import os
8

9
# USER MENU
10

11

12
class User:
13
    def __init__(self, mainw):
14
        self.mainw = mainw
15

16
    def user_mainmenu(self, a, b):
17
        self.mainframe = LabelFrame(
18
            self.mainw, width=800, height=140, bg="#f7f7f7")
19
        self.mainframe.place(x=330, y=120)
20
        mi = PhotoImage(file="images/items.png")
21
        mi = mi.subsample(a, b)
22
        self.aitems = Button(self.mainframe, text="Items", bd=5, font="roboto 11 bold",
23
                             bg="cyan", image=mi, compound=TOP, command=self.builditemtable)
24
        self.aitems.image = mi
25
        self.aitems.place(x=260, y=17)
26
        mi = PhotoImage(file="images/invoice2.png")
27
        mi = mi.subsample(a, b)
28
        self.aitems = Button(self.mainframe, text="Invoice", bd=5, bg="cyan",
29
                             font="roboto 11 bold", image=mi, compound=TOP, command=self.make_invoice)
30
        self.aitems.image = mi
31
        self.aitems.place(x=62, y=17)
32
        mi = PhotoImage(file="images/change1.png")
33
        mi = mi.subsample(a, b)
34
        self.changeuser = Button(self.mainframe, text="Sign out", bd=5,
35
                                 bg="cyan", font="roboto 11 bold", image=mi, compound=TOP)
36
        self.changeuser.image = mi
37
        self.changeuser.place(x=460, y=17)
38
        mi = PhotoImage(file="images/Door_Out-512.png")
39
        mi = mi.subsample(a, b)
40
        self.logout = Button(self.mainframe, text="Quit", bd=5,
41
                             bg="cyan", font="roboto 11 bold", image=mi, compound=TOP)
42
        self.logout.image = mi
43
        self.logout.place(x=670, y=17)
44
        self.tableframe1 = Frame(
45
            self.mainw, width=150, height=400, bg="#FFFFFF")
46
        self.tableframe1.place(x=1230, y=270, anchor=NE)
47
        self.tableframe1info = self.tableframe1.place_info()
48
        self.tableframe = Frame(self.mainw, width=350,
49
                                height=700, bg="#FFFFFF")
50
        self.tableframe.place(x=1110, y=300, anchor=NE)
51
        self.tableframeinfo = self.tableframe.place_info()
52
        self.entryframe = Frame(self.mainw, width=800,
53
                                height=350, bg="#FFFFFF")
54
        self.entryframe.place(x=810, y=460+20)
55
        self.entryframeinfo = self.entryframe.place_info()
56
        self.entryframe1 = Frame(
57
            self.mainw, width=500, height=350, bg="#FFFFFF")
58
        self.entryframe1.place(x=230, y=470+20)
59
        self.entryframe1info = self.entryframe1.place_info()
60

61
    def builditemtable(self):
62
        self.entryframe.place_forget()
63
        self.entryframe1.place_forget()
64
        self.tableframe.place(self.tableframeinfo)
65
        self.tableframe1.place_forget()
66
        scrollbarx = Scrollbar(self.tableframe, orient=HORIZONTAL)
67
        scrollbary = Scrollbar(self.tableframe, orient=VERTICAL)
68
        self.tree = ttk.Treeview(self.tableframe, columns=("Product ID", "Product Name", "Description", "Category",
69
                                                           'Price', 'Stocks'), selectmode="extended", height=18,
70
                                 yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
71
        self.tree.column('#0', stretch=NO, minwidth=0, width=0)
72
        self.tree.column('#1', stretch=NO, minwidth=0, width=100)
73
        self.tree.column('#2', stretch=NO, minwidth=0, width=150)
74
        self.tree.column('#3', stretch=NO, minwidth=0, width=150)
75
        self.tree.column('#4', stretch=NO, minwidth=0, width=100)
76
        self.tree.column('#5', stretch=NO, minwidth=0, width=100)
77
        self.tree.column('#6', stretch=NO, minwidth=0, width=100)
78
        self.tree.heading('Product ID', text="Product ID", anchor=W)
79
        self.tree.heading('Product Name', text="Product Name", anchor=W)
80
        self.tree.heading('Description', text="Description", anchor=W)
81
        self.tree.heading('Category', text="Category", anchor=W)
82
        self.tree.heading('Price', text="Price", anchor=W)
83
        self.tree.heading('Stocks', text="Stocks", anchor=W)
84
        self.tree.grid(row=1, column=0, sticky="W")
85
        scrollbary.config(command=self.tree.yview)
86
        scrollbarx.grid(row=2, column=0, sticky="we")
87
        scrollbarx.config(command=self.tree.xview)
88
        scrollbary.grid(row=1, column=1, sticky="ns", pady=30)
89
        self.getproducts()
90

91
    def getproducts(self):
92
        self.cur.execute("select * from products")
93
        productlist = self.cur.fetchall()
94
        for i in productlist:
95
            self.tree.insert('', 'end', values=(i))
96

97
    def make_invoice(self):
98
        self.tableframe.place_forget()
99
        self.entryframe.place(self.entryframeinfo)
100
        self.entryframe1.place(self.entryframe1info)
101
        self.tableframe1.place(self.tableframe1info)
102
        scrollbarx = Scrollbar(self.tableframe1, orient=HORIZONTAL)
103
        scrollbary = Scrollbar(self.tableframe1, orient=VERTICAL)
104
        self.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)
106
        self.tree.column('#0', stretch=NO, minwidth=0, width=0)
107
        self.tree.column('#1', stretch=NO, minwidth=0, width=140)
108
        self.tree.column('#2', stretch=NO, minwidth=0, width=150)
109
        self.tree.column('#3', stretch=NO, minwidth=0, width=170)
110
        self.tree.column('#4', stretch=NO, minwidth=0, width=130)
111
        self.tree.column('#5', stretch=NO, minwidth=0, width=130)
112
        self.tree.column('#6', stretch=NO, minwidth=0, width=130)
113
        self.tree.column('#7', stretch=NO, minwidth=0, width=130)
114
        self.tree.heading('Transaction ID', text="Transaction ID", anchor=W)
115
        self.tree.heading('Product ID', text="Product ID", anchor=W)
116
        self.tree.heading('Product Name', text="Product Name", anchor=W)
117
        self.tree.heading('Quantity', text="Quantity", anchor=W)
118
        self.tree.heading('Price', text="Price", anchor=W)
119
        self.tree.heading('Date', text="Date", anchor=W)
120
        self.tree.heading('Time', text="Time", anchor=W)
121
        self.tree.grid(row=1, column=0, sticky="W")
122
        scrollbary.config(command=self.tree.yview)
123
        scrollbarx.grid(row=2, column=0, sticky="we")
124
        scrollbarx.config(command=self.tree.xview)
125
        scrollbary.grid(row=1, column=1, sticky="ns", pady=30)
126
        self.tree.bind("<<TreeviewSelect>>", self.clicktranstable)
127
        self.user_input()
128

129
    def user_input(self):
130
        self.cur.execute('select max(trans_id) from sales')
131
        li = self.cur.fetchall()
132
        if (li[0][0] != None):
133
            self.transid = li[0][0] + 1
134
        else:
135
            self.transid = 100
136
        self.qty = StringVar(value=1)
137
        self.additem = StringVar()
138
        self.total = IntVar(value=0)
139
        Button(self.entryframe, text="Proceed", command=self.transtableadd, bd=10,
140
               width=8, height=7, bg="#FFFFFF", font="roboto 10").place(x=0, y=30)
141
        Button(self.entryframe, text="Add to cart", command=self.addtotrans, bd=10,
142
               width=10, height=3, bg="#FFFFFF", font="roboto 10").place(x=100, y=80)
143
        Button(self.entryframe, text="Remove", command=self.removecart, bd=10,
144
               width=10, height=3, bg="#FFFFFF", font="roboto 10").place(x=210, y=80)
145
        entercart = mycombobox(self.entryframe, width=20,
146
                               textvariable=self.additem, font="roboto 12")
147
        entercart.place(x=100, y=30, height=30)
148
        cartqty = Entry(self.entryframe, textvariable=self.qty,
149
                        width=9, bg="#ffffff", font="roboto 12")
150
        cartqty.place(x=320, y=30, height=30)
151
        carttotal = Entry(self.entryframe, textvariable=self.total,
152
                          width=20, state='readonly', bg="#ffffff", font="roboto 12")
153
        carttotal.place(x=130, y=185, height=60)
154
        Label(self.entryframe, text="Quantity",
155
              font="roboto 12 bold", bg="#ffffff").place(x=318, y=0)
156
        Label(self.entryframe, text="Search",
157
              font="roboto 12 bold", bg="#ffffff").place(x=100, y=0)
158
        Label(self.entryframe, text="Amount Due",
159
              font="roboto 14 bold", bg="#ffffff").place(x=0, y=205)
160
        self.cur.execute("select max(invoice) from sales")
161
        self.invoice = self.cur.fetchall()
162
        self.invoice = self.invoice[0][0] + 1
163
        Label(self.tableframe1, text="Invoice No. "+str(self.invoice),
164
              font="roboto 14 bold", bg="#ffffff").grid(row=0, column=0)
165
        self.cur.execute("select product_desc,product_price from products")
166
        li = self.cur.fetchall()
167
        self.inventory = []
168
        self.desc_price = dict()
169
        for i in range(0, len(li)):
170
            if (self.inventory.count(li[i][0]) == 0):
171
                self.inventory.append(li[i][0])
172
            self.desc_price[li[i][0]] = li[i][1]
173
        entercart.set_completion_list(self.inventory)
174
        li = ['Product Id', 'Product Name', 'Price', 'Left Stock']
175
        va = 0
176
        for i in range(0, 4):
177
            Label(self.entryframe1, text=li[i], font="roboto 14 bold", bg="#FFFFFF").place(
178
                x=0, y=va)
179
            va += 65
180
        self.cartitemid = StringVar()
181
        self.cartitem = StringVar()
182
        self.cartitemprice = StringVar()
183
        self.cartitemstock = StringVar()
184
        Entry(self.entryframe1, textvariable=self.cartitemid, font="roboto 14",
185
              bg="#FFFFFF", width=25, state='readonly').place(x=162, y=0, height=40)
186
        Entry(self.entryframe1, textvariable=self.cartitem, font="roboto 14",
187
              bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65, height=40)
188
        Entry(self.entryframe1, textvariable=self.cartitemprice, font="roboto 14",
189
              bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65*2, height=40)
190
        Entry(self.entryframe1, textvariable=self.cartitemstock, font="roboto 14",
191
              bg="#FFFFFF", width=25, state='readonly').place(x=162, y=65*3, height=40)
192
        self.id_qty = dict()
193
        self.cur.execute("select product_id from products")
194
        l = self.cur.fetchall()
195
        for i in range(0, len(l)):
196
            self.id_qty[l[i][0]] = 0
197

198
    def addtotrans(self):
199
        if (len(self.additem.get()) == 0 or self.inventory.count(self.additem.get()) == 0):
200
            messagebox.showerror("Error", "Product Not Found!")
201
            return
202
        else:
203
            if (not self.qty.get().isdigit()):
204
                messagebox.showerror('Error', 'Invalid quantity!')
205
                return
206
            if (int(self.qty.get()) <= 0):
207
                messagebox.showerror('Error', 'Invalid quantity!')
208
                return
209
            self.cur.execute(
210
                "select product_id,product_desc from products where product_desc = ? ", (self.additem.get(),))
211
            row = self.cur.fetchall()
212
            row = [list(row[0])]
213
            row[0].insert(0, self.transid)
214
            self.transid += 1
215
            row[0].append(int(self.qty.get()))
216
            row[0].append(
217
                (int(self.qty.get())*self.desc_price[self.additem.get()]))
218
            x = str(datetime.datetime.now().strftime("%d-%m-%y"))
219
            row[0].append(x)
220
            x = datetime.datetime.now()
221
            x = str(x.hour)+' : '+str(x.minute)+' : '+str(x.second)
222
            row[0].append(x)
223
            row = [tuple(row[0])]
224
            self.cartitemid.set(row[0][1])
225
            self.cartitemprice.set(self.desc_price[self.additem.get()])
226
            self.cartitem.set(row[0][2])
227
            self.cur.execute(
228
                "select stocks from products where product_id=?", (row[0][1],))
229
            li = self.cur.fetchall()
230
            if ((li[0][0]-self.id_qty[row[0][1]])-int(self.qty.get()) < 0):
231
                if (li[0][0] != 0):
232
                    messagebox.showerror(
233
                        'Error', 'Product with this quantity not available!')
234
                else:
235
                    messagebox.showerror('Error', 'Product out of stock!')
236
                return
237
            self.id_qty[row[0][1]] += int(self.qty.get())
238
            self.cartitemstock.set(li[0][0]-self.id_qty[row[0][1]])
239
            for data in row:
240
                self.tree.insert('', 'end', values=(data))
241
            self.total.set(self.total.get() + (int(self.qty.get())
242
                           * self.desc_price[self.additem.get()]))
243
            self.qty.set('1')
244
            self.additem.set('')
245

246
    def transtableadd(self):
247
        x = self.tree.get_children()
248
        if (len(x) == 0):
249
            messagebox.showerror('Error', 'Empty cart!')
250
            return
251
        if (messagebox.askyesno('Alert!', 'Do you want to proceed?') == False):
252
            return
253
        a = []
254
        self.cur.execute("select max(invoice) from sales")
255
        self.invoice = self.cur.fetchall()
256
        self.invoice = self.invoice[0][0]+1
257
        for i in x:
258
            l = self.tree.item(i)
259
            a.append(l['values'])
260
        for i in a:
261
            s = (str(i[5])).split('-')
262
            i[5] = s[2] + "-" + s[1] + "-" + s[0]
263
            self.cur.execute("insert into sales values (?,?,?,?,?,?)", (int(
264
                i[0]), int(self.invoice), int(i[1]), int(i[3]), i[5], i[6]))
265
            self.cur.execute(
266
                "select stocks from products where product_id=?", (int(i[1]),))
267
            l = self.cur.fetchall()
268
            self.cur.execute("update products set stocks=? where product_id=?",
269
                             (l[0][0]-self.id_qty[str(i[1])], int(i[1])))
270
            self.base.commit()
271
        messagebox.showinfo('Success', 'Transaction Successful!')
272
        self.makeprint()
273
        self.tree.delete(*self.tree.get_children())
274
        self.cartitemstock.set('')
275
        self.cartitem.set('')
276
        self.cartitemid.set('')
277
        self.cartitemprice.set('')
278
        self.total.set(0)
279
        self.additem.set('')
280
        self.qty.set('1')
281
        self.cur.execute("select product_id from products")
282
        l = self.cur.fetchall()
283
        for i in range(0, len(l)):
284
            self.id_qty[l[i][0]] = 0
285
        self.make_invoice()
286

287
    def removecart(self):
288
        re = self.tree.selection()
289
        if (len(re) == 0):
290
            messagebox.showerror('Error', 'No cart selected')
291
            return
292
        if (messagebox.askyesno('Alert!', 'Remove cart?') == True):
293
            x = self.tree.get_children()
294
            re = re[0]
295
            l = []
296
            fi = []
297
            for i in x:
298
                if (i != re):
299
                    l.append(tuple((self.tree.item(i))['values']))
300
                else:
301
                    fi = ((self.tree.item(i))['values'])
302
            self.tree.delete(*self.tree.get_children())
303
            for i in l:
304
                self.tree.insert('', 'end', values=(i))
305
            self.cartitemstock.set('')
306
            self.cartitem.set('')
307
            self.cartitemid.set('')
308
            self.cartitemprice.set('')
309
            self.additem.set('')
310
            self.qty.set('1')
311
            self.id_qty[str(fi[1])] -= fi[3]
312
            self.total.set(self.total.get()-fi[4])
313
            return
314

315
    def makeprint(self):
316
        if messagebox.askyesno("Alert!", "Print this transaction?") == True:
317
            pass
318
        ''''
319
           x = datetime.datetime.now()
320
           x = str(x.hour) + ' - ' + str(x.minute) + ' - ' + str(x.second)
321
           dir="E:\GUI\docs\ "
322
           if not os.path.exists(dir):
323
               os.makedirs(dir)
324
           shop="\t\t\t\t\t  The Bake Shop\n"
325
           address="\t\t\t\t Dwarka Sec-12, New Delhi\n"
326
           phone="\t\t\t\t     Phone-:1122334455\n"
327
           sample="\t\t\t\t\t   Invoice No.-:"+str(self.invoice)+"\n"
328
           dt="\t\t\t\t      "+x
329
           head="\n\n\t\t------------------------------------------------------\n"
330
           row="\t     Sno.\t\tProduct Name\t\tQuantity\tTotal\n"
331
           final=shop+address+phone+sample+dt+head+row
332
           file_name=str(dir)+str(x)+".rtf"
333
           f=open(file_name,"x")
334
           f.write(final)
335
           x=self.tree.get_children()
336
           for i in range(0,len(x)):
337
               f.write("\t     "+str(i+1)+"\t\t")
338
               for j in range(2,len(self.tree.item(x[i])['values'])-2):
339
                   f.write(str(self.tree.item(x[i])['values'][j])+"\t\t")
340
               f.write("\n")
341
           f.close()
342
       '''
343

344
    def clicktranstable(self, event):
345
        cur = self.tree.selection()
346
        cur = self.tree.item(cur)
347
        li = cur['values']
348
        if (len(li) == 7):
349
            self.cartitemid.set((li[1]))
350
            self.cartitem.set((li[2]))
351
            self.cur.execute(
352
                "select product_price,stocks from products where product_id=?", (li[1],))
353
            li = self.cur.fetchall()
354
            self.cartitemprice.set(li[0][0])
355
            self.cartitemstock.set(li[0][1]-self.id_qty[self.cartitemid.get()])
356

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.