""" 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 []