updated print module
This commit is contained in:
@@ -1614,4 +1614,147 @@ def create_locations():
|
||||
@warehouse_bp.route('/import_locations_csv', methods=['GET', 'POST'])
|
||||
def import_locations_csv():
|
||||
from app.warehouse import import_locations_csv_handler
|
||||
return import_locations_csv_handler()
|
||||
return import_locations_csv_handler()
|
||||
|
||||
@bp.route('/print_labels_silent/<int:order_id>', methods=['POST'])
|
||||
def print_labels_silent(order_id):
|
||||
"""Generate PDF and send directly to Windows Print Service - bypasses CORS"""
|
||||
print(f"DEBUG: print_labels_silent called for order_id: {order_id}")
|
||||
|
||||
if 'role' not in session or session['role'] not in ['superadmin', 'warehouse_manager', 'etichete']:
|
||||
return jsonify({'error': 'Access denied'}), 403
|
||||
|
||||
try:
|
||||
import requests
|
||||
import tempfile
|
||||
import os
|
||||
from .pdf_generator import generate_order_labels_pdf, update_order_printed_status
|
||||
from .print_module import get_db_connection
|
||||
|
||||
# Get printer name from request
|
||||
data = request.get_json() or {}
|
||||
printer_name = data.get('printer_name', 'default')
|
||||
|
||||
print(f"DEBUG: Using printer: {printer_name}")
|
||||
|
||||
# Get order data from database
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT id, comanda_productie, cod_articol, descr_com_prod, cantitate,
|
||||
data_livrare, dimensiune, com_achiz_client, nr_linie_com_client, customer_name,
|
||||
customer_article_number, open_for_order, line_number,
|
||||
printed_labels, created_at, updated_at
|
||||
FROM order_for_labels
|
||||
WHERE id = %s
|
||||
""", (order_id,))
|
||||
|
||||
order_data = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if not order_data:
|
||||
return jsonify({'error': 'Order not found'}), 404
|
||||
|
||||
# Convert to dictionary for easier access
|
||||
columns = ['id', 'comanda_productie', 'cod_articol', 'descr_com_prod', 'cantitate',
|
||||
'data_livrare', 'dimensiune', 'com_achiz_client', 'nr_linie_com_client',
|
||||
'customer_name', 'customer_article_number', 'open_for_order', 'line_number',
|
||||
'printed_labels', 'created_at', 'updated_at']
|
||||
order_dict = dict(zip(columns, order_data))
|
||||
|
||||
print(f"DEBUG: Order data: {order_dict}")
|
||||
|
||||
# Generate PDF content in memory
|
||||
pdf_content = generate_order_labels_pdf(order_dict)
|
||||
|
||||
# Save PDF to a location accessible by Windows service
|
||||
pdf_dir = r"C:\temp\quality_labels"
|
||||
if not os.path.exists(pdf_dir):
|
||||
os.makedirs(pdf_dir, exist_ok=True)
|
||||
|
||||
pdf_filename = f"order_{order_id}_{order_dict['comanda_productie']}.pdf"
|
||||
pdf_path = os.path.join(pdf_dir, pdf_filename)
|
||||
|
||||
# Write PDF to file
|
||||
with open(pdf_path, 'wb') as pdf_file:
|
||||
pdf_file.write(pdf_content)
|
||||
|
||||
print(f"DEBUG: Created PDF file: {pdf_path}")
|
||||
|
||||
try:
|
||||
# Send to Windows Print Service with local file path
|
||||
print_service_url = 'http://localhost:8765'
|
||||
|
||||
# Create the print request with local file path
|
||||
print_data = {
|
||||
'pdf_path': pdf_path,
|
||||
'printer_name': printer_name,
|
||||
'copies': 1,
|
||||
'silent': True,
|
||||
'order_id': order_id,
|
||||
'quantity': order_dict['cantitate']
|
||||
}
|
||||
|
||||
print(f"DEBUG: Sending print request to {print_service_url}/print/silent")
|
||||
|
||||
# Send request to Windows service
|
||||
response = requests.post(
|
||||
f'{print_service_url}/print/silent',
|
||||
json=print_data,
|
||||
headers={'Content-Type': 'application/json'},
|
||||
timeout=30
|
||||
)
|
||||
|
||||
print(f"DEBUG: Print service response: {response.status_code}")
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
print(f"DEBUG: Print result: {result}")
|
||||
|
||||
if result.get('success'):
|
||||
# Update order status
|
||||
update_order_printed_status(order_id)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Labels printed successfully',
|
||||
'printer': printer_name,
|
||||
'order': order_dict['comanda_productie'],
|
||||
'quantity': order_dict['cantitate']
|
||||
})
|
||||
else:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': result.get('error', 'Print service returned error')
|
||||
}), 500
|
||||
else:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Print service returned status {response.status_code}'
|
||||
}), 500
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"DEBUG: Print service connection error: {e}")
|
||||
# Fallback: Return the PDF for manual printing
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Windows Print Service not available: {str(e)}',
|
||||
'fallback': 'pdf_download',
|
||||
'pdf_path': pdf_path
|
||||
}), 503
|
||||
|
||||
finally:
|
||||
# Clean up PDF file
|
||||
try:
|
||||
os.unlink(pdf_path)
|
||||
print(f"DEBUG: Cleaned up PDF file: {pdf_path}")
|
||||
except Exception as e:
|
||||
print(f"DEBUG: Error cleaning up PDF file: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"DEBUG: Error in print_labels_silent: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user