- Created labels module with complete structure in app/modules/labels/ - Implemented print_module.py with database functions: * get_unprinted_orders_data() - Retrieve unprinted orders from database * get_printed_orders_data() - Retrieve printed orders from database * update_order_printed_status() - Mark orders as printed * search_orders_by_cp_code() - Search orders by production code - Created routes.py with Flask Blueprint and API endpoints: * GET /labels/ - Module home page with feature launchers * GET /labels/print-module - Main label printing interface * GET /labels/print-lost-labels - Lost label reprinting interface * GET /labels/api/unprinted-orders - API for unprinted orders * GET /labels/api/printed-orders - API for printed orders * POST /labels/api/search-orders - Search orders API * POST /labels/api/update-printed-status/<id> - Update status API - Migrated HTML templates from legacy app with theme support: * print_module.html - Thermal label printing with QZ Tray integration * print_lost_labels.html - Lost label search and reprint interface * Added comprehensive CSS variables for dark/light mode theming * Preserved all original JavaScript functionality for printing - Created index.html module home page with feature launchers - Registered labels blueprint in app/__init__.py with /labels prefix - Added 'Label Printing' module card to dashboard with print icon All functionality preserved from original implementation: - QZ Tray thermal printer integration - JsBarcode barcode generation (horizontal + vertical) - PDF export fallback - Session-based authentication for all routes - Database integration with proper error handling
201 lines
7.4 KiB
Python
201 lines
7.4 KiB
Python
"""
|
|
Labels Module - Print Module Functions
|
|
Handles retrieval of orders for label printing
|
|
"""
|
|
import logging
|
|
from app.database import get_db
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_unprinted_orders_data(limit=100):
|
|
"""
|
|
Retrieve unprinted orders from the database for label printing
|
|
Returns list of order dictionaries where printed_labels != 1
|
|
"""
|
|
try:
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
|
|
# Check if order_for_labels table exists
|
|
cursor.execute("SHOW TABLES LIKE 'order_for_labels'")
|
|
if not cursor.fetchone():
|
|
logger.warning("order_for_labels table does not exist")
|
|
return []
|
|
|
|
# Check if printed_labels column exists
|
|
cursor.execute("SHOW COLUMNS FROM order_for_labels LIKE 'printed_labels'")
|
|
column_exists = cursor.fetchone()
|
|
|
|
if column_exists:
|
|
# Use printed_labels column
|
|
cursor.execute("""
|
|
SELECT id, comanda_productie, cod_articol, descr_com_prod, cantitate,
|
|
com_achiz_client, nr_linie_com_client, customer_name,
|
|
customer_article_number, open_for_order, line_number,
|
|
created_at, updated_at, printed_labels, data_livrare, dimensiune
|
|
FROM order_for_labels
|
|
WHERE printed_labels IS NULL OR printed_labels = 0
|
|
ORDER BY created_at DESC
|
|
LIMIT %s
|
|
""", (limit,))
|
|
else:
|
|
# Fallback: get all orders if no printed_labels column
|
|
cursor.execute("""
|
|
SELECT id, comanda_productie, cod_articol, descr_com_prod, cantitate,
|
|
com_achiz_client, nr_linie_com_client, customer_name,
|
|
customer_article_number, open_for_order, line_number,
|
|
created_at, updated_at, 0 as printed_labels, data_livrare, dimensiune
|
|
FROM order_for_labels
|
|
ORDER BY created_at DESC
|
|
LIMIT %s
|
|
""", (limit,))
|
|
|
|
columns = [col[0] for col in cursor.description]
|
|
orders = []
|
|
|
|
for row in cursor.fetchall():
|
|
order_dict = {columns[i]: row[i] for i in range(len(columns))}
|
|
# Ensure date fields are strings
|
|
if order_dict.get('created_at'):
|
|
order_dict['created_at'] = str(order_dict['created_at'])
|
|
if order_dict.get('updated_at'):
|
|
order_dict['updated_at'] = str(order_dict['updated_at'])
|
|
if order_dict.get('data_livrare'):
|
|
order_dict['data_livrare'] = str(order_dict['data_livrare'])
|
|
orders.append(order_dict)
|
|
|
|
cursor.close()
|
|
logger.info(f"Retrieved {len(orders)} unprinted orders")
|
|
return orders
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error retrieving unprinted orders: {e}")
|
|
return []
|
|
|
|
|
|
def get_printed_orders_data(limit=100):
|
|
"""
|
|
Retrieve printed orders from the database
|
|
Returns list of order dictionaries where printed_labels = 1
|
|
"""
|
|
try:
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
|
|
# Check if order_for_labels table exists
|
|
cursor.execute("SHOW TABLES LIKE 'order_for_labels'")
|
|
if not cursor.fetchone():
|
|
logger.warning("order_for_labels table does not exist")
|
|
return []
|
|
|
|
# Check if printed_labels column exists
|
|
cursor.execute("SHOW COLUMNS FROM order_for_labels LIKE 'printed_labels'")
|
|
column_exists = cursor.fetchone()
|
|
|
|
if column_exists:
|
|
# Get orders where printed_labels = 1
|
|
cursor.execute("""
|
|
SELECT id, comanda_productie, cod_articol, descr_com_prod, cantitate,
|
|
com_achiz_client, nr_linie_com_client, customer_name,
|
|
customer_article_number, open_for_order, line_number,
|
|
created_at, updated_at, printed_labels, data_livrare, dimensiune
|
|
FROM order_for_labels
|
|
WHERE printed_labels = 1
|
|
ORDER BY updated_at DESC
|
|
LIMIT %s
|
|
""", (limit,))
|
|
else:
|
|
# Fallback: no printed orders if column doesn't exist
|
|
logger.info("printed_labels column does not exist - no printed orders available")
|
|
cursor.close()
|
|
return []
|
|
|
|
columns = [col[0] for col in cursor.description]
|
|
orders = []
|
|
|
|
for row in cursor.fetchall():
|
|
order_dict = {columns[i]: row[i] for i in range(len(columns))}
|
|
# Ensure date fields are strings
|
|
if order_dict.get('created_at'):
|
|
order_dict['created_at'] = str(order_dict['created_at'])
|
|
if order_dict.get('updated_at'):
|
|
order_dict['updated_at'] = str(order_dict['updated_at'])
|
|
if order_dict.get('data_livrare'):
|
|
order_dict['data_livrare'] = str(order_dict['data_livrare'])
|
|
orders.append(order_dict)
|
|
|
|
cursor.close()
|
|
logger.info(f"Retrieved {len(orders)} printed orders")
|
|
return orders
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error retrieving printed orders: {e}")
|
|
return []
|
|
|
|
|
|
def update_order_printed_status(order_id, printed=True):
|
|
"""
|
|
Update the printed_labels status for an order
|
|
"""
|
|
try:
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
|
UPDATE order_for_labels
|
|
SET printed_labels = %s
|
|
WHERE id = %s
|
|
""", (1 if printed else 0, order_id))
|
|
|
|
conn.commit()
|
|
cursor.close()
|
|
logger.info(f"Updated order {order_id} printed status to {printed}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error updating order printed status: {e}")
|
|
return False
|
|
|
|
|
|
def search_orders_by_cp_code(cp_code_search):
|
|
"""
|
|
Search for orders by CP code / Production Order
|
|
"""
|
|
try:
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
|
|
search_term = cp_code_search.strip().upper()
|
|
|
|
cursor.execute("""
|
|
SELECT id, comanda_productie, cod_articol, descr_com_prod, cantitate,
|
|
com_achiz_client, nr_linie_com_client, customer_name,
|
|
customer_article_number, open_for_order, line_number,
|
|
created_at, updated_at, printed_labels, data_livrare, dimensiune
|
|
FROM order_for_labels
|
|
WHERE UPPER(comanda_productie) LIKE %s
|
|
ORDER BY created_at DESC
|
|
LIMIT 100
|
|
""", (f"{search_term}%",))
|
|
|
|
columns = [col[0] for col in cursor.description]
|
|
orders = []
|
|
|
|
for row in cursor.fetchall():
|
|
order_dict = {columns[i]: row[i] for i in range(len(columns))}
|
|
if order_dict.get('created_at'):
|
|
order_dict['created_at'] = str(order_dict['created_at'])
|
|
if order_dict.get('updated_at'):
|
|
order_dict['updated_at'] = str(order_dict['updated_at'])
|
|
if order_dict.get('data_livrare'):
|
|
order_dict['data_livrare'] = str(order_dict['data_livrare'])
|
|
orders.append(order_dict)
|
|
|
|
cursor.close()
|
|
return orders
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error searching orders by CP code: {e}")
|
|
return []
|