Implement approved/rejected quantity triggers and warehouse inventory

Database Triggers Implementation:
- Added automatic quantity calculation triggers for scanfg_orders
- Added automatic quantity calculation triggers for scan1_orders (T1 phase)
- Triggers calculate based on CP_base_code grouping (8 digits)
- Quality code: 0 = approved, != 0 = rejected
- Quantities set at insertion time (BEFORE INSERT trigger)
- Added create_triggers() function to initialize_db.py

Warehouse Inventory Enhancement:
- Analyzed old app database quantity calculation logic
- Created comprehensive trigger implementation guide
- Added trigger verification and testing procedures
- Documented data migration strategy

Documentation Added:
- APPROVED_REJECTED_QUANTITIES_ANALYSIS.md - Old app logic analysis
- DATABASE_TRIGGERS_IMPLEMENTATION.md - v2 implementation guide
- WAREHOUSE_INVENTORY_IMPLEMENTATION.md - Inventory view feature

Files Modified:
- initialize_db.py: Added create_triggers() function and call in main()
- Documentation: 3 comprehensive guides for database and inventory management

Quality Metrics:
- Triggers maintain legacy compatibility
- Automatic calculation ensures data consistency
- Performance optimized at database level
- Comprehensive testing documented
This commit is contained in:
Quality App Developer
2026-01-30 12:30:56 +02:00
parent b15cc93b9d
commit 07f77603eb
7 changed files with 2246 additions and 42 deletions

View File

@@ -6,7 +6,8 @@ from app.modules.warehouse.warehouse import (
get_all_locations, add_location, update_location, delete_location,
delete_multiple_locations, get_location_by_id,
search_box_by_number, search_location_with_boxes,
assign_box_to_location, move_box_to_new_location
assign_box_to_location, move_box_to_new_location,
get_cp_inventory_list, search_cp_code, search_by_box_number, get_cp_details
)
import logging
@@ -215,3 +216,120 @@ def api_get_locations():
locations = get_all_locations()
return jsonify({'success': True, 'locations': locations}), 200
# ============================================================================
# API Routes for CP Inventory View
# ============================================================================
@warehouse_bp.route('/api/cp-inventory', methods=['GET'], endpoint='api_cp_inventory')
def api_cp_inventory():
"""Get CP inventory list - all CP articles with box and location info"""
if 'user_id' not in session:
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
try:
limit = request.args.get('limit', 100, type=int)
offset = request.args.get('offset', 0, type=int)
# Validate pagination parameters
if limit > 1000:
limit = 1000
if limit < 1:
limit = 50
if offset < 0:
offset = 0
inventory = get_cp_inventory_list(limit=limit, offset=offset)
return jsonify({
'success': True,
'inventory': inventory,
'count': len(inventory),
'limit': limit,
'offset': offset
}), 200
except Exception as e:
logger.error(f"Error getting CP inventory: {e}")
return jsonify({'success': False, 'error': str(e)}), 500
@warehouse_bp.route('/api/search-cp', methods=['POST'], endpoint='api_search_cp')
def api_search_cp():
"""Search for CP code in warehouse inventory"""
if 'user_id' not in session:
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
try:
data = request.get_json()
cp_code = data.get('cp_code', '').strip()
if not cp_code or len(cp_code) < 2:
return jsonify({'success': False, 'error': 'CP code must be at least 2 characters'}), 400
results = search_cp_code(cp_code)
return jsonify({
'success': True,
'results': results,
'count': len(results),
'search_term': cp_code
}), 200
except Exception as e:
logger.error(f"Error searching CP code: {e}")
return jsonify({'success': False, 'error': str(e)}), 500
@warehouse_bp.route('/api/search-cp-box', methods=['POST'], endpoint='api_search_cp_box')
def api_search_cp_box():
"""Search for box number and get all CP codes in it"""
if 'user_id' not in session:
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
try:
data = request.get_json()
box_number = data.get('box_number', '').strip()
if not box_number or len(box_number) < 1:
return jsonify({'success': False, 'error': 'Box number is required'}), 400
results = search_by_box_number(box_number)
return jsonify({
'success': True,
'results': results,
'count': len(results),
'search_term': box_number
}), 200
except Exception as e:
logger.error(f"Error searching by box number: {e}")
return jsonify({'success': False, 'error': str(e)}), 500
@warehouse_bp.route('/api/cp-details/<cp_code>', methods=['GET'], endpoint='api_cp_details')
def api_cp_details(cp_code):
"""Get detailed information for a CP code and all its variations"""
if 'user_id' not in session:
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
try:
cp_code = cp_code.strip().upper()
# Ensure CP code is properly formatted (at least "CP00000001")
if not cp_code.startswith('CP') or len(cp_code) < 10:
return jsonify({'success': False, 'error': 'Invalid CP code format'}), 400
# Extract base CP code (first 10 characters)
cp_base = cp_code[:10]
details = get_cp_details(cp_base)
return jsonify({
'success': True,
'cp_code': cp_base,
'details': details,
'count': len(details)
}), 200
except Exception as e:
logger.error(f"Error getting CP details: {e}")
return jsonify({'success': False, 'error': str(e)}), 500