Gestión de inventario con JSON en Python

El sistema de gestión de inventario se utiliza para gestionar los productos de la tienda y realizar un seguimiento de todas las existencias de mercancías y también podemos utilizarlo para comprobar el historial de compras de la tienda. Básicamente, estamos desarrollando un sistema para administrar/automatizar algunos procesos de cualquier tienda minorista mediante el uso de la computadora. Entonces, al usar este sistema, podemos mantener registros de datos de productos e incluso todas las compras realizadas en la tienda.

Así que aquí vamos a desarrollar un sistema de gestión de inventario usando python y para almacenar datos vamos a usar archivos JSON. En primer lugar, crearemos datos de muestra utilizando el propio código python y luego desarrollaremos todas las funciones una por una.

Tenemos que abrir archivos JSON en cada función y cargar datos JSON en formato de diccionario de python y nuevamente revertir el proceso de convertir datos de diccionario nuevamente al formato JSON y luego cerrar el archivo.

Crear datos y agregar un archivo JSON:

Generemos datos en un diccionario Python.

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 Jason 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

Así que hemos creado datos para nuestro sistema de gestión de inventario, ahora tenemos que crear funciones para acceder a estos datos y procesar varias funcionalidades en ellos.

Entonces, para este sistema, agregaremos dos partes principales

  • Funcionalidad de nivel de administrador. (usado para que el Administrador del sistema opere)
  • Funcionalidad a nivel de usuario. (será utilizado por los Clientes)

Funcionalidad de nivel de administrador

Básicamente, esto es solo para administrar el inventario para una persona que tiene permisos adicionales, como insertar, actualizar y eliminar detalles del producto, que deben estar solo a nivel de propietario o administrador.

La siguiente función ayudará a coordinar entre las funciones de administración

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...!!!")

Este nivel proporcionará las siguientes funciones

1. Función para mostrar todos los productos en inventario

En esta función, el administrador tendrá dos opciones: quiere toda la lista tal como está en la base de datos o quiere echar un vistazo a todos los productos por categorías.

Nota: Para que se vea bien en el formato tabular, usaremos 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. Función para mostrar detalles específicos del producto:

En esta función, el administrador deberá ingresar la identificación del producto para ver los detalles que desea verificar y obtendrá todos los detalles sobre ese producto en particular.

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 Whoes 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. Función de inserción para agregar nuevos productos en el inventario:

Esta función permitirá al administrador agregar nuevos detalles del producto en el inventario. También proporciona funcionalidad para agregar una nueva propiedad de atributo para un producto específico si el administrador desea agregar otras 5 propiedades/detalles básicos.

Nota: Aquí el programa es más específico con respecto a los datos que hemos insertado, podemos hacer cambios para que sea aplicable a todos los tipos

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. Eliminación de producto en inventario:

Aquí el administrador puede eliminar productos que están agotados o que desea eliminar. Además, arrojará un error en una identificación de producto no válida.

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. Actualizar datos del producto:

Esta función le dará al administrador dos opciones, ya sea que quiera actualizar todos los detalles del producto o que quiera editar cualquier detalle/atributo específico del producto. Además, arrojará un error en una identificación de producto no válida.

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. Mostrar informes de compra de usuarios al administrador:

El administrador puede echar un vistazo a las compras de los usuarios con dos opciones, como si quiere comprobar todos los informes de compra o quiere comprobar el informe de compra de cualquier cliente/usuario específico. Esto también proporciona funcionalidad para mostrar un mensaje de que no hay informes presentes si ningún usuario realizó ninguna compra. Además, arrojará un error en una identificación de usuario no válida.

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 Whoes 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. Eliminar toda la base de datos (Eliminar registros de inventario completos):

El administrador también puede eliminar toda la base de datos si quiere borrarlo todo.

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()

Funcionalidad de nivel de usuario:

Básicamente, esto es algo así como que un cliente puede operar para comprar productos y obtener su factura y el registro de facturas anteriores, y tampoco tiene funcionalidad o acceso al producto para insertarlo, modificarlo o eliminarlo.

La siguiente función ayudará a coordinar entre las funciones de administración

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...!!!")

Las siguientes son las funciones a nivel de usuario 

Nota: antes de que el usuario compre algo, debemos mostrarle una lista de todos los productos disponibles en el inventario para que podamos usar la misma función que hemos creado para el administrador para mostrar productos.

1. Mostrar informes de compra de usuarios:

Esta función ayudará al usuario a rastrear su compra y todos los informes de facturas hasta el día de hoy. Además, esta función proporciona información solo al usuario si ingresa la ID de usuario. (También podemos agregar funciones de contraseña aquí para aumentar más la funcionalidad)

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. Función para crear factura:

El usuario/cliente recibirá su factura con respecto a las compras realizadas por él justo después de la compra con una identificación de transacción única de 10 dígitos. Si un usuario compra varios productos de una sola vez, le dará una factura global.

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. Función para comprar productos para el usuario:

Esta es la función principal de esta parte. Permitirá que el usuario/cliente compre productos. Dará un error si el cliente compra un producto con una cantidad mayor que en stock. También el usuario tendrá una oportunidad más para corregir la cantidad si quiere ahorrar esfuerzos. o reiniciar el proceso o también puede omitir ese producto. Además, el Cliente recibirá mensajes agotados si el producto no está presente en el inventario (cantidad = 0). Los clientes pueden comprar varios productos simultáneamente mediante el uso de ID de productos (también podemos agregar comprar productos por su nombre) el usuario/cliente recibirá mensajes de error si ingresa opciones incorrectas.

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 Currenty 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()

Después de todo, tenemos que administrar las funcionalidades a nivel de usuario y administrador. Entonces, el código para eso es,

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...!!!")

Todo esto fue un completo Sistema de Gestión de Inventario con Funcionalidades de Administrador y Usuario.

A continuación se muestra la implementación completa:

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 Jason 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 Whoes 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 Whoes 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 Currenty 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...!!!")

Producción:

Publicación traducida automáticamente

Artículo escrito por ahc382000 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *