Spaces:
Sleeping

ScanItem / templates /index.html
HsiehMinChieh
fix: resolve jinja2 sale.items built-in method dictionary naming collision gotcha
c530b0f
Raw History Blame Contribute Delete
77.7 kB
<!DOCTYPE html> <html lang="zh-TW"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>⚡ 全能 POS 兼盤點系統 Ultimate</title> <!-- 引入高解析度字型 --> <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=Noto+Sans+TC:wght@300;400;500;700;900&display=swap" rel="stylesheet"> <script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script> <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.0/dist/JsBarcode.all.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script> <style> :root { --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%); --glass-bg: rgba(30, 41, 59, 0.7); --glass-border: rgba(255, 255, 255, 0.08); --text-primary: #f8fafc; --text-secondary: #94a3b8; --primary-indigo: #6366f1; --primary-indigo-hover: #4f46e5; --success-emerald: #10b981; --success-emerald-hover: #059669; --danger-rose: #f43f5e; --danger-rose-hover: #e11d48; --warning-amber: #f59e0b; } * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Plus Jakarta Sans', 'Noto Sans TC', -apple-system, sans-serif; -webkit-font-smoothing: antialiased; } body { background: var(--bg-gradient); color: var(--text-primary); min-height: 100vh; padding-bottom: 60px; } .container { max-width: 1000px; margin: 0 auto; padding: 20px; } /* --- Header & Stats Board --- */ header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; padding: 10px 0; border-bottom: 1px solid var(--glass-border); } header h1 { font-size: 1.8rem; font-weight: 800; background: linear-gradient(120deg, #818cf8, #34d399); -webkit-background-clip: text; -webkit-text-fill-color: transparent; display: flex; align-items: center; gap: 10px; } .stats-board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin-bottom: 25px; } .stat-card { background: var(--glass-bg); border: 1px solid var(--glass-border); border-radius: 16px; padding: 15px; text-align: center; backdrop-filter: blur(12px); box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3); transition: transform 0.3s, box-shadow 0.3s; } .stat-card:hover { transform: translateY(-2px); box-shadow: 0 15px 30px -5px rgba(99, 102, 241, 0.15); } .stat-label { font-size: 0.85rem; color: var(--text-secondary); font-weight: 500; margin-bottom: 6px; } .stat-value { font-size: 1.6rem; font-weight: 700; color: var(--text-primary); } .stat-value.highlight { color: var(--success-emerald); } /* --- Navigation Tabs --- */ .nav-tabs { display: flex; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--glass-border); border-radius: 50px; padding: 6px; margin-bottom: 25px; overflow-x: auto; backdrop-filter: blur(8px); } .tab-link { flex: 1; padding: 12px 18px; border: none; background: transparent; color: var(--text-secondary); font-weight: 600; font-size: 0.95rem; cursor: pointer; border-radius: 50px; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); display: flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap; } .tab-link.active { background: linear-gradient(135deg, var(--primary-indigo) 0%, #4f46e5 100%); color: var(--text-primary); box-shadow: 0 4px 15px rgba(99, 102, 241, 0.35); } .tab-panel { display: none; animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1); } .tab-panel.active { display: block; } /* --- Layout Cards --- */ .glass-card { background: var(--glass-bg); border: 1px solid var(--glass-border); border-radius: 20px; padding: 20px; backdrop-filter: blur(16px); box-shadow: 0 15px 35px rgba(0, 0, 0, 0.25); margin-bottom: 20px; } .card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; border-bottom: 1px solid rgba(255, 255, 255, 0.05); padding-bottom: 10px; } .card-title { font-size: 1.15rem; font-weight: 700; display: flex; align-items: center; gap: 8px; } /* --- General Inputs & Buttons --- */ .input-group { display: flex; gap: 12px; margin-bottom: 20px; } .styled-input { flex: 1; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--glass-border); border-radius: 12px; padding: 14px 18px; font-size: 1.1rem; color: var(--text-primary); outline: none; transition: all 0.3s; } .styled-input:focus { border-color: var(--primary-indigo); box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2); } .btn { padding: 12px 24px; border: none; border-radius: 12px; font-weight: 600; cursor: pointer; transition: all 0.3s; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; color: var(--text-primary); box-shadow: 0 4px 10px rgba(0,0,0,0.15); } .btn-primary { background: linear-gradient(135deg, var(--primary-indigo) 0%, #4f46e5 100%); } .btn-primary:hover { background: var(--primary-indigo-hover); transform: translateY(-1px); } .btn-success { background: linear-gradient(135deg, var(--success-emerald) 0%, #059669 100%); } .btn-success:hover { background: var(--success-emerald-hover); transform: translateY(-1px); } .btn-danger { background: linear-gradient(135deg, var(--danger-rose) 0%, #e11d48 100%); } .btn-danger:hover { background: var(--danger-rose-hover); transform: translateY(-1px); } .btn-secondary { background: rgba(71, 85, 105, 0.6); border: 1px solid var(--glass-border); } .btn-secondary:hover { background: rgba(71, 85, 105, 0.8); } .btn-icon { width: 36px; height: 36px; padding: 0; border-radius: 50%; } /* --- Camera Scanner UI --- */ #scanner-container { display: none; border-radius: 16px; overflow: hidden; border: 2px solid var(--primary-indigo); margin-bottom: 20px; box-shadow: 0 10px 30px rgba(99, 102, 241, 0.2); position: relative; } #reader { width: 100%; background: #000; } /* --- Mode Switch Banner --- */ .mode-banner { background: rgba(245, 158, 11, 0.15); border: 1px solid rgba(245, 158, 11, 0.3); border-radius: 12px; padding: 12px; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; font-size: 0.95rem; color: #fbd38d; } .mode-banner input { width: 18px; height: 18px; cursor: pointer; accent-color: var(--warning-amber); } /* --- Live Notification Alert --- */ .alert-notification { background: rgba(16, 185, 129, 0.2); border: 1px solid rgba(16, 185, 129, 0.4); color: #a7f3d0; border-radius: 12px; padding: 15px; margin-bottom: 20px; display: none; align-items: center; justify-content: center; gap: 10px; font-weight: 600; text-align: center; animation: pulseGlow 1.5s infinite; } .alert-notification.error { background: rgba(244, 63, 94, 0.2); border: 1px solid rgba(244, 63, 94, 0.4); color: #fecdd3; } .alert-notification.warning { background: rgba(245, 158, 11, 0.2); border: 1px solid rgba(245, 158, 11, 0.4); color: #fef3c7; } /* --- Tabs Split Views (Sub-panels) --- */ .split-view { display: grid; grid-template-columns: 1.2fr 0.8fr; gap: 20px; } @media (max-width: 768px) { .split-view { grid-template-columns: 1fr; } .stats-board { grid-template-columns: 1fr; gap: 10px; } } .view-toggle { display: flex; background: rgba(15, 23, 42, 0.4); border-radius: 8px; padding: 3px; margin-bottom: 15px; } .view-btn { flex: 1; padding: 8px 12px; border: none; background: transparent; color: var(--text-secondary); font-weight: 600; font-size: 0.85rem; cursor: pointer; border-radius: 6px; transition: all 0.2s; } .view-btn.active { background: rgba(99, 102, 241, 0.2); color: var(--text-primary); border: 1px solid rgba(99, 102, 241, 0.3); } /* --- Scrollable Lists & Tables --- */ .scroll-list { max-height: 480px; overflow-y: auto; padding-right: 5px; } /* 自訂拉條 */ .scroll-list::-webkit-scrollbar, body::-webkit-scrollbar { width: 6px; } .scroll-list::-webkit-scrollbar-track, body::-webkit-scrollbar-track { background: rgba(0, 0, 0, 0.1); } .scroll-list::-webkit-scrollbar-thumb, body::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.1); border-radius: 3px; } .log-item-row { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: rgba(30, 41, 59, 0.4); border: 1px solid rgba(255, 255, 255, 0.04); border-radius: 12px; margin-bottom: 10px; transition: background 0.2s, transform 0.2s; animation: slideInItem 0.3s cubic-bezier(0.16, 1, 0.3, 1); } .log-item-row:hover { background: rgba(30, 41, 59, 0.6); transform: scale(1.005); } .log-item-info { display: flex; flex-direction: column; gap: 3px; } .log-item-title { font-weight: 700; font-size: 1rem; color: var(--text-primary); } .log-item-subtitle { font-size: 0.8rem; color: var(--text-secondary); font-family: monospace; word-break: break-all; } .log-item-time { font-size: 0.8rem; color: var(--text-secondary); white-space: nowrap; } /* --- Table Styling (Inventory / Sales) --- */ .styled-table { width: 100%; border-collapse: collapse; text-align: left; } .styled-table th { padding: 12px 10px; color: var(--text-secondary); font-weight: 600; font-size: 0.85rem; border-bottom: 1px solid rgba(255, 255, 255, 0.05); } .styled-table td { padding: 14px 10px; border-bottom: 1px solid rgba(255, 255, 255, 0.03); font-size: 0.95rem; } .styled-table tbody tr:hover { background: rgba(255, 255, 255, 0.01); } .qty-control { display: flex; align-items: center; gap: 8px; } .qty-btn { width: 28px; height: 28px; border-radius: 6px; border: 1px solid rgba(255, 255, 255, 0.1); background: rgba(148, 163, 184, 0.1); color: var(--text-primary); font-size: 1rem; font-weight: bold; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; } .qty-btn:hover { background: var(--primary-indigo); border-color: var(--primary-indigo); } .qty-val { font-weight: 700; font-size: 1rem; width: 30px; text-align: center; } /* --- POS Shopping Cart UI --- */ .cart-container { display: flex; flex-direction: column; height: 480px; } .cart-list { flex: 1; overflow-y: auto; margin-bottom: 15px; border-bottom: 1px solid rgba(255, 255, 255, 0.05); padding-right: 5px; } .cart-total-box { background: rgba(15, 23, 42, 0.6); border-radius: 14px; padding: 15px; border: 1px solid var(--glass-border); margin-bottom: 15px; } .cart-total-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; } .cart-total-label { font-size: 0.95rem; color: var(--text-secondary); } .cart-total-val { font-size: 1.6rem; font-weight: 800; color: var(--success-emerald); } .cash-calculator { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 15px; } .calc-box { display: flex; flex-direction: column; gap: 5px; } .calc-label { font-size: 0.8rem; color: var(--text-secondary); } .calc-input { width: 100%; background: rgba(15, 23, 42, 0.8); border: 1px solid var(--glass-border); border-radius: 8px; padding: 10px; color: var(--text-primary); font-size: 1.1rem; font-weight: 700; outline: none; } .calc-val { padding: 10px; font-size: 1.1rem; font-weight: 700; color: var(--warning-amber); background: rgba(15, 23, 42, 0.4); border-radius: 8px; border: 1px solid var(--glass-border); } /* --- Receipt & Dialog Modal --- */ .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(15, 23, 42, 0.8); backdrop-filter: blur(8px); display: none; justify-content: center; align-items: center; z-index: 1000; animation: fadeIn 0.3s; } .modal-card { background: #ffffff; color: #1e293b; border-radius: 16px; padding: 24px; width: 90%; max-width: 400px; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); animation: scaleIn 0.3s cubic-bezier(0.16, 1, 0.3, 1); } .receipt-ticket { background: #f8fafc; border: 1px dashed #cbd5e1; padding: 15px; font-family: monospace; font-size: 0.85rem; color: #334155; margin-bottom: 20px; } .receipt-title { text-align: center; font-size: 1.1rem; font-weight: bold; margin-bottom: 10px; color: #0f172a; } .receipt-divider { border-top: 1px dashed #94a3b8; margin: 8px 0; } .receipt-row { display: flex; justify-content: space-between; margin-bottom: 4px; } /* --- Label Studio Card Grid --- */ .label-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 15px; margin-top: 20px; } .label-card { background: white; color: #000; border: 1px solid #e2e8f0; padding: 15px; text-align: center; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); page-break-inside: avoid; } .label-card .label-title { font-weight: 800; font-size: 1.05rem; margin-bottom: 6px; color: #1e293b; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .label-card .label-price { font-weight: 700; color: #e11d48; margin-bottom: 6px; font-size: 0.95rem; } .label-card .barcode-svg { max-width: 100%; height: auto; } .label-card .qr-wrapper { display: flex; justify-content: center; margin: 8px 0; } .label-card .label-code { font-family: monospace; font-size: 0.8rem; color: #64748b; margin-top: 6px; } /* --- Printing Styling --- */ @media print { body { background: white !important; color: black !important; padding: 0 !important; margin: 0 !important; } .no-print, header, .nav-tabs, .stats-board, .glass-card, .btn, .input-group, .modal-card .btn { display: none !important; } .modal-overlay { background: white !important; position: static !important; display: block !important; backdrop-filter: none !important; } .modal-card { box-shadow: none !important; padding: 0 !important; width: 100% !important; max-width: 100% !important; background: white !important; } .receipt-ticket { border: none !important; background: white !important; padding: 0 !important; } .label-grid { display: block !important; } .label-card { float: left !important; width: 48% !important; margin: 1% !important; border: 1px dashed #ccc !important; box-shadow: none !important; page-break-inside: avoid !important; } } /* --- Animations --- */ @keyframes slideUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes scaleIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } } @keyframes slideInItem { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } } @keyframes pulseGlow { 0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); } 70% { box-shadow: 0 0 0 10px rgba(16, 185, 129, 0); } 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); } } .highlight-flash { animation: rowFlash 1s ease; } @keyframes rowFlash { 0% { background: rgba(99, 102, 241, 0.4); } 100% { background: rgba(30, 41, 59, 0.4); } } </style> </head> <body> <div class="container"> <!-- --- Header --- --> <header class="no-print"> <h1>⚡ 全能 POS 兼盤點系統 <span style="font-size:0.7em; background:#4f46e5; padding:2px 8px; border-radius:8px; font-weight:normal; letter-spacing:1px; color:#fff;">ULTIMATE</span></h1> <span style="font-size:0.85rem; color:var(--text-secondary);" id="current-time"></span> </header> <!-- --- Stats Summary Board --- --> <div class="stats-board no-print"> <div class="stat-card"> <div class="stat-label">📊 累計盤點總量</div> <div class="stat-value" id="stat-total-qty">{{ stats.total_qty }}</div> </div> <div class="stat-card"> <div class="stat-label">🛒 今日收銀金額</div> <div class="stat-value highlight" id="stat-total-sales">${{ '{:,.0f}'.format(stats.total_sales) }}</div> </div> <div class="stat-card"> <div class="stat-label">🧾 交易結帳單數</div> <div class="stat-value" id="stat-tx-count">{{ stats.tx_count }}</div> </div> </div> <!-- --- Main Navigation Tabs --- --> <div class="nav-tabs no-print"> <button class="tab-link active" onclick="switchTab('scanner')">📱 盤點作業</button> <button class="tab-link" onclick="switchTab('pos')">🛒 收銀 POS</button> <button class="tab-link" onclick="switchTab('sales')">📈 銷售報表</button> <button class="tab-link" onclick="switchTab('generator')">🖨️ 產生標籤</button> <button class="tab-link" onclick="switchTab('settings')">⚙️ 系統設定</button> </div> <!-- --- Status Broadcast Banner --- --> <div id="status-alert" class="alert-notification no-print"></div> <!-- ==================== TABS 1: 盤點作業 ==================== --> <div id="view-scanner" class="tab-panel active"> <div class="split-view"> <!-- Left panel: Camera & Manual input --> <div> <div class="glass-card no-print"> <div class="card-header"> <div class="card-title">📷 條碼讀取區域</div> <span style="font-size:0.8rem; color:var(--text-secondary);">支援相機鏡頭或紅外線條碼掃描槍</span> </div> <div class="mode-banner"> <label style="display:flex; align-items:center; gap:8px; cursor:pointer;"> <input type="checkbox" id="invoice-mode-toggle"> 🧾 僅限掃描發票 (自動忽略非發票條碼雜訊) </label> </div> <div class="input-group"> <input type="text" id="scanner-input" class="styled-input" placeholder="點擊此處使用掃描槍 / 手動輸入條碼" autofocus> </div> <div style="display:flex; gap:10px;"> <button onclick="startCamera('reader-inventory', onInventoryCameraSuccess)" id="btn-start-inventory" class="btn btn-primary" style="flex:1;">📷 啟動相機鏡頭</button> <button onclick="stopCamera()" id="btn-stop-inventory" class="btn btn-danger" style="display:none; flex:1;">❌ 關閉鏡頭</button> </div> </div> <div id="scanner-container" class="no-print"><div id="reader-inventory"></div></div> <!-- Chronological log list --> <div class="glass-card no-print"> <div class="card-header"> <div class="card-title">🕒 即時盤點日誌 (實時流)</div> <a href="/download?type=raw" class="btn btn-secondary" style="font-size:0.8rem; padding:6px 12px; border-radius:8px;">📥 導出日誌 CSV</a> </div> <div class="scroll-list" id="log-list"> {% if records %} {% for record in records %} <div class="log-item-row"> <div class="log-item-info"> <span class="log-item-title" style="{{ 'color:#f43f5e' if record.name == '未知物品' else '' }}">{{ record.name }}</span> <span class="log-item-subtitle">{{ record.content }}</span> </div> <span class="log-item-time">{{ record.time.split(' ')[1] }}</span> </div> {% endfor %} {% else %} <p style="text-align:center; color:var(--text-secondary); padding:20px 0;" id="empty-log-text">暫無掃描紀錄,請開始掃描</p> {% endif %} </div> </div> </div> <!-- Right panel: Aggregated inventory view --> <div> <div class="glass-card"> <div class="card-header"> <div class="card-title">📋 盤點統計清單</div> <div class="no-print" style="display:flex; gap:6px;"> <a href="/download?type=inventory" class="btn btn-success" style="font-size:0.8rem; padding:6px 12px; border-radius:8px;">📥 導出匯總</a> </div> </div> <div class="scroll-list"> <table class="styled-table"> <thead> <tr> <th>品名 / 條碼</th> <th style="text-align:center; width:95px;">數量</th> <th class="no-print" style="text-align:center; width:45px;">操作</th> </tr> </thead> <tbody id="inventory-table-body"> {% if inventory %} {% for code, record in inventory.items() %} <tr id="inv-row-{{ code }}"> <td> <div style="font-weight:700; color:var(--text-primary);">{{ record.name }}</div> <div style="font-size:0.75rem; color:var(--text-secondary); font-family:monospace; margin-top:2px;">{{ code }}</div> </td> <td style="text-align:center;"> <div class="qty-control"> <button class="qty-btn no-print" onclick="adjustInventoryQty('{{ code }}', 'dec')">-</button> <span class="qty-val" id="inv-qty-{{ code }}">{{ record.quantity }}</span> <button class="qty-btn no-print" onclick="adjustInventoryQty('{{ code }}', 'inc')">+</button> </div> </td> <td class="no-print" style="text-align:center;"> <button onclick="deleteInventoryItem('{{ code }}')" class="qty-btn" style="color:var(--danger-rose); font-size:0.8rem; margin:0 auto;">🗑️</button> </td> </tr> {% endfor %} {% else %} <tr id="empty-inventory-row"> <td colspan="3" style="text-align:center; color:var(--text-secondary); padding:40px 0;">尚未盤點任何商品</td> </tr> {% endif %} </tbody> </table> </div> </div> </div> </div> </div> <!-- ==================== TABS 2: 收銀 POS ==================== --> <div id="view-pos" class="tab-panel"> <div class="split-view"> <!-- Left panel: Shopping Cart list --> <div class="glass-card"> <div class="card-header"> <div class="card-title">🛒 當前購物車清單</div> <button onclick="clearCart()" class="btn btn-secondary no-print" style="font-size:0.8rem; padding:6px 12px; border-radius:8px;">🗑️ 清空購物車</button> </div> <div class="cart-container"> <div class="cart-list"> <table class="styled-table"> <thead> <tr> <th>商品品名 / 條碼</th> <th style="text-align:right; width:70px;">單價</th> <th style="text-align:center; width:95px;">數量</th> <th style="text-align:right; width:80px;">小計</th> </tr> </thead> <tbody id="cart-table-body"> <tr id="empty-cart-row"> <td colspan="4" style="text-align:center; color:var(--text-secondary); padding:80px 0;">購物車為空,請掃描條碼加入商品</td> </tr> </tbody> </table> </div> <!-- Running totals --> <div class="cart-total-box"> <div class="cart-total-row"> <span class="cart-total-label">總商品數量</span> <span style="font-weight:700; font-size:1.1rem;" id="cart-qty-total">0 件</span> </div> <div class="cart-total-row"> <span class="cart-total-label">應收總金額</span> <span class="cart-total-val" id="cart-amount-total">$0</span> </div> </div> <!-- Cashier calculations --> <div class="cash-calculator no-print"> <div class="calc-box"> <span class="calc-label">💵 顧客實收金額</span> <input type="number" id="pos-cash-received" class="calc-input" placeholder="0" min="0" oninput="calculateChange()"> </div> <div class="calc-box"> <span class="calc-label">🪙 應找零錢</span> <div id="pos-change-due" class="calc-val">$0</div> </div> </div> <button onclick="checkoutPOS()" id="btn-checkout" class="btn btn-success no-print" style="width:100%; padding:15px; font-size:1.2rem; border-radius:14px; font-weight:bold;">🤝 確認結帳並列印收據</button> </div> </div> <!-- Right panel: Scanner & Manual lookup for POS --> <div class="glass-card no-print"> <div class="card-header"> <div class="card-title">🏷️ 商品銷售讀取器</div> </div> <div class="input-group"> <input type="text" id="pos-input" class="styled-input" placeholder="點擊使用掃描槍 / 手動鍵入條碼"> </div> <div style="display:flex; gap:10px; margin-bottom:20px;"> <button onclick="startCamera('reader-pos', onPOSCameraSuccess)" id="btn-start-pos" class="btn btn-primary" style="flex:1;">📷 啟動相機銷售鏡頭</button> <button onclick="stopCamera()" id="btn-stop-pos" class="btn btn-danger" style="display:none; flex:1;">❌ 關閉鏡頭</button> </div> <div id="pos-scanner-container" style="display:none; border-radius:12px; overflow:hidden; border:2px solid var(--primary-indigo); margin-top:10px;"><div id="reader-pos"></div></div> <!-- Mini item quick selector --> <div style="margin-top:20px;"> <div style="font-size:0.85rem; color:var(--text-secondary); margin-bottom:10px; font-weight:600;">⚡ 快速點選商品庫 (方便測試)</div> <div style="display:flex; flex-direction:column; gap:8px; max-height:220px; overflow-y:auto; padding-right:4px;" id="quick-pick-container"> <p style="text-align:center; color:var(--text-secondary); font-size:0.8rem; padding:15px;">載入商品庫中...</p> </div> </div> </div> </div> </div> <!-- ==================== TABS 3: 銷售報表 ==================== --> <div id="view-sales" class="tab-panel"> <div class="glass-card"> <div class="card-header"> <div class="card-title">📈 營業交易歷史明細</div> <div class="no-print" style="display:flex; gap:10px;"> <a href="/download?type=sales" class="btn btn-success" style="font-size:0.8rem; padding:6px 12px;">📥 匯出銷售 CSV</a> <button onclick="clearSalesHistory()" class="btn btn-danger" style="font-size:0.8rem; padding:6px 12px;">🗑️ 清空交易紀錄</button> </div> </div> <div class="scroll-list"> <table class="styled-table"> <thead> <tr> <th>交易單號</th> <th>交易時間</th> <th>銷售明細</th> <th style="text-align:right; width:90px;">應收總額</th> <th style="text-align:right; width:90px;">實收金額</th> <th style="text-align:right; width:80px;">找零</th> </tr> </thead> <tbody id="sales-table-body"> {% if sales %} {% for sale in sales %} <tr> <td style="font-family:monospace; font-weight:bold;">{{ sale['tx_id'] }}</td> <td style="font-size:0.85rem; color:var(--text-secondary);">{{ sale['time'] }}</td> <td> <div style="display:flex; flex-direction:column; gap:2px; font-size:0.9rem;"> {% for item in sale['items'] %} <div>🔹 {{ item['name'] }} (x{{ item['quantity'] }}) - ${% if item['price'] is defined %}{{ '{:,.0f}'.format(item['price'] * item['quantity']) }}{% else %}0{% endif %}</div> {% endfor %} </div> </td> <td style="text-align:right; font-weight:700; color:var(--success-emerald);">${{ '{:,.0f}'.format(sale['total']) }}</td> <td style="text-align:right;">${{ '{:,.0f}'.format(sale['cash_received']) }}</td> <td style="text-align:right; color:var(--warning-amber);">${{ '{:,.0f}'.format(sale['change']) }}</td> </tr> {% endfor %} {% else %} <tr id="empty-sales-row"> <td colspan="6" style="text-align:center; color:var(--text-secondary); padding:60px 0;">目前無銷售歷史交易</td> </tr> {% endif %} </tbody> </table> </div> </div> </div> <!-- ==================== TABS 4: 產生標籤 ==================== --> <div id="view-generator" class="tab-panel"> <div class="glass-card no-print"> <div class="card-header"> <div class="card-title">🖨️ 商品標籤列印設計</div> </div> <p style="font-size:0.9rem; color:var(--text-secondary); margin-bottom:15px;">從您上傳的商品 CSV 庫中,自動大量產生一維條碼或 QR 二維碼標籤紙,方便直接貼在庫存箱或商品上。</p> <div style="margin-bottom:20px; display:flex; gap:20px; align-items:center; flex-wrap:wrap;"> <div style="display:flex; gap:15px;"> <label style="cursor:pointer; display:flex; align-items:center; gap:6px;"> <input type="radio" name="codeType" value="barcode" checked style="accent-color:var(--primary-indigo);"> 條碼 (CODE128) </label> <label style="cursor:pointer; display:flex; align-items:center; gap:6px;"> <input type="radio" name="codeType" value="qrcode" style="accent-color:var(--primary-indigo);"> 二維碼 (QR Code) </label> </div> <div style="display:flex; gap:10px;"> <button onclick="generateLabels()" class="btn btn-primary">🔄 產生標籤預覽</button> <button onclick="window.print()" class="btn btn-success">🖨️ 列印標籤</button> </div> </div> </div> <!-- Preview print sheet area --> <div id="label-container" class="label-grid"> <p style="text-align:center; width:100%; color:var(--text-secondary); padding:40px 0;">請點選上方「產生標籤預覽」按鈕產生排版</p> </div> </div> <!-- ==================== TABS 5: 系統設定 ==================== --> <div id="view-settings" class="tab-panel"> <div class="glass-card"> <div class="card-header"> <div class="card-title">⚙️ 系統設定與商品庫管理</div> </div> <!-- Product database upload --> <div style="margin-bottom:25px; padding-bottom:25px; border-bottom:1px solid rgba(255, 255, 255, 0.05);"> <h3 style="font-size:1.05rem; font-weight:700; margin-bottom:8px;">📁 更新商品對照資料庫 (CSV 上傳)</h3> <p style="font-size:0.85rem; color:var(--text-secondary); margin-bottom:15px;"> 請上傳一個 CSV 檔案,欄位格式必須為:<strong>條碼, 商品名稱, 價格(選填)</strong>。支援 UTF-8 編碼。<br> 如果不包含價格,系統預設價格將自動設為 0 元。 </p> <div style="display:flex; gap:15px; align-items:center; flex-wrap:wrap;"> <input type="file" id="csv-file" accept=".csv" style="display:none;" onchange="updateFileNameLabel()"> <button onclick="document.getElementById('csv-file').click()" class="btn btn-secondary">📂 選擇 CSV 檔案</button> <span id="csv-file-name" style="font-size:0.9rem; color:var(--text-secondary);">尚未選擇檔案</span> <button onclick="uploadCSV()" id="btn-upload-csv" class="btn btn-primary">⚡ 上傳並載入對照表</button> </div> <p style="margin-top:15px; font-size:0.9rem;"> 目前對照資料庫商品總筆數:<strong style="color:var(--primary-indigo);" id="db-count-label">{{ db_count }}</strong> 筆 </p> </div> <!-- Database operations --> <div> <h3 style="font-size:1.05rem; font-weight:700; margin-bottom:12px; color:var(--danger-rose);">⚠️ 資料危險管理區</h3> <p style="font-size:0.85rem; color:var(--text-secondary); margin-bottom:15px;">執行此處操作將會徹底刪除對應的磁碟資料庫檔案,請務必小心!</p> <div style="display:flex; gap:12px; flex-wrap:wrap;"> <button onclick="clearRecords()" class="btn btn-danger">🗑️ 徹底清空盤點庫存與日誌</button> <button onclick="clearSalesHistory()" class="btn btn-danger">🗑️ 徹底清空收銀銷售明細</button> </div> </div> </div> </div> </div> <!-- end container --> <!-- ==================== RECEIPT PRINT DIALOG MODAL ==================== --> <div id="receipt-modal" class="modal-overlay"> <div class="modal-card"> <div class="receipt-ticket"> <div class="receipt-title">🧾 電子收銀收據</div> <div style="text-align:center; font-size:0.75rem; color:#64748b;" id="receipt-tx-id">TX-00000000</div> <div style="text-align:center; font-size:0.75rem; color:#64748b; margin-bottom:10px;" id="receipt-time">2026-05-28 16:00:00</div> <div class="receipt-divider"></div> <div id="receipt-items-list"> <!-- Dynamic insert --> </div> <div class="receipt-divider"></div> <div class="receipt-row" style="font-weight:bold; font-size:0.95rem; color:#0f172a;"> <span>應付總額:</span> <span id="receipt-total-amount">$0</span> </div> <div class="receipt-row" style="color:#64748b;"> <span>實收現金:</span> <span id="receipt-cash-received">$0</span> </div> <div class="receipt-row" style="font-weight:bold; color:#475569;"> <span>應找零錢:</span> <span id="receipt-change-due">$0</span> </div> <div class="receipt-divider"></div> <div style="text-align:center; font-size:0.75rem; color:#94a3b8; margin-top:10px;">🌟 謝謝惠顧!歡迎再次光臨 🌟</div> </div> <div style="display:flex; gap:10px;" class="no-print"> <button onclick="window.print()" class="btn btn-success" style="flex:1;">🖨️ 直接列印小票</button> <button onclick="closeReceiptModal()" class="btn btn-secondary" style="flex:1;">❌ 關閉視窗</button> </div> </div> </div> <!-- ==================== FRONTEND JAVASCRIPT ==================== --> <script> // 全域狀態管理 let localProductDB = {}; // 從伺服器下載的商品庫對照快取 let shoppingCart = []; // POS 購物車 let html5QrcodeScanner = null; // 相機實體 let activeScannerId = null; // 當前活動相機的容器 ID // 啟動初始化 window.addEventListener('DOMContentLoaded', () => { fetchStats(); fetchAllProducts(); updateTime(); setInterval(updateTime, 1000); // 綁定輸入框 Enter 事件 const scannerInput = document.getElementById('scanner-input'); scannerInput.focus(); scannerInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { processInventoryBarcode(this.value); this.value = ''; } }); const posInput = document.getElementById('pos-input'); posInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { processPOSBarcode(this.value); this.value = ''; } }); }); function updateTime() { const timeSpan = document.getElementById('current-time'); if (timeSpan) { const now = new Date(); timeSpan.innerText = '🕒 ' + now.toLocaleString('zh-TW', { hour12: false }); } } // --- 1. Tab 切換邏輯 --- function switchTab(tabName) { // 切換 CSS document.querySelectorAll('.tab-panel').forEach(el => el.classList.remove('active')); document.querySelectorAll('.tab-link').forEach(el => el.classList.remove('active')); document.getElementById(`view-${tabName}`).classList.add('active'); // 點選 active 標籤 const tabs = document.querySelectorAll('.tab-link'); const map = { 'scanner': 0, 'pos': 1, 'sales': 2, 'generator': 3, 'settings': 4 }; tabs[map[tabName]].classList.add('active'); // 關閉可能已啟動的相機 stopCamera(); // 焦點輔助 if (tabName === 'scanner') { document.getElementById('scanner-input').focus(); } else if (tabName === 'pos') { document.getElementById('pos-input').focus(); renderQuickPick(); } } // --- 2. 廣播式橫幅通知 --- function triggerAlert(type, message) { const alertBox = document.getElementById('status-alert'); alertBox.innerText = message; alertBox.className = 'alert-notification no-print ' + type; alertBox.style.display = 'flex'; // 3秒後自動淡出 setTimeout(() => { alertBox.style.display = 'none'; }, 3500); } // --- 3. 獲取核心統計數據 --- function fetchStats() { fetch('/api/stats') .then(res => res.json()) .then(resp => { if(resp.status === 'success') { updateStatsUI(resp.stats); } }); } function updateStatsUI(stats) { document.getElementById('stat-total-qty').innerText = stats.total_qty; document.getElementById('stat-total-sales').innerText = '$' + Number(stats.total_sales).toLocaleString('zh-TW', {maximumFractionDigits: 0}); document.getElementById('stat-tx-count').innerText = stats.tx_count; } // 載入商品對照庫 function fetchAllProducts() { fetch('/api/products') .then(res => res.json()) .then(resp => { if (resp.status === 'success') { localProductDB = resp.data; renderQuickPick(); } }); } // --- 4. 盤點掃描核心邏輯 (Zero reload) --- function processInventoryBarcode(code) { code = code.trim(); if(!code) return; // 發票過濾 const isInvoiceMode = document.getElementById('invoice-mode-toggle').checked; if (isInvoiceMode) { if (!(code.length > 20 && /^[A-Z]{2}/.test(code))) { triggerAlert('error', '⛔ 這不是發票,已過濾'); return; } } // 發送至後端 fetch('/api/scan', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ qr_content: code }) }) .then(res => res.json()) .then(resp => { if(resp.status === 'success') { triggerAlert('success', `✅ 已盤點:${resp.data.name}`); // 1. 動態更新即時日誌流 updateLiveLogUI(resp.data); // 2. 動態更新盤點總計列表 updateInventoryTableUI(code, resp.inventory_item); // 3. 更新頂部統計看板 updateStatsUI(resp.stats); } else { triggerAlert('error', resp.message); } }) .catch(err => { triggerAlert('error', '❌ 連線失敗,請檢查網路'); }); } function updateLiveLogUI(record) { const list = document.getElementById('log-list'); const emptyTxt = document.getElementById('empty-log-text'); if(emptyTxt) emptyTxt.remove(); const item = document.createElement('div'); item.className = 'log-item-row highlight-flash'; const info = document.createElement('div'); info.className = 'log-item-info'; const title = document.createElement('span'); title.className = 'log-item-title'; if (record.name === '未知物品') { title.style.color = '#f43f5e'; } title.innerText = record.name; const subtitle = document.createElement('span'); subtitle.className = 'log-item-subtitle'; subtitle.innerText = record.content; info.appendChild(title); info.appendChild(subtitle); const time = document.createElement('span'); time.className = 'log-item-time'; time.innerText = record.time.split(' ')[1]; item.appendChild(info); item.appendChild(time); list.insertBefore(item, list.firstChild); // 最多保留 30 筆日誌 if (list.children.length > 30) { list.removeChild(list.lastChild); } } function updateInventoryTableUI(code, invItem) { const tbody = document.getElementById('inventory-table-body'); const emptyRow = document.getElementById('empty-inventory-row'); if(emptyRow) emptyRow.remove(); const existingRow = document.getElementById(`inv-row-${code}`); if(existingRow) { // 如果已經存在該列,直接更新數量並閃爍動畫 const qtySpan = document.getElementById(`inv-qty-${code}`); qtySpan.innerText = invItem.quantity; existingRow.classList.add('highlight-flash'); setTimeout(() => existingRow.classList.remove('highlight-flash'), 1000); } else { // 如果是新商品,插入一列新行 const tr = document.createElement('tr'); tr.id = `inv-row-${code}`; tr.className = 'highlight-flash'; tr.innerHTML = ` <td> <div style="font-weight:700; color:var(--text-primary);">${invItem.name}</div> <div style="font-size:0.75rem; color:var(--text-secondary); font-family:monospace; margin-top:2px;">${code}</div> </td> <td style="text-align:center;"> <div class="qty-control"> <button class="qty-btn no-print" onclick="adjustInventoryQty('${code}', 'dec')">-</button> <span class="qty-val" id="inv-qty-${code}">${invItem.quantity}</span> <button class="qty-btn no-print" onclick="adjustInventoryQty('${code}', 'inc')">+</button> </div> </td> <td class="no-print" style="text-align:center;"> <button onclick="deleteInventoryItem('${code}')" class="qty-btn" style="color:var(--danger-rose); font-size:0.8rem; margin:0 auto;">🗑️</button> </td> `; tbody.insertBefore(tr, tbody.firstChild); setTimeout(() => tr.classList.remove('highlight-flash'), 1000); } } // 修改盤點清單上的數量 +/- function adjustInventoryQty(code, action) { fetch('/api/inventory/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code: code, action: action, value: 1 }) }) .then(res => res.json()) .then(resp => { if(resp.status === 'success') { const qtySpan = document.getElementById(`inv-qty-${code}`); if(qtySpan) { qtySpan.innerText = resp.inventory_item.quantity; updateStatsUI(resp.stats); } } }); } // 刪除整列盤點商品 function deleteInventoryItem(code) { if(!confirm("⚠️ 確定要從盤點清單中刪除此商品嗎?")) return; fetch('/api/inventory/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code: code }) }) .then(res => res.json()) .then(resp => { if(resp.status === 'success') { const row = document.getElementById(`inv-row-${code}`); if(row) { row.remove(); updateStatsUI(resp.stats); triggerAlert('warning', '已從盤點統計中移除商品'); // 若全清空,補回空行 const tbody = document.getElementById('inventory-table-body'); if (tbody.children.length === 0) { tbody.innerHTML = ` <tr id="empty-inventory-row"> <td colspan="3" style="text-align:center; color:var(--text-secondary); padding:40px 0;">尚未盤點任何商品</td> </tr> `; } } } }); } // --- 5. 收銀 POS 購物車核心邏輯 --- function renderQuickPick() { const container = document.getElementById('quick-pick-container'); if(!container) return; container.innerHTML = ''; const entries = Object.entries(localProductDB); if(entries.length === 0) { container.innerHTML = '<p style="text-align:center; color:var(--text-secondary); font-size:0.8rem; padding:15px;">商品庫為空,請先前往系統設定上傳對照表 CSV</p>'; return; } entries.forEach(([code, prod]) => { const btn = document.createElement('div'); btn.style.display = 'flex'; btn.style.justify = 'space-between'; btn.style.alignItems = 'center'; btn.style.padding = '8px 12px'; btn.style.background = 'rgba(255, 255, 255, 0.03)'; btn.style.borderRadius = '8px'; btn.style.border = '1px solid rgba(255, 255, 255, 0.05)'; btn.style.cursor = 'pointer'; btn.style.fontSize = '0.9rem'; btn.onclick = () => processPOSBarcode(code); btn.innerHTML = ` <div style="display:flex; flex-direction:column;"> <span style="font-weight:700; color:var(--text-primary);">${prod.name}</span> <span style="font-size:0.75rem; color:var(--text-secondary); font-family:monospace;">${code}</span> </div> <span style="font-weight:bold; color:var(--success-emerald); font-size:0.95rem;">$${prod.price}</span> `; container.appendChild(btn); }); } function processPOSBarcode(code) { code = code.trim(); if(!code) return; let prod = { name: "未知商品", price: 0.0 }; if(localProductDB[code]) { prod = localProductDB[code]; } // 新增至購物車快取 const cartItem = shoppingCart.find(item => item.code === code); if (cartItem) { cartItem.quantity += 1; triggerAlert('success', `➕ 購物車累計:${prod.name} (x${cartItem.quantity})`); } else { shoppingCart.push({ code: code, name: prod.name, price: prod.price, quantity: 1 }); triggerAlert('success', `🛒 已加入購物車:${prod.name}`); } renderCart(); } function renderCart() { const tbody = document.getElementById('cart-table-body'); tbody.innerHTML = ''; if(shoppingCart.length === 0) { tbody.innerHTML = ` <tr id="empty-cart-row"> <td colspan="4" style="text-align:center; color:var(--text-secondary); padding:80px 0;">購物車為空,請掃描條碼加入商品</td> </tr> `; document.getElementById('cart-qty-total').innerText = '0 件'; document.getElementById('cart-amount-total').innerText = '$0'; calculateChange(); return; } let totalQty = 0; let totalAmount = 0; shoppingCart.forEach(item => { const subtotal = item.price * item.quantity; totalQty += item.quantity; totalAmount += subtotal; const tr = document.createElement('tr'); tr.innerHTML = ` <td> <div style="font-weight:700; color:var(--text-primary);">${item.name}</div> <div style="font-size:0.75rem; color:var(--text-secondary); font-family:monospace; margin-top:2px;">${item.code}</div> </td> <td style="text-align:right; font-weight:600;">$${item.price}</td> <td style="text-align:center;"> <div class="qty-control" style="justify-content:center;"> <button class="qty-btn" onclick="adjustCartQty('${item.code}', -1)">-</button> <span class="qty-val">${item.quantity}</span> <button class="qty-btn" onclick="adjustCartQty('${item.code}', 1)">+</button> </div> </td> <td style="text-align:right; font-weight:700; color:var(--success-emerald);">$${subtotal}</td> `; tbody.appendChild(tr); }); document.getElementById('cart-qty-total').innerText = `${totalQty} 件`; document.getElementById('cart-amount-total').innerText = `$${totalAmount}`; calculateChange(); } function adjustCartQty(code, val) { const cartItem = shoppingCart.find(item => item.code === code); if (cartItem) { cartItem.quantity += val; if(cartItem.quantity <= 0) { shoppingCart = shoppingCart.filter(item => item.code !== code); } renderCart(); } } function clearCart() { if(shoppingCart.length === 0) return; if(!confirm("⚠️ 確定要清空整部購物車嗎?")) return; shoppingCart = []; renderCart(); triggerAlert('warning', '購物車已清空'); } // 計算應找零錢 function calculateChange() { const totalText = document.getElementById('cart-amount-total').innerText; const total = parseFloat(totalText.replace('$', '')) || 0; const cash = parseFloat(document.getElementById('pos-cash-received').value) || 0; const change = Math.max(0, cash - total); document.getElementById('pos-change-due').innerText = `$${change}`; } // 收銀 POS 結帳 function checkoutPOS() { if(shoppingCart.length === 0) { alert("購物車內無商品!"); return; } const totalText = document.getElementById('cart-amount-total').innerText; const total = parseFloat(totalText.replace('$', '')) || 0; const cash = parseFloat(document.getElementById('pos-cash-received').value) || 0; if (cash < total) { alert(`金額不足!還缺 ${total - cash} 元`); return; } // 發送結帳請求 fetch('/api/pos/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ items: shoppingCart, cash_received: cash }) }) .then(res => res.json()) .then(resp => { if (resp.status === 'success') { // 更新頂部數據看板 updateStatsUI(resp.stats); // 顯示列印收據 Modal showReceiptModal(resp.sale); // 清空本地購物車 shoppingCart = []; renderCart(); document.getElementById('pos-cash-received').value = ''; document.getElementById('pos-change-due').innerText = '$0'; // 更新銷售歷史 DOM updateSalesTableUI(resp.sale); } else { alert("結帳失敗:" + resp.message); } }) .catch(err => { alert("結帳異常,請檢查網路連線"); }); } // 彈出精美收據小票視窗 function showReceiptModal(sale) { document.getElementById('receipt-tx-id').innerText = sale.tx_id; document.getElementById('receipt-time').innerText = sale.time; const listDiv = document.getElementById('receipt-items-list'); listDiv.innerHTML = ''; sale.items.forEach(item => { const row = document.createElement('div'); row.className = 'receipt-row'; row.innerHTML = ` <span>${item.name} (x${item.quantity})</span> <span>$${item.price * item.quantity}</span> `; listDiv.appendChild(row); }); document.getElementById('receipt-total-amount').innerText = `$${sale.total}`; document.getElementById('receipt-cash-received').innerText = `$${sale.cash_received}`; document.getElementById('receipt-change-due').innerText = `$${sale.change}`; document.getElementById('receipt-modal').style.display = 'flex'; } function closeReceiptModal() { document.getElementById('receipt-modal').style.display = 'none'; } function updateSalesTableUI(sale) { const tbody = document.getElementById('sales-table-body'); const emptyRow = document.getElementById('empty-sales-row'); if(emptyRow) emptyRow.remove(); const tr = document.createElement('tr'); let itemsHTML = '<div style="display:flex; flex-direction:column; gap:2px; font-size:0.9rem;">'; sale.items.forEach(item => { itemsHTML += `<div>🔹 ${item.name} (x${item.quantity}) - $${item.price * item.quantity}</div>`; }); itemsHTML += '</div>'; tr.innerHTML = ` <td style="font-family:monospace; font-weight:bold;">${sale.tx_id}</td> <td style="font-size:0.85rem; color:var(--text-secondary);">${sale.time}</td> <td>${itemsHTML}</td> <td style="text-align:right; font-weight:700; color:var(--success-emerald);">$${sale.total}</td> <td style="text-align:right;">$${sale.cash_received}</td> <td style="text-align:right; color:var(--warning-amber);">$${sale.change}</td> `; tbody.insertBefore(tr, tbody.firstChild); } // --- 6. 清理資料庫 --- function clearRecords() { if(!confirm("⚠️ 警告:確定要清空所有盤點紀錄與日誌嗎?這將會刪除 scans.json 庫存!")) return; fetch('/api/clear', { method: 'POST' }) .then(res => res.json()) .then(resp => { alert(resp.message); window.location.reload(); }); } function clearSalesHistory() { if(!confirm("⚠️ 警告:確定要清空所有銷售歷史紀錄嗎?這會刪除銷售金額統計!")) return; fetch('/api/sales/clear', { method: 'POST' }) .then(res => res.json()) .then(resp => { alert(resp.message); window.location.reload(); }); } // --- 7. 相機鏡頭操作核心 (不重整網頁相容 EAN/QR) --- function onInventoryCameraSuccess(decodedText) { // 短暫暫停一下避免重覆讀取 if(html5QrcodeScanner) { html5QrcodeScanner.pause(true); setTimeout(() => { if (html5QrcodeScanner) html5QrcodeScanner.resume(); }, 2000); } processInventoryBarcode(decodedText); } function onPOSCameraSuccess(decodedText) { if(html5QrcodeScanner) { html5QrcodeScanner.pause(true); setTimeout(() => { if (html5QrcodeScanner) html5QrcodeScanner.resume(); }, 2000); } processPOSBarcode(decodedText); } function startCamera(containerId, successCallback) { stopCamera(); // 若有正在開的則關閉 activeScannerId = containerId; let btnStartId = containerId === 'reader-inventory' ? 'btn-start-inventory' : 'btn-start-pos'; let btnStopId = containerId === 'reader-inventory' ? 'btn-stop-inventory' : 'btn-stop-pos'; let frameContainerId = containerId === 'reader-inventory' ? 'scanner-container' : 'pos-scanner-container'; document.getElementById(frameContainerId).style.display = 'block'; document.getElementById(btnStartId).style.display = 'none'; document.getElementById(btnStopId).style.display = 'block'; const formats = [ Html5QrcodeSupportedFormats.QR_CODE, Html5QrcodeSupportedFormats.EAN_13, Html5QrcodeSupportedFormats.EAN_8, Html5QrcodeSupportedFormats.CODE_128, Html5QrcodeSupportedFormats.CODE_39, Html5QrcodeSupportedFormats.UPC_A ]; html5QrcodeScanner = new Html5QrcodeScanner(containerId, { fps: 10, qrbox: { width: 280, height: 180 }, videoConstraints: { facingMode: "environment" }, formatsToSupport: formats }, false); html5QrcodeScanner.render(successCallback, (error) => {}); } function stopCamera() { if (html5QrcodeScanner) { const scanner = html5QrcodeScanner; html5QrcodeScanner = null; scanner.clear().then(() => { document.getElementById('scanner-container').style.display = 'none'; document.getElementById('pos-scanner-container').style.display = 'none'; document.getElementById('btn-start-inventory').style.display = 'block'; document.getElementById('btn-stop-inventory').style.display = 'none'; document.getElementById('btn-start-pos').style.display = 'block'; document.getElementById('btn-stop-pos').style.display = 'none'; if (activeScannerId === 'reader-inventory') { document.getElementById('scanner-input').focus(); } else { document.getElementById('pos-input').focus(); } }).catch(err => { console.error("關閉相機失敗", err); }); } } // --- 8. 標籤大量產生與印表排版 --- function generateLabels() { const container = document.getElementById('label-container'); const codeType = document.querySelector('input[name="codeType"]:checked').value; container.innerHTML = '<p style="text-align:center; width:100%; color:var(--text-secondary);">產生排版中,請稍候...</p>'; fetch('/api/products') .then(res => res.json()) .then(resp => { container.innerHTML = ''; const data = resp.data; if (Object.keys(data).length === 0) { container.innerHTML = '<p style="text-align:center; width:100%; color:var(--danger-rose);">商品資料庫是空的,請先在系統設定中上傳對照表 CSV 檔案。</p>'; return; } for (const [code, item] of Object.entries(data)) { const card = document.createElement('div'); card.className = 'label-card'; const title = document.createElement('div'); title.className = 'label-title'; title.innerText = item.name; card.appendChild(title); const price = document.createElement('div'); price.className = 'label-price'; price.innerText = item.price > 0 ? `NT$ ${item.price}` : '特價商品'; card.appendChild(price); if (codeType === 'barcode') { const canvas = document.createElement('svg'); canvas.className = 'barcode-svg'; card.appendChild(canvas); try { JsBarcode(canvas, code, { format: "CODE128", displayValue: false, // 自訂顯示下方即可 height: 50, margin: 0 }); } catch(e) { const errDiv = document.createElement('div'); errDiv.innerText = "無法產生一維條碼"; errDiv.style.color = "red"; errDiv.style.fontSize = "0.75rem"; card.appendChild(errDiv); } } else { const qrWrap = document.createElement('div'); qrWrap.className = 'qr-wrapper'; card.appendChild(qrWrap); try { new QRCode(qrWrap, { text: code, width: 90, height: 90 }); } catch(e) {} } const codeText = document.createElement('div'); codeText.className = 'label-code'; codeText.innerText = code; card.appendChild(codeText); container.appendChild(card); } }) .catch(err => { alert("載入商品庫失敗"); }); } // --- 9. 商品庫 CSV 上傳與解析輔助 --- function updateFileNameLabel() { const input = document.getElementById('csv-file'); const label = document.getElementById('csv-file-name'); if (input.files.length > 0) { label.innerText = input.files[0].name; } else { label.innerText = '尚未選擇檔案'; } } function uploadCSV() { const fileInput = document.getElementById('csv-file'); if (fileInput.files.length === 0) { alert("請先選擇要上傳的 CSV 檔案"); return; } const formData = new FormData(); formData.append("file", fileInput.files[0]); const btn = document.getElementById('btn-upload-csv'); btn.innerText = "上傳並載入中..."; btn.disabled = true; fetch('/api/upload_csv', { method: 'POST', body: formData }) .then(res => res.json()) .then(resp => { alert(resp.message); if (resp.status === 'success') { document.getElementById('db-count-label').innerText = resp.count; fetchAllProducts(); // 重新下載快取 } }) .catch(err => { alert("上傳對照表連線錯誤,請檢查伺服器狀態"); }) .finally(() => { btn.innerText = "⚡ 上傳並載入對照表"; btn.disabled = false; }); } </script> </body> </html>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>⚡ 全能 POS 兼盤點系統 Ultimate</title>
<!-- 引入高解析度字型 -->
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=Noto+Sans+TC:wght@300;400;500;700;900&display=swap" rel="stylesheet">
<script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.0/dist/JsBarcode.all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
:root {
--bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
--glass-bg: rgba(30, 41, 59, 0.7);
--glass-border: rgba(255, 255, 255, 0.08);
--text-primary: #f8fafc;
--text-secondary: #94a3b8;
--primary-indigo: #6366f1;
--primary-indigo-hover: #4f46e5;
--success-emerald: #10b981;
--success-emerald-hover: #059669;
--danger-rose: #f43f5e;
--danger-rose-hover: #e11d48;
--warning-amber: #f59e0b;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Plus Jakarta Sans', 'Noto Sans TC', -apple-system, sans-serif;
-webkit-font-smoothing: antialiased;
}
body {
background: var(--bg-gradient);
color: var(--text-primary);
min-height: 100vh;
padding-bottom: 60px;
}
.container {
max-width: 1000px;
margin: 0 auto;
padding: 20px;
}
/* --- Header & Stats Board --- */
header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 25px;
padding: 10px 0;
border-bottom: 1px solid var(--glass-border);
}
header h1 {
font-size: 1.8rem;
font-weight: 800;
background: linear-gradient(120deg, #818cf8, #34d399);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
display: flex;
align-items: center;
gap: 10px;
}
.stats-board {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin-bottom: 25px;
}
.stat-card {
background: var(--glass-bg);
border: 1px solid var(--glass-border);
border-radius: 16px;
padding: 15px;
text-align: center;
backdrop-filter: blur(12px);
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3);
transition: transform 0.3s, box-shadow 0.3s;
}
.stat-card:hover {
transform: translateY(-2px);
box-shadow: 0 15px 30px -5px rgba(99, 102, 241, 0.15);
}
.stat-label {
font-size: 0.85rem;
color: var(--text-secondary);
font-weight: 500;
margin-bottom: 6px;
}