0% found this document useful (0 votes)
85 views

Stock Py

This document contains the code for a stock management program with the following key features: 1. It connects to a MySQL database and defines functions for product, purchase, sales and user management. 2. The product management functions allow a user to add, search, update and delete products from a product table. 3. Purchase management functions allow a user to add and view orders by inserting records into an orders table. 4. Sales management functions allow a user to record sales and view sales reports by inserting into a sales table. 5. Additional functions create and manage the database structure and tables.

Uploaded by

Tejas Agrahari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Stock Py

This document contains the code for a stock management program with the following key features: 1. It connects to a MySQL database and defines functions for product, purchase, sales and user management. 2. The product management functions allow a user to add, search, update and delete products from a product table. 3. Purchase management functions allow a user to add and view orders by inserting records into an orders table. 4. Sales management functions allow a user to record sales and view sales reports by inserting into a sales table. 5. Additional functions create and manage the database structure and tables.

Uploaded by

Tejas Agrahari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1 # STOCK MANAGEMENT PROGRAM MADE BY-

2 ##################################################################################
3 # "KRISHNA KUMAR" "TEJAS AGRAHARI" "SRAJANIKA KAITHWAS"
4
5
6
7 import os
8 import mysql.connector as myconnect
9 import datetime
10 now = datetime.datetime.now()
11
12 def connect_check(con):
13 try:
14 print("connection checking...")
15 myconnect.connect(host=con[2].strip("\n"),
16 port=int(con[0]),
17 user=con[3],
18 passwd=con[1].strip("\n") )
19 print ("database connected...")
20 correction = con
21 return correction
22 except:
23 print ("error, in connecting database...")
24 correct_con=mysql_c_o_nnect(sys=False)
25 return(connect_check(correct_con))
26
27 def file_formation():
28 try:
29 path="e:\stock\data"
30 os.makedirs(path)
31 file = open("e:\stock\data\data.txt", "w")
32 file.close()
33 mysql_c_o_nnect()
34 except FileExistsError:
35 print ("please remove the file named 'stock' from the e drive")
36 input("enter a key to countinue...")
37 file_formation()
38
39 def mysql_c_o_nnect(sys=True):
40 try:
41 path=r"e:\stock\data\data.txt"
42 with open(path,"r") as my1:
43 lst1=my1.readlines()
44 if ( (len(lst1)!= 4) or (sys==False) ):
45 with open(r"e:\stock\data\data.txt","w") as my:
46 print("enter the correct credentials of mysql sever ")
47 pr=int(input("enter port number: "))
48 pa=input("enter password: ")
49 hs=input("enter hostname: ")
50 us=input("enter username: ")
51 my.write(str(pr))
52 my.write("\n"+pa)
53 my.write("\n"+hs)
54 my.write("\n"+us)
55 return ([pr,pa,hs,us])
56 else:
57 return (lst1)
58 except:
59 file_formation()
60 def product_mgmt(con):
61 while True :
62 print("\t\t\t 1. Add New Product")
63 print("\t\t\t 2. List Product")
64 print("\t\t\t 3. Update Product")
65 print("\t\t\t 4. Delete Product")
66 print("\t\t\t 5. Back (Main Menu)")
67 p=int (input("\t\tEnter Your Choice :"))
68 if p==1:
69 add_product(con)
70 elif p==2:
71 search_product(con)
72 elif p==3:
73 update_product(con)
74 elif p==4:
75 delete_product(con)
76 elif p== 5 :
77 break
78 else:
79 print ("wrong choice enter again!!!")
80
81 def purchase_mgmt(con ):
82 while True :
83 print("\t\t\t 1. Add Order")
84 print("\t\t\t 2. List Order")
85 print("\t\t\t 3. Back (Main Menu)")
86 o=int (input("\t\tEnter Your Choice :"))
87 if o==1 :
88 add_order(con)
89 elif o==2 :
90 list_order(con)
91 elif o== 3 :
92 break
93 else:
94 print ("wrong choice enter again!!!")
95
96 def sales_mgmt(con ):
97 while True :
98 print("\t\t\t 1. Sale Items")
99 print("\t\t\t 2. List Sales")
100 print("\t\t\t 3. Back (Main Menu)")
101 s=int (input("\t\tEnter Your Choice :"))
102 if s== 1 :
103 sale_product(con)
104 elif s== 2 :
105 list_sale(con)
106 elif s== 3 :
107 break
108 else:
109 print ("wrong choice enter again!!!")
110
111 def user_mgmt( con):
112 while True :
113 print("\t\t\t 1. Add user")
114 print("\t\t\t 2. List user")
115 print("\t\t\t 3. Back (Main Menu)")
116 u=int (input("\t\tEnter Your Choice :"))
117 if u==1:
118 add_user(con)
119 elif u==2:
120 list_user(con)
121 elif u==3:
122 break
123 else:
124 print ("wrong choice enter again!!!")
125
126 def create_database(con,recreate=False):
127 mydb=myconnect.connect(host=con[2].strip("\n"),
128 port=int(con[0]),
129 user=con[3],
130 passwd=con[1].strip("\n"))
131 mycursor=mydb.cursor()
132 if recreate:
133 print("recreating...")
134 mycursor.execute("drop database stock;")
135 print (" Adding database name...")
136 sql = "create database if not exists stock;"
137 mycursor.execute(sql)
138 mycursor.execute("use stock;")
139 print(" Creating PRODUCT table")
140 sql = "CREATE TABLE if not exists product (\
141 pcode int(4) PRIMARY KEY,\
142 pname varchar(30) NOT NULL,\
143 pprice float(8,2) ,\
144 pqty int(10) ,\
145 pcat varchar(30));"
146 mycursor.execute(sql)
147 print(" Creating ORDER table")
148 sql = "CREATE TABLE if not exists orders (\
149 orderid int(4) PRIMARY KEY ,\
150 orderdate DATE ,\
151 pcode char(30) NOT NULL , \
152 pprice float(8,2) ,\
153 pqty int(10) ,\
154 supplier varchar(50),\
155 pcat varchar(30));"
156 mycursor.execute(sql)
157 print(" ORDER table created")
158
159 print(" Creating SALES table")
160 sql = "CREATE TABLE if not exists sales (\
161 salesid int(4) PRIMARY KEY ,\
162 salesdate DATE ,\
163 pcode varchar(30) references product(pcode), \
164 pprice float(10,2) ,\
165 pqty int(4) ,\
166 Total double(30,2)\
167 );"
168
169 mycursor.execute(sql)
170 print(" SALES table created")
171 sql = "CREATE TABLE if not exists user (\
172 uid varchar(30) PRIMARY KEY,\
173 uname varchar(30) NOT NULL,\
174 upwd varchar(30));"
175 mycursor.execute(sql)
176 print(" USER table created")
177 print(" Database created...")
178
179 def list_database(con):
180 mydb=myconnect.connect(host=con[2].strip("\n"),
181 port=int(con[0]),
182 user=con[3],
183 passwd=con[1].strip("\n"),database="stock")
184 mycursor=mydb.cursor()
185 sql="show tables;"
186 mycursor.execute(sql)
187 print ("here's your database components:")
188 for i in mycursor:
189 print(i)
190
191 def add_order(con):
192 mydb=myconnect.connect(host=con[2].strip("\n"),
193 port=int(con[0]),
194 user=con[3],
195 passwd=con[1].strip("\n"),database="stock")
196 mycursor=mydb.cursor()
197 now = datetime.datetime.now()
198 sql="INSERT INTO orders(orderid,orderdate,pcode,pprice,pqty,supplier,pcat)\
199 values (%s,%s,%s,%s,%s,%s,%s)"
200 code=int(input("Enter product code :"))
201 oid=now.year+now.month+now.day+now.hour+now.minute+now.second
202 qty=int(input("Enter product quantity : "))
203 price=float(input("Enter Product unit price: "))
204 cat=input("Enter product category: ")
205 supplier=input("Enter Supplier details: ")
206 val=(oid,now,code,price,qty,supplier,cat)
207 mycursor.execute(sql,val)
208 mydb.commit()
209
210 def list_order(con):
211 mydb=myconnect.connect(host=con[2].strip("\n"),
212 port=int(con[0]),
213 user=con[3],
214 passwd=con[1].strip("\n"),database="stock")
215 mycursor=mydb.cursor()
216 sql="SELECT * from orders"
217 mycursor.execute(sql)
218 clrscr()
219 print("\t\t\t\t\t\t\t ORDER DETAILS")
220 print("-"*85)
221 print("orderid Date Product code price quantity Supplier
Category")
222 print("-"*85)
223 for i in mycursor:
224 print(i[0],"\t",i[1],"\t",i[2],"\t ",i[3],"\t",i[4],"\t ",i[5],"\t",i[6])
225 print("-"*85)
226
227
228 def db_mgmt(con):
229 while True :
230 print("\t\t\t 1. Database creation")
231 print("\t\t\t 2. List Database")
232 print("\t\t\t 3. Want to check mannually the connection of database")
233 print("\t\t\t 4. Recreate database")
234 print("\t\t\t 5. Back (Main Menu)")
235 p=int (input("\t\tEnter Your Choice :"))
236 if p ==1 :
237 create_database(con)
238 elif p ==2 :
239 list_database(con)
240 elif p== 5 :
241 break
242 elif p==3 :
243 program()
244 elif p==4:
245 create_database(con,recreate=True)
246 else:
247 print ("wrong input enter again...")
248
249 def add_product(con):
250 mydb=myconnect.connect(host=con[2].strip("\n"),
251 port=int(con[0]),
252 user=con[3],
253 passwd=con[1].strip("\n"),database="stock")
254 mycursor=mydb.cursor()
255 sql="INSERT INTO product(pcode,pname,pprice,pqty,pcat) values (%s,%s,%s,%s,%s)"
256 code=int(input("\t\tEnter product code :"))
257 search="SELECT count(*) FROM product WHERE pcode=%s;"
258 val=(code,)
259 mycursor.execute(search,val)
260 for x in mycursor:
261 cnt=x[0]
262 if cnt==0:
263 name=input("\t\tEnter product name :")
264 qty=int(input("\t\tEnter product quantity :"))
265 price=float(input("\t\tEnter product unit price :"))
266 cat=input("\t\tEnter Product category :")
267 val=(code,name,price,qty,cat)
268 mycursor.execute(sql,val)
269 mydb.commit()
270 else:
271 print("\t\t Product already exist")
272 def update_product(con):
273 mydb=myconnect.connect(host=con[2].strip("\n"),
274 port=int(con[0]),
275 user=con[3],
276 passwd=con[1].strip("\n"),database="stock")
277 mycursor=mydb.cursor()
278 code=int(input("Enter the product code :"))
279 qty=int(input("Enter the quantity :"))
280 sql="UPDATE product SET pqty=pqty+%s WHERE pcode=%s;"
281 val=(qty,code)
282 mycursor.execute(sql,val)
283 mydb.commit()
284 print("\t\t Product details updated")
285
286 def delete_product(con):
287 mydb=myconnect.connect(host=con[2].strip("\n"),
288 port=int(con[0]),
289 user=con[3],
290 passwd=con[1].strip("\n"),database="stock")
291 mycursor=mydb.cursor()
292 code=int(input("Enter the product code :"))
293 sql="DELETE FROM product WHERE pcode = %s;"
294 val=(code,)
295 mycursor.execute(sql,val)
296 mydb.commit()
297 print(mycursor.rowcount," record(s) deleted")
298
299 def search_product(con):
300 while True :
301 print("\t\t\t 1. List all product")
302 print("\t\t\t 2. List product code wise")
303 print("\t\t\t 3. List product categoty wise")
304 print("\t\t\t 4. Back (Main Menu)")
305 s=int (input("\t\tEnter Your Choice :"))
306 if s == 1 :
307 list_product(con)
308 elif s== 2 :
309 code=int(input(" Enter product code :"))
310 list_prcode(code,con)
311 elif s== 3 :
312 cat=input("Enter category :")
313 list_prcat(cat,con)
314 elif s== 4 :
315 break
316 else:
317 print ("wrong input recieved...proceed with right one...")
318
319 def list_product(con):
320 mydb=myconnect.connect(host=con[2].strip("\n"),
321 port=int(con[0]),
322 user=con[3],
323 passwd=con[1].strip("\n"),database="stock")
324 mycursor=mydb.cursor()
325 sql="SELECT * from product"
326 mycursor.execute(sql)
327 clrscr()
328 print("\t\t\t\t PRODUCT DETAILS")
329 print("\t\t","-"*47)
330 print("\t\t code name price quantity category")
331 print("\t\t","-"*47)
332 for i in mycursor:
333 print("\t\t",i[0],"\t",i[1],"\t",i[2],"\t ",i[3],"\t",i[4])
334 print("\t\t","-"*47)
335
336 def list_prcode(code,con):
337 mydb=myconnect.connect(host=con[2].strip("\n"),
338 port=int(con[0]),
339 user=con[3],
340 passwd=con[1].strip("\n"),database="stock")
341 mycursor=mydb.cursor()
342 sql="SELECT * from product WHERE pcode=%s"
343 val=(code,)
344 mycursor.execute(sql,val)
345 clrscr()
346 print("\t\t\t\t PRODUCT DETAILS")
347 print("\t\t","-"*47)
348 print("\t\t code name price quantity category")
349 print("\t\t","-"*47)
350 for i in mycursor:
351 print("\t\t",i[0],"\t",i[1],"\t",i[2],"\t ",i[3],"\t\t",i[4])
352 print("\t\t","-"*47)
353
354
355 def sale_product(con):
356 mydb=myconnect.connect(host=con[2].strip("\n"),
357 port=int(con[0]),
358 user=con[3],
359 passwd=con[1].strip("\n"),database="stock")
360 mycursor=mydb.cursor()
361 pcode=input("Enter product code: ")
362 sql="SELECT count(*) from product WHERE pcode=%s;"
363 val=(pcode,)
364 mycursor.execute(sql,val)
365 for x in mycursor:
366 cnt=x[0]
367 if cnt !=0 :
368 sql="SELECT * from product WHERE pcode=%s;"
369 val=(pcode,)
370 mycursor.execute(sql,val)
371 for x in mycursor:
372 print(x)
373 price=int(x[2])
374 pqty=int(x[3])
375 qty=int(input("Enter no of quantity :"))
376 if qty <= pqty:
377 total=qty*price
378 print ("Collect Rs. ", total)
379 sql="INSERT into sales values(%s,%s,%s,%s,%s,%s)"
380 val=(int(cnt)+1,datetime.datetime.now(),pcode,price,qty,total)
381 mycursor.execute(sql,val)
382 sql="UPDATE product SET pqty=pqty-%s WHERE pcode=%s"
383 val=(qty,pcode)
384 mycursor.execute(sql,val)
385 mydb.commit()
386 else:
387 print(" Quantity not Available")
388 else:
389 print(" Product is not avalaible")
390
391 def list_sale(con):
392 mydb=myconnect.connect(host=con[2].strip("\n"),
393 port=int(con[0]),
394 user=con[3],
395 passwd=con[1].strip("\n"),database="stock")
396 mycursor=mydb.cursor()
397 sql="SELECT * FROM sales"
398 mycursor.execute(sql)
399 print(" \t\t\t\tSALES DETAILS")
400 print("-"*80)
401 print("Sales id Date Product Code Price Quantity
Total")
402 print("-"*80)
403 for x in mycursor:
404 print(x[0],"\t",x[1],"\t",x[2],"\t ",x[3],"\t\t",x[4],"\t\t",x[5])
405 print("-"*80)
406
407
408 def list_prcat(cat,con):
409 mydb=myconnect.connect(host=con[2].strip("\n"),
410 port=int(con[0]),
411 user=con[3],
412 passwd=con[1].strip("\n"),database="stock")
413 mycursor=mydb.cursor()
414 print (cat)
415 sql="SELECT * from product WHERE pcat =%s"
416 val=(cat,)
417 mycursor.execute(sql,val)
418 clrscr()
419 print("\t\t\t\t PRODUCT DETAILS")
420 print("\t\t","-"*47)
421 print("\t\t code name price quantity category")
422 print("\t\t","-"*47)
423 for i in mycursor:
424 print("\t\t",i[0],"\t",i[1],"\t",i[2],"\t ",i[3],"\t\t",i[4])
425 print("\t\t","-"*47)
426
427 def add_user(con):
428 mydb=myconnect.connect(host=con[2].strip("\n"),
429 port=int(con[0]),
430 user=con[3],
431 passwd=con[1].strip("\n"),database="stock")
432 mycursor=mydb.cursor()
433 uid=input("Enter emaid id :")
434 name=input(" Enter Name :")
435 paswd=input("Enter Password :")
436 sql="INSERT INTO user values (%s,%s,%s);"
437 val=(uid,name,paswd)
438 mycursor.execute(sql,val)
439 mydb.commit()
440 print(mycursor.rowcount, " user created")
441
442
443 def list_user(con):
444 mydb=myconnect.connect(host=con[2].strip("\n"),
445 port=int(con[0]),
446 user=con[3],
447 passwd=con[1].strip("\n"),database="stock")
448 sql="SELECT uid,uname from user"
449 mycursor=mydb.cursor()
450 mycursor.execute(sql)
451 clrscr()
452 print("\t\t\t\t USER DETAILS")
453 print("\t\t","-"*27)
454 print("\t\t UID name ")
455 print("\t\t","-"*27)
456 for i in mycursor:
457 print("\t\t",i[0],"\t",i[1])
458 print("\t\t","-"*27)
459
460 def clrscr():
461 print("\n"*5)
462
463 def program():
464 try:
465 while True:
466 clrscr()
467 connection = mysql_c_o_nnect()
468 connec = connect_check(connection)
469 print("\t\t\t STOCK MANAGEMENT")
470 print("\t\t\t ****************\n")
471 print("\t\t 1. PRODUCT MANAGEMENT")
472 print("\t\t 2. PURCHASE MANAGEMENT")
473 print("\t\t 3. SALES MANAGEMENT")
474 print("\t\t 4. USER MANAGEMENT")
475 print("\t\t 5. DATABASE SETUP")
476 print("\t\t 6. CHANGE SQL CREDENTIALS")
477 print("\t\t 7. EXIT\n")
478 n=int(input("Enter your choice :"))
479 if n== 1:
480 product_mgmt(connec)
481 elif n== 2:
482 os.system('cls')
483 purchase_mgmt(connec)
484 elif n== 3:
485 sales_mgmt(connec)
486 elif n== 4:
487 user_mgmt(connec)
488 elif n==5 :
489 db_mgmt(connec)
490 elif n== 7:
491 print("goodbye!!!")
492 break
493 elif n==6:
494 mysql_c_o_nnect(sys=False)
495 else:
496 print ("wrong input given... enter again...")
497 except:
498 print("program crashed due to some undefined error ...\n\n\n program is
restarting..")
499 print ("maybe database setup is not fullfilled here!!! ")
500 print(" program is restarting..")
501 program()
502
503
504 ################################_main_###############################################
############
505
506 program()
507
508 #************************************************************************************
****************
509 #************************************************************************************
****************
510

You might also like