The Inventory Management system is used to manage store products and keep track of all goods stock and also we can use it to check the purchases history of the store. Basically, we are developing a system to manage/automate some processes of any retail store by using the computer. So by using this system we can keep record data of products and even all purchases happened in-store.
So here we are going to develop an inventory management system using python and for storing data we are going to use JSON files. First of all, we will create sample data by using python code itself and then developing all functions one by one.
We have to open JSON files on each function and loading JSON data in python dictionary format and again reverse the process of converting dictionary data back to JSON format then closing the file.
Creating Data and Adding In JSON File:
Let’s generate data in a Python dictionary.
Python3
import json # Creating Dictionary to store data available_products = { 1001 : { "name" : "avocado" , "price" : 230 , "category" : "grocery" , "quantity" : 10 , "date" : "10/03/2021" }, 1002 : { "name" : "lotion" , "price" : 250 , "category" : "beauty & personal" , "quantity" : 100 , "date" : "15/07/2021" }, 1003 : { "name" : "pain reliever" , "price" : 500 , "category" : "health" , "quantity" : 200 , "date" : "12/04/2021" }, 1004 : { "name" : "dry pasta" , "price" : 20 , "category" : "grocery" , "quantity" : 50 , "date" : "27/06/2021" }, 1005 : { "name" : "toothbrush" , "price" : 700 , "category" : "beauty & personal" , "quantity" : 100 , "date" : "30/01/2021" }, 1006 : { "name" : "halloween candy" , "price" : 33 , "category" : "grocery" , "quantity" : 56 , "date" : "22/02/2021" }, 1007 : { "name" : "mascara" , "price" : 765 , "category" : "beauty & personal" , "quantity" : 70 , "date" : "11/03/2021" }, 1008 : { "name" : "capsicum" , "price" : 764 , "category" : "grocery" , "quantity" : 90 , "date" : "16/02/2021" }, 1009 : { "name" : "blush" , "price" : 87 , "category" : "beauty & personal" , "quantity" : 50 , "date" : "17/07/2021" }, 1010 : { "name" : "granola bars" , "price" : 24 , "category" : "grocery" , "quantity" : 60 , "date" : "20/05/2021" }, } # Formatting Dictionary into JSON format js = json.dumps(available_products) ''' json.dumps() function converts a Python object into a json string ''' js # so we got all data in json string format here # Create Json File for DataBase and Write data Into File fd = open ( "data.json" , 'w' ) fd.write(js) # writing string into file fd.close() # Close File After Inserting Data |
So we have created data for our inventory management system now we have to create functions to access this data and process various functionalities on it.
So for this system, we will add two main parts
- Admin Level Functionality. (used for Administrator of the system to operate)
- User Level Functionality. (will be used by Customers)
Admin Level Functionality
Basically, This is just for managing the Inventory for a person having extra permissions like inserting, updating, and deleting product details which should be at the owner or manager level only.
Following Function will help to coordinate between admin Functions
Python3
def admin(): print ( "============= Welcome to the Admin Inventory Management System =====" ) while ( 1 ): print ( "1)Display DataBase/All Products with there details" ) print ( "2)Display Specific Product with its details" ) print ( "3)Insert Data Into DataBase" ) print ( "4)Update Product in Database" ) print ( "5)Delete Product in DataBase" ) print ( "6)Display User Purchase Reports" ) print ( "7)Exit" ) print ( "Enter Your Choice :- " ) n = int ( input ()) if (n = = 1 ): display_data() elif (n = = 2 ): display_specific_data() elif (n = = 3 ): add_new() elif (n = = 4 ): update_prod_data() elif (n = = 5 ): delete_prod() elif (n = = 6 ): display_reports_admin() elif (n = = 7 ): break else : print ( "Invalid Choice...!!!" ) |
This level will provide the following Functions
1. Function To Show All Products In Inventory
In This Function, Admin Will have two options either he wants all list as it is in Database or else he wants to have a look at all products by their categories.
Note: To make it look good in the tabular format we will use pandas.
Python3
def display_data(): import pandas as pd import json fd = open ( "data.json" , 'r' ) # Open file in read mode txt = fd.read() # reading data from file data = json.loads(txt) # This will parse the JSON data, # populates a Python dictionary with the data fd.close() print ("Enter '0' To Display Data Category Wise or '1' \ To Show Data As its Sequence Of Insertion : - ") n = int ( input ()) # Display All Records if (n = = 1 ): table = pd.DataFrame( columns = [ 'ID' , 'name' , 'price' , 'category' , 'quantity' , 'date' ]) # Creating Pandas dataframe to show data in table # format later for i in data.keys(): '''Fetch all keys in dictionary''' temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] table = table.append(temp) table = table.reset_index(drop = True ) # This will reset index of dataframe from IPython.display import display display(table) elif (n = = 0 ): # Display Records by Category table = pd.DataFrame( columns = [ 'ID' , 'name' , 'price' , 'category' , 'quantity' , 'date' ]) cat = [] for i in data.keys(): temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] if (j = = 'category' ): cat.append(data[i][j]) table = table.append(temp) table = table.reset_index(drop = True ) cat = set (cat) cat = list (cat) for k in cat: temp = pd.DataFrame() temp = table[table[ 'category' ] = = k] print ( "Data Of Products Of Category " + k + " is:- " ) from IPython.display import display display(temp) else : print ( "Enter Valid Choice...!!!" ) # display_data() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
2. Function To Display Specific Product Details:
In this Function, Admin will have to enter the Product ID of the product to see the details he wants to check he will get all details regarding that particular product.
Python3
def display_specific_data(): import pandas as pd import json fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter Product ID Whose Details You Want to Have a Look on :- " ) i = input () # Following Code will Filter out Product ID from Records if i in data.keys(): temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] from IPython.display import display display(temp) else : print ("You Have Entered Wrong Product ID \ that is not Present in DataBase...!!!") # display_specific_data() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
3. Insertion Function To Add New Products in Inventory:
This Function will allow Admin to Add New Product Details in Inventory. Also provides Functionality to add new attribute property for a specific product if Admin wants to add other than basic 5 properties/details.
Note: Here program is more specific regarding data we have inserted we can make changes to make it applicable to all types
Python3
def add_new(): import json fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter New Product ID :- " ) id = input () if id not in data.keys(): print ( "Enter Product Name :- " ) name = input () print ( "Enter Price of Product(price for product quantity as 1) :- " ) price = input () print ( "Enter Category of Product :- " ) category = input () print ( "Enter Quantity of Product :- " ) quantity = input () print ( "Enter The Date on Which Product is Added in Inventory :- " ) date = input () data[ id ] = { 'name' : name, 'price' : price, 'category' : category, 'quantity' : quantity, 'date' : date} print ("Please Press '0' to Add New Attributes\ / Properties of Product or Press '1' to Continue : - ") z = int ( input ()) if (z = = 0 ): print ( "Enter Number of New Attributes/Properties of Product :- " ) n = int ( input ()) for i in range (n): print ( "Enter Attribute Name That you Want To Add :- " ) nam = input () print ( "Enter The " + str (nam) + " of Product :- " ) pro = input () data[ id ][nam] = pro print ( "Product ID " + str ( id ) + " Added Successfully...!!!" ) else : print ("The Product ID you Have Entered Is Already Present\ t in DataBase Please Check...!!!") js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # add_new() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
4. Deletion Of Product in Inventory:
Here Admin can remove products that are out of stock or want to delete. Also, it will Throw an Error on an invalid product ID.
Python3
def delete_prod(): import json fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter The Product ID of The Product Which You Want To Delete :- " ) temp = input () if temp in data.keys(): # here we are removing that particular data data.pop(temp) print ( "Product ID " + str (temp) + " Deleted Successfully...!!!" ) else : print ( "Invalid Product ID...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # delete_prod() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
5. Update Product Data:
This Function will provide Admin two options either he wants to update all details of the product or he wants to edit any specific detail/attribute of the product. Also, it will Throw an Error on an invalid product ID.
Python3
def update_prod_data(): import json fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ("Enter The Product ID of The Product\ Which You Want To Update : - ") temp = input () if temp in data.keys(): print ("Want to update whole product data\ press '0' else '1' for specific data : - ") q = int ( input ()) if (q = = 0 ): print ( "Enter Product Name :- " ) name = input () print ( "Enter Price of Product(price for product quantity as 1) :- " ) price = input () print ( "Enter Category of Product :- " ) category = input () print ( "Enter Quantity of Product :- " ) quantity = input () print ( "Enter The Date on Which Product is Added in Inventory :- " ) date = input () data[temp] = { 'name' : name, 'price' : price, 'category' : category, 'quantity' : quantity, 'date' : date} print ( "Please Press '0' to Add more Attributes\ / Properties of Product or Press '1' to Continue : - ") z = int ( input ()) if (z = = 0 ): print ( "Enter Number of New Attributes/Properties of Product :- " ) n = int ( input ()) for i in range (n): print ( "Enter Attribute Name That you Want To Add :- " ) nam = input () print ( "Enter The " + str (nam) + " of Product :- " ) pro = input () data[temp][nam] = pro print ( "Product ID " + str (temp) + " Updated Successfully...!!!" ) elif (q = = 1 ): print ( "Enter Which Attribute of Product You want to Update :- " ) p = input () if p in data[temp].keys(): print ( "Enter " + str (p) + " of Product :- " ) u = input () data[temp][p] = u print ( "Product ID " + str (temp) + "'s attribute " + str (p) + " is Updated Successfully...!!!" ) else : print ( "Invalid Product Attribute...!!!" ) else : print ( "Invalid Choice...!!!" ) else : print ( "Invalid Product ID...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # update_prod_data() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
6. Display User Purchase Reports to Admin:
Admin can have a look at user purchases with two options like either he wants to check all purchase reports or want to check the purchase report of any specific customer/user. This Also Provide Functionality to show a message that no reports are present if no purchase happened by any user. Also, it will Throw an Error on an invalid User ID.
Python3
def display_reports_admin(): import os.path import pandas as pd import json if (os.path.isfile( "user_data.json" ) is False ): # Check for if file is present or not # File will be generated only # if any user will do some purchase print ( "No User Reports are Present" ) return fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() print ("Enter '0' to Check All Bills / Reports and \ '1' To Check Specific User Bills / Reports : - ") n = int ( input ()) if (n = = 1 ): print ( "Enter User ID Whose Details You Want to Have a Look on" ) i = input () temp = pd.DataFrame() if i in user_data.keys(): for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () temp = temp.reset_index(drop = True ) from IPython.display import display display(temp) else : print ( "You Have Entered Wrong User ID that is not Present in DataBase...!!!" ) elif (n = = 0 ): table = pd.DataFrame() for i in user_data.keys(): temp = pd.DataFrame() for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () table = table.append(temp) table = table.reset_index(drop = True ) from IPython.display import display display(table) else : print ( "Please Enter Valid Choice...!!!" ) # display_reports_admin() # Uncomment This Line To Run This Function # Note :- Ensure You Have data.json File to Fetch Data |
7. Delete Whole Database (Deleting whole inventory records):
Admin Can Delete the Whole Database also if he wants to clear it all.
Python3
def delete_all(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() data = {} # Replacing Data with NULL Dictionary js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() |
User Level Functionality:
This is basically something like a customer can operate to buy products and get his bill and previous bills record too have no functionally or access to the product to either insert or modify or delete it.
Following Function will help to coordinate between admin Functions
Python3
def user(): print (" = = = = = = = = = = = = = = = \ Welcome to the User Inventory Management System\ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ") while ( 1 ): print ( "1)Display All Products With Details" ) print ( "2)Display Specific Product With Details" ) print ( "3)Display All Purchase Bills" ) print ( "4)Buy The Product" ) print ( "5)Exit" ) print ( "Enter Your Choice :- " ) n = int ( input ()) if (n = = 1 ): display_data() elif (n = = 2 ): display_specific_data() elif (n = = 3 ): display_user_data() elif (n = = 4 ): buy_product() elif (n = = 5 ): break else : print ( "Invalid Choice...!!!" ) |
Following Are The Functions at User Level
Note: Before the user buys something we should show him a list of all products available in inventory so we can use the same function we have created for admin for displaying products.
1. Display User Purchase Reports:
This Function will help the user to track his purchase and all bills reports till today. Also, This Function Provides Information Only to the User if he enters User ID. (we can add password features also here to increase more functionality)
Python3
def display_user_data(): import os.path import json if (os.path.isfile( "user_data.json" ) is False ): print ( "No User Reports are Present" ) return fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() print ( "Enter your User ID to Display All your Bills :- " ) i = input () temp = pd.DataFrame() if i in user_data.keys(): for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () temp = temp.reset_index(drop = True ) from IPython.display import display display(temp) else : print ( "You Have Entered Wrong User ID that is not Present in DataBase...!!!" ) |
2. Function to Create Bill:
User/Customer will get his bill regarding purchases done by him just after the purchase with a unique 10 digit Transaction ID. If a user buys multiple products in one go it will give an aggregate bill.
Python3
def generate_bill(user_id, prod_id, price, time_date, purchase_no, name, category, quantity_all, transaction_id): print (" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ = = = = = = = = = = = = = = = = = Bill = = = = = = = = = = = = = = = = \ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ") print ( "########################################" ) print ( " User ID :-" , user_id) print ( "############################################" ) amount = 0 n = len (purchase_no) for i in range (n): print ( "--------------------------------------" ) amount = amount + float (price[i]) * float (quantity_all[i]) print ( "Purchase number" , purchase_no[i], "\nPurchase Time :-" , time_date[i], "\nProduct ID :-" , prod_id[i], "\nName Of Product :-" , name[i], "\nCategory Of Product :-" , category[i], "\nPrice of Product per Item :-" , price[i], "\nPurchase Quantity :-" , quantity_all[i]) print ( "-------------------------------------------------" ) print ( "*********************************************************" ) print ( " Total Payable Bill :-" , amount, "Transaction ID :-" , transaction_id) print ( "*************************************" ) |
3. Function to Buy Product for user:
This is the main function of this part this will allow user/customer to buy products it will give an error if the customer buys a product with high quantity than in stock also user will get one more chance to correct quantity if he wants to save efforts or restarting the process or he can skip that product too. Also, Customer will get messages out of stock if the product is not present in inventory (quantity=0). Customers can buy multiple products simultaneously by using product IDs (we can add buying product by its name also) user/customer will error messages if he enters wrong choices.
Python3
def buy_product(): import time import random import os.path if (os.path.isfile( "user_data.json" ) is False ): user_data = {} else : fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ("Enter Your User ID if You are Old Customer\ else press '0' To New User ID : - ") p = int ( input ()) if (p = = 0 ): if ( len (user_data.keys()) = = 0 ): user_id = 1000 else : user_id = int ( list (user_data.keys())[ - 1 ]) + 1 else : if str (p) in user_data.keys(): user_id = p else : user_id = - 1 if (user_id ! = - 1 ): user_id = str (user_id) price = [] time_date = [] purchase_no = [] name = [] category = [] quantity_all = [] prod_id = [] transaction_id = ''.join(random.choice( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' ) for i in range ( 10 )) print ( "Enter Number of Products You Want To Buy :- " ) n = int ( input ()) print ( "Enter Data As Follows :- " ) if user_id not in user_data.keys(): user_data[user_id] = {} g = 0 else : g = int ( list (user_data[user_id].keys())[ - 1 ]) + 1 for i in range (n): print ( "Enter Product ID of Product " + str (i + 1 ) + " that you want to buy" ) id = input () if id in data.keys(): user_data[user_id][ str (i + 1 + g)] = {} user_data[user_id][ str (i + 1 + g)][ 'time_date' ] = str (time.ctime()) time_date.append( str (time.ctime())) if ( float (data[ id ][ 'quantity' ]) = = 0 ): print ( "Product You Want is Currently Out Of Stock...!!!" ) continue purchase_no.append(i + 1 + g) name.append(data[ id ][ 'name' ]) user_data[user_id][ str (i + 1 + g)][ 'name' ] = data[ id ][ 'name' ] prod_id.append( id ) user_data[user_id][ str (i + 1 + g)][ 'product_id' ] = id category.append(data[ id ][ 'category' ]) user_data[user_id][ str ( i + 1 + g)][ 'category' ] = data[ id ][ 'category' ] print ( "For Product " + str (data[ id ][ 'name' ]) + " Available Quantity is :- " + str (data[ id ][ 'quantity' ])) print ( "Enter Quantity of Product " + str (i + 1 ) + " that you want to buy" ) quantity = input () if ( float (quantity) < = float (data[ id ][ 'quantity' ])): data[ id ][ 'quantity' ] = str ( float (data[ id ][ 'quantity' ]) - float (quantity)) quantity_all.append(quantity) user_data[user_id][ str (i + 1 + g)][ 'quantity' ] = str (quantity) price.append(data[ id ][ 'price' ]) user_data[user_id][ str (i + 1 + g)][ 'price' ] = data[ id ][ 'price' ] user_data[user_id][ str ( i + 1 + g)][ 'Transaction ID' ] = str (transaction_id) else : print ( "The Quantity You Have Asked is Quite High\ Than That is Available in Stock") print ( "Did you Want To buy According to The Quantity\ Available in Stock then Enter '0' Else '1' \ to skip This Product") key = int ( input ()) if (key = = 0 ): print ( "Enter Quantity of Product " + str (i + 1 ) + " that you want to buy" ) quantity = intput() if ( float (quantity) < = float (data[ id ][ 'quantity' ])): data[ id ][ 'quantity' ] = str ( float (data[ id ][ 'quantity' ]) - float (quantity)) quantity_all.append(quantity) user_data[user_id][ str ( i + 1 )][ 'quantity' ] = str (quantity) price.append(data[ id ][ 'price' ]) user_data[user_id][ str ( i + 1 )][ 'price' ] = data[ id ][ 'price' ] user_data[user_id][ str ( i + 1 + g)][ 'Transaction ID' ] = str (transaction_id) else : print ( "Invalid Operation Got Repeated...!!!" ) elif (key = = 1 ): continue else : print ( "Invalid Choice...!!!" ) else : print ( "Invalid Product ID...!!!" ) if ( len (purchase_no) ! = 0 ): generate_bill(user_id, prod_id, price, time_date, purchase_no, name, category, quantity_all, transaction_id) else : print ( "User ID Doesn't Exists...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() js = json.dumps(user_data) fd = open ( "user_data.json" , 'w' ) fd.write(js) fd.close() |
After all, we have to manage user and admin-level functionalities. So, code for that is,
Python3
import json import pandas as pd while ( 1 ): print ( "Choose Any One of The Following :- " ) print ( "1)Admin" ) print ( "2)User" ) print ( "3)Exit" ) print ( "Enter Your Choice Here :- " ) n = int ( input ()) if (n = = 1 ): admin() elif (n = = 2 ): user() elif (n = = 3 ): break else : print ( "Invalid Choice...!!!" ) |
This was all complete Inventory Management System with Admin and User Functionalities.
Below is the full implementation:
Python3
import pandas as pd import json import os.path import time import random # Creating Dictionary to store data available_products = { 1001 : { "name" : "avocado" , "price" : 230 , "category" : "grocery" , "quantity" : 10 , "date" : "10/03/2021" }, 1002 : { "name" : "lotion" , "price" : 250 , "category" : "beauty & personal" , "quantity" : 100 , "date" : "15/07/2021" }, 1003 : { "name" : "pain reliever" , "price" : 500 , "category" : "health" , "quantity" : 200 , "date" : "12/04/2021" }, 1004 : { "name" : "dry pasta" , "price" : 20 , "category" : "grocery" , "quantity" : 50 , "date" : "27/06/2021" }, 1005 : { "name" : "toothbrush" , "price" : 700 , "category" : "beauty & personal" , "quantity" : 100 , "date" : "30/01/2021" }, 1006 : { "name" : "halloween candy" , "price" : 33 , "category" : "grocery" , "quantity" : 56 , "date" : "22/02/2021" }, 1007 : { "name" : "mascara" , "price" : 765 , "category" : "beauty & personal" , "quantity" : 70 , "date" : "11/03/2021" }, 1008 : { "name" : "capsicum" , "price" : 764 , "category" : "grocery" , "quantity" : 90 , "date" : "16/02/2021" }, 1009 : { "name" : "blush" , "price" : 87 , "category" : "beauty & personal" , "quantity" : 50 , "date" : "17/07/2021" }, 1010 : { "name" : "granola bars" , "price" : 24 , "category" : "grocery" , "quantity" : 60 , "date" : "20/05/2021" }, } # Formatting Dictionary into JSON format js = json.dumps(available_products) # json.dumps() function converts a # Python object into a json string js # so we got all data in json string format here # Create Json File for DataBase and Write data Into File fd = open ( "data.json" , 'w' ) # it will open file into write mode if file # does not exists then it will create file too''' fd.write(js) # writing string into file fd.close() # Close File After Inserting Data def admin(): print (" = = = = = = = = \ Welcome to the Admin Inventory Management System \ = = = = = = = = = = = = = = ") while ( 1 ): print ( "1)Display DataBase/All Products with there details" ) print ( "2)Display Specific Product with its details" ) print ( "3)Insert Data Into DataBase" ) print ( "4)Update Product in Database" ) print ( "5)Delete Product in DataBase" ) print ( "6)Display User Purchase Reports" ) print ( "7)Exit" ) print ( "Enter Your Choice :- " ) n = int ( input ()) if (n = = 1 ): display_data() elif (n = = 2 ): display_specific_data() elif (n = = 3 ): add_new() elif (n = = 4 ): update_prod_data() elif (n = = 5 ): delete_prod() elif (n = = 6 ): display_reports_admin() elif (n = = 7 ): break else : print ( "Invalid Choice...!!!" ) def display_data(): fd = open ( "data.json" , 'r' ) txt = fd.read() # reading data from file data = json.loads(txt) # This will parse the JSON data, populates a # Python dictionary with the data fd.close() print ("Enter '0' To Display Data Category Wise or '1' \ To Show Data As its Sequence Of Insertion : - ") n = int ( input ()) # Display All Records if (n = = 1 ): table = pd.DataFrame( columns = [ 'ID' , 'name' , 'price' , 'category' , 'quantity' , 'date' ]) # Creating Pandas dataframe to show data in table format later for i in data.keys(): # Fetch all keys in dictionary temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] table = table.append(temp) table = table.reset_index(drop = True ) '' 'This will reset index of dataframe' '' from IPython.display import display display(table) elif (n = = 0 ): # Display Records by Category table = pd.DataFrame( columns = [ 'ID' , 'name' , 'price' , 'category' , 'quantity' , 'date' ]) cat = [] for i in data.keys(): temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] if (j = = 'category' ): cat.append(data[i][j]) table = table.append(temp) table = table.reset_index(drop = True ) cat = set (cat) cat = list (cat) for k in cat: temp = pd.DataFrame() temp = table[table[ 'category' ] = = k] print ( "Data Of Products Of Category " + k + " is:- " ) from IPython.display import display display(temp) else : print ( "Enter Valid Choice...!!!" ) # display_data() # Uncomment This Line To Run This Function def display_specific_data(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter Product ID Whose Details You Want to Have a Look on :- " ) i = input () # Following Code will Filter out Product ID from Records if i in data.keys(): temp = pd.DataFrame(columns = [ 'ID' ]) temp[ 'ID' ] = [i] for j in data[i].keys(): temp[j] = [data[i][j]] from IPython.display import display display(temp) else : print ("You Have Entered Wrong Product ID \ that is not Present in DataBase...!!!") # display_specific_data() # Uncomment This Line To Run This Function def add_new(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter New Product ID :- " ) id = input () if id not in data.keys(): print ( "Enter Product Name :- " ) name = input () print ( "Enter Price of Product(price for product quantity as 1) :- " ) price = input () print ( "Enter Category of Product :- " ) category = input () print ( "Enter Quantity of Product :- " ) quantity = input () print ( "Enter The Date on Which Product is Added in Inventory :- " ) date = input () data[ id ] = { 'name' : name, 'price' : price, 'category' : category, 'quantity' : quantity, 'date' : date} print ("Please Press '0' to Add New\ Attributes / Properties of Product or Press '1' to Continue : - ") z = int ( input ()) if (z = = 0 ): print ( "Enter Number of New Attributes/Properties of Product :- " ) n = int ( input ()) for i in range (n): print ( "Enter Attribute Name That you Want To Add :- " ) nam = input () print ( "Enter The " + str (nam) + " of Product :- " ) pro = input () data[ id ][nam] = pro print ( "Product ID " + str ( id ) + " Added Successfully...!!!" ) else : print ("The Product ID you Have Entered Is\ Already Present in DataBase Please Check...!!!") js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # add_new() # Uncomment This Line To Run This Function def delete_prod(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ( "Enter The Product ID of The Product Which You Want To Delete :- " ) temp = input () if temp in data.keys(): data.pop(temp) # here we are removing that particular data print ( "Product ID " + str (temp) + " Deleted Successfully...!!!" ) else : print ( "Invalid Product ID...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # delete_prod() # Uncomment This Line To Run This Function def update_prod_data(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ("Enter The Product ID of The Product\ Which You Want To Update : - ") temp = input () if temp in data.keys(): print ("Want to update whole product data\ press '0' else '1' for specific data : - ") q = int ( input ()) if (q = = 0 ): print ( "Enter Product Name :- " ) name = input () print ("Enter Price of Product(price for \ product quantity as 1 ) : - ") price = input () print ( "Enter Category of Product :- " ) category = input () print ( "Enter Quantity of Product :- " ) quantity = input () print ("Enter The Date on Which Product\ is Added in Inventory : - ") date = input () data[temp] = { 'name' : name, 'price' : price, 'category' : category, 'quantity' : quantity, 'date' : date} print ( "Please Press '0' to Add more Attributes/Properties of Product or Press '1' to Continue :- " ) z = int ( input ()) if (z = = 0 ): print ( "Enter Number of New Attributes/Properties of Product :- " ) n = int ( input ()) for i in range (n): print ( "Enter Attribute Name That you Want To Add :- " ) nam = input () print ( "Enter The " + str (nam) + " of Product :- " ) pro = input () data[temp][nam] = pro print ( "Product ID " + str (temp) + " Updated Successfully...!!!" ) elif (q = = 1 ): print ( "Enter Which Attribute of Product You want to Update :- " ) p = input () if p in data[temp].keys(): print ( "Enter " + str (p) + " of Product :- " ) u = input () data[temp][p] = u print ( "Product ID " + str (temp) + "'s attribute " + str (p) + " is Updated Successfully...!!!" ) else : print ( "Invalid Product Attribute...!!!" ) else : print ( "Invalid Choice...!!!" ) else : print ( "Invalid Product ID...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() # update_prod_data() # Uncomment This Line To Run This Function def display_reports_admin(): if (os.path.isfile( "user_data.json" ) is False ): # Check for if file is present or not # File will be generated only if any user will do some purchase print ( "No User Reports are Present" ) return fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() print ("Enter '0' to Check All Bills / Reports\ and '1' To Check Specific User Bills / Reports : - ") n = int ( input ()) if (n = = 1 ): print ( "Enter User ID Whose Details You Want to Have a Look on" ) i = input () temp = pd.DataFrame() if i in user_data.keys(): for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () temp = temp.reset_index(drop = True ) from IPython.display import display display(temp) else : print ( "You Have Entered Wrong User ID that is not Present in DataBase...!!!" ) elif (n = = 0 ): table = pd.DataFrame() for i in user_data.keys(): temp = pd.DataFrame() for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () table = table.append(temp) table = table.reset_index(drop = True ) from IPython.display import display display(table) else : print ( "Please Enter Valid Choice...!!!" ) # display_reports_admin() # Uncomment This Line To Run This Function def delete_all(): fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() data = {} # Replacing Data with NULL Dictionary js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() def user(): print ( "======= Welcome to the User Inventory Management System ====" ) while ( 1 ): print ( "1)Display All Products With Details" ) print ( "2)Display Specific Product With Details" ) print ( "3)Display All Purchase Bills" ) print ( "4)Buy The Product" ) print ( "5)Exit" ) print ( "Enter Your Choice :- " ) n = int ( input ()) if (n = = 1 ): display_data() elif (n = = 2 ): display_specific_data() elif (n = = 3 ): display_user_data() elif (n = = 4 ): buy_product() elif (n = = 5 ): break else : print ( "Invalid Choice...!!!" ) def display_user_data(): if (os.path.isfile( "user_data.json" ) is False ): print ( "No User Reports are Present" ) return fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() print ( "Enter your User ID to Display All your Bills :- " ) i = input () temp = pd.DataFrame() if i in user_data.keys(): for j in user_data[i].keys(): d = dict () d[ 'User ID' ] = i d[ 'Purchase Number' ] = j for k in user_data[i][j].keys(): d[k] = user_data[i][j][k] temp = temp.append(d, ignore_index = True ) d = dict () temp = temp.reset_index(drop = True ) from IPython.display import display display(temp) else : print ( "You Have Entered Wrong User ID that is not Present in DataBase...!!!" ) def generate_bill(user_id, prod_id, price, time_date, purchase_no, name, category, quantity_all, transaction_id): print ( "========= Bill ========" ) print ( "#######################" ) print ( " User ID :-" , user_id) print ( "#################" ) amount = 0 n = len (purchase_no) for i in range (n): print ( "-----------------------------------------" ) amount = amount + float (price[i]) * float (quantity_all[i]) print ( "Purchase number" , purchase_no[i], "\nPurchase Time :-" , time_date[i], "\nProduct ID :-" , prod_id[i], "\nName Of Product :-" , name[i], "\nCategory Of Product :-" , category[i], "\nPrice of Product per Item :-" , price[i], "\nPurchase Quantity :-" , quantity_all[i]) print ( "-----------------------------------" ) print ( "*****************************************" ) print ( " Total Payable Bill :-" , amount, "Transaction ID :-" , transaction_id) print ( "***************************************" ) def buy_product(): if (os.path.isfile( "user_data.json" ) is False ): user_data = {} else : fd = open ( "user_data.json" , 'r' ) txt = fd.read() user_data = json.loads(txt) fd.close() fd = open ( "data.json" , 'r' ) txt = fd.read() data = json.loads(txt) fd.close() print ("Enter Your User ID if You are Old \ Customer else press '0' To New User ID : - ") p = int ( input ()) if (p = = 0 ): if ( len (user_data.keys()) = = 0 ): user_id = 1000 else : user_id = int ( list (user_data.keys())[ - 1 ]) + 1 else : if str (p) in user_data.keys(): user_id = p else : user_id = - 1 if (user_id ! = - 1 ): user_id = str (user_id) price = [] time_date = [] purchase_no = [] name = [] category = [] quantity_all = [] prod_id = [] transaction_id = ''.join(random.choice( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' ) for i in range ( 10 )) print ( "Enter Number of Products You Want To Buy :- " ) n = int ( input ()) print ( "Enter Data As Follows :- " ) if user_id not in user_data.keys(): user_data[user_id] = {} g = 0 else : g = int ( list (user_data[user_id].keys())[ - 1 ]) + 1 for i in range (n): print ( "Enter Product ID of Product " + str (i + 1 ) + " that you want to buy" ) id = input () if id in data.keys(): user_data[user_id][ str (i + 1 + g)] = {} user_data[user_id][ str (i + 1 + g)][ 'time_date' ] = str (time.ctime()) time_date.append( str (time.ctime())) if ( float (data[ id ][ 'quantity' ]) = = 0.0 ): print ( "Product You Want is Currently Out Of Stock...!!!" ) continue purchase_no.append(i + 1 + g) name.append(data[ id ][ 'name' ]) user_data[user_id][ str (i + 1 + g)][ 'name' ] = data[ id ][ 'name' ] prod_id.append( id ) user_data[user_id][ str (i + 1 + g)][ 'product_id' ] = id category.append(data[ id ][ 'category' ]) user_data[user_id][ str ( i + 1 + g)][ 'category' ] = data[ id ][ 'category' ] print ( "For Product " + str (data[ id ][ 'name' ]) + " Available Quantity is :- " + str (data[ id ][ 'quantity' ])) print ( "Enter Quantity of Product " + str (i + 1 ) + " that you want to buy" ) quantity = input () if ( float (quantity) < = float (data[ id ][ 'quantity' ])): data[ id ][ 'quantity' ] = str ( float (data[ id ][ 'quantity' ]) - float (quantity)) quantity_all.append(quantity) user_data[user_id][ str (i + 1 + g)][ 'quantity' ] = str (quantity) price.append(data[ id ][ 'price' ]) user_data[user_id][ str (i + 1 + g)][ 'price' ] = data[ id ][ 'price' ] user_data[user_id][ str ( i + 1 + g)][ 'Transaction ID' ] = str (transaction_id) else : print ( "The Quantity You Have Asked is Quite High Than\ That is Available in Stock") print ( "Did you Want To buy According to The Quantity\ Available in Stock then Enter '0' Else '1' \ to skip This Product") key = int ( input ()) if (key = = 0 ): print ( "Enter Quantity of Product " + str (i + 1 ) + " that you want to buy" ) quantity = intput() if ( float (quantity) < = float (data[ id ][ 'quantity' ])): data[ id ][ 'quantity' ] = str ( float (data[ id ][ 'quantity' ]) - float (quantity)) quantity_all.append(quantity) user_data[user_id][ str ( i + 1 )][ 'quantity' ] = str (quantity) price.append(data[ id ][ 'price' ]) user_data[user_id][ str ( i + 1 )][ 'price' ] = data[ id ][ 'price' ] user_data[user_id][ str ( i + 1 + g)][ 'Transaction ID' ] = str (transaction_id) else : print ( "Invalid Operation Got Repeated...!!!" ) elif (key = = 1 ): continue else : print ( "Invalid Choice...!!!" ) else : print ( "Invalid Product ID...!!!" ) if ( len (purchase_no) ! = 0 ): generate_bill(user_id, prod_id, price, time_date, purchase_no, name, category, quantity_all, transaction_id) else : print ( "User ID Doesn't Exists...!!!" ) js = json.dumps(data) fd = open ( "data.json" , 'w' ) fd.write(js) fd.close() js = json.dumps(user_data) fd = open ( "user_data.json" , 'w' ) fd.write(js) fd.close() while ( 1 ): print ( "Choose Any One of The Following :- " ) print ( "1)Admin" ) print ( "2)User" ) print ( "3)Exit" ) print ( "Enter Your Choice Here :- " ) n = int ( input ()) if (n = = 1 ): admin() elif (n = = 2 ): user() elif (n = = 3 ): break else : print ( "Invalid Choice...!!!" ) |
Output: