updated view in label preview

This commit is contained in:
Quality System Admin
2025-10-27 20:25:31 +02:00
parent d142129de6
commit a960285d59
15 changed files with 1889 additions and 685 deletions

View File

@@ -150,7 +150,8 @@ class DailyMirrorDatabase:
# Prepare insert statement with new schema
insert_sql = """
INSERT INTO dm_production_orders (
production_order, open_for_order_line, client_order_line,
production_order, production_order_line, line_number,
open_for_order_line, client_order_line,
customer_code, customer_name, article_code, article_description,
quantity_requested, unit_of_measure, delivery_date, opening_date,
closing_date, data_planificare, production_status,
@@ -161,7 +162,7 @@ class DailyMirrorDatabase:
phase_t3_sewing, t3_operator_name, t3_registration_date,
design_number, classification, model_description, model_lb2,
needle_position, needle_row, priority
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
open_for_order_line = VALUES(open_for_order_line),
client_order_line = VALUES(client_order_line),
@@ -302,7 +303,7 @@ class DailyMirrorDatabase:
return None
def import_orders_data(self, file_path):
"""Import orders data from Excel file with enhanced error handling"""
"""Import orders data from Excel file with enhanced error handling and multi-line support"""
try:
# Ensure we have a database connection
if not self.connection:
@@ -327,120 +328,125 @@ class DailyMirrorDatabase:
'error_message': f'Orders file not found: {file_path}'
}
# Try to get sheet names first
# Read from DataSheet - the correct sheet for orders data
try:
excel_file = pd.ExcelFile(file_path)
sheet_names = excel_file.sheet_names
logger.info(f"Available sheets in orders file: {sheet_names}")
df = pd.read_excel(file_path, sheet_name='DataSheet', engine='openpyxl', header=0)
logger.info(f"Successfully read orders data from DataSheet: {len(df)} rows, {len(df.columns)} columns")
logger.info(f"Available columns: {list(df.columns)[:15]}...")
except Exception as e:
logger.warning(f"Could not get sheet names: {e}")
sheet_names = ['DataSheet', 'Sheet1']
# Try multiple approaches to read the Excel file
df = None
sheet_used = None
approaches = [
('openpyxl', 0, 'read_only'),
('openpyxl', 0, 'normal'),
('openpyxl', 1, 'normal'),
('xlrd', 0, 'normal') if file_path.endswith('.xls') else None,
('default', 0, 'normal')
]
for approach in approaches:
if approach is None:
continue
engine, sheet_name, mode = approach
try:
logger.info(f"Trying to read orders with engine: {engine}, sheet: {sheet_name}, mode: {mode}")
if engine == 'default':
df = pd.read_excel(file_path, sheet_name=sheet_name, header=0)
elif mode == 'read_only':
df = pd.read_excel(file_path, sheet_name=sheet_name, engine=engine, header=0)
else:
df = pd.read_excel(file_path, sheet_name=sheet_name, engine=engine, header=0)
sheet_used = f"{engine} (sheet: {sheet_name}, mode: {mode})"
logger.info(f"Successfully read orders data with: {sheet_used}")
break
except Exception as e:
logger.warning(f"Failed with {engine}, sheet {sheet_name}, mode {mode}: {e}")
continue
if df is None:
logger.error("Could not read the orders file with any method")
logger.error(f"Failed to read DataSheet from orders file: {e}")
return {
'success_count': 0,
'error_count': 1,
'total_rows': 0,
'error_message': 'Could not read the orders Excel file. The file may have formatting issues or be corrupted.'
'error_message': f'Could not read DataSheet from orders file: {e}'
}
logger.info(f"Loaded orders data from {sheet_used}: {len(df)} rows, {len(df.columns)} columns")
logger.info(f"Available columns: {list(df.columns)[:10]}...")
cursor = self.connection.cursor()
success_count = 0
created_count = 0
updated_count = 0
error_count = 0
# Prepare insert statement for orders
# Prepare insert statement matching the actual table structure
insert_sql = """
INSERT INTO dm_orders (
order_id, customer_code, customer_name, client_order,
article_code, article_description, quantity_requested, delivery_date,
order_status, product_group, order_date
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
order_line, order_id, line_number, customer_code, customer_name,
client_order_line, article_code, article_description,
quantity_requested, balance, unit_of_measure, delivery_date, order_date,
order_status, article_status, priority, product_group, production_order,
production_status, model, closed
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
order_id = VALUES(order_id),
line_number = VALUES(line_number),
customer_code = VALUES(customer_code),
customer_name = VALUES(customer_name),
client_order = VALUES(client_order),
client_order_line = VALUES(client_order_line),
article_code = VALUES(article_code),
article_description = VALUES(article_description),
quantity_requested = VALUES(quantity_requested),
balance = VALUES(balance),
unit_of_measure = VALUES(unit_of_measure),
delivery_date = VALUES(delivery_date),
order_status = VALUES(order_status),
product_group = VALUES(product_group),
order_date = VALUES(order_date),
order_status = VALUES(order_status),
article_status = VALUES(article_status),
priority = VALUES(priority),
product_group = VALUES(product_group),
production_order = VALUES(production_order),
production_status = VALUES(production_status),
model = VALUES(model),
closed = VALUES(closed),
updated_at = CURRENT_TIMESTAMP
"""
# Process each row with the actual column mapping and better null handling
# Safe value helper functions
def safe_str(value, default=''):
if pd.isna(value):
return default
return str(value).strip() if value != '' else default
def safe_int(value, default=None):
if pd.isna(value):
return default
try:
if isinstance(value, str):
value = value.strip()
if value == '':
return default
return int(float(value))
except (ValueError, TypeError):
return default
def safe_float(value, default=None):
if pd.isna(value):
return default
try:
if isinstance(value, str):
value = value.strip()
if value == '':
return default
return float(value)
except (ValueError, TypeError):
return default
# Process each row with the new schema
for index, row in df.iterrows():
try:
# Helper function to safely get values and handle NaN
def safe_get(row, column, default=''):
value = row.get(column, default)
if pd.isna(value) or value == 'nan':
return default
return str(value).strip() if isinstance(value, str) else value
# Create concatenated unique keys
order_id = safe_str(row.get('Comanda'), f'ORD_{index:06d}')
line_number = safe_int(row.get('Linie'), 1)
order_line = f"{order_id}-{line_number}"
def safe_get_int(row, column, default=0):
value = row.get(column, default)
if pd.isna(value) or value == 'nan':
return default
try:
return int(float(value)) if value != '' else default
except (ValueError, TypeError):
return default
# Create concatenated client order line
client_order = safe_str(row.get('Com. Achiz. Client'))
client_order_line_num = safe_str(row.get('Nr. linie com. client'))
client_order_line = f"{client_order}-{client_order_line_num}" if client_order and client_order_line_num else ''
# Map columns based on the actual Vizual. Artic. Comenzi Deschise format
# Map all fields from Excel to database (21 fields, removed client_order)
data = (
safe_get(row, 'Comanda', f'ORD_{index:06d}'), # Order ID
safe_get(row, 'Cod. Client'), # Customer Code
safe_get(row, 'Customer Name'), # Customer Name
safe_get(row, 'Com. Achiz. Client'), # Client Order
safe_get(row, 'Cod Articol'), # Article Code
safe_get(row, 'Part Description', safe_get(row, 'Descr. Articol')), # Article Description
safe_get_int(row, 'Cantitate'), # Quantity
self._parse_date(row.get('Data livrare')), # Delivery Date
safe_get(row, 'Statut Comanda', 'PENDING'), # Order Status
safe_get(row, 'Model'), # Product Group
self._parse_date(row.get('Data Comenzii')) # Order Date
order_line, # order_line (UNIQUE key: order_id-line_number)
order_id, # order_id
line_number, # line_number
safe_str(row.get('Cod. Client')), # customer_code
safe_str(row.get('Customer Name')), # customer_name
client_order_line, # client_order_line (concatenated)
safe_str(row.get('Cod Articol')), # article_code
safe_str(row.get('Part Description')), # article_description
safe_int(row.get('Cantitate')), # quantity_requested
safe_float(row.get('Balanta')), # balance
safe_str(row.get('U.M.')), # unit_of_measure
self._parse_date(row.get('Data livrare')), # delivery_date
self._parse_date(row.get('Data Comenzii')), # order_date
safe_str(row.get('Statut Comanda')), # order_status
safe_str(row.get('Stare Articol')), # article_status
safe_int(row.get('Prioritate')), # priority
safe_str(row.get('Grup')), # product_group
safe_str(row.get('Comanda Productie')), # production_order
safe_str(row.get('Stare CP')), # production_status
safe_str(row.get('Model')), # model
safe_str(row.get('Inchis')) # closed
)
cursor.execute(insert_sql, data)
@@ -454,12 +460,12 @@ class DailyMirrorDatabase:
success_count += 1
except Exception as row_error:
logger.warning(f"Error processing row {index}: {row_error}")
logger.warning(f"Error processing row {index} (order_line: {order_line if 'order_line' in locals() else 'unknown'}): {row_error}")
error_count += 1
continue
self.connection.commit()
logger.info(f"Orders import completed: {success_count} successful, {error_count} errors")
logger.info(f"Orders import completed: {success_count} successful ({created_count} created, {updated_count} updated), {error_count} errors")
return {
'success_count': success_count,
@@ -472,6 +478,8 @@ class DailyMirrorDatabase:
except Exception as e:
logger.error(f"Error importing orders data: {e}")
import traceback
logger.error(traceback.format_exc())
return {
'success_count': 0,
'error_count': 1,
@@ -566,76 +574,74 @@ class DailyMirrorDatabase:
updated_count = 0
error_count = 0
# Prepare insert statement for deliveries
# Prepare insert statement for deliveries - simple INSERT, every Excel row gets a database row
insert_sql = """
INSERT INTO dm_deliveries (
shipment_id, order_id, customer_code, customer_name,
shipment_id, order_id, client_order_line, customer_code, customer_name,
article_code, article_description, quantity_delivered,
shipment_date, delivery_date, delivery_status, total_value
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
customer_code = VALUES(customer_code),
customer_name = VALUES(customer_name),
article_code = VALUES(article_code),
article_description = VALUES(article_description),
quantity_delivered = VALUES(quantity_delivered),
shipment_date = VALUES(shipment_date),
delivery_date = VALUES(delivery_date),
delivery_status = VALUES(delivery_status),
total_value = VALUES(total_value),
updated_at = CURRENT_TIMESTAMP
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
# Process each row with the actual column mapping and better null handling
for index, row in df.iterrows():
try:
# Helper function to safely get values and handle NaN
def safe_get(row, column, default=''):
value = row.get(column, default)
if pd.isna(value) or value == 'nan':
# Safe value helper functions
def safe_str(value, default=''):
if pd.isna(value):
return default
return str(value).strip() if isinstance(value, str) else value
return str(value).strip() if value != '' else default
def safe_get_float(row, column, default=0.0):
value = row.get(column, default)
if pd.isna(value) or value == 'nan':
def safe_int(value, default=None):
if pd.isna(value):
return default
try:
return float(value) if value != '' else default
if isinstance(value, str):
value = value.strip()
if value == '':
return default
return int(float(value))
except (ValueError, TypeError):
return default
def safe_get_int(row, column, default=0):
value = row.get(column, default)
if pd.isna(value) or value == 'nan':
def safe_float(value, default=None):
if pd.isna(value):
return default
try:
return int(float(value)) if value != '' else default
if isinstance(value, str):
value = value.strip()
if value == '':
return default
return float(value)
except (ValueError, TypeError):
return default
# Create concatenated client order line: Com. Achiz. Client + "-" + Linie
client_order = safe_str(row.get('Com. Achiz. Client'))
linie = safe_str(row.get('Linie'))
client_order_line = f"{client_order}-{linie}" if client_order and linie else ''
# Map columns based on the actual Articole livrate_returnate format
data = (
safe_get(row, 'Document Number', f'SH_{index:06d}'), # Shipment ID
safe_get(row, 'Comanda'), # Order ID
safe_get(row, 'Cod. Client'), # Customer Code
safe_get(row, 'Nume client'), # Customer Name
safe_get(row, 'Cod Articol'), # Article Code
safe_get(row, 'Part Description'), # Article Description
safe_get_int(row, 'Cantitate'), # Quantity Delivered
safe_str(row.get('Document Number'), f'SH_{index:06d}'), # Shipment ID
safe_str(row.get('Comanda')), # Order ID
client_order_line, # Client Order Line (concatenated)
safe_str(row.get('Cod. Client')), # Customer Code
safe_str(row.get('Nume client')), # Customer Name
safe_str(row.get('Cod Articol')), # Article Code
safe_str(row.get('Part Description')), # Article Description
safe_int(row.get('Cantitate')), # Quantity Delivered
self._parse_date(row.get('Data')), # Shipment Date
self._parse_date(row.get('Data')), # Delivery Date (same as shipment for now)
safe_get(row, 'Stare', 'DELIVERED'), # Delivery Status
safe_get_float(row, 'Total Price') # Total Value
safe_str(row.get('Stare'), 'DELIVERED'), # Delivery Status
safe_float(row.get('Total Price')) # Total Value
)
cursor.execute(insert_sql, data)
# Track created vs updated
# Track created rows (simple INSERT always creates)
if cursor.rowcount == 1:
created_count += 1
elif cursor.rowcount == 2:
updated_count += 1
success_count += 1