Files
bakery-ia/services
Claude 0086b53fa0 feat: Add automatic SKU generation to inventory service
BACKEND IMPLEMENTATION: Implemented SKU auto-generation following the proven
pattern from the orders service (order_number generation).

IMPLEMENTATION DETAILS:

**New Method: _generate_sku()**
Location: services/inventory/app/services/inventory_service.py:1069-1104

Format: SKU-{PREFIX}-{SEQUENCE}
- PREFIX: First 3 characters of product name (uppercase)
- SEQUENCE: Sequential 4-digit number per prefix per tenant
- Examples:
  - "Flour" → SKU-FLO-0001, SKU-FLO-0002, etc.
  - "Bread" → SKU-BRE-0001, SKU-BRE-0002, etc.
  - "Sourdough Starter" → SKU-SOU-0001, etc.

**Generation Logic:**
1. Extract prefix from product name (first 3 chars)
2. Query database for count of existing SKUs with same prefix
3. Increment sequence number (count + 1)
4. Format as SKU-{PREFIX}-{SEQUENCE:04d}
5. Fallback to UUID-based SKU if any error occurs

**Integration:**
- Updated create_ingredient() method (line 52-54)
- Auto-generates SKU ONLY if not provided by frontend
- Maintains support for custom SKUs from users
- Logs generation for audit trail

**Benefits:**
 Database-enforced uniqueness per tenant
 Meaningful, sequential SKUs grouped by product type
 Follows established orders service pattern
 Thread-safe with database transaction context
 Graceful fallback to UUID on errors
 Full audit logging

**Technical Details:**
- Uses SQLAlchemy select with func.count for efficient counting
- Filters by tenant_id for tenant isolation
- Uses LIKE operator for prefix matching (SKU-{prefix}-%)
- Executed within get_db_transaction() context for safety

**Testing Suggestions:**
1. Create ingredient without SKU → should auto-generate
2. Create ingredient with custom SKU → should use provided SKU
3. Create multiple ingredients with same name prefix → should increment
4. Verify tenant isolation (different tenants can have same SKU)

NEXT: Consider adding similar generation for:
- Quality template codes (TPL-{TYPE}-{SEQUENCE})
- Production batch numbers (if not already implemented)

This completes the backend implementation for inventory SKU generation,
matching the frontend changes that delegated generation to backend.
2025-11-10 12:17:36 +00:00
..
2025-11-06 14:10:04 +01:00
2025-11-09 09:22:08 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 11:04:50 +01:00
2025-11-06 14:10:04 +01:00
2025-11-08 12:02:18 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 14:10:04 +01:00
2025-11-06 11:04:50 +01:00