import pymysql
from tabulate import tabulate
from datetime import datetime
# Connect to the database
def connect_to_db():
return [Link](host="localhost",
user="root",
password="chris123",
database="inventory")
# Add a new product
def add_product():
conn = connect_to_db()
cursor = [Link]()
name = input("Enter product name: ").strip()
category = input("Enter category: ").strip()
try:
price = float(input("Enter price: "))
stock = int(input("Enter stock quantity: "))
query = "INSERT INTO products (name, category, price, stock) VALUES (%s, %s, %s, %s)"
[Link](query, (name, category, price, stock))
[Link]()
print("✅ Product added successfully!")
except ValueError:
print("❌ Invalid input for price or stock.")
finally:
[Link]()
# View all products
def view_products():
conn = connect_to_db()
cursor = [Link]()
[Link]("SELECT * FROM products")
results = [Link]()
if results:
print(tabulate(results, headers=["ID", "Name", "Category", "Price", "Stock"], tablefmt="grid"))
else:
print("⚠️No products found.")
[Link]()
# Search for a product by name or category
def search_products():
conn = connect_to_db()
cursor = [Link]()
search_term = input("Enter product name or category to search: ").strip()
query = "SELECT * FROM products WHERE name LIKE %s OR category LIKE %s"
[Link](query, (f"%{search_term}%", f"%{search_term}%"))
results = [Link]()
if results:
print(tabulate(results, headers=["ID", "Name", "Category", "Price", "Stock"], tablefmt="grid"))
else:
print("⚠️No matching products found.")
[Link]()
# Update product details
def update_product():
conn = connect_to_db()
cursor = [Link]()
try:
product_id = int(input("Enter product ID to update: "))
print("1. Update Name\n2. Update Category\n3. Update Price\n4. Update Stock")
choice = input("Enter your choice: ")
if choice == "1":
new_name = input("Enter new name: ")
[Link]("UPDATE products SET name = %s WHERE product_id = %s", (new_name,
product_id))
elif choice == "2":
new_category = input("Enter new category: ")
[Link]("UPDATE products SET category = %s WHERE product_id = %s", (new_category,
product_id))
elif choice == "3":
new_price = float(input("Enter new price: "))
[Link]("UPDATE products SET price = %s WHERE product_id = %s", (new_price,
product_id))
elif choice == "4":
new_stock = int(input("Enter new stock quantity: "))
[Link]("UPDATE products SET stock = %s WHERE product_id = %s", (new_stock,
product_id))
else:
print("❌ Invalid choice!")
[Link]()
return
[Link]()
print("✅ Product updated successfully!")
except ValueError:
print("❌ Invalid input for ID, price, or stock.")
finally:
[Link]()
# Delete a product
def delete_product():
conn = connect_to_db()
cursor = [Link]()
try:
product_id = int(input("Enter product ID to delete: "))
[Link]("DELETE FROM products WHERE product_id = %s", (product_id,))
[Link]()
print("✅ Product deleted successfully!")
except ValueError:
print("❌ Invalid input for product ID.")
finally:
[Link]()
# Record a sale
def record_sale():
conn = connect_to_db()
cursor = [Link]()
try:
product_id = int(input("Enter product ID: "))
quantity = int(input("Enter quantity sold: "))
sale_date = [Link]().strftime("%Y-%m-%d %H:%M:%S")
[Link]("SELECT stock FROM products WHERE product_id = %s", (product_id,))
result = [Link]()
if result and result[0] >= quantity:
[Link]("UPDATE products SET stock = stock - %s WHERE product_id = %s", (quantity,
product_id))
[Link]("INSERT INTO sales (product_id, quantity, sale_date) VALUES (%s, %s, %s)",
(product_id, quantity, sale_date))
[Link]()
print("✅ Sale recorded successfully!")
else:
print("❌ Insufficient stock!")
except ValueError:
print("❌ Invalid input for product ID or quantity.")
finally:
[Link]()
# View sales history
def view_sales():
conn = connect_to_db()
cursor = [Link]()
[Link]("SELECT sales.sale_id, [Link], [Link], sales.sale_date FROM sales JOIN
products ON sales.product_id = products.product_id")
results = [Link]()
if results:
print(tabulate(results, headers=["Sale ID", "Product Name", "Quantity", "Sale Date"],
tablefmt="grid"))
else:
print("⚠️No sales records found.")
[Link]()
# Generate sales report
def generate_report():
conn = connect_to_db()
cursor = [Link]()
[Link]("SELECT [Link], SUM([Link]) AS total_quantity, SUM([Link] *
[Link]) AS total_revenue FROM sales JOIN products ON sales.product_id = products.product_id
GROUP BY [Link]")
results = [Link]()
if results:
print(tabulate(results, headers=["Product Name", "Total Quantity Sold", "Total Revenue"],
tablefmt="grid"))
else:
print("⚠️No sales data available.")
[Link]()
# Main menu
def main_menu():
while True:
print("\t\t📦 INVENTORY MANAGEMENT SYSTEM 📦")
print("\t\t==================================\n")
print("\t\t\t\t MAIN MENU")
print("\t\t\t\t =========\n")
print("\t1. Add Product \t 2. View Products \n")
print("\t3. Search Products \t 4. Update Product \n")
print("\t5. Delete Product \t 6. Record Sale \n")
print("\t7. View Sales History \t8. Generate Sales Report \n")
print("\t9. Exit")
choice = input("\nEnter your choice: ")
if choice == "1":
add_product()
elif choice == "2":
view_products()
elif choice == "3":
search_products()
elif choice == "4":
update_product()
elif choice == "5":
delete_product()
elif choice == "6":
record_sale()
elif choice == "7":
view_sales()
elif choice == "8":
generate_report()
elif choice == "9":
print("Exiting system...")
break
else:
print("❌ Invalid choice! Please try again.")
if __name__ == "__main__":
main_menu()