Advertisement
enkacang

Untitled

Jun 9th, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.30 KB | None | 0 0
  1. import mysql.connector
  2. from tkinter import *
  3. from tkinter import ttk
  4. from tkinter import messagebox
  5. from datetime import datetime
  6.  
  7. host_name = "localhost"
  8. user = "root"
  9. password = "root"
  10. database = "miniProject"
  11.  
  12. purchase_db_location = ""
  13.  
  14. # ------------------------- Database ---------------------------#
  15.  
  16. mydb = mysql.connector.connect(
  17.   host= host_name,
  18.   user= user,
  19.   password= password,
  20.   database = database
  21. )
  22. mycursor = mydb.cursor()
  23.  
  24. ##mycursor.execute("CREATE DATABASE purchase")
  25.  
  26. #mycursor.execute("CREATE TABLE purchase (id VARCHAR(20) PRIMARY KEY, item VARCHAR(255), price DECIMAL(20,2))")
  27. #mycursor.execute("CREATE TABLE stock (item VARCHAR(255), stock varchar(25), price DECIMAL(20,2))")
  28.  
  29. #item1 = "Vitamin C"
  30. #stock = "100"
  31. #rice = 150
  32. #sql = "INSERT INTO stock (item,stock,price) VALUES(%s,%s,%s)"
  33. #val = (item1,stock,price)
  34. #mycursor.execute(sql,val)
  35. #mydb.commit()
  36.  
  37.  
  38. def insertDb():
  39.     global id, item
  40.     tprice = 0
  41.     productList=[]
  42.  
  43.     mydb = mysql.connector.connect(
  44.         host= host_name,
  45.         user= user,
  46.         password= password,
  47.         database = database
  48.     )
  49.  
  50.     mycursor = mydb.cursor()
  51.  
  52.     last_value = MenuList.index("end")
  53.  
  54.     print(last_value)
  55.  
  56.     last_value = MenuList.index("end")
  57.  
  58.     for x in range(1,last_value):
  59.         getValue = MenuList.get(x)
  60.         print(getValue,"here")
  61.         tprice += int(getValue[4])
  62.         productD = getValue[0]+" = " + getValue[2] + ","
  63.         productList.insert(x,productD)
  64.  
  65.         product = menu.get()
  66.         quantity = getValue[2]
  67.  
  68.         print(product,quantity)
  69.  
  70.         val = "Select stock from stock where item ='" + product + "';"
  71.         mycursor.execute(val)
  72.         myresult = mycursor.fetchone()
  73.  
  74.         print(myresult)
  75.         listToStrStock = ' '.join([str(elem) for elem in myresult])
  76.         newStock = str(int(listToStrStock) - int(quantity))
  77.  
  78.         sql = "UPDATE stock set stock='"+newStock+"' Where item ='" + product + "';"
  79.         mycursor.execute(sql)
  80.  
  81.         mydb.commit()
  82.  
  83.        
  84.  
  85.     print(productList)
  86.     print(tprice)
  87.  
  88.     listToStr = ' '.join([str(elem) for elem in productList])
  89.     resit_id = datetime.now().strftime("%d%m%Y%H%M%S")
  90.  
  91.     sql = "INSERT INTO purchase (id,item,price) VALUES(%s,%s,%s)"
  92.     val = (resit_id,listToStr,tprice)
  93.  
  94.     mycursor.execute(sql,val)
  95.  
  96.     mydb.commit()
  97.  
  98.     print("Sucecs")
  99.  
  100.     MenuList.delete(0,END)
  101.     txtQuantity.delete(0,END)
  102.     ayat = "productName" , ":" , "quantity"  , ":" , 0 ,"totalPrice"
  103.     MenuList.insert(0, ayat)
  104.  
  105.     messagebox.showinfo("showinfo", "Succesfull Purchase")
  106.  
  107.     ##---------------------------------------------------- INSERT INTO DB
  108.  
  109.  
  110.  
  111.  
  112. def insertStockDb():
  113.     mydb = mysql.connector.connect(
  114.         host= host_name,
  115.         user= user,
  116.         password= password,
  117.         database = database
  118.     )
  119.  
  120.     mycursor = mydb.cursor()
  121.  
  122.     product = menu1.get()
  123.     quantity = txtQuantity1.get()
  124.     val = "Select stock from stock where item ='" + product + "';"
  125.     mycursor.execute(val)
  126.     myresult = mycursor.fetchone()
  127.  
  128.     print(myresult)
  129.     listToStrStock = ' '.join([str(elem) for elem in myresult])
  130.     newStock = str(int(listToStrStock) + int(quantity))
  131.  
  132.     sql = "UPDATE stock set stock='"+newStock+"' Where item ='" + product + "';"
  133.     mycursor.execute(sql)
  134.  
  135.     mydb.commit()
  136.  
  137.     messagebox.showinfo("showinfo", "Succesfull Add Stock")
  138.  
  139. # ------------------------- Function ---------------------------#
  140. def view_purchase():
  141.     root = Tk()
  142.     root.title("View Purchase - KEDAI RUNCIT PAK ALI")
  143.     root.geometry("800x420")
  144.  
  145.     mydb = mysql.connector.connect(
  146.         host= host_name,
  147.         user= user,
  148.         password= password,
  149.         database = database
  150.     )
  151.  
  152.     mycursor = mydb.cursor()
  153.  
  154.     string_view = ""
  155.  
  156.     val = "Select * from purchase;"
  157.     mycursor.execute(val)
  158.     myresult = mycursor.fetchall()
  159.  
  160.     for row in myresult:
  161.             string_view = string_view + ("-ID : {} -ITEM : {} -Price: RM{} \n".format(row[0],row[1],row[2]))
  162.  
  163.     print("Total rows are:  ", len(myresult))
  164.  
  165.     #----------------------------------------------------- TKINTER HANDLER
  166.  
  167.     T = Text(root, height = 50, width = 100, bg = "light yellow")
  168.     T.insert(1.0, string_view)
  169.     T.pack()
  170.  
  171.     btnCancel = Button(root, text ="Close",width=10, bg='brown', command=root.destroy)
  172.     btnCancel.place(x=700,y=380)
  173.  
  174.     root.mainloop()
  175.  
  176. def add_stock():
  177.     global menu1
  178.     global txtQuantity1
  179.  
  180.     root = Tk()
  181.     root.title("Add Stock - KEDAI RUNCIT PAK ALI")
  182.     root.geometry("400x150")
  183.     menu1 = ttk.Combobox(root, values=["Fish Cake", "Printer Ink","Eraser","Herbs","Water Bottle", "Vitamin C"])
  184.     menu1.current(0)
  185.     menu1.place(x=40 , y=40)
  186.     label1 = Label(root, text = "Quantity")
  187.     label1.place(x = 200,y = 20)
  188.     txtQuantity1 = Entry(root, textvariable="txtQuantity")
  189.     txtQuantity1.place(x=200,y=40)
  190.     btnAdd1 = Button(root, text ="Submit",width=10, bg='brown', command=insertStockDb)
  191.     btnAdd1.place(x=150,y=80)
  192.     root.mainloop()
  193.  
  194. def add_stock_to_database():
  195.     pass
  196.  
  197. def view_stock():
  198.     root = Tk()
  199.     root.title("View Stock - KEDAI RUNCIT PAK ALI")
  200.     root.geometry("800x420")
  201.  
  202.     mydb = mysql.connector.connect(
  203.         host= host_name,
  204.         user= user,
  205.         password= password,
  206.         database = database
  207.     )
  208.  
  209.     mycursor = mydb.cursor()
  210.  
  211.     string_view = ""
  212.  
  213.     val = "Select * from stock;"
  214.     mycursor.execute(val)
  215.     myresult = mycursor.fetchall()
  216.  
  217.     for row in myresult:
  218.             string_view = string_view + ("-ITEM : {} -STOCK : {}Pcs -Price: RM{} \n".format(row[0],row[1],row[2]))
  219.  
  220.     print("Total rows are:  ", len(myresult))
  221.  
  222.     #----------------------------------------------------- TKINTER HANDLER
  223.  
  224.     T = Text(root, height = 50, width = 100, bg = "light yellow")
  225.     T.insert(1.0, string_view)
  226.     T.pack()
  227.  
  228.     btnCancel = Button(root, text ="Close",width=10, bg='brown', command=root.destroy)
  229.     btnCancel.place(x=700,y=380)
  230.  
  231.     root.mainloop()
  232.  
  233. def priceList(productName,quantity):
  234.     tprice = 0
  235.  
  236.     i = 0
  237.  
  238.     if (productName == "Fish Cake"):
  239.         price = 15
  240.         totalPrice = price * int(quantity)
  241.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  242.         MenuList.insert(1 , ayat)
  243.        
  244.        
  245.     elif (productName == "Printer Ink"):
  246.         price = 150
  247.         totalPrice = price * int(quantity)
  248.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  249.         MenuList.insert(1, ayat)
  250.  
  251.     elif (productName == "Eraser"):
  252.         price = 150
  253.         totalPrice = price * int(quantity)
  254.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  255.         MenuList.insert(1, ayat)
  256.  
  257.     elif (productName == "Herbs"):
  258.         price = 150
  259.         totalPrice = price * int(quantity)
  260.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  261.         MenuList.insert(1, ayat)
  262.  
  263.     elif (productName == "Water Bottle"):
  264.         price = 150
  265.         totalPrice = price * int(quantity)
  266.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  267.         MenuList.insert(1, ayat)
  268.  
  269.     elif (productName == "Vitamin C"):
  270.         price = 150
  271.         totalPrice = price * int(quantity)
  272.         ayat = productName , ":" , quantity  , ":" ,  totalPrice
  273.         MenuList.insert(1, ayat)
  274.  
  275.     last_value = MenuList.index("end")
  276.  
  277.     for x in range(0,last_value):
  278.         getValue = MenuList.get(x)
  279.         print(getValue,"here")
  280.         tprice += int(getValue[4])
  281.  
  282.     txttotalP.config(text=tprice)
  283.     txtQuantity.delete(0, END)
  284.  
  285. def enter():
  286.  
  287.     productName = menu.get()
  288.     quantity = txtQuantity.get()
  289.  
  290.     if( not quantity.isnumeric() or quantity==""):
  291.         messagebox.showwarning("showwarning", "Please insert number")
  292.     else:
  293.         priceList(productName,quantity)
  294.  
  295. def deleteSelected():
  296.     try:
  297.         selection = MenuList.curselection()
  298.         MenuList.delete(selection[0])
  299.     except:
  300.         messagebox.showwarning("showwarning", "Please select a list to cancel")
  301.  
  302. def newFile():
  303.     MenuList.delete(0,END)
  304.     txtQuantity.delete(0,END)
  305.     menu.delete(0,END)
  306.     ayat = "productName" , ":" , "quantity"  , ":" , 0 ,"totalPrice"
  307.     MenuList.insert(0, ayat)
  308.  
  309. # ------------------------------- GUI --------------------------------#
  310.  
  311. root = Tk()
  312. root.title('Main Menu')
  313. root.title("KEDAI RUNCIT PAK ALI")
  314.  
  315. # ------------------------- Menu ---------------------------#
  316. menubar = Menu(root)
  317. root.config(menu=menubar)
  318.  
  319. file_menu = Menu(menubar,tearoff=0)
  320. file_menu.add_command(label='New',command=newFile)
  321. file_menu.add_command(label='Exit',command=root.destroy)
  322. menubar.add_cascade(label="File",menu=file_menu)
  323.  
  324. file_menu_edit = Menu(menubar,tearoff=0)
  325. file_menu_edit.add_command(label='Add Stock',command=add_stock)
  326. menubar.add_cascade(label="Edit",menu=file_menu_edit)
  327.  
  328. file_menu_view = Menu(menubar,tearoff=0)
  329. file_menu_view.add_command(label='View Stock',command=view_stock)
  330. file_menu_view.add_command(label='View Purchase',command=view_purchase)
  331. menubar.add_cascade(label="View",menu=file_menu_view)
  332.  
  333. # ----------------------- Content ---------------------------#
  334.  
  335. MenuList = Listbox(root,width=44, height=12,)
  336.  
  337. ayat = "productName" , ":" , "quantity"  , ":" , 0 ,"totalPrice"
  338. MenuList.insert(0, ayat)
  339. #MenuList.grid(row=0,columnspan=3)
  340. MenuList.place(x = 115, y=20)
  341.  
  342. menu = ttk.Combobox(root, values=["Fish Cake", "Printer Ink","Eraser","Herbs","Water Bottle", "Vitamin C"])
  343. #print(dict(menu))
  344. menu.current(0)
  345. menu.place(x=115 , y=240)
  346.  
  347. lbltotal = Label(root, text = "Total Price :")
  348. lbltotal.place(x = 400,y = 100)
  349.  
  350. txttotalP = Label(root, text = "RM0")
  351. txttotalP.place(x = 400,y = 130)
  352.  
  353. lbl1 = Label(root, text = "Quantity")
  354. lbl1.place(x = 270,y = 215)
  355.  
  356. txtQuantity = Entry(textvariable="quantity")
  357. txtQuantity.place(x=270,y=240)
  358.  
  359. btnConfirm = Button(root, text ="Confirm",width=10,fg='brown', bg='light green', command=insertDb)
  360. btnConfirm.place(x=115,y=270)
  361.  
  362. btnEnter = Button(root, text ="Enter",width=10,fg='purple', bg='light green',command = enter)
  363. btnEnter.place(x=218,y=270)
  364.  
  365. btnCancel = Button(root, text ="Cancel",width=10,fg='yellow', bg='red', command=deleteSelected)
  366. btnCancel.place(x=315,y=270)
  367.  
  368. root.geometry("500x350")
  369.  
  370. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement