diff --git a/py_app/app/__pycache__/routes.cpython-311.pyc b/py_app/app/__pycache__/routes.cpython-311.pyc
index ea20127..b39d66d 100644
Binary files a/py_app/app/__pycache__/routes.cpython-311.pyc and b/py_app/app/__pycache__/routes.cpython-311.pyc differ
diff --git a/py_app/app/db_create_scripts/create_scan_1db.py b/py_app/app/db_create_scripts/create_scan_1db.py
new file mode 100644
index 0000000..c2b6bcf
--- /dev/null
+++ b/py_app/app/db_create_scripts/create_scan_1db.py
@@ -0,0 +1,42 @@
+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!")
+
+ # Create the scan1_orders table
+ create_table_query = """
+ CREATE TABLE IF NOT EXISTS scan1_orders (
+ Id INT AUTO_INCREMENT PRIMARY KEY, -- Auto-incremented ID with 6 digits
+ operator_code VARCHAR(4) NOT NULL, -- Operator code with 4 characters
+ CP_full_code VARCHAR(15) NOT NULL, -- Full CP code with up to 15 characters
+ OC1_code VARCHAR(4) NOT NULL, -- OC1 code with 4 characters
+ OC2_code VARCHAR(4) NOT NULL, -- OC2 code with 4 characters
+ CP_base_code VARCHAR(10) GENERATED ALWAYS AS (LEFT(CP_full_code, 10)) STORED, -- Auto-generated base code (first 10 characters of CP_full_code)
+ quality_code INT(3) NOT NULL, -- Quality code with 3 digits
+ date DATE NOT NULL, -- Date in format dd-mm-yyyy
+ time TIME NOT NULL, -- Time in format hh:mm:ss
+ approved_quantity INT DEFAULT 0, -- Auto-incremented quantity for quality_code = 000
+ rejected_quantity INT DEFAULT 0 -- Auto-incremented quantity for quality_code != 000
+ );
+ """
+ cursor.execute(create_table_query)
+ print("Table 'scan1_orders' created successfully!")
+
+ # Commit changes and close the connection
+ conn.commit()
+ cursor.close()
+ conn.close()
+
+except mariadb.Error as e:
+ print(f"Error connecting to the database: {e}")
\ No newline at end of file
diff --git a/py_app/app/db_create_scripts/create_triggers.py b/py_app/app/db_create_scripts/create_triggers.py
new file mode 100644
index 0000000..f0bf158
--- /dev/null
+++ b/py_app/app/db_create_scripts/create_triggers.py
@@ -0,0 +1,63 @@
+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!")
+
+ # Create trigger for approved_quantity
+ create_approved_trigger = """
+ DELIMITER //
+ CREATE TRIGGER increment_approved_quantity
+ BEFORE INSERT ON scan1_orders
+ FOR EACH ROW
+ BEGIN
+ IF NEW.quality_code = 000 THEN
+ SET NEW.approved_quantity = (
+ SELECT IFNULL(MAX(approved_quantity), 0) + 1
+ FROM scan1_orders
+ WHERE CP_base_code = NEW.CP_base_code
+ );
+ END IF;
+ END//
+ DELIMITER ;
+ """
+ cursor.execute(create_approved_trigger)
+ print("Trigger 'increment_approved_quantity' created successfully!")
+
+ # Create trigger for rejected_quantity
+ create_rejected_trigger = """
+ DELIMITER //
+ CREATE TRIGGER increment_rejected_quantity
+ BEFORE INSERT ON scan1_orders
+ FOR EACH ROW
+ BEGIN
+ IF NEW.quality_code != 000 THEN
+ SET NEW.rejected_quantity = (
+ SELECT IFNULL(MAX(rejected_quantity), 0) + 1
+ FROM scan1_orders
+ WHERE CP_base_code = NEW.CP_base_code
+ );
+ END IF;
+ END//
+ DELIMITER ;
+ """
+ cursor.execute(create_rejected_trigger)
+ print("Trigger 'increment_rejected_quantity' created successfully!")
+
+ # Commit changes and close the connection
+ conn.commit()
+ cursor.close()
+ conn.close()
+
+except mariadb.Error as e:
+ print(f"Error connecting to the database or creating triggers: {e}")
\ No newline at end of file
diff --git a/py_app/app/query.py b/py_app/app/query.py
new file mode 100644
index 0000000..57e72bb
--- /dev/null
+++ b/py_app/app/query.py
@@ -0,0 +1,34 @@
+import mariadb
+
+# Database connection credentials
+db_config = {
+ "user": "trasabilitate",
+ "password": "Initial01!",
+ "host": "localhost",
+ "database": "trasabilitate_database"
+}
+
+try:
+ # Connect to the database
+ conn = mariadb.connect(**db_config)
+ cursor = conn.cursor()
+
+ # Query to fetch all records from the scan1 table
+ query = "SELECT * FROM scan1_orders ORDER BY Id DESC LIMIT 15"
+ cursor.execute(query)
+
+ # Fetch and print the results
+ rows = cursor.fetchall()
+ if rows:
+ print("Records in the 'scan1_orders' table:")
+ for row in rows:
+ print(row)
+ else:
+ print("No records found in the 'scan1_orders' table.")
+
+ # Close the connection
+ cursor.close()
+ conn.close()
+
+except mariadb.Error as e:
+ print(f"Error connecting to the database: {e}")
\ No newline at end of file
diff --git a/py_app/app/routes.py b/py_app/app/routes.py
index 9d96574..598019d 100644
--- a/py_app/app/routes.py
+++ b/py_app/app/routes.py
@@ -1,5 +1,5 @@
import os
-import pyodbc
+import mariadb
from flask import Blueprint, render_template, redirect, url_for, request, flash, session, current_app
from .models import User
from . import db
@@ -7,7 +7,7 @@ from . import db
bp = Blueprint('main', __name__)
def get_db_connection():
- """Reads the external_server.conf file and returns a database connection."""
+ """Reads the external_server.conf file and returns a MariaDB database connection."""
settings_file = os.path.join(current_app.instance_path, 'external_server.conf')
if not os.path.exists(settings_file):
raise FileNotFoundError("The external_server.conf file is missing in the instance folder.")
@@ -19,15 +19,14 @@ def get_db_connection():
key, value = line.strip().split('=', 1)
settings[key] = value
- # Create a database connection string
- connection_string = (
- f"DRIVER={{ODBC Driver 17 for SQL Server}};"
- f"SERVER={settings['server_domain']},{settings['port']};"
- f"DATABASE={settings['database_name']};"
- f"UID={settings['username']};"
- f"PWD={settings['password']};"
+ # Create a database connection
+ return mariadb.connect(
+ user=settings['username'],
+ password=settings['password'],
+ host=settings['server_domain'],
+ port=int(settings['port']),
+ database=settings['database_name']
)
- return pyodbc.connect(connection_string)
@bp.route('/login', methods=['GET', 'POST'])
def login():
@@ -99,18 +98,37 @@ def scan():
date = request.form.get('date')
time = request.form.get('time')
+ # Print the values to the terminal for debugging with single quotes
+ print("Values to be inserted:")
+ print(f"Operator Code: '{operator_code}'")
+ print(f"CP Code: '{cp_code}'")
+ print(f"OC1 Code: '{oc1_code}'")
+ print(f"OC2 Code: '{oc2_code}'")
+ print(f"Defect Code: '{defect_code}'")
+ print(f"Date: '{date}'")
+ print(f"Time: '{time}'")
+
try:
+ # Connect to the database
conn = get_db_connection()
cursor = conn.cursor()
- cursor.execute(
- "INSERT INTO scanare (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, quantity) "
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
- operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, 1
- )
+
+ # Insert query
+ insert_query = """
+ INSERT INTO scan1_orders (operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ """
+ print(f"Executing query: {insert_query}")
+ print(f"With values: ('{operator_code}', '{cp_code}', '{oc1_code}', '{oc2_code}', '{defect_code}', '{date}', '{time}')")
+
+ # Execute the query
+ cursor.execute(insert_query, (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time))
conn.commit()
conn.close()
+
flash('Scan data saved successfully.')
- except Exception as e:
+ except mariadb.Error as e:
+ print(f"Error saving scan data: {e}")
flash(f"Error saving scan data: {e}")
# Fetch the latest scan data for display
@@ -118,10 +136,16 @@ def scan():
try:
conn = get_db_connection()
cursor = conn.cursor()
- cursor.execute("SELECT TOP 14 * FROM scanare ORDER BY id DESC")
+ cursor.execute("""
+ SELECT Id, operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time, approved_quantity, rejected_quantity
+ FROM scan1_orders
+ ORDER BY Id DESC
+ LIMIT 15
+ """)
scan_data = cursor.fetchall()
conn.close()
- except Exception as e:
+ except mariadb.Error as e:
+ print(f"Error fetching scan data: {e}")
flash(f"Error fetching scan data: {e}")
return render_template('scan.html', scan_data=scan_data)
diff --git a/py_app/app/templates/scan.html b/py_app/app/templates/scan.html
index 42e5e81..c39af97 100644
--- a/py_app/app/templates/scan.html
+++ b/py_app/app/templates/scan.html
@@ -10,7 +10,7 @@
-
+
@@ -22,7 +22,7 @@
-
+
@@ -38,28 +38,30 @@
ID
- Operator Code
+ Op Code
CP Code
OC1 Code
OC2 Code
Defect Code
Date
Time
- Quantity
+ Apr. Quantity
+ Rejec. Quantity