updated to uplodad orders
This commit is contained in:
151
py_app/app/db_create_scripts/add_printed_labels_column.py
Normal file
151
py_app/app/db_create_scripts/add_printed_labels_column.py
Normal file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Database script to add the printed_labels column to the order_for_labels table
|
||||
This column will track whether labels have been printed for each order (boolean: 0=false, 1=true)
|
||||
Default value: 0 (false)
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import mariadb
|
||||
from flask import Flask
|
||||
|
||||
# Add the app directory to the path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
def get_db_connection():
|
||||
"""Get database connection using settings from external_server.conf"""
|
||||
# Go up two levels from this script to reach py_app directory, then to instance
|
||||
app_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
settings_file = os.path.join(app_root, 'instance', 'external_server.conf')
|
||||
|
||||
settings = {}
|
||||
with open(settings_file, 'r') as f:
|
||||
for line in f:
|
||||
key, value = line.strip().split('=', 1)
|
||||
settings[key] = value
|
||||
|
||||
return mariadb.connect(
|
||||
user=settings['username'],
|
||||
password=settings['password'],
|
||||
host=settings['server_domain'],
|
||||
port=int(settings['port']),
|
||||
database=settings['database_name']
|
||||
)
|
||||
|
||||
def add_printed_labels_column():
|
||||
"""
|
||||
Adds the printed_labels column to the order_for_labels table after the line_number column
|
||||
Column type: TINYINT(1) (boolean: 0=false, 1=true)
|
||||
Default value: 0 (false)
|
||||
"""
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if table exists
|
||||
cursor.execute("SHOW TABLES LIKE 'order_for_labels'")
|
||||
result = cursor.fetchone()
|
||||
|
||||
if not result:
|
||||
print("❌ Table 'order_for_labels' does not exist. Please create it first.")
|
||||
return False
|
||||
|
||||
# Check if column already exists
|
||||
cursor.execute("SHOW COLUMNS FROM order_for_labels LIKE 'printed_labels'")
|
||||
column_exists = cursor.fetchone()
|
||||
|
||||
if column_exists:
|
||||
print("ℹ️ Column 'printed_labels' already exists.")
|
||||
# Show current structure
|
||||
cursor.execute("DESCRIBE order_for_labels")
|
||||
columns = cursor.fetchall()
|
||||
print("\n📋 Current table structure:")
|
||||
for col in columns:
|
||||
null_info = 'NULL' if col[2] == 'YES' else 'NOT NULL'
|
||||
default_info = f" DEFAULT {col[4]}" if col[4] else ""
|
||||
print(f" 📌 {col[0]:<25} {col[1]:<20} {null_info}{default_info}")
|
||||
else:
|
||||
# Add the column after line_number
|
||||
alter_table_sql = """
|
||||
ALTER TABLE order_for_labels
|
||||
ADD COLUMN printed_labels TINYINT(1) NOT NULL DEFAULT 0
|
||||
COMMENT 'Boolean flag: 0=labels not printed, 1=labels printed'
|
||||
AFTER line_number
|
||||
"""
|
||||
|
||||
cursor.execute(alter_table_sql)
|
||||
conn.commit()
|
||||
print("✅ Column 'printed_labels' added successfully!")
|
||||
|
||||
# Show the updated structure
|
||||
cursor.execute("DESCRIBE order_for_labels")
|
||||
columns = cursor.fetchall()
|
||||
print("\n📋 Updated table structure:")
|
||||
for col in columns:
|
||||
null_info = 'NULL' if col[2] == 'YES' else 'NOT NULL'
|
||||
default_info = f" DEFAULT {col[4]}" if col[4] else ""
|
||||
highlight = "🆕 " if col[0] == 'printed_labels' else " "
|
||||
print(f"{highlight}{col[0]:<25} {col[1]:<20} {null_info}{default_info}")
|
||||
|
||||
# Show count of existing records that will have printed_labels = 0
|
||||
cursor.execute("SELECT COUNT(*) FROM order_for_labels")
|
||||
count = cursor.fetchone()[0]
|
||||
if count > 0:
|
||||
print(f"\n📊 {count} existing records now have printed_labels = 0 (false)")
|
||||
|
||||
conn.close()
|
||||
|
||||
except mariadb.Error as e:
|
||||
print(f"❌ Database error: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def verify_column():
|
||||
"""Verify the column was added correctly"""
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Test the column functionality
|
||||
cursor.execute("SELECT COUNT(*) as total, SUM(printed_labels) as printed FROM order_for_labels")
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result:
|
||||
total, printed = result
|
||||
print(f"\n🔍 Verification:")
|
||||
print(f" 📦 Total orders: {total}")
|
||||
print(f" 🖨️ Printed orders: {printed or 0}")
|
||||
print(f" 📄 Unprinted orders: {total - (printed or 0)}")
|
||||
|
||||
conn.close()
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Verification failed: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🔧 Adding printed_labels column to order_for_labels table...")
|
||||
print("="*60)
|
||||
|
||||
success = add_printed_labels_column()
|
||||
|
||||
if success:
|
||||
print("\n🔍 Verifying column addition...")
|
||||
verify_column()
|
||||
print("\n✅ Database modification completed successfully!")
|
||||
print("\n📝 Column Details:")
|
||||
print(" • Name: printed_labels")
|
||||
print(" • Type: TINYINT(1) (boolean)")
|
||||
print(" • Default: 0 (false - labels not printed)")
|
||||
print(" • Values: 0 = not printed, 1 = printed")
|
||||
print(" • Position: After line_number column")
|
||||
else:
|
||||
print("\n❌ Database modification failed!")
|
||||
|
||||
print("="*60)
|
||||
110
py_app/app/db_create_scripts/create_order_for_labels_table.py
Normal file
110
py_app/app/db_create_scripts/create_order_for_labels_table.py
Normal file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Database script to create the order_for_labels table
|
||||
This table will store order information for label generation
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import mariadb
|
||||
from flask import Flask
|
||||
|
||||
# Add the app directory to the path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
def get_db_connection():
|
||||
"""Get database connection using settings from external_server.conf"""
|
||||
# Go up two levels from this script to reach py_app directory, then to instance
|
||||
app_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
settings_file = os.path.join(app_root, 'instance', 'external_server.conf')
|
||||
|
||||
settings = {}
|
||||
with open(settings_file, 'r') as f:
|
||||
for line in f:
|
||||
key, value = line.strip().split('=', 1)
|
||||
settings[key] = value
|
||||
|
||||
return mariadb.connect(
|
||||
user=settings['username'],
|
||||
password=settings['password'],
|
||||
host=settings['server_domain'],
|
||||
port=int(settings['port']),
|
||||
database=settings['database_name']
|
||||
)
|
||||
|
||||
def create_order_for_labels_table():
|
||||
"""
|
||||
Creates the order_for_labels table with the specified structure
|
||||
"""
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# First check if table already exists
|
||||
cursor.execute("SHOW TABLES LIKE 'order_for_labels'")
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result:
|
||||
print("Table 'order_for_labels' already exists.")
|
||||
# Show current structure
|
||||
cursor.execute("DESCRIBE order_for_labels")
|
||||
columns = cursor.fetchall()
|
||||
print("\nCurrent table structure:")
|
||||
for col in columns:
|
||||
print(f" {col[0]} - {col[1]} {'NULL' if col[2] == 'YES' else 'NOT NULL'}")
|
||||
else:
|
||||
# Create the table
|
||||
create_table_sql = """
|
||||
CREATE TABLE order_for_labels (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique identifier',
|
||||
comanda_productie VARCHAR(15) NOT NULL COMMENT 'Production Order',
|
||||
cod_articol VARCHAR(15) COMMENT 'Article Code',
|
||||
descr_com_prod VARCHAR(50) NOT NULL COMMENT 'Production Order Description',
|
||||
cantitate INT(3) NOT NULL COMMENT 'Quantity',
|
||||
com_achiz_client VARCHAR(25) COMMENT 'Client Purchase Order',
|
||||
nr_linie_com_client INT(3) COMMENT 'Client Order Line Number',
|
||||
customer_name VARCHAR(50) COMMENT 'Customer Name',
|
||||
customer_article_number VARCHAR(25) COMMENT 'Customer Article Number',
|
||||
open_for_order VARCHAR(25) COMMENT 'Open for Order Status',
|
||||
line_number INT(3) COMMENT 'Line Number',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'Record creation timestamp',
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Record update timestamp'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Table for storing order information for label generation'
|
||||
"""
|
||||
|
||||
cursor.execute(create_table_sql)
|
||||
conn.commit()
|
||||
print("✅ Table 'order_for_labels' created successfully!")
|
||||
|
||||
# Show the created structure
|
||||
cursor.execute("DESCRIBE order_for_labels")
|
||||
columns = cursor.fetchall()
|
||||
print("\n📋 Table structure:")
|
||||
for col in columns:
|
||||
null_info = 'NULL' if col[2] == 'YES' else 'NOT NULL'
|
||||
default_info = f" DEFAULT {col[4]}" if col[4] else ""
|
||||
print(f" 📌 {col[0]:<25} {col[1]:<20} {null_info}{default_info}")
|
||||
|
||||
conn.close()
|
||||
|
||||
except mariadb.Error as e:
|
||||
print(f"❌ Database error: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🏗️ Creating order_for_labels table...")
|
||||
print("="*50)
|
||||
|
||||
success = create_order_for_labels_table()
|
||||
|
||||
if success:
|
||||
print("\n✅ Database setup completed successfully!")
|
||||
else:
|
||||
print("\n❌ Database setup failed!")
|
||||
|
||||
print("="*50)
|
||||
Reference in New Issue
Block a user