SAP HANA Integration
The system integrates with SAP HANA database to validate item availability in real-time.
Overview
SAP HANA integration provides:
- ✅ Real-time item availability checking
- ✅ Inventory status validation
- ✅ Sales and purchasing flag verification
- ✅ Automated SKU verification during upload
- ✅ Missing item identification
Configuration
Prerequisites
- SAP HANA server access credentials
- Network connectivity to SAP HANA server
- Python
hdbclidriver installed - Database schema access permissions
Environment Setup
Add to .env file:
HANA_HOST=172.16.25.3
HANA_PORT=30015
HANA_USER=your_username
HANA_PASSWORD=your_password
HANA_SCHEMA=SGI_LIVE_HANA
Test Connection
cd /mnt/data/punch_quotation
source venv/bin/activate
python -c "
from hdbcli import dbapi
import os
from dotenv import load_dotenv
load_dotenv()
try:
conn = dbapi.connect(
address=os.getenv('HANA_HOST'),
port=int(os.getenv('HANA_PORT', 30015)),
user=os.getenv('HANA_USER'),
password=os.getenv('HANA_PASSWORD')
)
print('✓ Successfully connected to SAP HANA')
conn.close()
except Exception as e:
print(f'✗ Connection failed: {e}')
"
Database Schema
OITM Table Structure
The system queries the OITM table (Item Master Data):
| Column | Type | Description |
|---|---|---|
| ItemCode | VARCHAR | Unique item identifier |
| ItemName | VARCHAR | Item description |
| validFor | CHAR(1) | Y=Active, N=Inactive |
| InvntItem | CHAR(1) | Y=Inventory item |
| SellItem | CHAR(1) | Y=Sales item |
| PrchseItem | CHAR(1) | Y=Purchasing item |
Query Template
SELECT
T1."ItemCode",
T1."ItemName",
CASE WHEN T1."validFor" = 'Y' THEN 'Yes' ELSE 'No' END AS "Is_Active",
CASE WHEN T1."InvntItem" = 'Y' THEN 'Yes' ELSE 'No' END AS "Inventory",
CASE WHEN T1."SellItem" = 'Y' THEN 'Yes' ELSE 'No' END AS "Sales",
CASE WHEN T1."PrchseItem" = 'Y' THEN 'Yes' ELSE 'No' END AS "Purchasing"
FROM
SGI_LIVE_HANA."OITM" T1
WHERE
T1."ItemCode" IN ('AAP-001', 'AAP-002', ...)
Automatic SKU Checking
When Does It Run?
SKU checking happens automatically during file upload:
- File Upload - User uploads Excel file
- Data Extraction - System extracts item codes
- De-duplication - Remove duplicate item codes
- SAP Query - Batch query to SAP HANA
- Result Mapping - Map results to items
- Display - Show in Item Check tab
Process Flow
Upload File
↓
Extract Items (Column C - KODE ITEM)
↓
Filter Non-Items (dates, notes, etc.)
↓
Collect Unique Item Codes
↓
Query SAP HANA (OITM table)
↓
Map Results to Items
↓
Display in Item Check Tab
Performance
- Small uploads (< 50 unique items): ~2-3 seconds
- Medium uploads (50-200 items): ~5-10 seconds
- Large uploads (200+ items): ~15-30 seconds
Query performance depends on:
- Network latency to SAP HANA
- Number of unique items
- SAP HANA server load
Item Check Results
Available Items (Green ✓)
Item found in SAP HANA with all flags:
✓ Item Code: AAP10-40-N12-A18
✓ Item Name: ANGULAR PIN
✓ Active: Yes
✓ Inventory: Yes
✓ Sales: Yes
✓ Purchasing: Yes
Missing Items (Red ✗)
Item not found in SAP HANA:
✗ Item Code: NEW-ITEM-001
- Item Name: Missing
- Active: -
- Inventory: -
- Sales: -
- Purchasing: -
Inactive Items (Yellow ⚠)
Item exists but marked inactive:
⚠ Item Code: OLD-ITEM-001
⚠ Item Name: OBSOLETE PART
⚠ Active: No
✓ Inventory: Yes
✓ Sales: No
✓ Purchasing: No
Data Filtering
System automatically excludes:
Non-Item Entries
- Dates:
2025-10-23,23-10-2025 - Delivery notes:
11 HARI KERJA,15 working days - Text notes:
CATATAN,NOTE,ESTIMASI PENGIRIMAN - Totals:
TOTAL,SUBTOTAL,TAX - Company names:
PT. PUNCH,PT. SOMAGEDE
Filter Logic
# Excluded patterns
exclude_patterns = [
'note', 'catatan', 'estimasi', 'pengiriman',
'nett price', 'masa berlaku', 'delivery',
'hari kerja', 'working day', 'total', 'tax'
]
# Date pattern
date_pattern = r'^\d{4}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{4}$|^\d{1,3}\s*hari'
Error Handling
Connection Errors
Symptoms:
- "SAP Connection Error" message
- No item check data displayed
- Processing completes but no SAP info
Causes:
- SAP HANA server unreachable
- Invalid credentials
- Network firewall blocking port 30015
- Schema not accessible
Solutions:
- Test connection manually
- Verify credentials in
.env - Check network connectivity
- Contact SAP administrator
Timeout Errors
Symptoms:
- Processing hangs during SAP query
- "Timeout" error in logs
Causes:
- Too many items in single query
- SAP HANA server overloaded
- Network latency issues
Solutions:
- Split large files into smaller batches
- Retry during off-peak hours
- Increase timeout in settings
Schema Errors
Symptoms:
- "Table or view not found"
- Permission denied errors
Causes:
- Wrong schema name
- User lacks read permissions
- OITM table not accessible
Solutions:
- Verify schema name:
HANA_SCHEMA=SGI_LIVE_HANA - Request read access from SAP admin
- Test with different schema if available
Sync Inventory Feature
Manual Sync
After initial upload, you can re-sync:
- Go to Session Detail page
- Navigate to "Item Check" tab
- Click "Resync Missing Items" button
- System re-queries SAP HANA
- Updates displayed immediately
Use Cases
- New items added to SAP HANA after upload
- Initial connection failed
- Item data updated in SAP
- Verify current inventory status
Logging
Log Locations
SAP queries logged in:
logs/extraction.log- Detailed extraction logslogs/django.log- General application logs
Log Entries
INFO: Processing 145 unique descriptions for SAP lookup
INFO: SAP HANA connection established successfully
INFO: Executing SAP query for 145 items
INFO: ✓ SAP query returned 132 rows for 145 items
INFO: ✓ SAP lookup completed. Found 132 items in SAP, 13 missing
View Logs
# Real-time extraction logs
./view_logs.sh extraction
# All logs
./view_logs.sh all
Best Practices
Connection Management
- Use connection pooling for high-volume uploads
- Close connections after queries complete
- Handle timeouts gracefully with retries
- Cache results during session (already implemented)
Query Optimization
- Batch queries instead of individual lookups
- Use IN clause for multiple items
- Limit result columns to necessary fields only
- Index ItemCode column for faster lookups
Error Recovery
- Log all errors with full context
- Continue processing even if SAP unavailable
- Allow manual resync after fixing issues
- Display partial results when some queries fail
Security
Credentials
- Store in
.envfile (never commit to Git) - Use read-only SAP user account
- Limit schema access to OITM table only
- Rotate passwords regularly
Network
- Use VPN for remote access
- Restrict port 30015 to application server
- Monitor failed connection attempts
- Log all SAP queries for audit
Troubleshooting
Item Not Found but Exists in SAP
Check:
- Item code spelling matches exactly
- No extra spaces in item code
- Correct schema configured
- Item in active status
All Items Showing Missing
Check:
- SAP HANA connection successful
- Schema name correct
- User has read permissions
- Network connectivity
Slow Query Performance
Solutions:
- Reduce number of items per query
- Check SAP HANA server load
- Verify network latency
- Consider caching strategy