updated validation and auto submit
This commit is contained in:
41
py_app/app/db_create_scripts/create_scanfg_orders.py
Normal file
41
py_app/app/db_create_scripts/create_scanfg_orders.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import mariadb
|
||||
|
||||
# Database connection credentials
|
||||
# (reuse from create_scan_1db.py or update as needed)
|
||||
db_config = {
|
||||
"user": "trasabilitate",
|
||||
"password": "Initial01!",
|
||||
"host": "localhost",
|
||||
"database": "trasabilitate_database"
|
||||
}
|
||||
|
||||
try:
|
||||
conn = mariadb.connect(**db_config)
|
||||
cursor = conn.cursor()
|
||||
print("Connected to the database successfully!")
|
||||
|
||||
# Create the scanfg_orders table (same structure as scan1_orders)
|
||||
create_table_query = """
|
||||
CREATE TABLE IF NOT EXISTS scanfg_orders (
|
||||
Id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
operator_code VARCHAR(4) NOT NULL,
|
||||
CP_full_code VARCHAR(15) NOT NULL UNIQUE,
|
||||
OC1_code VARCHAR(4) NOT NULL,
|
||||
OC2_code VARCHAR(4) NOT NULL,
|
||||
CP_base_code VARCHAR(10) GENERATED ALWAYS AS (LEFT(CP_full_code, 10)) STORED,
|
||||
quality_code INT(3) NOT NULL,
|
||||
date DATE NOT NULL,
|
||||
time TIME NOT NULL,
|
||||
approved_quantity INT DEFAULT 0,
|
||||
rejected_quantity INT DEFAULT 0
|
||||
);
|
||||
"""
|
||||
cursor.execute(create_table_query)
|
||||
print("Table 'scanfg_orders' created successfully!")
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
except mariadb.Error as e:
|
||||
print(f"Error connecting to the database: {e}")
|
||||
73
py_app/app/db_create_scripts/create_triggers_fg.py
Normal file
73
py_app/app/db_create_scripts/create_triggers_fg.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import mariadb
|
||||
|
||||
# Database connection credentials
|
||||
db_config = {
|
||||
"user": "trasabilitate",
|
||||
"password": "Initial01!",
|
||||
"host": "localhost",
|
||||
"database": "trasabilitate_database"
|
||||
}
|
||||
|
||||
# Connect to the database
|
||||
try:
|
||||
conn = mariadb.connect(**db_config)
|
||||
cursor = conn.cursor()
|
||||
print("Connected to the database successfully!")
|
||||
|
||||
# Delete old triggers if they exist
|
||||
try:
|
||||
cursor.execute("DROP TRIGGER IF EXISTS increment_approved_quantity_fg;")
|
||||
print("Old trigger 'increment_approved_quantity_fg' deleted successfully.")
|
||||
except mariadb.Error as e:
|
||||
print(f"Error deleting old trigger 'increment_approved_quantity_fg': {e}")
|
||||
|
||||
try:
|
||||
cursor.execute("DROP TRIGGER IF EXISTS increment_rejected_quantity_fg;")
|
||||
print("Old trigger 'increment_rejected_quantity_fg' deleted successfully.")
|
||||
except mariadb.Error as e:
|
||||
print(f"Error deleting old trigger 'increment_rejected_quantity_fg': {e}")
|
||||
|
||||
# Create corrected trigger for approved_quantity in scanfg_orders
|
||||
create_approved_trigger_fg = """
|
||||
CREATE TRIGGER increment_approved_quantity_fg
|
||||
BEFORE INSERT ON scanfg_orders
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.quality_code = 000 THEN
|
||||
SET NEW.approved_quantity = (
|
||||
SELECT COUNT(*)
|
||||
FROM scanfg_orders
|
||||
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
|
||||
) + 1;
|
||||
SET NEW.rejected_quantity = (
|
||||
SELECT COUNT(*)
|
||||
FROM scanfg_orders
|
||||
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
|
||||
);
|
||||
ELSE
|
||||
SET NEW.approved_quantity = (
|
||||
SELECT COUNT(*)
|
||||
FROM scanfg_orders
|
||||
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
|
||||
);
|
||||
SET NEW.rejected_quantity = (
|
||||
SELECT COUNT(*)
|
||||
FROM scanfg_orders
|
||||
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
|
||||
) + 1;
|
||||
END IF;
|
||||
END;
|
||||
"""
|
||||
cursor.execute(create_approved_trigger_fg)
|
||||
print("Trigger 'increment_approved_quantity_fg' created successfully for scanfg_orders table!")
|
||||
|
||||
# Commit changes and close the connection
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
print("\n✅ All triggers for scanfg_orders table created successfully!")
|
||||
print("The approved_quantity and rejected_quantity will now be calculated automatically.")
|
||||
|
||||
except mariadb.Error as e:
|
||||
print(f"Error connecting to the database or creating triggers: {e}")
|
||||
Reference in New Issue
Block a user