v0.13.0 - Add Consumption Sheets module
New module for tracking production consumption with lot scanning: - Admin configuration for process types (AD WIP, etc.) - Dynamic table creation per process - Flexible header/detail field definitions with Excel cell mapping - Duplicate detection with configurable key field - Staff scanning interface with duplicate warnings (same session/cross session) - Excel export using uploaded templates with multi-page support - Template settings for rows per page and detail start row
This commit is contained in:
2
app.py
2
app.py
@@ -38,7 +38,7 @@ app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=1)
|
|||||||
|
|
||||||
|
|
||||||
# 1. Define the version
|
# 1. Define the version
|
||||||
APP_VERSION = '0.12.1'
|
APP_VERSION = '0.13.0'
|
||||||
|
|
||||||
# 2. Inject it into all templates automatically
|
# 2. Inject it into all templates automatically
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,5 @@
|
|||||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, session
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, session
|
||||||
from db import query_db, execute_db
|
from db import query_db, execute_db, get_db
|
||||||
from utils import role_required
|
from utils import role_required
|
||||||
|
|
||||||
cons_sheets_bp = Blueprint('cons_sheets', __name__)
|
cons_sheets_bp = Blueprint('cons_sheets', __name__)
|
||||||
@@ -49,12 +49,101 @@ def create_process():
|
|||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?)
|
||||||
''', [process_key, process_name, session['user_id']])
|
''', [process_key, process_name, session['user_id']])
|
||||||
|
|
||||||
|
# Create dynamic detail table for this process
|
||||||
|
create_process_detail_table(process_key)
|
||||||
|
|
||||||
flash(f'Process "{process_name}" created successfully!', 'success')
|
flash(f'Process "{process_name}" created successfully!', 'success')
|
||||||
return redirect(url_for('cons_sheets.process_detail', process_id=process_id))
|
return redirect(url_for('cons_sheets.process_detail', process_id=process_id))
|
||||||
|
|
||||||
return render_template('cons_sheets/create_process.html')
|
return render_template('cons_sheets/create_process.html')
|
||||||
|
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
|
||||||
|
def get_db_path():
|
||||||
|
"""Get the database path"""
|
||||||
|
db_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'database', 'scanlook.db')
|
||||||
|
print(f"DEBUG: Database path is: {db_path}")
|
||||||
|
print(f"DEBUG: Path exists: {os.path.exists(db_path)}")
|
||||||
|
return db_path
|
||||||
|
|
||||||
|
|
||||||
|
def create_process_detail_table(process_key):
|
||||||
|
"""Create the dynamic detail table for a process with system columns"""
|
||||||
|
table_name = f'cons_proc_{process_key}_details'
|
||||||
|
print(f"DEBUG: Creating table {table_name}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
db_path = get_db_path()
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute(f'''
|
||||||
|
CREATE TABLE IF NOT EXISTS {table_name} (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
session_id INTEGER NOT NULL,
|
||||||
|
scanned_by INTEGER NOT NULL,
|
||||||
|
scanned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
duplicate_status TEXT DEFAULT 'normal' CHECK(duplicate_status IN ('normal', 'dup_same_session', 'dup_other_session')),
|
||||||
|
duplicate_info TEXT,
|
||||||
|
comment TEXT,
|
||||||
|
is_deleted INTEGER DEFAULT 0,
|
||||||
|
FOREIGN KEY (session_id) REFERENCES cons_sessions(id),
|
||||||
|
FOREIGN KEY (scanned_by) REFERENCES Users(user_id)
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
|
||||||
|
# Create index on session_id
|
||||||
|
cursor.execute(f'CREATE INDEX IF NOT EXISTS idx_{process_key}_session ON {table_name}(session_id, is_deleted)')
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
print(f"DEBUG: Table {table_name} created successfully!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERROR creating table {table_name}: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def add_column_to_detail_table(process_key, field_name, field_type):
|
||||||
|
"""Add a column to the process detail table"""
|
||||||
|
table_name = f'cons_proc_{process_key}_details'
|
||||||
|
|
||||||
|
# Map field types to SQLite types
|
||||||
|
sqlite_type = 'TEXT'
|
||||||
|
if field_type == 'INTEGER':
|
||||||
|
sqlite_type = 'INTEGER'
|
||||||
|
elif field_type == 'REAL':
|
||||||
|
sqlite_type = 'REAL'
|
||||||
|
|
||||||
|
conn = sqlite3.connect(get_db_path())
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
cursor.execute(f'ALTER TABLE {table_name} ADD COLUMN {field_name} {sqlite_type}')
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
# Column might already exist
|
||||||
|
print(f"Note: Could not add column {field_name}: {e}")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def rename_column_in_detail_table(process_key, old_name, new_name):
|
||||||
|
"""Rename a column (for soft delete)"""
|
||||||
|
table_name = f'cons_proc_{process_key}_details'
|
||||||
|
|
||||||
|
conn = sqlite3.connect(get_db_path())
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
cursor.execute(f'ALTER TABLE {table_name} RENAME COLUMN {old_name} TO {new_name}')
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Note: Could not rename column {old_name}: {e}")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/admin/consumption-sheets/<int:process_id>')
|
@cons_sheets_bp.route('/admin/consumption-sheets/<int:process_id>')
|
||||||
@role_required('owner', 'admin')
|
@role_required('owner', 'admin')
|
||||||
def process_detail(process_id):
|
def process_detail(process_id):
|
||||||
@@ -278,13 +367,20 @@ def add_field(process_id, table_type):
|
|||||||
''', [process_id, table_type], one=True)
|
''', [process_id, table_type], one=True)
|
||||||
sort_order = (max_sort['max_sort'] or 0) + 1
|
sort_order = (max_sort['max_sort'] or 0) + 1
|
||||||
|
|
||||||
|
# For detail fields, check for duplicate key checkbox
|
||||||
|
is_duplicate_key = 1 if (table_type == 'detail' and request.form.get('is_duplicate_key')) else 0
|
||||||
|
|
||||||
# Insert the field
|
# Insert the field
|
||||||
execute_db('''
|
execute_db('''
|
||||||
INSERT INTO cons_process_fields
|
INSERT INTO cons_process_fields
|
||||||
(process_id, table_type, field_name, field_label, field_type, max_length, is_required, sort_order, excel_cell)
|
(process_id, table_type, field_name, field_label, field_type, max_length, is_required, is_duplicate_key, sort_order, excel_cell)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
''', [process_id, table_type, field_name, field_label, field_type,
|
''', [process_id, table_type, field_name, field_label, field_type,
|
||||||
int(max_length) if max_length else None, is_required, sort_order, excel_cell or None])
|
int(max_length) if max_length else None, is_required, is_duplicate_key, sort_order, excel_cell or None])
|
||||||
|
|
||||||
|
# For detail fields, also add the column to the dynamic table
|
||||||
|
if table_type == 'detail':
|
||||||
|
add_column_to_detail_table(process['process_key'], field_name, field_type)
|
||||||
|
|
||||||
flash(f'Field "{field_label}" added successfully!', 'success')
|
flash(f'Field "{field_label}" added successfully!', 'success')
|
||||||
return redirect(url_for('cons_sheets.process_fields', process_id=process_id))
|
return redirect(url_for('cons_sheets.process_fields', process_id=process_id))
|
||||||
@@ -310,6 +406,7 @@ def edit_field(process_id, field_id):
|
|||||||
field_type = request.form.get('field_type', 'TEXT')
|
field_type = request.form.get('field_type', 'TEXT')
|
||||||
max_length = request.form.get('max_length', '')
|
max_length = request.form.get('max_length', '')
|
||||||
is_required = 1 if request.form.get('is_required') else 0
|
is_required = 1 if request.form.get('is_required') else 0
|
||||||
|
is_duplicate_key = 1 if (field['table_type'] == 'detail' and request.form.get('is_duplicate_key')) else 0
|
||||||
excel_cell = request.form.get('excel_cell', '').strip().upper()
|
excel_cell = request.form.get('excel_cell', '').strip().upper()
|
||||||
|
|
||||||
if not field_label:
|
if not field_label:
|
||||||
@@ -318,9 +415,9 @@ def edit_field(process_id, field_id):
|
|||||||
|
|
||||||
execute_db('''
|
execute_db('''
|
||||||
UPDATE cons_process_fields
|
UPDATE cons_process_fields
|
||||||
SET field_label = ?, field_type = ?, max_length = ?, is_required = ?, excel_cell = ?
|
SET field_label = ?, field_type = ?, max_length = ?, is_required = ?, is_duplicate_key = ?, excel_cell = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
''', [field_label, field_type, int(max_length) if max_length else None, is_required, excel_cell or None, field_id])
|
''', [field_label, field_type, int(max_length) if max_length else None, is_required, is_duplicate_key, excel_cell or None, field_id])
|
||||||
|
|
||||||
flash(f'Field "{field_label}" updated successfully!', 'success')
|
flash(f'Field "{field_label}" updated successfully!', 'success')
|
||||||
return redirect(url_for('cons_sheets.process_fields', process_id=process_id))
|
return redirect(url_for('cons_sheets.process_fields', process_id=process_id))
|
||||||
@@ -335,13 +432,20 @@ def edit_field(process_id, field_id):
|
|||||||
def delete_field(process_id, field_id):
|
def delete_field(process_id, field_id):
|
||||||
"""Soft-delete a field (rename column, set is_active = 0)"""
|
"""Soft-delete a field (rename column, set is_active = 0)"""
|
||||||
field = query_db('SELECT * FROM cons_process_fields WHERE id = ? AND process_id = ?', [field_id, process_id], one=True)
|
field = query_db('SELECT * FROM cons_process_fields WHERE id = ? AND process_id = ?', [field_id, process_id], one=True)
|
||||||
|
process = query_db('SELECT * FROM cons_processes WHERE id = ?', [process_id], one=True)
|
||||||
|
|
||||||
if not field:
|
if not field or not process:
|
||||||
return jsonify({'success': False, 'message': 'Field not found'})
|
return jsonify({'success': False, 'message': 'Field not found'})
|
||||||
|
|
||||||
# Soft delete: set is_active = 0
|
# Soft delete: set is_active = 0
|
||||||
execute_db('UPDATE cons_process_fields SET is_active = 0 WHERE id = ?', [field_id])
|
execute_db('UPDATE cons_process_fields SET is_active = 0 WHERE id = ?', [field_id])
|
||||||
|
|
||||||
|
# For detail fields, rename the column to preserve data
|
||||||
|
if field['table_type'] == 'detail':
|
||||||
|
old_name = field['field_name']
|
||||||
|
new_name = f"Del_{field_id}_{old_name}"
|
||||||
|
rename_column_in_detail_table(process['process_key'], old_name, new_name)
|
||||||
|
|
||||||
return jsonify({'success': True, 'message': f'Field "{field["field_label"]}" deleted'})
|
return jsonify({'success': True, 'message': f'Field "{field["field_label"]}" deleted'})
|
||||||
|
|
||||||
|
|
||||||
@@ -351,6 +455,20 @@ def delete_field(process_id, field_id):
|
|||||||
|
|
||||||
from utils import login_required
|
from utils import login_required
|
||||||
|
|
||||||
|
def get_detail_table_name(process_key):
|
||||||
|
"""Get the dynamic detail table name for a process"""
|
||||||
|
return f'cons_proc_{process_key}_details'
|
||||||
|
|
||||||
|
|
||||||
|
def get_duplicate_key_field(process_id):
|
||||||
|
"""Get the field marked as duplicate key for a process"""
|
||||||
|
return query_db('''
|
||||||
|
SELECT * FROM cons_process_fields
|
||||||
|
WHERE process_id = ? AND table_type = 'detail' AND is_duplicate_key = 1 AND is_active = 1
|
||||||
|
LIMIT 1
|
||||||
|
''', [process_id], one=True)
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/cons-sheets')
|
@cons_sheets_bp.route('/cons-sheets')
|
||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
@@ -368,23 +486,38 @@ def index():
|
|||||||
flash('You do not have access to this module', 'danger')
|
flash('You do not have access to this module', 'danger')
|
||||||
return redirect(url_for('home'))
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
# Get user's active sessions with process info
|
# Get user's active sessions with process info and scan counts
|
||||||
active_sessions = query_db('''
|
active_sessions = query_db('''
|
||||||
SELECT cs.*, cp.process_name, cp.process_key,
|
SELECT cs.*, cp.process_name, cp.process_key
|
||||||
(SELECT COUNT(*) FROM cons_session_details WHERE session_id = cs.id AND is_deleted = 0) as scan_count
|
|
||||||
FROM cons_sessions cs
|
FROM cons_sessions cs
|
||||||
JOIN cons_processes cp ON cs.process_id = cp.id
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
WHERE cs.created_by = ? AND cs.status = 'active'
|
WHERE cs.created_by = ? AND cs.status = 'active'
|
||||||
ORDER BY cs.created_at DESC
|
ORDER BY cs.created_at DESC
|
||||||
''', [user_id])
|
''', [user_id])
|
||||||
|
|
||||||
|
# Get scan counts for each session from their dynamic tables
|
||||||
|
sessions_with_counts = []
|
||||||
|
for sess in active_sessions:
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
try:
|
||||||
|
count_result = query_db(f'''
|
||||||
|
SELECT COUNT(*) as scan_count FROM {table_name}
|
||||||
|
WHERE session_id = ? AND is_deleted = 0
|
||||||
|
''', [sess['id']], one=True)
|
||||||
|
sess_dict = dict(sess)
|
||||||
|
sess_dict['scan_count'] = count_result['scan_count'] if count_result else 0
|
||||||
|
except:
|
||||||
|
sess_dict = dict(sess)
|
||||||
|
sess_dict['scan_count'] = 0
|
||||||
|
sessions_with_counts.append(sess_dict)
|
||||||
|
|
||||||
# Get available process types for creating new sessions
|
# Get available process types for creating new sessions
|
||||||
processes = query_db('''
|
processes = query_db('''
|
||||||
SELECT * FROM cons_processes WHERE is_active = 1 ORDER BY process_name
|
SELECT * FROM cons_processes WHERE is_active = 1 ORDER BY process_name
|
||||||
''')
|
''')
|
||||||
|
|
||||||
return render_template('cons_sheets/staff_index.html',
|
return render_template('cons_sheets/staff_index.html',
|
||||||
sessions=active_sessions,
|
sessions=sessions_with_counts,
|
||||||
processes=processes)
|
processes=processes)
|
||||||
|
|
||||||
|
|
||||||
@@ -474,79 +607,90 @@ def scan_session(session_id):
|
|||||||
ORDER BY cpf.sort_order, cpf.id
|
ORDER BY cpf.sort_order, cpf.id
|
||||||
''', [session_id])
|
''', [session_id])
|
||||||
|
|
||||||
# Get scanned details
|
# Get detail fields for this process (convert to dicts for JSON serialization)
|
||||||
scans = query_db('''
|
detail_fields_rows = query_db('''
|
||||||
SELECT csd.*, u.full_name as scanned_by_name
|
|
||||||
FROM cons_session_details csd
|
|
||||||
JOIN Users u ON csd.scanned_by = u.user_id
|
|
||||||
WHERE csd.session_id = ? AND csd.is_deleted = 0
|
|
||||||
ORDER BY csd.scanned_at DESC
|
|
||||||
''', [session_id])
|
|
||||||
|
|
||||||
# Get detail fields for reference
|
|
||||||
detail_fields = query_db('''
|
|
||||||
SELECT * FROM cons_process_fields
|
SELECT * FROM cons_process_fields
|
||||||
WHERE process_id = ? AND table_type = 'detail' AND is_active = 1
|
WHERE process_id = ? AND table_type = 'detail' AND is_active = 1
|
||||||
ORDER BY sort_order, id
|
ORDER BY sort_order, id
|
||||||
''', [sess['process_id']])
|
''', [sess['process_id']])
|
||||||
|
detail_fields = [dict(row) for row in detail_fields_rows] if detail_fields_rows else []
|
||||||
|
|
||||||
|
# Get scanned details from the dynamic table
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
scans = query_db(f'''
|
||||||
|
SELECT t.*, u.full_name as scanned_by_name
|
||||||
|
FROM {table_name} t
|
||||||
|
JOIN Users u ON t.scanned_by = u.user_id
|
||||||
|
WHERE t.session_id = ? AND t.is_deleted = 0
|
||||||
|
ORDER BY t.scanned_at DESC
|
||||||
|
''', [session_id])
|
||||||
|
|
||||||
|
# Get the duplicate key field (convert to dict for JSON)
|
||||||
|
dup_key_field_row = get_duplicate_key_field(sess['process_id'])
|
||||||
|
dup_key_field = dict(dup_key_field_row) if dup_key_field_row else None
|
||||||
|
|
||||||
return render_template('cons_sheets/scan_session.html',
|
return render_template('cons_sheets/scan_session.html',
|
||||||
session=sess,
|
session=sess,
|
||||||
header_values=header_values,
|
header_values=header_values,
|
||||||
scans=scans,
|
scans=scans,
|
||||||
detail_fields=detail_fields)
|
detail_fields=detail_fields,
|
||||||
|
dup_key_field=dup_key_field)
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/scan', methods=['POST'])
|
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/scan', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def scan_lot(session_id):
|
def scan_lot(session_id):
|
||||||
"""Process a lot scan with duplicate detection"""
|
"""Process a scan with duplicate detection using dynamic tables"""
|
||||||
sess = query_db('SELECT * FROM cons_sessions WHERE id = ? AND status = "active"', [session_id], one=True)
|
sess = query_db('''
|
||||||
|
SELECT cs.*, cp.process_key, cp.id as process_id
|
||||||
|
FROM cons_sessions cs
|
||||||
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
|
WHERE cs.id = ? AND cs.status = 'active'
|
||||||
|
''', [session_id], one=True)
|
||||||
|
|
||||||
if not sess:
|
if not sess:
|
||||||
return jsonify({'success': False, 'message': 'Session not found or archived'})
|
return jsonify({'success': False, 'message': 'Session not found or archived'})
|
||||||
|
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
lot_number = data.get('lot_number', '').strip()
|
field_values = data.get('field_values', {}) # Dict of field_name: value
|
||||||
item_number = data.get('item_number', '').strip()
|
|
||||||
weight = data.get('weight')
|
|
||||||
confirm_duplicate = data.get('confirm_duplicate', False)
|
confirm_duplicate = data.get('confirm_duplicate', False)
|
||||||
check_only = data.get('check_only', False)
|
check_only = data.get('check_only', False)
|
||||||
|
|
||||||
if not lot_number:
|
# Get the duplicate key field
|
||||||
return jsonify({'success': False, 'message': 'Lot number required'})
|
dup_key_field = get_duplicate_key_field(sess['process_id'])
|
||||||
|
|
||||||
if not check_only and weight is None:
|
if not dup_key_field:
|
||||||
return jsonify({'success': False, 'message': 'Weight required'})
|
return jsonify({'success': False, 'message': 'No duplicate key field configured for this process'})
|
||||||
|
|
||||||
if not check_only:
|
dup_key_value = field_values.get(dup_key_field['field_name'], '').strip()
|
||||||
try:
|
|
||||||
weight = float(weight)
|
if not dup_key_value:
|
||||||
except (ValueError, TypeError):
|
return jsonify({'success': False, 'message': f'{dup_key_field["field_label"]} is required'})
|
||||||
return jsonify({'success': False, 'message': 'Invalid weight value'})
|
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
|
||||||
# Check for duplicates in SAME session
|
# Check for duplicates in SAME session
|
||||||
same_session_dup = query_db('''
|
same_session_dup = query_db(f'''
|
||||||
SELECT * FROM cons_session_details
|
SELECT * FROM {table_name}
|
||||||
WHERE session_id = ? AND lot_number = ? AND is_deleted = 0
|
WHERE session_id = ? AND {dup_key_field['field_name']} = ? AND is_deleted = 0
|
||||||
''', [session_id, lot_number], one=True)
|
''', [session_id, dup_key_value], one=True)
|
||||||
|
|
||||||
# Check for duplicates in OTHER sessions (with header info for context)
|
# Check for duplicates in OTHER sessions (need to check all sessions of same process type)
|
||||||
other_session_dup = query_db('''
|
other_session_dup = query_db(f'''
|
||||||
SELECT csd.*, cs.id as other_session_id, cs.created_at as other_session_date,
|
SELECT t.*, cs.id as other_session_id, cs.created_at as other_session_date,
|
||||||
u.full_name as other_user,
|
u.full_name as other_user,
|
||||||
(SELECT field_value FROM cons_session_header_values
|
(SELECT field_value FROM cons_session_header_values
|
||||||
WHERE session_id = cs.id AND field_id = (
|
WHERE session_id = cs.id AND field_id = (
|
||||||
SELECT id FROM cons_process_fields
|
SELECT id FROM cons_process_fields
|
||||||
WHERE process_id = cs.process_id AND field_name LIKE '%wo%' AND is_active = 1 LIMIT 1
|
WHERE process_id = cs.process_id AND field_name LIKE '%wo%' AND is_active = 1 LIMIT 1
|
||||||
)) as other_wo
|
)) as other_wo
|
||||||
FROM cons_session_details csd
|
FROM {table_name} t
|
||||||
JOIN cons_sessions cs ON csd.session_id = cs.id
|
JOIN cons_sessions cs ON t.session_id = cs.id
|
||||||
JOIN Users u ON csd.scanned_by = u.user_id
|
JOIN Users u ON t.scanned_by = u.user_id
|
||||||
WHERE csd.lot_number = ? AND csd.session_id != ? AND csd.is_deleted = 0
|
WHERE t.{dup_key_field['field_name']} = ? AND t.session_id != ? AND t.is_deleted = 0
|
||||||
ORDER BY csd.scanned_at DESC
|
ORDER BY t.scanned_at DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
''', [lot_number, session_id], one=True)
|
''', [dup_key_value, session_id], one=True)
|
||||||
|
|
||||||
duplicate_status = 'normal'
|
duplicate_status = 'normal'
|
||||||
duplicate_info = None
|
duplicate_info = None
|
||||||
@@ -586,19 +730,36 @@ def scan_lot(session_id):
|
|||||||
'message': duplicate_info
|
'message': duplicate_info
|
||||||
})
|
})
|
||||||
|
|
||||||
# Insert the scan
|
# Get all active detail fields for this process
|
||||||
detail_id = execute_db('''
|
detail_fields = query_db('''
|
||||||
INSERT INTO cons_session_details
|
SELECT * FROM cons_process_fields
|
||||||
(session_id, item_number, lot_number, weight, scanned_by, duplicate_status, duplicate_info)
|
WHERE process_id = ? AND table_type = 'detail' AND is_active = 1
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
ORDER BY sort_order, id
|
||||||
''', [session_id, item_number, lot_number, weight, session['user_id'], duplicate_status, duplicate_info])
|
''', [sess['process_id']])
|
||||||
|
|
||||||
|
# Build dynamic INSERT statement
|
||||||
|
field_names = ['session_id', 'scanned_by', 'duplicate_status', 'duplicate_info']
|
||||||
|
field_placeholders = ['?', '?', '?', '?']
|
||||||
|
values = [session_id, session['user_id'], duplicate_status, duplicate_info]
|
||||||
|
|
||||||
|
for field in detail_fields:
|
||||||
|
field_names.append(field['field_name'])
|
||||||
|
field_placeholders.append('?')
|
||||||
|
values.append(field_values.get(field['field_name'], ''))
|
||||||
|
|
||||||
|
insert_sql = f'''
|
||||||
|
INSERT INTO {table_name} ({', '.join(field_names)})
|
||||||
|
VALUES ({', '.join(field_placeholders)})
|
||||||
|
'''
|
||||||
|
|
||||||
|
detail_id = execute_db(insert_sql, values)
|
||||||
|
|
||||||
# If this is a same-session duplicate, update the original scan too
|
# If this is a same-session duplicate, update the original scan too
|
||||||
updated_entry_ids = []
|
updated_entry_ids = []
|
||||||
if duplicate_status == 'dup_same_session' and same_session_dup:
|
if duplicate_status == 'dup_same_session' and same_session_dup:
|
||||||
execute_db('''
|
execute_db(f'''
|
||||||
UPDATE cons_session_details
|
UPDATE {table_name}
|
||||||
SET duplicate_status = 'dup_same_session', duplicate_info = 'Duplicate lot'
|
SET duplicate_status = 'dup_same_session', duplicate_info = 'Duplicate'
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
''', [same_session_dup['id']])
|
''', [same_session_dup['id']])
|
||||||
updated_entry_ids.append(same_session_dup['id'])
|
updated_entry_ids.append(same_session_dup['id'])
|
||||||
@@ -611,16 +772,28 @@ def scan_lot(session_id):
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/cons-sheets/detail/<int:detail_id>')
|
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/detail/<int:detail_id>')
|
||||||
@login_required
|
@login_required
|
||||||
def get_detail(detail_id):
|
def get_detail(session_id, detail_id):
|
||||||
"""Get detail info for editing"""
|
"""Get detail info for editing"""
|
||||||
detail = query_db('''
|
sess = query_db('''
|
||||||
SELECT csd.*, u.full_name as scanned_by_name
|
SELECT cs.*, cp.process_key
|
||||||
FROM cons_session_details csd
|
FROM cons_sessions cs
|
||||||
JOIN Users u ON csd.scanned_by = u.user_id
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
WHERE csd.id = ?
|
WHERE cs.id = ?
|
||||||
''', [detail_id], one=True)
|
''', [session_id], one=True)
|
||||||
|
|
||||||
|
if not sess:
|
||||||
|
return jsonify({'success': False, 'message': 'Session not found'})
|
||||||
|
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
|
||||||
|
detail = query_db(f'''
|
||||||
|
SELECT t.*, u.full_name as scanned_by_name
|
||||||
|
FROM {table_name} t
|
||||||
|
JOIN Users u ON t.scanned_by = u.user_id
|
||||||
|
WHERE t.id = ? AND t.session_id = ?
|
||||||
|
''', [detail_id, session_id], one=True)
|
||||||
|
|
||||||
if not detail:
|
if not detail:
|
||||||
return jsonify({'success': False, 'message': 'Detail not found'})
|
return jsonify({'success': False, 'message': 'Detail not found'})
|
||||||
@@ -628,11 +801,23 @@ def get_detail(detail_id):
|
|||||||
return jsonify({'success': True, 'detail': dict(detail)})
|
return jsonify({'success': True, 'detail': dict(detail)})
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/cons-sheets/detail/<int:detail_id>/update', methods=['POST'])
|
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/detail/<int:detail_id>/update', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def update_detail(detail_id):
|
def update_detail(session_id, detail_id):
|
||||||
"""Update a scanned detail"""
|
"""Update a scanned detail"""
|
||||||
detail = query_db('SELECT * FROM cons_session_details WHERE id = ?', [detail_id], one=True)
|
sess = query_db('''
|
||||||
|
SELECT cs.*, cp.process_key, cp.id as process_id
|
||||||
|
FROM cons_sessions cs
|
||||||
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
|
WHERE cs.id = ?
|
||||||
|
''', [session_id], one=True)
|
||||||
|
|
||||||
|
if not sess:
|
||||||
|
return jsonify({'success': False, 'message': 'Session not found'})
|
||||||
|
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
|
||||||
|
detail = query_db(f'SELECT * FROM {table_name} WHERE id = ? AND session_id = ?', [detail_id, session_id], one=True)
|
||||||
|
|
||||||
if not detail:
|
if not detail:
|
||||||
return jsonify({'success': False, 'message': 'Detail not found'})
|
return jsonify({'success': False, 'message': 'Detail not found'})
|
||||||
@@ -642,33 +827,54 @@ def update_detail(detail_id):
|
|||||||
return jsonify({'success': False, 'message': 'Permission denied'})
|
return jsonify({'success': False, 'message': 'Permission denied'})
|
||||||
|
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
item_number = data.get('item_number', '').strip()
|
field_values = data.get('field_values', {})
|
||||||
lot_number = data.get('lot_number', '').strip()
|
|
||||||
weight = data.get('weight')
|
|
||||||
comment = data.get('comment', '')
|
comment = data.get('comment', '')
|
||||||
|
|
||||||
if not lot_number:
|
# Get all active detail fields for this process
|
||||||
return jsonify({'success': False, 'message': 'Lot number required'})
|
detail_fields = query_db('''
|
||||||
|
SELECT * FROM cons_process_fields
|
||||||
|
WHERE process_id = ? AND table_type = 'detail' AND is_active = 1
|
||||||
|
''', [sess['process_id']])
|
||||||
|
|
||||||
try:
|
# Build dynamic UPDATE statement
|
||||||
weight = float(weight)
|
set_clauses = ['comment = ?']
|
||||||
except (ValueError, TypeError):
|
values = [comment]
|
||||||
return jsonify({'success': False, 'message': 'Invalid weight'})
|
|
||||||
|
|
||||||
execute_db('''
|
for field in detail_fields:
|
||||||
UPDATE cons_session_details
|
if field['field_name'] in field_values:
|
||||||
SET item_number = ?, lot_number = ?, weight = ?, comment = ?
|
set_clauses.append(f"{field['field_name']} = ?")
|
||||||
|
values.append(field_values[field['field_name']])
|
||||||
|
|
||||||
|
values.append(detail_id)
|
||||||
|
|
||||||
|
update_sql = f'''
|
||||||
|
UPDATE {table_name}
|
||||||
|
SET {', '.join(set_clauses)}
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
''', [item_number, lot_number, weight, comment, detail_id])
|
'''
|
||||||
|
|
||||||
|
execute_db(update_sql, values)
|
||||||
|
|
||||||
return jsonify({'success': True})
|
return jsonify({'success': True})
|
||||||
|
|
||||||
|
|
||||||
@cons_sheets_bp.route('/cons-sheets/detail/<int:detail_id>/delete', methods=['POST'])
|
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/detail/<int:detail_id>/delete', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def delete_detail(detail_id):
|
def delete_detail(session_id, detail_id):
|
||||||
"""Soft-delete a scanned detail"""
|
"""Soft-delete a scanned detail"""
|
||||||
detail = query_db('SELECT * FROM cons_session_details WHERE id = ?', [detail_id], one=True)
|
sess = query_db('''
|
||||||
|
SELECT cs.*, cp.process_key
|
||||||
|
FROM cons_sessions cs
|
||||||
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
|
WHERE cs.id = ?
|
||||||
|
''', [session_id], one=True)
|
||||||
|
|
||||||
|
if not sess:
|
||||||
|
return jsonify({'success': False, 'message': 'Session not found'})
|
||||||
|
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
|
||||||
|
detail = query_db(f'SELECT * FROM {table_name} WHERE id = ? AND session_id = ?', [detail_id, session_id], one=True)
|
||||||
|
|
||||||
if not detail:
|
if not detail:
|
||||||
return jsonify({'success': False, 'message': 'Detail not found'})
|
return jsonify({'success': False, 'message': 'Detail not found'})
|
||||||
@@ -677,7 +883,7 @@ def delete_detail(detail_id):
|
|||||||
if detail['scanned_by'] != session['user_id'] and session['role'] not in ['owner', 'admin']:
|
if detail['scanned_by'] != session['user_id'] and session['role'] not in ['owner', 'admin']:
|
||||||
return jsonify({'success': False, 'message': 'Permission denied'})
|
return jsonify({'success': False, 'message': 'Permission denied'})
|
||||||
|
|
||||||
execute_db('UPDATE cons_session_details SET is_deleted = 1 WHERE id = ?', [detail_id])
|
execute_db(f'UPDATE {table_name} SET is_deleted = 1 WHERE id = ?', [detail_id])
|
||||||
|
|
||||||
return jsonify({'success': True})
|
return jsonify({'success': True})
|
||||||
|
|
||||||
@@ -697,4 +903,218 @@ def archive_session(session_id):
|
|||||||
|
|
||||||
execute_db('UPDATE cons_sessions SET status = "archived" WHERE id = ?', [session_id])
|
execute_db('UPDATE cons_sessions SET status = "archived" WHERE id = ?', [session_id])
|
||||||
|
|
||||||
return jsonify({'success': True})
|
return jsonify({'success': True})
|
||||||
|
|
||||||
|
|
||||||
|
@cons_sheets_bp.route('/cons-sheets/session/<int:session_id>/export')
|
||||||
|
@login_required
|
||||||
|
def export_session(session_id):
|
||||||
|
"""Export session to Excel using the process template"""
|
||||||
|
from flask import Response
|
||||||
|
from io import BytesIO
|
||||||
|
import openpyxl
|
||||||
|
from openpyxl.utils import get_column_letter, column_index_from_string
|
||||||
|
from copy import copy
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# Get session with process info
|
||||||
|
sess = query_db('''
|
||||||
|
SELECT cs.*, cp.process_name, cp.process_key, cp.id as process_id,
|
||||||
|
cp.template_file, cp.template_filename, cp.rows_per_page, cp.detail_start_row
|
||||||
|
FROM cons_sessions cs
|
||||||
|
JOIN cons_processes cp ON cs.process_id = cp.id
|
||||||
|
WHERE cs.id = ?
|
||||||
|
''', [session_id], one=True)
|
||||||
|
|
||||||
|
if not sess:
|
||||||
|
flash('Session not found', 'danger')
|
||||||
|
return redirect(url_for('cons_sheets.index'))
|
||||||
|
|
||||||
|
if not sess['template_file']:
|
||||||
|
flash('No template configured for this process', 'danger')
|
||||||
|
return redirect(url_for('cons_sheets.scan_session', session_id=session_id))
|
||||||
|
|
||||||
|
# Get header fields and values
|
||||||
|
header_fields = query_db('''
|
||||||
|
SELECT cpf.field_name, cpf.excel_cell, cshv.field_value
|
||||||
|
FROM cons_process_fields cpf
|
||||||
|
LEFT JOIN cons_session_header_values cshv ON cpf.id = cshv.field_id AND cshv.session_id = ?
|
||||||
|
WHERE cpf.process_id = ? AND cpf.table_type = 'header' AND cpf.is_active = 1 AND cpf.excel_cell IS NOT NULL
|
||||||
|
''', [session_id, sess['process_id']])
|
||||||
|
|
||||||
|
# Get detail fields with their column mappings
|
||||||
|
detail_fields = query_db('''
|
||||||
|
SELECT field_name, excel_cell, field_type
|
||||||
|
FROM cons_process_fields
|
||||||
|
WHERE process_id = ? AND table_type = 'detail' AND is_active = 1 AND excel_cell IS NOT NULL
|
||||||
|
ORDER BY sort_order, id
|
||||||
|
''', [sess['process_id']])
|
||||||
|
|
||||||
|
# Get all scanned details
|
||||||
|
table_name = get_detail_table_name(sess['process_key'])
|
||||||
|
scans = query_db(f'''
|
||||||
|
SELECT * FROM {table_name}
|
||||||
|
WHERE session_id = ? AND is_deleted = 0
|
||||||
|
ORDER BY scanned_at ASC
|
||||||
|
''', [session_id])
|
||||||
|
|
||||||
|
# Load the template
|
||||||
|
template_bytes = BytesIO(sess['template_file'])
|
||||||
|
wb = openpyxl.load_workbook(template_bytes)
|
||||||
|
ws = wb.active
|
||||||
|
|
||||||
|
rows_per_page = sess['rows_per_page'] or 30
|
||||||
|
detail_start_row = sess['detail_start_row'] or 11
|
||||||
|
|
||||||
|
# Calculate how many pages we need
|
||||||
|
total_scans = len(scans) if scans else 0
|
||||||
|
num_pages = max(1, (total_scans + rows_per_page - 1) // rows_per_page) if total_scans > 0 else 1
|
||||||
|
|
||||||
|
# Helper function to fill header values on a sheet
|
||||||
|
def fill_header(worksheet, header_fields):
|
||||||
|
for field in header_fields:
|
||||||
|
if field['excel_cell'] and field['field_value']:
|
||||||
|
try:
|
||||||
|
worksheet[field['excel_cell']] = field['field_value']
|
||||||
|
except:
|
||||||
|
pass # Skip invalid cell references
|
||||||
|
|
||||||
|
# Helper function to clear detail rows on a sheet
|
||||||
|
def clear_details(worksheet, detail_fields, start_row, num_rows):
|
||||||
|
for i in range(num_rows):
|
||||||
|
row_num = start_row + i
|
||||||
|
for field in detail_fields:
|
||||||
|
if field['excel_cell']:
|
||||||
|
try:
|
||||||
|
col_letter = field['excel_cell'].upper().strip()
|
||||||
|
cell_ref = f"{col_letter}{row_num}"
|
||||||
|
worksheet[cell_ref] = None
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Helper function to fill detail rows on a sheet
|
||||||
|
def fill_details(worksheet, scans_subset, detail_fields, start_row):
|
||||||
|
for i, scan in enumerate(scans_subset):
|
||||||
|
row_num = start_row + i
|
||||||
|
for field in detail_fields:
|
||||||
|
if field['excel_cell']:
|
||||||
|
try:
|
||||||
|
col_letter = field['excel_cell'].upper().strip()
|
||||||
|
cell_ref = f"{col_letter}{row_num}"
|
||||||
|
value = scan[field['field_name']]
|
||||||
|
# Convert to appropriate type
|
||||||
|
if field['field_type'] == 'REAL' and value:
|
||||||
|
value = float(value)
|
||||||
|
elif field['field_type'] == 'INTEGER' and value:
|
||||||
|
value = int(value)
|
||||||
|
worksheet[cell_ref] = value
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error filling cell: {e}")
|
||||||
|
|
||||||
|
# Fill the first page
|
||||||
|
fill_header(ws, header_fields)
|
||||||
|
first_page_scans = scans[:rows_per_page] if scans else []
|
||||||
|
fill_details(ws, first_page_scans, detail_fields, detail_start_row)
|
||||||
|
|
||||||
|
# Create additional pages if needed
|
||||||
|
for page_num in range(2, num_pages + 1):
|
||||||
|
# Copy the worksheet within the same workbook
|
||||||
|
new_ws = wb.copy_worksheet(ws)
|
||||||
|
new_ws.title = f"Page {page_num}"
|
||||||
|
|
||||||
|
# Clear detail rows (they have Page 1 data)
|
||||||
|
clear_details(new_ws, detail_fields, detail_start_row, rows_per_page)
|
||||||
|
|
||||||
|
# Fill details for this page
|
||||||
|
start_idx = (page_num - 1) * rows_per_page
|
||||||
|
end_idx = start_idx + rows_per_page
|
||||||
|
page_scans = scans[start_idx:end_idx]
|
||||||
|
fill_details(new_ws, page_scans, detail_fields, detail_start_row)
|
||||||
|
|
||||||
|
# Rename first sheet if we have multiple pages
|
||||||
|
if num_pages > 1:
|
||||||
|
ws.title = "Page 1"
|
||||||
|
|
||||||
|
# Save to BytesIO
|
||||||
|
output = BytesIO()
|
||||||
|
wb.save(output)
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
# Generate filename
|
||||||
|
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||||
|
base_filename = f"{sess['process_key']}_{session_id}_{timestamp}"
|
||||||
|
|
||||||
|
# Check if PDF export is requested
|
||||||
|
export_format = request.args.get('format', 'xlsx')
|
||||||
|
print(f"DEBUG: Export format requested: {export_format}")
|
||||||
|
|
||||||
|
if export_format == 'pdf':
|
||||||
|
# Use win32com to convert to PDF (requires Excel installed)
|
||||||
|
try:
|
||||||
|
import tempfile
|
||||||
|
import pythoncom
|
||||||
|
import win32com.client as win32
|
||||||
|
print("DEBUG: pywin32 imported successfully")
|
||||||
|
|
||||||
|
# Save Excel to temp file
|
||||||
|
temp_xlsx = tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False)
|
||||||
|
temp_xlsx.write(output.getvalue())
|
||||||
|
temp_xlsx.close()
|
||||||
|
print(f"DEBUG: Temp Excel saved to: {temp_xlsx.name}")
|
||||||
|
|
||||||
|
temp_pdf = temp_xlsx.name.replace('.xlsx', '.pdf')
|
||||||
|
|
||||||
|
# Initialize COM for this thread
|
||||||
|
pythoncom.CoInitialize()
|
||||||
|
print("DEBUG: COM initialized")
|
||||||
|
|
||||||
|
try:
|
||||||
|
excel = win32.Dispatch('Excel.Application')
|
||||||
|
excel.Visible = False
|
||||||
|
excel.DisplayAlerts = False
|
||||||
|
print("DEBUG: Excel application started")
|
||||||
|
|
||||||
|
workbook = excel.Workbooks.Open(temp_xlsx.name)
|
||||||
|
print("DEBUG: Workbook opened")
|
||||||
|
|
||||||
|
workbook.ExportAsFixedFormat(0, temp_pdf) # 0 = PDF format
|
||||||
|
print(f"DEBUG: Exported to PDF: {temp_pdf}")
|
||||||
|
|
||||||
|
workbook.Close(False)
|
||||||
|
excel.Quit()
|
||||||
|
print("DEBUG: Excel closed")
|
||||||
|
finally:
|
||||||
|
pythoncom.CoUninitialize()
|
||||||
|
|
||||||
|
# Read the PDF
|
||||||
|
with open(temp_pdf, 'rb') as f:
|
||||||
|
pdf_data = f.read()
|
||||||
|
print(f"DEBUG: PDF read, size: {len(pdf_data)} bytes")
|
||||||
|
|
||||||
|
# Clean up temp files
|
||||||
|
import os
|
||||||
|
os.unlink(temp_xlsx.name)
|
||||||
|
os.unlink(temp_pdf)
|
||||||
|
print("DEBUG: Temp files cleaned up")
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
pdf_data,
|
||||||
|
mimetype='application/pdf',
|
||||||
|
headers={'Content-Disposition': f'attachment; filename={base_filename}.pdf'}
|
||||||
|
)
|
||||||
|
except ImportError as e:
|
||||||
|
print(f"ERROR: Import failed - {e}")
|
||||||
|
# Fall back to Excel export
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERROR: PDF export failed - {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
# Fall back to Excel export
|
||||||
|
|
||||||
|
# Default: return Excel file
|
||||||
|
print("DEBUG: Returning Excel file")
|
||||||
|
return Response(
|
||||||
|
output.getvalue(),
|
||||||
|
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
|
headers={'Content-Disposition': f'attachment; filename={base_filename}.xlsx'}
|
||||||
|
)
|
||||||
@@ -198,6 +198,7 @@ def init_database():
|
|||||||
field_type TEXT NOT NULL CHECK(field_type IN ('TEXT', 'INTEGER', 'REAL', 'DATE', 'DATETIME')),
|
field_type TEXT NOT NULL CHECK(field_type IN ('TEXT', 'INTEGER', 'REAL', 'DATE', 'DATETIME')),
|
||||||
max_length INTEGER,
|
max_length INTEGER,
|
||||||
is_required INTEGER DEFAULT 0,
|
is_required INTEGER DEFAULT 0,
|
||||||
|
is_duplicate_key INTEGER DEFAULT 0,
|
||||||
is_active INTEGER DEFAULT 1,
|
is_active INTEGER DEFAULT 1,
|
||||||
sort_order INTEGER DEFAULT 0,
|
sort_order INTEGER DEFAULT 0,
|
||||||
excel_cell TEXT,
|
excel_cell TEXT,
|
||||||
@@ -219,6 +220,7 @@ def init_database():
|
|||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
|
|
||||||
|
# Note: Header values still use flexible key-value storage
|
||||||
# cons_session_header_values - Flexible storage for header field values
|
# cons_session_header_values - Flexible storage for header field values
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS cons_session_header_values (
|
CREATE TABLE IF NOT EXISTS cons_session_header_values (
|
||||||
@@ -231,24 +233,9 @@ def init_database():
|
|||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
|
|
||||||
# cons_session_details - Scanned lot details
|
# Note: Detail tables are created dynamically per process as cons_proc_{process_key}_details
|
||||||
cursor.execute('''
|
# They include system columns (id, session_id, scanned_by, scanned_at, duplicate_status,
|
||||||
CREATE TABLE IF NOT EXISTS cons_session_details (
|
# duplicate_info, comment, is_deleted) plus custom fields defined in cons_process_fields
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
session_id INTEGER NOT NULL,
|
|
||||||
item_number TEXT,
|
|
||||||
lot_number TEXT NOT NULL,
|
|
||||||
weight REAL,
|
|
||||||
scanned_by INTEGER NOT NULL,
|
|
||||||
scanned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
duplicate_status TEXT DEFAULT 'normal' CHECK(duplicate_status IN ('normal', 'dup_same_session', 'dup_other_session')),
|
|
||||||
duplicate_info TEXT,
|
|
||||||
comment TEXT,
|
|
||||||
is_deleted INTEGER DEFAULT 0,
|
|
||||||
FOREIGN KEY (session_id) REFERENCES cons_sessions(id),
|
|
||||||
FOREIGN KEY (scanned_by) REFERENCES Users(user_id)
|
|
||||||
)
|
|
||||||
''')
|
|
||||||
|
|
||||||
# Create Indexes
|
# Create Indexes
|
||||||
# MASTER baseline indexes
|
# MASTER baseline indexes
|
||||||
@@ -271,8 +258,7 @@ def init_database():
|
|||||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_process_fields_active ON cons_process_fields(process_id, is_active)')
|
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_process_fields_active ON cons_process_fields(process_id, is_active)')
|
||||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_sessions_process ON cons_sessions(process_id, status)')
|
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_sessions_process ON cons_sessions(process_id, status)')
|
||||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_sessions_user ON cons_sessions(created_by, status)')
|
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_sessions_user ON cons_sessions(created_by, status)')
|
||||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_session_details_session ON cons_session_details(session_id, is_deleted)')
|
# Note: Detail table indexes are created dynamically when process tables are created
|
||||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_cons_session_details_lot ON cons_session_details(lot_number)')
|
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
@@ -47,6 +47,16 @@
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if table_type == 'detail' %}
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="checkbox-label">
|
||||||
|
<input type="checkbox" name="is_duplicate_key" value="1">
|
||||||
|
<span>Use for duplicate detection</span>
|
||||||
|
</label>
|
||||||
|
<p class="form-hint">Check this for the field that should be checked for duplicates (e.g., Lot Number)</p>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="excel_cell" class="form-label">Excel Cell{% if table_type == 'detail' %} (Column){% endif %}</label>
|
<label for="excel_cell" class="form-label">Excel Cell{% if table_type == 'detail' %} (Column){% endif %}</label>
|
||||||
<input type="text" id="excel_cell" name="excel_cell" class="form-input"
|
<input type="text" id="excel_cell" name="excel_cell" class="form-input"
|
||||||
|
|||||||
@@ -54,6 +54,16 @@
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if field.table_type == 'detail' %}
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="checkbox-label">
|
||||||
|
<input type="checkbox" name="is_duplicate_key" value="1" {% if field.is_duplicate_key %}checked{% endif %}>
|
||||||
|
<span>Use for duplicate detection</span>
|
||||||
|
</label>
|
||||||
|
<p class="form-hint">Check this for the field that should be checked for duplicates (e.g., Lot Number)</p>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="excel_cell" class="form-label">Excel Cell{% if field.table_type == 'detail' %} (Column){% endif %}</label>
|
<label for="excel_cell" class="form-label">Excel Cell{% if field.table_type == 'detail' %} (Column){% endif %}</label>
|
||||||
<input type="text" id="excel_cell" name="excel_cell" class="form-input"
|
<input type="text" id="excel_cell" name="excel_cell" class="form-input"
|
||||||
|
|||||||
@@ -19,36 +19,33 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Scan Input Card -->
|
{% if not dup_key_field %}
|
||||||
|
<div class="alert alert-warning" style="margin: var(--space-lg) 0; padding: var(--space-lg); background: rgba(255,170,0,0.2); border-radius: var(--radius-md);">
|
||||||
|
<strong>⚠️ No duplicate key configured!</strong><br>
|
||||||
|
Please configure a detail field with "Use for duplicate detection" checked in the admin panel.
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="scan-card scan-card-active">
|
<div class="scan-card scan-card-active">
|
||||||
<div class="scan-header">
|
<div class="scan-header">
|
||||||
<h2 class="scan-title">Scan Lot Number</h2>
|
<h2 class="scan-title">Scan {{ dup_key_field.field_label if dup_key_field else 'Item' }}</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form id="lotScanForm" class="scan-form">
|
<form id="lotScanForm" class="scan-form">
|
||||||
<div class="scan-input-group">
|
<div class="scan-input-group">
|
||||||
<input type="text"
|
<input type="text" name="dup_key_input" id="dupKeyInput" inputmode="none"
|
||||||
name="lot_number"
|
class="scan-input" placeholder="Scan {{ dup_key_field.field_label if dup_key_field else 'Item' }}"
|
||||||
id="lotInput"
|
autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus
|
||||||
inputmode="none"
|
{% if not dup_key_field %}disabled{% endif %}>
|
||||||
class="scan-input"
|
|
||||||
placeholder="Scan Lot Number"
|
|
||||||
autocomplete="off"
|
|
||||||
autocorrect="off"
|
|
||||||
autocapitalize="off"
|
|
||||||
spellcheck="false"
|
|
||||||
autofocus>
|
|
||||||
<button type="submit" style="display: none;"></button>
|
<button type="submit" style="display: none;"></button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Duplicate Confirmation Modal (Same Session - Blue) -->
|
|
||||||
<div id="duplicateSameModal" class="modal">
|
<div id="duplicateSameModal" class="modal">
|
||||||
<div class="modal-content modal-duplicate">
|
<div class="modal-content modal-duplicate">
|
||||||
<div class="duplicate-lot-number" id="dupSameLotNumber"></div>
|
<div class="duplicate-lot-number" id="dupSameLotNumber"></div>
|
||||||
<h3 class="duplicate-title" style="color: var(--color-duplicate);">Already Scanned</h3>
|
<h3 class="duplicate-title" style="color: var(--color-duplicate);">Already Scanned</h3>
|
||||||
<p class="duplicate-message">This lot was already scanned in this session.<br>Add it again?</p>
|
<p class="duplicate-message">This was already scanned in this session.<br>Add it again?</p>
|
||||||
<div class="modal-actions">
|
<div class="modal-actions">
|
||||||
<button type="button" class="btn btn-secondary btn-lg" onclick="cancelDuplicate()">No</button>
|
<button type="button" class="btn btn-secondary btn-lg" onclick="cancelDuplicate()">No</button>
|
||||||
<button type="button" class="btn btn-lg" style="background: var(--color-duplicate); color: white;" onclick="confirmDuplicate()">Yes, Add</button>
|
<button type="button" class="btn btn-lg" style="background: var(--color-duplicate); color: white;" onclick="confirmDuplicate()">Yes, Add</button>
|
||||||
@@ -56,7 +53,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Duplicate Confirmation Modal (Other Session - Orange) -->
|
|
||||||
<div id="duplicateOtherModal" class="modal">
|
<div id="duplicateOtherModal" class="modal">
|
||||||
<div class="modal-content modal-duplicate">
|
<div class="modal-content modal-duplicate">
|
||||||
<div class="duplicate-lot-number" id="dupOtherLotNumber"></div>
|
<div class="duplicate-lot-number" id="dupOtherLotNumber"></div>
|
||||||
@@ -70,66 +66,57 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Weight Entry Modal -->
|
<div id="fieldsModal" class="modal">
|
||||||
<div id="weightModal" class="modal">
|
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<h3 class="modal-title">Enter Weight</h3>
|
<h3 class="modal-title">Enter Details</h3>
|
||||||
<div class="modal-lot-info">
|
<div class="modal-lot-info">
|
||||||
<div id="modalLotNumber" class="modal-lot-number"></div>
|
<div id="modalDupKeyValue" class="modal-lot-number"></div>
|
||||||
</div>
|
</div>
|
||||||
<form id="weightForm" class="weight-form">
|
<form id="fieldsForm" class="weight-form">
|
||||||
|
{% for field in detail_fields %}
|
||||||
|
{% if not field.is_duplicate_key %}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label">Item Number (optional)</label>
|
<label class="form-label">{{ field.field_label }}{% if field.is_required %}<span style="color: var(--color-danger);">*</span>{% endif %}</label>
|
||||||
<input type="text" id="itemInput" class="form-input" placeholder="Item/SKU">
|
{% if field.field_type == 'REAL' %}
|
||||||
|
<input type="number" id="field_{{ field.field_name }}" name="{{ field.field_name }}" class="form-input" step="0.01" inputmode="decimal" {% if field.is_required %}required{% endif %}>
|
||||||
|
{% elif field.field_type == 'INTEGER' %}
|
||||||
|
<input type="number" id="field_{{ field.field_name }}" name="{{ field.field_name }}" class="form-input" step="1" {% if field.is_required %}required{% endif %}>
|
||||||
|
{% elif field.field_type == 'DATE' %}
|
||||||
|
<input type="date" id="field_{{ field.field_name }}" name="{{ field.field_name }}" class="form-input" {% if field.is_required %}required{% endif %}>
|
||||||
|
{% else %}
|
||||||
|
<input type="text" id="field_{{ field.field_name }}" name="{{ field.field_name }}" class="form-input" {% if field.max_length %}maxlength="{{ field.max_length }}"{% endif %} {% if field.is_required %}required{% endif %}>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<input
|
{% endif %}
|
||||||
type="number"
|
{% endfor %}
|
||||||
id="weightInput"
|
|
||||||
class="weight-input"
|
|
||||||
placeholder="Weight (lbs)"
|
|
||||||
step="0.01"
|
|
||||||
min="0"
|
|
||||||
inputmode="decimal"
|
|
||||||
autocomplete="off"
|
|
||||||
required
|
|
||||||
>
|
|
||||||
<div class="modal-actions">
|
<div class="modal-actions">
|
||||||
<button type="button" class="btn btn-secondary" onclick="cancelWeight()">Cancel</button>
|
<button type="button" class="btn btn-secondary" onclick="cancelFields()">Cancel</button>
|
||||||
<button type="submit" class="btn btn-primary">Save</button>
|
<button type="submit" class="btn btn-primary">Save</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Scanned Lots Grid -->
|
|
||||||
<div class="scans-section">
|
<div class="scans-section">
|
||||||
<div class="scans-header">
|
<div class="scans-header">
|
||||||
<h3 class="scans-title">Scanned Lots (<span id="scanListCount">{{ scans|length }}</span>)</h3>
|
<h3 class="scans-title">Scanned Items (<span id="scanListCount">{{ scans|length }}</span>)</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="scansList" class="scans-grid">
|
<div id="scansList" class="scans-grid">
|
||||||
{% for scan in scans %}
|
{% for scan in scans %}
|
||||||
<div class="scan-row scan-row-{{ scan.duplicate_status }}"
|
<div class="scan-row scan-row-{{ scan.duplicate_status }}" data-detail-id="{{ scan.id }}" onclick="openScanDetail({{ scan.id }})">
|
||||||
data-detail-id="{{ scan.id }}"
|
{% for field in detail_fields %}
|
||||||
onclick="openScanDetail({{ scan.id }})">
|
<div class="scan-row-cell">{% if field.field_type == 'REAL' %}{{ '%.1f'|format(scan[field.field_name]|float) if scan[field.field_name] else '-' }}{% else %}{{ scan[field.field_name] or '-' }}{% endif %}</div>
|
||||||
<div class="scan-row-lot">{{ scan.lot_number }}</div>
|
{% endfor %}
|
||||||
<div class="scan-row-item">{{ scan.item_number or 'N/A' }}</div>
|
|
||||||
<div class="scan-row-weight">{{ '%.1f'|format(scan.weight) if scan.weight else '-' }} lbs</div>
|
|
||||||
<div class="scan-row-status">
|
<div class="scan-row-status">
|
||||||
{% if scan.duplicate_status == 'dup_same_session' %}
|
{% if scan.duplicate_status == 'dup_same_session' %}<span class="status-dot status-dot-blue"></span> Dup
|
||||||
<span class="status-dot status-dot-blue"></span> Duplicate
|
{% elif scan.duplicate_status == 'dup_other_session' %}<span class="status-dot status-dot-orange"></span> Warn
|
||||||
{% elif scan.duplicate_status == 'dup_other_session' %}
|
{% else %}<span class="status-dot status-dot-green"></span> OK{% endif %}
|
||||||
<span class="status-dot status-dot-orange"></span> Warning
|
|
||||||
{% else %}
|
|
||||||
<span class="status-dot status-dot-green"></span> OK
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Scan Detail/Edit Modal -->
|
|
||||||
<div id="scanDetailModal" class="modal">
|
<div id="scanDetailModal" class="modal">
|
||||||
<div class="modal-content modal-large">
|
<div class="modal-content modal-large">
|
||||||
<div class="modal-header-bar">
|
<div class="modal-header-bar">
|
||||||
@@ -140,257 +127,179 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Action Buttons -->
|
|
||||||
<div class="finish-section">
|
<div class="finish-section">
|
||||||
<div class="action-buttons-row">
|
<div class="action-buttons-row">
|
||||||
<a href="{{ url_for('cons_sheets.index') }}" class="btn btn-secondary btn-block btn-lg">
|
<a href="{{ url_for('cons_sheets.index') }}" class="btn btn-secondary btn-block btn-lg">← Back to Sessions</a>
|
||||||
← Back to Sessions
|
<button class="btn btn-success btn-block btn-lg" onclick="exportToExcel()">📊 Export to Excel</button>
|
||||||
</a>
|
|
||||||
<button class="btn btn-success btn-block btn-lg" onclick="exportToExcel()">
|
|
||||||
📊 Export to Excel
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.header-values {
|
.header-values { display: flex; flex-wrap: wrap; gap: var(--space-sm); margin: var(--space-sm) 0; }
|
||||||
display: flex;
|
.header-pill { background: var(--color-surface-elevated); padding: var(--space-xs) var(--space-sm); border-radius: var(--radius-sm); font-size: 0.8rem; color: var(--color-text-muted); }
|
||||||
flex-wrap: wrap;
|
.header-pill strong { color: var(--color-text); }
|
||||||
gap: var(--space-sm);
|
.scan-row { display: grid; grid-template-columns: repeat({{ detail_fields|length }}, 1fr) auto; gap: var(--space-sm); padding: var(--space-md); background: var(--color-surface); border: 2px solid var(--color-border); border-radius: var(--radius-md); margin-bottom: var(--space-sm); cursor: pointer; transition: var(--transition); }
|
||||||
margin: var(--space-sm) 0;
|
.scan-row:hover { border-color: var(--color-primary); }
|
||||||
}
|
.scan-row-cell { font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
.scan-row-dup_same_session { border-left: 4px solid var(--color-duplicate) !important; background: rgba(0, 163, 255, 0.1) !important; }
|
||||||
.header-pill {
|
.scan-row-dup_other_session { border-left: 4px solid var(--color-warning) !important; background: rgba(255, 170, 0, 0.1) !important; }
|
||||||
background: var(--color-surface-elevated);
|
.scan-row-normal { border-left: 4px solid var(--color-success); }
|
||||||
padding: var(--space-xs) var(--space-sm);
|
.modal-duplicate { text-align: center; padding: var(--space-xl); }
|
||||||
border-radius: var(--radius-sm);
|
.duplicate-lot-number { font-family: var(--font-mono); font-size: 1.5rem; font-weight: 700; color: var(--color-primary); margin-bottom: var(--space-md); }
|
||||||
font-size: 0.8rem;
|
.duplicate-title { font-size: 1.25rem; margin-bottom: var(--space-sm); }
|
||||||
color: var(--color-text-muted);
|
.duplicate-message { color: var(--color-text-muted); margin-bottom: var(--space-lg); }
|
||||||
}
|
|
||||||
|
|
||||||
.header-pill strong {
|
|
||||||
color: var(--color-text);
|
|
||||||
}
|
|
||||||
|
|
||||||
.scan-row-dup_same_session {
|
|
||||||
border-left: 4px solid var(--color-duplicate) !important;
|
|
||||||
background: rgba(0, 163, 255, 0.1) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.scan-row-dup_other_session {
|
|
||||||
border-left: 4px solid var(--color-warning) !important;
|
|
||||||
background: rgba(255, 170, 0, 0.1) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.scan-row-normal {
|
|
||||||
border-left: 4px solid var(--color-success);
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal-duplicate {
|
|
||||||
text-align: center;
|
|
||||||
padding: var(--space-xl);
|
|
||||||
}
|
|
||||||
|
|
||||||
.duplicate-lot-number {
|
|
||||||
font-family: var(--font-mono);
|
|
||||||
font-size: 1.5rem;
|
|
||||||
font-weight: 700;
|
|
||||||
color: var(--color-primary);
|
|
||||||
margin-bottom: var(--space-md);
|
|
||||||
}
|
|
||||||
|
|
||||||
.duplicate-title {
|
|
||||||
font-size: 1.25rem;
|
|
||||||
margin-bottom: var(--space-sm);
|
|
||||||
}
|
|
||||||
|
|
||||||
.duplicate-message {
|
|
||||||
color: var(--color-text-muted);
|
|
||||||
margin-bottom: var(--space-lg);
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let currentLotNumber = '';
|
const detailFields = {{ detail_fields|tojson|safe }};
|
||||||
|
const dupKeyFieldName = {{ (dup_key_field.field_name if dup_key_field else '')|tojson|safe }};
|
||||||
|
const sessionId = {{ session.id }};
|
||||||
|
|
||||||
|
let currentDupKeyValue = '';
|
||||||
let currentDuplicateStatus = '';
|
let currentDuplicateStatus = '';
|
||||||
let currentDuplicateInfo = '';
|
|
||||||
let isDuplicateConfirmed = false;
|
let isDuplicateConfirmed = false;
|
||||||
let isProcessing = false;
|
let isProcessing = false;
|
||||||
|
|
||||||
// Lot scan handler
|
|
||||||
document.getElementById('lotScanForm').addEventListener('submit', function(e) {
|
document.getElementById('lotScanForm').addEventListener('submit', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (isProcessing) return;
|
if (isProcessing) return;
|
||||||
|
const scannedValue = document.getElementById('dupKeyInput').value.trim();
|
||||||
const scannedValue = document.getElementById('lotInput').value.trim();
|
|
||||||
if (!scannedValue) return;
|
if (!scannedValue) return;
|
||||||
|
|
||||||
isProcessing = true;
|
isProcessing = true;
|
||||||
currentLotNumber = scannedValue;
|
currentDupKeyValue = scannedValue;
|
||||||
document.getElementById('lotInput').value = '';
|
document.getElementById('dupKeyInput').value = '';
|
||||||
|
|
||||||
checkDuplicate();
|
checkDuplicate();
|
||||||
});
|
});
|
||||||
|
|
||||||
function checkDuplicate() {
|
function checkDuplicate() {
|
||||||
fetch('{{ url_for("cons_sheets.scan_lot", session_id=session.id) }}', {
|
const fieldValues = {};
|
||||||
|
fieldValues[dupKeyFieldName] = currentDupKeyValue;
|
||||||
|
fetch(`/cons-sheets/session/${sessionId}/scan`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ field_values: fieldValues, check_only: true })
|
||||||
lot_number: currentLotNumber,
|
|
||||||
check_only: true
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.needs_confirmation) {
|
if (data.needs_confirmation) {
|
||||||
currentDuplicateStatus = data.duplicate_status;
|
currentDuplicateStatus = data.duplicate_status;
|
||||||
currentDuplicateInfo = data.duplicate_info;
|
|
||||||
|
|
||||||
if (data.duplicate_status === 'dup_same_session') {
|
if (data.duplicate_status === 'dup_same_session') {
|
||||||
document.getElementById('dupSameLotNumber').textContent = currentLotNumber;
|
document.getElementById('dupSameLotNumber').textContent = currentDupKeyValue;
|
||||||
document.getElementById('duplicateSameModal').style.display = 'flex';
|
document.getElementById('duplicateSameModal').style.display = 'flex';
|
||||||
} else if (data.duplicate_status === 'dup_other_session') {
|
} else if (data.duplicate_status === 'dup_other_session') {
|
||||||
document.getElementById('dupOtherLotNumber').textContent = currentLotNumber;
|
document.getElementById('dupOtherLotNumber').textContent = currentDupKeyValue;
|
||||||
document.getElementById('dupOtherInfo').textContent = data.duplicate_info;
|
document.getElementById('dupOtherInfo').textContent = data.duplicate_info;
|
||||||
document.getElementById('duplicateOtherModal').style.display = 'flex';
|
document.getElementById('duplicateOtherModal').style.display = 'flex';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
showWeightModal();
|
showFieldsModal();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => { console.error('Error:', error); showFieldsModal(); });
|
||||||
console.error('Error:', error);
|
|
||||||
showWeightModal();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function confirmDuplicate() {
|
function confirmDuplicate() {
|
||||||
isDuplicateConfirmed = true;
|
isDuplicateConfirmed = true;
|
||||||
document.getElementById('duplicateSameModal').style.display = 'none';
|
document.getElementById('duplicateSameModal').style.display = 'none';
|
||||||
document.getElementById('duplicateOtherModal').style.display = 'none';
|
document.getElementById('duplicateOtherModal').style.display = 'none';
|
||||||
showWeightModal();
|
showFieldsModal();
|
||||||
}
|
}
|
||||||
|
|
||||||
function cancelDuplicate() {
|
function cancelDuplicate() {
|
||||||
document.getElementById('duplicateSameModal').style.display = 'none';
|
document.getElementById('duplicateSameModal').style.display = 'none';
|
||||||
document.getElementById('duplicateOtherModal').style.display = 'none';
|
document.getElementById('duplicateOtherModal').style.display = 'none';
|
||||||
document.getElementById('lotInput').focus();
|
document.getElementById('dupKeyInput').focus();
|
||||||
isDuplicateConfirmed = false;
|
isDuplicateConfirmed = false;
|
||||||
isProcessing = false;
|
isProcessing = false;
|
||||||
currentDuplicateStatus = '';
|
currentDuplicateStatus = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
function showWeightModal() {
|
function showFieldsModal() {
|
||||||
document.getElementById('modalLotNumber').textContent = currentLotNumber;
|
document.getElementById('modalDupKeyValue').textContent = currentDupKeyValue;
|
||||||
document.getElementById('weightModal').style.display = 'flex';
|
document.getElementById('fieldsModal').style.display = 'flex';
|
||||||
document.getElementById('itemInput').value = '';
|
const form = document.getElementById('fieldsForm');
|
||||||
document.getElementById('weightInput').value = '';
|
form.reset();
|
||||||
document.getElementById('weightInput').focus();
|
// Focus on first required field, or first input if none required
|
||||||
|
const firstRequired = form.querySelector('input[required]');
|
||||||
|
const firstInput = form.querySelector('input:not([disabled])');
|
||||||
|
if (firstRequired) firstRequired.focus();
|
||||||
|
else if (firstInput) firstInput.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
function cancelWeight() {
|
function cancelFields() {
|
||||||
document.getElementById('weightModal').style.display = 'none';
|
document.getElementById('fieldsModal').style.display = 'none';
|
||||||
document.getElementById('lotInput').focus();
|
document.getElementById('dupKeyInput').focus();
|
||||||
isDuplicateConfirmed = false;
|
isDuplicateConfirmed = false;
|
||||||
isProcessing = false;
|
isProcessing = false;
|
||||||
currentDuplicateStatus = '';
|
currentDuplicateStatus = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weight form handler
|
document.getElementById('fieldsForm').addEventListener('submit', function(e) {
|
||||||
document.getElementById('weightForm').addEventListener('submit', function(e) {
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const weight = document.getElementById('weightInput').value;
|
submitScan();
|
||||||
const itemNumber = document.getElementById('itemInput').value.trim();
|
|
||||||
|
|
||||||
if (!weight || weight <= 0) {
|
|
||||||
alert('Please enter a valid weight');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
submitScan(itemNumber, weight);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function submitScan(itemNumber, weight) {
|
function submitScan() {
|
||||||
fetch('{{ url_for("cons_sheets.scan_lot", session_id=session.id) }}', {
|
const fieldValues = {};
|
||||||
|
fieldValues[dupKeyFieldName] = currentDupKeyValue;
|
||||||
|
detailFields.forEach(field => {
|
||||||
|
if (!field.is_duplicate_key) {
|
||||||
|
const input = document.getElementById('field_' + field.field_name);
|
||||||
|
if (input) fieldValues[field.field_name] = input.value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
fetch(`/cons-sheets/session/${sessionId}/scan`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ field_values: fieldValues, confirm_duplicate: isDuplicateConfirmed })
|
||||||
lot_number: currentLotNumber,
|
|
||||||
item_number: itemNumber,
|
|
||||||
weight: parseFloat(weight),
|
|
||||||
confirm_duplicate: isDuplicateConfirmed
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
document.getElementById('weightModal').style.display = 'none';
|
document.getElementById('fieldsModal').style.display = 'none';
|
||||||
|
|
||||||
// Update existing rows if needed
|
|
||||||
if (data.updated_entry_ids && data.updated_entry_ids.length > 0) {
|
if (data.updated_entry_ids && data.updated_entry_ids.length > 0) {
|
||||||
data.updated_entry_ids.forEach(id => {
|
data.updated_entry_ids.forEach(id => {
|
||||||
const row = document.querySelector(`[data-detail-id="${id}"]`);
|
const row = document.querySelector(`[data-detail-id="${id}"]`);
|
||||||
if (row) {
|
if (row) {
|
||||||
row.className = 'scan-row scan-row-dup_same_session';
|
row.className = 'scan-row scan-row-dup_same_session';
|
||||||
row.querySelector('.scan-row-status').innerHTML = '<span class="status-dot status-dot-blue"></span> Duplicate';
|
row.querySelector('.scan-row-status').innerHTML = '<span class="status-dot status-dot-blue"></span> Dup';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
addScanToList(data.detail_id, fieldValues, data.duplicate_status);
|
||||||
// Add new row
|
currentDupKeyValue = '';
|
||||||
addScanToList(data.detail_id, currentLotNumber, itemNumber, weight, data.duplicate_status);
|
|
||||||
|
|
||||||
// Reset state
|
|
||||||
currentLotNumber = '';
|
|
||||||
isDuplicateConfirmed = false;
|
isDuplicateConfirmed = false;
|
||||||
isProcessing = false;
|
isProcessing = false;
|
||||||
currentDuplicateStatus = '';
|
currentDuplicateStatus = '';
|
||||||
document.getElementById('lotInput').focus();
|
document.getElementById('dupKeyInput').focus();
|
||||||
} else {
|
} else {
|
||||||
alert(data.message || 'Error saving scan');
|
alert(data.message || 'Error saving scan');
|
||||||
isProcessing = false;
|
isProcessing = false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => { console.error('Error:', error); alert('Error saving scan'); isProcessing = false; });
|
||||||
console.error('Error:', error);
|
|
||||||
alert('Error saving scan');
|
|
||||||
isProcessing = false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function addScanToList(detailId, lotNumber, itemNumber, weight, duplicateStatus) {
|
function addScanToList(detailId, fieldValues, duplicateStatus) {
|
||||||
const scansList = document.getElementById('scansList');
|
const scansList = document.getElementById('scansList');
|
||||||
|
|
||||||
let statusClass = duplicateStatus || 'normal';
|
let statusClass = duplicateStatus || 'normal';
|
||||||
let statusDot = 'green';
|
let statusDot = 'green', statusText = 'OK';
|
||||||
let statusText = 'OK';
|
if (duplicateStatus === 'dup_same_session') { statusDot = 'blue'; statusText = 'Dup'; }
|
||||||
|
else if (duplicateStatus === 'dup_other_session') { statusDot = 'orange'; statusText = 'Warn'; }
|
||||||
if (duplicateStatus === 'dup_same_session') {
|
|
||||||
statusDot = 'blue';
|
|
||||||
statusText = 'Duplicate';
|
|
||||||
} else if (duplicateStatus === 'dup_other_session') {
|
|
||||||
statusDot = 'orange';
|
|
||||||
statusText = 'Warning';
|
|
||||||
}
|
|
||||||
|
|
||||||
const scanRow = document.createElement('div');
|
const scanRow = document.createElement('div');
|
||||||
scanRow.className = 'scan-row scan-row-' + statusClass;
|
scanRow.className = 'scan-row scan-row-' + statusClass;
|
||||||
scanRow.setAttribute('data-detail-id', detailId);
|
scanRow.setAttribute('data-detail-id', detailId);
|
||||||
scanRow.onclick = function() { openScanDetail(detailId); };
|
scanRow.onclick = function() { openScanDetail(detailId); };
|
||||||
scanRow.innerHTML = `
|
let cellsHtml = '';
|
||||||
<div class="scan-row-lot">${lotNumber}</div>
|
detailFields.forEach(field => {
|
||||||
<div class="scan-row-item">${itemNumber || 'N/A'}</div>
|
let value = fieldValues[field.field_name] || '-';
|
||||||
<div class="scan-row-weight">${parseFloat(weight).toFixed(1)} lbs</div>
|
if (field.field_type === 'REAL' && value !== '-') value = parseFloat(value).toFixed(1);
|
||||||
<div class="scan-row-status">
|
cellsHtml += `<div class="scan-row-cell">${value}</div>`;
|
||||||
<span class="status-dot status-dot-${statusDot}"></span> ${statusText}
|
});
|
||||||
</div>
|
cellsHtml += `<div class="scan-row-status"><span class="status-dot status-dot-${statusDot}"></span> ${statusText}</div>`;
|
||||||
`;
|
scanRow.innerHTML = cellsHtml;
|
||||||
|
|
||||||
scansList.insertBefore(scanRow, scansList.firstChild);
|
scansList.insertBefore(scanRow, scansList.firstChild);
|
||||||
|
|
||||||
// Update count
|
|
||||||
const countSpan = document.getElementById('scanListCount');
|
const countSpan = document.getElementById('scanListCount');
|
||||||
const scanCountSpan = document.getElementById('scanCount');
|
const scanCountSpan = document.getElementById('scanCount');
|
||||||
const newCount = scansList.children.length;
|
const newCount = scansList.children.length;
|
||||||
@@ -399,137 +308,97 @@ function addScanToList(detailId, lotNumber, itemNumber, weight, duplicateStatus)
|
|||||||
}
|
}
|
||||||
|
|
||||||
function openScanDetail(detailId) {
|
function openScanDetail(detailId) {
|
||||||
fetch('/cons-sheets/detail/' + detailId)
|
fetch(`/cons-sheets/session/${sessionId}/detail/${detailId}`)
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.success) {
|
if (data.success) displayScanDetail(data.detail);
|
||||||
displayScanDetail(data.detail);
|
else alert('Error loading details');
|
||||||
} else {
|
|
||||||
alert('Error loading details');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayScanDetail(detail) {
|
function displayScanDetail(detail) {
|
||||||
const content = document.getElementById('scanDetailContent');
|
const content = document.getElementById('scanDetailContent');
|
||||||
|
|
||||||
let statusBadge = '<span class="badge badge-success">✓ OK</span>';
|
let statusBadge = '<span class="badge badge-success">✓ OK</span>';
|
||||||
if (detail.duplicate_status === 'dup_same_session') {
|
if (detail.duplicate_status === 'dup_same_session') statusBadge = '<span class="badge badge-duplicate">🔵 Duplicate</span>';
|
||||||
statusBadge = '<span class="badge badge-duplicate">🔵 Duplicate (Same Session)</span>';
|
else if (detail.duplicate_status === 'dup_other_session') statusBadge = '<span class="badge badge-warning">🟠 Previously Consumed</span>';
|
||||||
} else if (detail.duplicate_status === 'dup_other_session') {
|
let detailRows = '';
|
||||||
statusBadge = '<span class="badge badge-warning">🟠 Previously Consumed</span>';
|
detailFields.forEach(field => {
|
||||||
}
|
let value = detail[field.field_name] || 'N/A';
|
||||||
|
if (field.field_type === 'REAL' && value !== 'N/A') value = parseFloat(value).toFixed(2);
|
||||||
|
detailRows += `<div class="detail-row"><span class="detail-label">${field.field_label}:</span><span class="detail-value">${value}</span></div>`;
|
||||||
|
});
|
||||||
|
let editFields = '';
|
||||||
|
detailFields.forEach(field => {
|
||||||
|
let inputType = 'text', extraAttrs = '';
|
||||||
|
if (field.field_type === 'REAL') { inputType = 'number'; extraAttrs = 'step="0.01"'; }
|
||||||
|
else if (field.field_type === 'INTEGER') { inputType = 'number'; extraAttrs = 'step="1"'; }
|
||||||
|
else if (field.field_type === 'DATE') { inputType = 'date'; }
|
||||||
|
let value = detail[field.field_name] || '';
|
||||||
|
editFields += `<div class="form-group"><label class="form-label">${field.field_label}</label><input type="${inputType}" id="edit_${field.field_name}" class="form-input" value="${value}" ${extraAttrs}></div>`;
|
||||||
|
});
|
||||||
content.innerHTML = `
|
content.innerHTML = `
|
||||||
<div class="detail-section">
|
<div class="detail-section">${detailRows}
|
||||||
<div class="detail-row">
|
<div class="detail-row"><span class="detail-label">Status:</span><span class="detail-value">${statusBadge}</span></div>
|
||||||
<span class="detail-label">Lot Number:</span>
|
|
||||||
<span class="detail-value detail-lot">${detail.lot_number}</span>
|
|
||||||
</div>
|
|
||||||
<div class="detail-row">
|
|
||||||
<span class="detail-label">Status:</span>
|
|
||||||
<span class="detail-value">${statusBadge}</span>
|
|
||||||
</div>
|
|
||||||
${detail.duplicate_info ? `<div class="detail-row"><span class="detail-label">Info:</span><span class="detail-value">${detail.duplicate_info}</span></div>` : ''}
|
${detail.duplicate_info ? `<div class="detail-row"><span class="detail-label">Info:</span><span class="detail-value">${detail.duplicate_info}</span></div>` : ''}
|
||||||
<div class="detail-row">
|
<div class="detail-row"><span class="detail-label">Scanned:</span><span class="detail-value">${detail.scanned_at} by ${detail.scanned_by_name}</span></div>
|
||||||
<span class="detail-label">Scanned:</span>
|
|
||||||
<span class="detail-value">${detail.scanned_at} by ${detail.scanned_by_name}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="detail-section">
|
|
||||||
<h4 class="detail-section-title">Edit Scan</h4>
|
|
||||||
<div class="detail-form">
|
|
||||||
<div class="form-group">
|
|
||||||
<label class="form-label">Item Number</label>
|
|
||||||
<input type="text" id="editItem" class="form-input" value="${detail.item_number || ''}">
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label class="form-label">Lot Number</label>
|
|
||||||
<input type="text" id="editLot" class="form-input" value="${detail.lot_number || ''}">
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label class="form-label">Weight (lbs)</label>
|
|
||||||
<input type="number" id="editWeight" class="form-input" value="${detail.weight || ''}" step="0.01" min="0">
|
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label class="form-label">Comment</label>
|
|
||||||
<textarea id="editComment" class="form-textarea" rows="3">${detail.comment || ''}</textarea>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div class="detail-section"><h4 class="detail-section-title">Edit Scan</h4><div class="detail-form">${editFields}
|
||||||
|
<div class="form-group"><label class="form-label">Comment</label><textarea id="editComment" class="form-textarea" rows="3">${detail.comment || ''}</textarea></div>
|
||||||
|
</div></div>
|
||||||
<div class="detail-actions">
|
<div class="detail-actions">
|
||||||
<button class="btn btn-secondary" onclick="closeScanDetail()">Cancel</button>
|
<button class="btn btn-secondary" onclick="closeScanDetail()">Cancel</button>
|
||||||
<button class="btn btn-danger" onclick="deleteDetail(${detail.id})">Delete</button>
|
<button class="btn btn-danger" onclick="deleteDetail(${detail.id})">Delete</button>
|
||||||
<button class="btn btn-primary" onclick="saveDetail(${detail.id})">Save Changes</button>
|
<button class="btn btn-primary" onclick="saveDetail(${detail.id})">Save Changes</button>
|
||||||
</div>
|
</div>`;
|
||||||
`;
|
|
||||||
|
|
||||||
document.getElementById('scanDetailModal').style.display = 'flex';
|
document.getElementById('scanDetailModal').style.display = 'flex';
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeScanDetail() {
|
function closeScanDetail() { document.getElementById('scanDetailModal').style.display = 'none'; }
|
||||||
document.getElementById('scanDetailModal').style.display = 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveDetail(detailId) {
|
function saveDetail(detailId) {
|
||||||
const item = document.getElementById('editItem').value.trim();
|
const fieldValues = {};
|
||||||
const lot = document.getElementById('editLot').value.trim();
|
detailFields.forEach(field => {
|
||||||
const weight = document.getElementById('editWeight').value;
|
const input = document.getElementById('edit_' + field.field_name);
|
||||||
|
if (input) fieldValues[field.field_name] = input.value;
|
||||||
|
});
|
||||||
const comment = document.getElementById('editComment').value;
|
const comment = document.getElementById('editComment').value;
|
||||||
|
fetch(`/cons-sheets/session/${sessionId}/detail/${detailId}/update`, {
|
||||||
if (!lot) {
|
|
||||||
alert('Lot number is required');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch('/cons-sheets/detail/' + detailId + '/update', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({ item_number: item, lot_number: lot, weight: parseFloat(weight), comment: comment })
|
body: JSON.stringify({ field_values: fieldValues, comment: comment })
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.success) {
|
if (data.success) { closeScanDetail(); location.reload(); }
|
||||||
closeScanDetail();
|
else alert(data.message || 'Error updating');
|
||||||
location.reload();
|
|
||||||
} else {
|
|
||||||
alert(data.message || 'Error updating');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteDetail(detailId) {
|
function deleteDetail(detailId) {
|
||||||
if (!confirm('Delete this scan?')) return;
|
if (!confirm('Delete this scan?')) return;
|
||||||
|
fetch(`/cons-sheets/session/${sessionId}/detail/${detailId}/delete`, {
|
||||||
fetch('/cons-sheets/detail/' + detailId + '/delete', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'}
|
headers: {'Content-Type': 'application/json'}
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.success) {
|
if (data.success) { closeScanDetail(); location.reload(); }
|
||||||
closeScanDetail();
|
else alert(data.message || 'Error deleting');
|
||||||
location.reload();
|
|
||||||
} else {
|
|
||||||
alert(data.message || 'Error deleting');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportToExcel() {
|
function exportToExcel() {
|
||||||
alert('Excel export coming soon! For now, you can view and print the data from the admin panel.');
|
window.location.href = `/cons-sheets/session/${sessionId}/export?format=xlsx`;
|
||||||
// TODO: Implement Excel export
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keyboard shortcuts
|
|
||||||
document.addEventListener('keydown', function(e) {
|
document.addEventListener('keydown', function(e) {
|
||||||
if (e.key === 'Escape') {
|
if (e.key === 'Escape') {
|
||||||
document.getElementById('weightModal').style.display = 'none';
|
document.getElementById('fieldsModal').style.display = 'none';
|
||||||
document.getElementById('duplicateSameModal').style.display = 'none';
|
document.getElementById('duplicateSameModal').style.display = 'none';
|
||||||
document.getElementById('duplicateOtherModal').style.display = 'none';
|
document.getElementById('duplicateOtherModal').style.display = 'none';
|
||||||
document.getElementById('scanDetailModal').style.display = 'none';
|
document.getElementById('scanDetailModal').style.display = 'none';
|
||||||
document.getElementById('lotInput').focus();
|
document.getElementById('dupKeyInput').focus();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user