// 委外发料管理 (() => { let issueList = []; let outsourcingOrders = []; let currentPage = 1; const pageSize = 20; Router.register('/outsourcing-mgmt/material-issue', async () => { const html = `
发料单号 委外工单号 物料编码 物料名称 发料数量 单位 发料日期 创建人 创建时间
加载中...
`; setTimeout(() => { document.getElementById('add-issue-btn')?.addEventListener('click', () => openModal()); document.getElementById('search-keyword')?.addEventListener('keypress', (e) => { if (e.key === 'Enter') search(); }); const today = new Date().toISOString().split('T')[0]; const dateInput = document.getElementById('issue-date'); if (dateInput) dateInput.value = today; loadList(); loadOutsourcingOrders(); }, 0); return html; }); async function loadList() { try { const res = await API.get('/api/outsourcing-material-issue'); issueList = res.list || []; renderList(); } catch (e) { console.error('加载发料列表失败:', e); document.getElementById('issue-list').innerHTML = '加载失败'; } } async function loadOutsourcingOrders() { try { const res = await API.get('/api/outsourcing-orders'); outsourcingOrders = res.list || []; const select = document.getElementById('outsourcing-order-no'); if (select) { select.innerHTML = '' + outsourcingOrders.map(o => ``).join(''); } } catch (e) { console.error('加载委外工单失败:', e); } } async function loadOrderMaterials() { const orderNo = document.getElementById('outsourcing-order-no').value; if (!orderNo) { document.getElementById('material-section').style.display = 'none'; return; } try { const order = outsourcingOrders.find(o => o.order_no === orderNo); if (!order) return; const res = await API.get(`/api/outsourcing-orders/${order.id}`); const materials = res.materials || []; if (materials.length > 0) { document.getElementById('material-section').style.display = 'block'; document.getElementById('material-list').innerHTML = materials.map((m, idx) => ` ${escapeHtml(m.material_code)} ${escapeHtml(m.material_name)} ${m.bom_unit_qty} ${m.need_qty} ${escapeHtml(m.unit)} `).join(''); } } catch (e) { console.error('加载物料明细失败:', e); alert('加载物料明细失败'); } } function renderList() { const tbody = document.getElementById('issue-list'); const keyword = (document.getElementById('search-keyword')?.value || '').toLowerCase(); let filtered = issueList; if (keyword) { filtered = issueList.filter(item => (item.issue_no || '').toLowerCase().includes(keyword) || (item.outsourcing_order_no || '').toLowerCase().includes(keyword) || (item.material_code || '').toLowerCase().includes(keyword) || (item.material_name || '').toLowerCase().includes(keyword) ); } const totalPages = Math.ceil(filtered.length / pageSize); const start = (currentPage - 1) * pageSize; const pageData = filtered.slice(start, start + pageSize); if (pageData.length === 0) { tbody.innerHTML = '暂无数据'; } else { tbody.innerHTML = pageData.map(item => ` ${escapeHtml(item.issue_no || '')} ${escapeHtml(item.outsourcing_order_no || '')} ${escapeHtml(item.material_code || '')} ${escapeHtml(item.material_name || '')} ${item.issue_qty || 0} ${escapeHtml(item.unit || 'pcs')} ${escapeHtml(item.issue_date || '')} ${escapeHtml(item.created_by || '')} ${formatTime(item.created_at)} `).join(''); } renderPagination(totalPages); } function renderPagination(totalPages) { const container = document.getElementById('pagination'); if (totalPages <= 1) { container.innerHTML = ''; return; } let html = ''; html += ``; html += `第 ${currentPage} / ${totalPages} 页`; html += ``; container.innerHTML = html; } function openModal() { document.getElementById('outsourcing-order-no').value = ''; const today = new Date().toISOString().split('T')[0]; document.getElementById('issue-date').value = today; document.getElementById('material-section').style.display = 'none'; document.getElementById('issue-modal').style.display = 'flex'; } function closeModal() { document.getElementById('issue-modal').style.display = 'none'; } async function save() { const outsourcing_order_no = document.getElementById('outsourcing-order-no').value.trim(); const issue_date = document.getElementById('issue-date').value.trim(); if (!outsourcing_order_no || !issue_date) { alert('请填写所有必填字段'); return; } const materials = []; const inputs = document.querySelectorAll('[id^="issue-qty-"]'); inputs.forEach(input => { const qty = parseInt(input.value) || 0; if (qty > 0) { materials.push({ material_code: input.dataset.materialCode, material_name: input.dataset.materialName, issue_qty: qty, unit: input.dataset.unit }); } }); if (materials.length === 0) { alert('请至少填写一条发料数量'); return; } try { const res = await API.post('/api/outsourcing-material-issue', { outsourcing_order_no, materials, issue_date }); alert(`发料成功,发料单号:${res.issue_no}`); closeModal(); loadList(); } catch (e) { alert(e.message || '发料失败'); } } function search() { currentPage = 1; renderList(); } function resetSearch() { document.getElementById('search-keyword').value = ''; currentPage = 1; renderList(); } function goPage(page) { currentPage = page; renderList(); } function formatTime(ts) { if (!ts) return '-'; try { const d = new Date(ts); return d.toLocaleString('zh-CN', { hour12: false }); } catch { return ts; } } function escapeHtml(str) { if (!str) return ''; return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } window.MaterialIssue = { search, resetSearch, closeModal, save, goPage, loadOrderMaterials }; })();