diff --git a/py_app/app/__pycache__/routes.cpython-312.pyc b/py_app/app/__pycache__/routes.cpython-312.pyc index 2dad955..b23cf0e 100644 Binary files a/py_app/app/__pycache__/routes.cpython-312.pyc and b/py_app/app/__pycache__/routes.cpython-312.pyc differ diff --git a/py_app/app/db_create_scripts/create_scanfg_orders.py b/py_app/app/db_create_scripts/create_scanfg_orders.py new file mode 100644 index 0000000..45dd5bb --- /dev/null +++ b/py_app/app/db_create_scripts/create_scanfg_orders.py @@ -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}") diff --git a/py_app/app/db_create_scripts/create_triggers_fg.py b/py_app/app/db_create_scripts/create_triggers_fg.py new file mode 100644 index 0000000..17a5ba0 --- /dev/null +++ b/py_app/app/db_create_scripts/create_triggers_fg.py @@ -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}") diff --git a/py_app/app/routes.py b/py_app/app/routes.py index 7cecbde..d93f99d 100755 --- a/py_app/app/routes.py +++ b/py_app/app/routes.py @@ -299,6 +299,79 @@ def logout(): session.pop('role', None) return redirect(url_for('main.login')) +# Finish Goods Scan Route +@bp.route('/fg_scan', methods=['GET', 'POST']) +def fg_scan(): + if 'role' not in session or session['role'] not in ['superadmin', 'administrator', 'admin', 'scan']: + flash('Access denied: Scan users only.') + return redirect(url_for('main.dashboard')) + + if request.method == 'POST': + # Handle form submission + operator_code = request.form.get('operator_code') + cp_code = request.form.get('cp_code') + oc1_code = request.form.get('oc1_code') + oc2_code = request.form.get('oc2_code') + defect_code = request.form.get('defect_code') + date = request.form.get('date') + time = request.form.get('time') + + try: + # Connect to the database + conn = get_db_connection() + cursor = conn.cursor() + + # Check if the CP_full_code already exists in scanfg_orders + cursor.execute("SELECT Id FROM scanfg_orders WHERE CP_full_code = ?", (cp_code,)) + existing_entry = cursor.fetchone() + + if existing_entry: + # Update the existing entry + update_query = """ + UPDATE scanfg_orders + SET operator_code = ?, OC1_code = ?, OC2_code = ?, quality_code = ?, date = ?, time = ? + WHERE CP_full_code = ? + """ + cursor.execute(update_query, (operator_code, oc1_code, oc2_code, defect_code, date, time, cp_code)) + flash('Existing entry updated successfully.') + else: + # Insert a new entry + insert_query = """ + INSERT INTO scanfg_orders (operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time) + VALUES (?, ?, ?, ?, ?, ?, ?) + """ + cursor.execute(insert_query, (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time)) + flash('New entry inserted successfully.') + + # Commit the transaction + conn.commit() + conn.close() + + except mariadb.Error as e: + print(f"Error saving finish goods scan data: {e}") + flash(f"Error saving scan data: {e}") + + # Fetch the latest scan data for display from scanfg_orders + scan_data = [] + try: + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute(""" + SELECT Id, operator_code, CP_base_code, OC1_code, OC2_code, quality_code, date, time, approved_quantity, rejected_quantity + FROM scanfg_orders + ORDER BY Id DESC + LIMIT 15 + """) + raw_scan_data = cursor.fetchall() + # Apply formatting to scan data for consistent date display + scan_data = [[format_cell_data(cell) for cell in row] for row in raw_scan_data] + conn.close() + except mariadb.Error as e: + print(f"Error fetching finish goods scan data: {e}") + flash(f"Error fetching scan data: {e}") + + return render_template('fg_scan.html', scan_data=scan_data) + @bp.route('/create_user', methods=['POST']) def create_user(): return create_user_handler() diff --git a/py_app/app/templates/fg_scan.html b/py_app/app/templates/fg_scan.html new file mode 100644 index 0000000..ab88ad9 --- /dev/null +++ b/py_app/app/templates/fg_scan.html @@ -0,0 +1,473 @@ +{% extends "base.html" %} + +{% block title %}Finish Good Scan{% endblock %} +{% block head %} + + + +{% endblock %} +{% block content %} +
| ID | +Op Code | +CP Code | +OC1 Code | +OC2 Code | +Defect Code | +Date | +Time | +Apr. Quantity | +Rejec. Quantity | +
|---|---|---|---|---|---|---|---|---|---|
| {{ row[0] }} | +{{ row[1] }} | +{{ row[2] }} | +{{ row[3] }} | +{{ row[4] }} | +{{ row[5] }} | +{{ row[6] }} | +{{ row[7] }} | +{{ row[8] }} | +{{ row[9] }} | +
Access the scanning module for production orders.
- Finish goods scanning + Finish goods scan