- Commented out floating help button HTML in print_module.html and print_lost_labels.html - Commented out floating help button CSS in both templates - Help page functionality not implemented yet, will be added in future updates - Allows templates to render without 500 errors
201 lines
6.9 KiB
Python
201 lines
6.9 KiB
Python
"""
|
|
Labels Module Routes
|
|
Handles label printing pages and API endpoints
|
|
"""
|
|
from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request
|
|
import logging
|
|
|
|
from .print_module import (
|
|
get_unprinted_orders_data,
|
|
get_printed_orders_data,
|
|
update_order_printed_status,
|
|
search_orders_by_cp_code
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
labels_bp = Blueprint('labels', __name__, url_prefix='/labels')
|
|
|
|
|
|
@labels_bp.route('/', methods=['GET'])
|
|
def labels_index():
|
|
"""Labels module home page"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
return render_template('modules/labels/index.html')
|
|
|
|
|
|
@labels_bp.route('/print-module', methods=['GET'])
|
|
def print_module():
|
|
"""Label printing interface with thermal printer support"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
return render_template('modules/labels/print_module.html')
|
|
|
|
|
|
@labels_bp.route('/print-lost-labels', methods=['GET'])
|
|
def print_lost_labels():
|
|
"""Print lost/missing labels interface"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
return render_template('modules/labels/print_lost_labels.html')
|
|
|
|
|
|
@labels_bp.route('/help/<page>', methods=['GET'])
|
|
def help(page='index'):
|
|
"""Help page for labels module"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
# Map page names to help content
|
|
help_pages = {
|
|
'print_module': {
|
|
'title': 'Print Module Help',
|
|
'content': '''
|
|
<h3>Print Labels - Thermal Printer Guide</h3>
|
|
<p>This module helps you print labels directly to thermal printers.</p>
|
|
<h4>Features:</h4>
|
|
<ul>
|
|
<li>Live label preview in thermal format</li>
|
|
<li>Real-time printer selection</li>
|
|
<li>Barcode generation</li>
|
|
<li>PDF export fallback</li>
|
|
<li>Batch printing support</li>
|
|
</ul>
|
|
<h4>How to use:</h4>
|
|
<ol>
|
|
<li>Select orders from the list</li>
|
|
<li>Preview labels in the preview pane</li>
|
|
<li>Select your printer</li>
|
|
<li>Click "Print Labels" to send to printer</li>
|
|
</ol>
|
|
'''
|
|
},
|
|
'print_lost_labels': {
|
|
'title': 'Print Lost Labels Help',
|
|
'content': '''
|
|
<h3>Print Lost Labels - Reprint Guide</h3>
|
|
<p>Use this page to search and reprint labels for orders that need reprinting.</p>
|
|
<h4>Features:</h4>
|
|
<ul>
|
|
<li>Search orders by production code</li>
|
|
<li>Filter previously printed orders</li>
|
|
<li>Reprint with updated information</li>
|
|
</ul>
|
|
<h4>How to use:</h4>
|
|
<ol>
|
|
<li>Enter the production order code</li>
|
|
<li>Click "Search" to find the order</li>
|
|
<li>Select the order and preview</li>
|
|
<li>Click "Reprint Labels" to print again</li>
|
|
</ol>
|
|
'''
|
|
}
|
|
}
|
|
|
|
help_data = help_pages.get(page, help_pages.get('index', {'title': 'Help', 'content': 'No help available'}))
|
|
|
|
return f'''
|
|
<html>
|
|
<head>
|
|
<title>{help_data['title']}</title>
|
|
<style>
|
|
body {{ font-family: Arial, sans-serif; padding: 20px; }}
|
|
h3 {{ color: #333; }}
|
|
ul, ol {{ margin: 10px 0; padding-left: 20px; }}
|
|
li {{ margin: 5px 0; }}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
{help_data['content']}
|
|
</body>
|
|
</html>
|
|
'''
|
|
|
|
|
|
# ============================================================================
|
|
# API Endpoints for Labels Module
|
|
# ============================================================================
|
|
|
|
@labels_bp.route('/api/unprinted-orders', methods=['GET'], endpoint='api_unprinted_orders')
|
|
def api_unprinted_orders():
|
|
"""Get all unprinted orders for label printing"""
|
|
if 'user_id' not in session:
|
|
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
|
|
|
|
try:
|
|
limit = request.args.get('limit', 100, type=int)
|
|
if limit > 500:
|
|
limit = 500
|
|
if limit < 1:
|
|
limit = 1
|
|
|
|
orders = get_unprinted_orders_data(limit)
|
|
return jsonify({'success': True, 'orders': orders, 'count': len(orders)}), 200
|
|
except Exception as e:
|
|
logger.error(f"Error getting unprinted orders: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@labels_bp.route('/api/printed-orders', methods=['GET'], endpoint='api_printed_orders')
|
|
def api_printed_orders():
|
|
"""Get all printed orders"""
|
|
if 'user_id' not in session:
|
|
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
|
|
|
|
try:
|
|
limit = request.args.get('limit', 100, type=int)
|
|
if limit > 500:
|
|
limit = 500
|
|
if limit < 1:
|
|
limit = 1
|
|
|
|
orders = get_printed_orders_data(limit)
|
|
return jsonify({'success': True, 'orders': orders, 'count': len(orders)}), 200
|
|
except Exception as e:
|
|
logger.error(f"Error getting printed orders: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@labels_bp.route('/api/search-orders', methods=['POST'], endpoint='api_search_orders')
|
|
def api_search_orders():
|
|
"""Search for orders by CP code"""
|
|
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) < 1:
|
|
return jsonify({'success': False, 'error': 'CP code is required'}), 400
|
|
|
|
results = search_orders_by_cp_code(cp_code)
|
|
return jsonify({'success': True, 'orders': results, 'count': len(results)}), 200
|
|
except Exception as e:
|
|
logger.error(f"Error searching orders: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
@labels_bp.route('/api/update-printed-status/<int:order_id>', methods=['POST'], endpoint='api_update_printed_status')
|
|
def api_update_printed_status(order_id):
|
|
"""Mark an order as printed"""
|
|
if 'user_id' not in session:
|
|
return jsonify({'success': False, 'error': 'Unauthorized'}), 401
|
|
|
|
try:
|
|
data = request.get_json() or {}
|
|
printed = data.get('printed', True)
|
|
|
|
success = update_order_printed_status(order_id, printed)
|
|
if success:
|
|
return jsonify({'success': True, 'message': 'Order status updated'}), 200
|
|
else:
|
|
return jsonify({'success': False, 'error': 'Failed to update order'}), 500
|
|
except Exception as e:
|
|
logger.error(f"Error updating order status: {e}")
|
|
return jsonify({'success': False, 'error': str(e)}), 500
|