updated to set boxes , and updated to fill boxes
This commit is contained in:
@@ -68,6 +68,64 @@ def ensure_boxes_crates_table():
|
||||
except Exception as e:
|
||||
print(f"Error ensuring boxes_crates table: {e}")
|
||||
|
||||
def ensure_box_contents_table():
|
||||
"""Ensure box_contents table exists for tracking CP codes in boxes"""
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SHOW TABLES LIKE 'box_contents'")
|
||||
result = cursor.fetchone()
|
||||
if not result:
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS box_contents (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
box_id BIGINT NOT NULL,
|
||||
cp_code VARCHAR(15) NOT NULL,
|
||||
scan_id BIGINT,
|
||||
scanned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
scanned_by VARCHAR(100),
|
||||
FOREIGN KEY (box_id) REFERENCES boxes_crates(id) ON DELETE CASCADE,
|
||||
INDEX idx_box_id (box_id),
|
||||
INDEX idx_cp_code (cp_code)
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
print("box_contents table created successfully")
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f"Error ensuring box_contents table: {e}")
|
||||
|
||||
def ensure_location_contents_table():
|
||||
"""Ensure location_contents table exists for tracking boxes in locations"""
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SHOW TABLES LIKE 'location_contents'")
|
||||
result = cursor.fetchone()
|
||||
if not result:
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS location_contents (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
location_id BIGINT NOT NULL,
|
||||
box_id BIGINT NOT NULL,
|
||||
placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
placed_by VARCHAR(100),
|
||||
removed_at TIMESTAMP NULL,
|
||||
removed_by VARCHAR(100),
|
||||
status ENUM('active', 'removed') DEFAULT 'active',
|
||||
FOREIGN KEY (location_id) REFERENCES warehouse_locations(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (box_id) REFERENCES boxes_crates(id) ON DELETE CASCADE,
|
||||
INDEX idx_location_id (location_id),
|
||||
INDEX idx_box_id (box_id),
|
||||
INDEX idx_status (status)
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
print("location_contents table created successfully")
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f"Error ensuring location_contents table: {e}")
|
||||
|
||||
# Add warehouse-specific functions below
|
||||
def add_location(location_code, size, description):
|
||||
conn = get_db_connection()
|
||||
@@ -418,6 +476,17 @@ def manage_boxes_handler():
|
||||
elif action == "add_box":
|
||||
created_by = session.get('user', 'Unknown')
|
||||
message = add_box(None, created_by) # Create box without location
|
||||
|
||||
# Check if this is an AJAX request
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
# Extract box number from message (format: "Box 12345678 created successfully")
|
||||
import re
|
||||
match = re.search(r'Box (\d{8})', message)
|
||||
if match:
|
||||
return jsonify({'success': True, 'box_number': match.group(1), 'message': message})
|
||||
else:
|
||||
return jsonify({'success': False, 'error': message})
|
||||
|
||||
session['flash_message'] = message
|
||||
elif action == "update_status":
|
||||
box_id = request.form.get("box_id")
|
||||
@@ -467,3 +536,127 @@ def delete_location_by_id(location_id):
|
||||
except Exception as e:
|
||||
print(f"Error deleting location: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
def assign_cp_to_box_handler():
|
||||
"""Handle assigning CP code to a box"""
|
||||
from flask import request, jsonify, session
|
||||
import json
|
||||
|
||||
try:
|
||||
# Ensure box_contents table exists
|
||||
ensure_box_contents_table()
|
||||
|
||||
data = json.loads(request.data)
|
||||
box_number = data.get('box_number')
|
||||
cp_code = data.get('cp_code')
|
||||
scanned_by = session.get('user', 'Unknown')
|
||||
|
||||
if not box_number or not cp_code:
|
||||
return jsonify({'success': False, 'error': 'Missing box_number or cp_code'}), 400
|
||||
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Find the box by number
|
||||
cursor.execute("SELECT id FROM boxes_crates WHERE box_number = %s", (box_number,))
|
||||
box = cursor.fetchone()
|
||||
|
||||
if not box:
|
||||
conn.close()
|
||||
return jsonify({'success': False, 'error': f'Box {box_number} not found'}), 404
|
||||
|
||||
box_id = box[0]
|
||||
|
||||
# Insert into box_contents
|
||||
cursor.execute("""
|
||||
INSERT INTO box_contents (box_id, cp_code, scanned_by)
|
||||
VALUES (%s, %s, %s)
|
||||
""", (box_id, cp_code, scanned_by))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'CP {cp_code} assigned to box {box_number}'
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"Error in assign_cp_to_box_handler: {e}")
|
||||
print(traceback.format_exc())
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
def view_warehouse_inventory_handler():
|
||||
"""Handle warehouse inventory view - shows CP codes, boxes, and locations"""
|
||||
from flask import render_template, request
|
||||
|
||||
try:
|
||||
# Ensure tables exist
|
||||
ensure_box_contents_table()
|
||||
ensure_location_contents_table()
|
||||
|
||||
# Get search parameters
|
||||
search_cp = request.args.get('search_cp', '').strip()
|
||||
search_box = request.args.get('search_box', '').strip()
|
||||
search_location = request.args.get('search_location', '').strip()
|
||||
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Build query with joins and filters
|
||||
query = """
|
||||
SELECT
|
||||
bc.cp_code,
|
||||
b.box_number,
|
||||
wl.location_code,
|
||||
bc.scanned_at,
|
||||
bc.scanned_by,
|
||||
lc.placed_at,
|
||||
lc.placed_by,
|
||||
b.status as box_status,
|
||||
lc.status as location_status
|
||||
FROM box_contents bc
|
||||
INNER JOIN boxes_crates b ON bc.box_id = b.id
|
||||
LEFT JOIN location_contents lc ON b.id = lc.box_id AND lc.status = 'active'
|
||||
LEFT JOIN warehouse_locations wl ON lc.location_id = wl.id
|
||||
WHERE 1=1
|
||||
"""
|
||||
|
||||
params = []
|
||||
|
||||
if search_cp:
|
||||
query += " AND bc.cp_code LIKE %s"
|
||||
params.append(f"%{search_cp}%")
|
||||
|
||||
if search_box:
|
||||
query += " AND b.box_number LIKE %s"
|
||||
params.append(f"%{search_box}%")
|
||||
|
||||
if search_location:
|
||||
query += " AND wl.location_code LIKE %s"
|
||||
params.append(f"%{search_location}%")
|
||||
|
||||
query += " ORDER BY bc.scanned_at DESC"
|
||||
|
||||
cursor.execute(query, params)
|
||||
inventory_data = cursor.fetchall()
|
||||
|
||||
conn.close()
|
||||
|
||||
return render_template(
|
||||
'warehouse_inventory.html',
|
||||
inventory_data=inventory_data,
|
||||
search_cp=search_cp,
|
||||
search_box=search_box,
|
||||
search_location=search_location
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
error_trace = traceback.format_exc()
|
||||
print(f"Error in view_warehouse_inventory_handler: {e}")
|
||||
print(error_trace)
|
||||
return f"<h1>Error loading warehouse inventory</h1><pre>{error_trace}</pre>", 500
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user