From 3d585e563f06bdd17fb263c39799eb718eabbade Mon Sep 17 00:00:00 2001 From: Samir Boulahtit Date: Sun, 23 Nov 2025 09:36:00 +0100 Subject: [PATCH] feat: add script to create inventory entries for products MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created script to populate inventory table for products that have 0 inventory. Issue: - Products had 0 available_inventory because inventory table was empty - available_inventory is calculated from inventory_entries relationship - Empty inventory table = 0 inventory for all products - Add to cart fails when inventory is 0 Solution: - Create inventory entries for all products - Each product gets 100 units in 'Main Warehouse' - Script can be run multiple times (only creates missing entries) Usage: python scripts/create_inventory.py šŸ¤– Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- scripts/create_inventory.py | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 scripts/create_inventory.py diff --git a/scripts/create_inventory.py b/scripts/create_inventory.py new file mode 100755 index 00000000..a69a341e --- /dev/null +++ b/scripts/create_inventory.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +""" +Create Inventory Entries for Products + +This script creates inventory entries for all products that don't have inventory. +Each product gets 100 units in stock. +""" + +import sys +from pathlib import Path + +# Add project root to path +sys.path.insert(0, str(Path(__file__).parent.parent)) + +import sqlite3 +from datetime import datetime, UTC +import os +from dotenv import load_dotenv + +# Load environment variables +load_dotenv() + +# Get database URL +database_url = os.getenv('DATABASE_URL', 'wizamart.db') +db_path = database_url.replace('sqlite:///', '') + +print(f"šŸ“¦ Creating inventory entries in {db_path}...") + +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +# Get products without inventory +cursor.execute(""" + SELECT p.id, p.vendor_id, p.product_id + FROM products p + LEFT JOIN inventory i ON p.id = i.product_id + WHERE i.id IS NULL +""") +products_without_inventory = cursor.fetchall() + +if not products_without_inventory: + print("āœ… All products already have inventory entries") + conn.close() + sys.exit(0) + +print(f"šŸ“¦ Creating inventory for {len(products_without_inventory)} products...") + +# Create inventory entries +for product_id, vendor_id, sku in products_without_inventory: + cursor.execute(""" + INSERT INTO inventory ( + vendor_id, + product_id, + location, + quantity, + reserved_quantity, + created_at, + updated_at + ) VALUES (?, ?, ?, ?, ?, ?, ?) + """, ( + vendor_id, + product_id, + 'Main Warehouse', + 100, # Total quantity + 0, # Reserved quantity + datetime.now(UTC), + datetime.now(UTC) + )) + +conn.commit() + +print(f"āœ… Created inventory entries for {len(products_without_inventory)} products") + +# Show stats +cursor.execute("SELECT COUNT(*) FROM inventory") +total_count = cursor.fetchone()[0] +print(f"\nšŸ“Š Total inventory entries: {total_count}") + +cursor.execute("SELECT product_id, location, quantity, reserved_quantity FROM inventory LIMIT 5") +print("\nšŸ“¦ Sample inventory:") +for row in cursor.fetchall(): + available = row[2] - row[3] + print(f" Product ID {row[0]}: {available} available at {row[1]}") + +conn.close() + +print("\nāœ… Done!")