Update App.jsx via upload script - 2026-11-17 15:39:58

This commit is contained in:
mike 2026-11-17 15:40:19 +08:00
parent 7dcbd6ef61
commit 111c44c9fb
1 changed files with 89 additions and 57 deletions

146
App.jsx
View File

@ -2253,6 +2253,9 @@ const Styles = () => (
.cd-ship-row { grid-template-columns: minmax(0, 2fr) minmax(0, 1.2fr) auto auto; align-items: center; }
.cd-hs-row { grid-template-columns: minmax(0, 1fr) minmax(0, 2.6fr) auto; }
.cd-req { color: #d92d20; }
.cd-show-miss .input:invalid, .cd-show-miss .textarea:invalid { border-color: #f04438 !important; background: #fff6f5; }
.cd-miss-chip { border: 1px solid #fda29b; background: #fef3f2; color: #b42318; font-size: 12px; font-weight: 600; border-radius: 99px; padding: 3px 10px; cursor: pointer; }
.cd-ok-chip { background: #ecfdf3; color: #067647; border: 1px solid #abefc6; font-size: 12px; font-weight: 600; border-radius: 99px; padding: 3px 10px; }
.cd-missing { border-color: #f04438 !important; background: #fff6f5; }
.cd-prev-box { overflow: hidden; display: flex; justify-content: center; }
.cd-prev-box iframe { border: 0; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.12); transform-origin: top center; flex: none; }
@ -6055,6 +6058,24 @@ const CD_PREPARER = { name: 'Mike', phone: '15201273157' };
const cdEsc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])).replace(/\n/g, '<br>');
const cdQty2 = (it) => (String(it.qty2 ?? '').trim() || String(it.qty ?? '').trim()); // 发票数量没单独填就等于成交数量
const cdFileBase = (d) => `预报关单_${String(d.contractNo || d.date || '').replace(/[\\/:*?"<>|\s]+/g, '_')}`;
// 必填检查:除了"标记唛码及备注"都要填(发票数量不填按成交数量,最终目的国不填按贸易国)
function cdMissing(d, defs, customers) {
const m = []; const e = (v) => !String(v ?? '').trim();
const head = [['境内发货人', d.shipper], ['境内发货人的统一社会信用代码', d.shipperCode], ['境外收货人(关联客户)', d.customerId && customers.some((c) => c.id === d.customerId) ? d.customerId : ''],
['合同协议号', d.contractNo], ['贸易国(地区)', d.tradeCountry], ['成交方式', d.terms], ['件数', d.packages], ['毛重', d.grossWeight], ['净重', d.netWeight], ['日期', d.date]];
if (!d.producerSame) head.push(['生产销售单位', d.producer], ['生产销售单位的统一社会信用代码', d.producerCode]);
for (const [k, v] of head) if (e(v)) m.push(k);
const items = (d.items || []).filter((it) => it.name || it.hsCode || it.qty || it.spec || it.unitPrice);
if (!items.length) m.push('商品明细(至少一项)');
(d.items || []).forEach((it, i) => {
if (!items.includes(it)) return;
const p = `第 ${i + 1} 项:`;
for (const [k, v] of [['商品编号(HS)', it.hsCode], ['商品名称', it.name], ['规格型号(申报要素)', it.spec], ['成交数量', it.qty], ['成交单位', it.unit], ['小计净重', it.kg], ['发票单位', it.unit2], ['单价', it.unitPrice], ['币制', it.currency], ['原产国', it.origin], ['最终目的国', it.dest || d.tradeCountry], ['境内货源地', it.source]]) if (e(v)) m.push(p + k);
const def = cdElemDef(defs, it.hsCode);
if (def && it.spec) { const vals = cdSplit(it.spec); def.elements.forEach((el, k) => { if (e(vals[k])) m.push(`${p}申报要素"${el.name}"`); }); }
});
return m;
}
// 整理成一份结构化数据,HTML 和 Excel 都从这里取
function cdDocData(d, customers, defs) {
const cust = customers.find((c) => c.id === d.customerId);
@ -6085,33 +6106,38 @@ function cdDocData(d, customers, defs) {
return { head, items, totals, made, notes: d.notes };
}
const CD_DOC_CSS = `
.cdx{font-family:"Microsoft YaHei","PingFang SC","Noto Sans SC",sans-serif;font-size:11px;color:#111;background:#fff;width:760px;margin:0 auto;padding:18px 16px}
.cdx{font-family:"Microsoft YaHei","PingFang SC","Noto Sans SC",sans-serif;font-size:11px;color:#1d2939;background:#fff;width:760px;margin:0 auto;padding:18px 16px}
.cdx *{box-sizing:border-box}
.cdx h1{text-align:center;font-size:20px;margin:0 0 8px;letter-spacing:4px}
.cdx .made{background:#fff3b0;border:1.5px solid #f0b400;color:#7a4b00;font-weight:700;font-size:12.5px;text-align:center;padding:7px 10px;border-radius:4px;margin-bottom:10px}
.cdx table{width:100%;border-collapse:collapse}.cdx td,.cdx th{border:1px solid #444;padding:3px 5px;vertical-align:top}
.cdx .l{font-size:9.5px;color:#444}.cdx .l span{color:#888}.cdx .v{font-size:12px;font-weight:600;min-height:15px;word-break:break-all}
.cdx th{background:#eee;font-size:10.5px}.cdx .c{text-align:center}.cdx .r{text-align:right;white-space:nowrap}.cdx .m{font-family:Consolas,monospace}
.cdx .spec{font-size:10.5px;color:#333;word-break:break-all}.cdx .tot{margin-top:6px;text-align:right;font-weight:700}
.cdx h2{font-size:13px;margin:16px 0 6px;padding-left:7px;border-left:3px solid #1f4e8c}
.cdx .el{margin-bottom:8px;page-break-inside:avoid}.cdx .el-h{background:#eef3fa;font-weight:700}
.cdx .el td.k{width:42%;color:#333;background:#fafafa}.cdx .el td.e{font-weight:600}
.cdx .note{margin-top:10px;font-size:10px;color:#555}
.cdx h1{text-align:center;font-size:21px;margin:0 0 8px;letter-spacing:6px;color:#1f4e8c}
.cdx h1:after{content:"";display:block;width:60px;height:3px;background:#2e7dd7;margin:6px auto 0;border-radius:2px}
.cdx .made{background:#fff4c2;border:1px solid #f5c542;color:#7a4b00;font-weight:700;font-size:12.5px;text-align:center;padding:7px 10px;border-radius:5px;margin-bottom:10px}
.cdx table{width:100%;border-collapse:collapse}.cdx td,.cdx th{border:0.5px solid #b9c6d8;padding:3px 5px;vertical-align:top;font-size:11px;line-height:1.45}
.cdx .hd td{background:#f5f8fd}
.cdx .l{font-size:9.5px;color:#4a6a96}.cdx .l span{color:#8aa0bd}.cdx .v{font-size:12px;font-weight:600;min-height:15px;word-break:break-all;color:#0f2a4f}
.cdx th{background:#2b5f9e;color:#fff;font-size:10.5px;font-weight:600;border-color:#2b5f9e}
.cdx .it tbody tr:nth-child(even) td{background:#f7f9fc}
.cdx .c{text-align:center}.cdx .r{text-align:right;white-space:nowrap}.cdx .m{font-family:Consolas,monospace;color:#1f4e8c;font-weight:600}
.cdx .spec{font-size:10.5px;color:#475467;word-break:break-all}.cdx .cur{color:#b54708;font-weight:600}
.cdx .tot{margin-top:6px;text-align:right}.cdx .tot span{display:inline-block;background:#e8f1fd;color:#1f4e8c;font-weight:700;padding:3px 10px;border-radius:10px;margin-left:6px}
.cdx h2{font-size:13px;margin:16px 0 6px;padding-left:7px;border-left:3px solid #2e7dd7;color:#1f4e8c}
.cdx .el{margin-bottom:8px;page-break-inside:avoid}.cdx .el-h td{background:#e8f1fd;font-weight:700;color:#1f4e8c}
.cdx .el td.k{width:42%;color:#4a6a96;background:#fafbfd}.cdx .el td.e{font-weight:600;color:#0f2a4f}
.cdx .note{margin-top:10px;font-size:10px;color:#667085}
.cdx tr{page-break-inside:avoid}`;
function cdDocBody(x) {
const cell = ([label, code, val]) => `<td><div class="l">${cdEsc(label)}${code ? ` <span>(${cdEsc(code)})</span>` : ''}</div><div class="v">${cdEsc(val) || '&nbsp;'}</div></td>`;
const rows = x.items.map((r) => `<tr><td class="c">${r.no}</td><td class="m">${cdEsc(r.it.hsCode)}</td>
<td><b>${cdEsc(r.it.name)}</b><div class="spec">${cdEsc(r.it.spec)}</div></td>
<td class="r">${r.qtyLines.map(cdEsc).join('<br>')}</td><td class="r">${r.priceLines.map(cdEsc).join('<br>')}</td>
<td class="r">${r.qtyLines.map(cdEsc).join('<br>')}</td><td class="r">${cdEsc(r.priceLines[0])}<br>${cdEsc(r.priceLines[1])}<br><span class="cur">${cdEsc(r.priceLines[2])}</span></td>
<td class="c">${cdEsc(r.it.origin)}</td><td class="c">${cdEsc(r.dest)}</td><td class="c">${cdEsc(r.it.source)}</td></tr>`).join('');
const els = x.items.filter((r) => r.elems.length).map((r) => `<table class="el"><tr class="el-h"><td colspan="2">项号 ${r.no} ${cdEsc(r.it.hsCode)} ${cdEsc(r.it.name)}</td></tr>
${r.elems.map(([k, v]) => `<tr><td class="k">${cdEsc(k)}</td><td class="e">${cdEsc(v) || '&nbsp;'}</td></tr>`).join('')}</table>`).join('');
return `<div class="cdx"><h1>预报关单</h1><div class="made">${cdEsc(x.made)}</div>
<table>${x.head.map((r) => `<tr>${r.map(cell).join('')}</tr>`).join('')}
<table class="hd">${x.head.map((r) => `<tr>${r.map(cell).join('')}</tr>`).join('')}
${x.notes ? `<tr><td colspan="3"><div class="l">标记唛码及备注</div><div class="v" style="font-weight:400">${cdEsc(x.notes)}</div></td></tr>` : ''}</table>
<table style="margin-top:8px"><thead><tr><th style="width:30px">项号</th><th style="width:78px">商品编号</th><th>商品名称及规格型号</th><th style="width:72px">数量及单位</th><th style="width:94px">单价/总价/币制</th><th style="width:56px">原产国(地区)</th><th style="width:62px">最终目的国(地区)</th><th style="width:62px">境内货源地</th></tr></thead>
<table class="it" style="margin-top:8px"><thead><tr><th style="width:30px">项号</th><th style="width:78px">商品编号</th><th>商品名称及规格型号</th><th style="width:72px">数量及单位</th><th style="width:94px">单价/总价/币制</th><th style="width:56px">原产国(地区)</th><th style="width:62px">最终目的国(地区)</th><th style="width:62px">境内货源地</th></tr></thead>
<tbody>${rows || '<tr><td colspan="8" class="c" style="color:#999">还没有商品</td></tr>'}</tbody></table>
${x.totals ? `<div class="tot">${cdEsc(x.totals)}</div>` : ''}
${x.totals ? `<div class="tot">${x.totals.split('  ').map((t) => `<span>${cdEsc(t)}</span>`).join('')}</div>` : ''}
${els ? `<h2>申报要素明细</h2>${els}` : ''}
<div class="note">本预报关单由客户档案系统生成,供与报关行核对,以海关正式报关单为准。</div></div>`;
}
@ -6122,12 +6148,12 @@ async function downloadCustomsPdf(d, customers, defs) {
const html2pdf = mod.default || mod;
const host = document.createElement('div');
host.style.cssText = 'position:fixed;left:-10000px;top:0;background:#fff';
host.innerHTML = `<style>${CD_DOC_CSS}</style>${cdDocBody(cdDocData(d, customers, defs))}`;
host.innerHTML = `<style>${CD_DOC_CSS} .cdx{width:718px;padding:10px 4px}</style>${cdDocBody(cdDocData(d, customers, defs))}`;
document.body.appendChild(host);
try {
await html2pdf().set({
margin: [8, 8, 8, 8], filename: `${cdFileBase(d)}.pdf`,
image: { type: 'jpeg', quality: 0.95 }, html2canvas: { scale: 2, backgroundColor: '#ffffff' },
image: { type: 'jpeg', quality: 0.95 }, html2canvas: { scale: 2, backgroundColor: '#ffffff', windowWidth: 760, scrollX: 0, scrollY: 0 },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }, pagebreak: { mode: ['css'], avoid: ['tr', '.el'] },
}).from(host.querySelector('.cdx')).save();
} finally { host.remove(); }
@ -6174,14 +6200,14 @@ function downloadCustomsXlsx(d, customers, defs) {
put([]);
put(['项号', '商品编号', '商品名称及规格型号', '数量及单位', '单价/总价/币制', '原产国(地区)', '最终目的国(地区)', '境内货源地'].map((v, i) => [COLS[i], v, 3]), 30);
for (const it of x.items) {
put([['A', it.no, 7], ['B', it.it.hsCode, 7], ['C', `${it.it.name}\n${it.it.spec || ''}`, 6], ['D', it.qtyLines.join('\n'), 8], ['E', it.priceLines.join('\n'), 8], ['F', it.it.origin, 7], ['G', it.dest, 7], ['H', it.it.source, 7]], 48);
put([['A', it.no, 7], ['B', it.it.hsCode, 7], ['C', `${it.it.name}\n${it.it.spec || ''}`, 6], ['D', it.qtyLines.join('\n'), 8], ['E', it.priceLines.join('\n'), 8], ['F', it.it.origin, 7], ['G', it.dest, 7], ['H', it.it.source, 7]], Math.max(48, (2 + Math.ceil(String(it.it.spec || '').length / 26)) * 14));
}
if (x.totals) { put([['A', x.totals, 9]], 20); merge('A', 'H'); }
const withEl = x.items.filter((it) => it.elems.length);
if (withEl.length) {
put([]); put([['A', '申报要素明细', 10]], 22); merge('A', 'H');
for (const it of withEl) {
put([['A', `项号 ${it.no} ${it.it.hsCode} ${it.it.name}`, 3]], 20); merge('A', 'H');
put([['A', `项号 ${it.no} ${it.it.hsCode} ${it.it.name}`, 14]], 20); merge('A', 'H');
for (const [k, v] of it.elems) { put([['A', k, 11], ['D', v, 12]], 18); merge('A', 'C'); merge('D', 'H'); }
}
}
@ -6209,19 +6235,19 @@ function downloadCustomsXlsx(d, customers, defs) {
<sheetPr><pageSetUpPr fitToPage="1"/></sheetPr><cols>${widths.map((w, i) => `<col min="${i + 1}" max="${i + 1}" width="${w}" customWidth="1"/>`).join('')}</cols>
<sheetData>${sheetRows}</sheetData>${merges.length ? `<mergeCells count="${merges.length}">${merges.map((m) => `<mergeCell ref="${m}"/>`).join('')}</mergeCells>` : ''}
<pageMargins left="0.4" right="0.4" top="0.5" bottom="0.5" header="0.3" footer="0.3"/><pageSetup paperSize="9" orientation="portrait" fitToWidth="1" fitToHeight="0"/></worksheet>`;
const B = '<border><left style="thin"><color rgb="FF444444"/></left><right style="thin"><color rgb="FF444444"/></right><top style="thin"><color rgb="FF444444"/></top><bottom style="thin"><color rgb="FF444444"/></bottom><diagonal/></border>';
const B = '<border><left style="thin"><color rgb="FFB9C6D8"/></left><right style="thin"><color rgb="FFB9C6D8"/></right><top style="thin"><color rgb="FFB9C6D8"/></top><bottom style="thin"><color rgb="FFB9C6D8"/></bottom><diagonal/></border>';
const xf = (font, fill, border, h, v, wrap) => `<xf numFmtId="0" fontId="${font}" fillId="${fill}" borderId="${border}" applyFont="1" applyFill="1" applyBorder="1" applyAlignment="1"><alignment${h ? ` horizontal="${h}"` : ''} vertical="${v || 'top'}"${wrap ? ' wrapText="1"' : ''}/></xf>`;
const styles = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<fonts count="6"><font><sz val="10"/><name val="Microsoft YaHei"/></font><font><b/><sz val="16"/><name val="Microsoft YaHei"/></font><font><b/><sz val="11"/><color rgb="FF7A4B00"/><name val="Microsoft YaHei"/></font><font><b/><sz val="10"/><name val="Microsoft YaHei"/></font><font><sz val="9"/><color rgb="FF555555"/><name val="Microsoft YaHei"/></font><font><b/><sz val="11"/><color rgb="FF1F4E8C"/><name val="Microsoft YaHei"/></font></fonts>
<fills count="5"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill><fill><patternFill patternType="solid"><fgColor rgb="FFFFF3B0"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFEEEEEE"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFF7F7F7"/><bgColor indexed="64"/></patternFill></fill></fills>
<borders count="3"><border><left/><right/><top/><bottom/><diagonal/></border>${B}<border><left style="medium"><color rgb="FFF0B400"/></left><right style="medium"><color rgb="FFF0B400"/></right><top style="medium"><color rgb="FFF0B400"/></top><bottom style="medium"><color rgb="FFF0B400"/></bottom><diagonal/></border></borders>
<fonts count="7"><font><sz val="10"/><name val="Microsoft YaHei"/></font><font><b/><sz val="16"/><name val="Microsoft YaHei"/></font><font><b/><sz val="11"/><color rgb="FF7A4B00"/><name val="Microsoft YaHei"/></font><font><b/><sz val="10"/><name val="Microsoft YaHei"/></font><font><sz val="9"/><color rgb="FF555555"/><name val="Microsoft YaHei"/></font><font><b/><sz val="11"/><color rgb="FF1F4E8C"/><name val="Microsoft YaHei"/></font><font><b/><sz val="10"/><color rgb="FFFFFFFF"/><name val="Microsoft YaHei"/></font></fonts>
<fills count="8"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill><fill><patternFill patternType="solid"><fgColor rgb="FFFFF3B0"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFEEEEEE"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFF7F7F7"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FF2B5F9E"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFF5F8FD"/><bgColor indexed="64"/></patternFill></fill><fill><patternFill patternType="solid"><fgColor rgb="FFE8F1FD"/><bgColor indexed="64"/></patternFill></fill></fills>
<borders count="3"><border><left/><right/><top/><bottom/><diagonal/></border>${B}<border><left style="thin"><color rgb="FFF5C542"/></left><right style="thin"><color rgb="FFF5C542"/></right><top style="thin"><color rgb="FFF5C542"/></top><bottom style="thin"><color rgb="FFF5C542"/></bottom><diagonal/></border></borders>
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>
<cellXfs count="14">${[
xf(0, 0, 0), xf(1, 0, 0, 'center', 'center'), xf(2, 2, 2, 'center', 'center', 1), xf(3, 3, 1, 'center', 'center', 1),
xf(4, 4, 1, 'left', 'top', 1), xf(3, 0, 1, 'left', 'top', 1), xf(0, 0, 1, 'left', 'top', 1), xf(0, 0, 1, 'center', 'top', 1),
xf(0, 0, 1, 'right', 'top', 1), xf(3, 0, 0, 'right', 'center'), xf(5, 0, 0, 'left', 'center'), xf(0, 4, 1, 'left', 'center', 1),
xf(3, 0, 1, 'left', 'center', 1), xf(4, 0, 0, 'left', 'top', 1),
<cellXfs count="15">${[
xf(0, 0, 0), xf(5, 0, 0, 'center', 'center'), xf(2, 2, 2, 'center', 'center', 1), xf(6, 5, 1, 'center', 'center', 1),
xf(4, 6, 1, 'left', 'top', 1), xf(3, 0, 1, 'left', 'top', 1), xf(0, 0, 1, 'left', 'top', 1), xf(0, 0, 1, 'center', 'top', 1),
xf(0, 0, 1, 'right', 'top', 1), xf(3, 0, 0, 'right', 'center'), xf(5, 0, 0, 'left', 'center'), xf(4, 6, 1, 'left', 'center', 1),
xf(3, 0, 1, 'left', 'center', 1), xf(4, 0, 0, 'left', 'top', 1), xf(5, 7, 1, 'left', 'center'),
].join('')}</cellXfs>
<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles></styleSheet>`;
const blob = cdZip([
@ -6311,8 +6337,8 @@ function CustomsPage({ decls, setDecls, shipperList, setShipperList, hsList, set
<div className="row-title">{d.contractNo || '(无合同协议号)'} · {d.shipper || '(未填境内发货人)'}{d.tradeCountry ? ` → ${d.tradeCountry}` : ''}</div>
<div className="row-sub">{d.date}{custName(d.customerId) ? ` · ${custName(d.customerId)}` : ''} · {n} 项商品{d.packages ? ` · ${d.packages} 件` : ''}{d.grossWeight ? ` · 毛重 ${d.grossWeight} kg` : ''}{Object.entries(s.by).map(([c, v]) => ` · ${cdFmt(v)} ${cdCurLabel(c)}`).join('')}</div>
</div>
<button className="btn btn-sm" onClick={(e) => { e.stopPropagation(); downloadCustomsPdf(d, customers, hsDefs).catch(() => notify('生成 PDF 失败,请重试')); }}><Download size={12} />PDF</button>
<button className="btn btn-sm" onClick={(e) => { e.stopPropagation(); downloadCustomsXlsx(d, customers, hsDefs); }}><Download size={12} />Excel</button>
<button className="btn btn-sm" onClick={(e) => { e.stopPropagation(); if (cdMissing(d, hsDefs, customers).length) { notify('还有必填项没填,请打开这份报关单补全后再下载'); setSelId(d.id); return; } downloadCustomsPdf(d, customers, hsDefs).catch(() => notify('生成 PDF 失败,请重试')); }}><Download size={12} />PDF</button>
<button className="btn btn-sm" onClick={(e) => { e.stopPropagation(); if (cdMissing(d, hsDefs, customers).length) { notify('还有必填项没填,请打开这份报关单补全后再下载'); setSelId(d.id); return; } downloadCustomsXlsx(d, customers, hsDefs); }}><Download size={12} />Excel</button>
<button className="btn btn-sm" onClick={(e) => { e.stopPropagation(); duplicate(d); }}><Copy size={12} />复制</button>
<button className="btn btn-sm btn-ghost btn-danger" onClick={(e) => { e.stopPropagation(); remove(d); }}><Trash2 size={13} /></button>
</div>
@ -6333,11 +6359,14 @@ function CustomsEditor({ d, update, onBack, onDelete, onDuplicate, customers, or
const moveItem = (it, dir) => update((x) => { const items = [...x.items]; const i = items.findIndex((y) => y.id === it.id), j = i + dir; if (j < 0 || j >= items.length) return {}; [items[i], items[j]] = [items[j], items[i]]; return { items }; });
const s = cdSums(d);
const [pdfBusy, setPdfBusy] = useState(false);
// 发票单位必填:缺的项提醒一下(数量没单独填的按成交数量算)
// 除了"标记唛码及备注",其余都必填:缺了就不能下载,并把空着的格子标红
const [showMiss, setShowMiss] = useState(false);
const missing = cdMissing(d, hsDefs, customers);
const checkReady = () => {
const miss = d.items.map((it, i) => ((it.name || it.hsCode) && (!String(it.unit2 || '').trim() || !cdQty2(it)) ? i + 1 : 0)).filter(Boolean);
if (miss.length) return window.confirm(`第 ${miss.join('、')} 项商品的"发票单位"还没填,仍然下载吗?`);
return true;
if (!missing.length) return true;
setShowMiss(true);
alert(`还有 ${missing.length} 项必填没有填,不能下载:\n\n${missing.slice(0, 15).join('\n')}${missing.length > 15 ? `\n……等 ${missing.length} 项` : ''}\n\n空着的格子已经标红。`);
return false;
};
const custOrders = orders.filter((o) => !d.customerId || o.customerId === d.customerId);
function pickCustomer(id) {
@ -6356,10 +6385,12 @@ function CustomsEditor({ d, update, onBack, onDelete, onDuplicate, customers, or
const pr = cdProducer(d);
return (
<>
<div className={showMiss ? 'cd-show-miss' : ''}>
<div style={{ display: 'flex', gap: 8, marginBottom: 14, flexWrap: 'wrap', alignItems: 'center' }}>
<button className="btn btn-sm" onClick={onBack}><ChevronRight size={13} style={{ transform: 'rotate(180deg)' }} />返回列表</button>
<span style={{ fontWeight: 700 }}>{d.contractNo || '新报关'}</span>
<span style={{ fontSize: 12, color: 'var(--ink-soft)' }}>改动自动保存</span>
{missing.length > 0 ? <button className="cd-miss-chip" onClick={() => setShowMiss((v) => !v)} title={missing.join('\n')}>还缺 {missing.length} 项必填{showMiss ? '(已标红)' : ',点击标出'}</button> : <span className="cd-ok-chip">必填项已填完</span>}
<div style={{ marginLeft: 'auto', display: 'flex', gap: 8 }}>
<button className="btn btn-sm btn-primary" disabled={pdfBusy} onClick={async () => { if (!checkReady()) return; setPdfBusy(true); try { await downloadCustomsPdf(d, customers, hsDefs); } catch (e) { notify('生成 PDF 失败,请重试'); } finally { setPdfBusy(false); } }}><Download size={13} />{pdfBusy ? '生成中…' : '下载报关单(PDF)'}</button>
<button className="btn btn-sm" onClick={() => { if (checkReady()) downloadCustomsXlsx(d, customers, hsDefs); }}><Download size={13} />下载 Excel</button>
@ -6375,32 +6406,32 @@ function CustomsEditor({ d, update, onBack, onDelete, onDuplicate, customers, or
<select className="cd-pick" value="" onChange={(e) => { const x = shipperList.find((y) => y.id === e.target.value); if (x) set({ shipper: x.name, shipperCode: x.code }); }}>
<option value="">从"境内发货人"里选…</option>{shipperList.map((x) => <option key={x.id} value={x.id}>{x.name}</option>)}
</select></span>
<input className="input" list="cd-shippers" value={d.shipper} onChange={(e) => { const v = e.target.value; const x = shipperList.find((y) => y.name === v); set({ shipper: v, ...(x ? { shipperCode: x.code } : {}) }); }} placeholder="选择或手动填写公司全称" />
<input className="input" required list="cd-shippers" value={d.shipper} onChange={(e) => { const v = e.target.value; const x = shipperList.find((y) => y.name === v); set({ shipper: v, ...(x ? { shipperCode: x.code } : {}) }); }} placeholder="选择或手动填写公司全称" />
<datalist id="cd-shippers">{[...new Set([...shipperList.map((x) => x.name), ...shippers])].map((x) => <option key={x} value={x} />)}</datalist>
</div>
<label className="field"><span className="field-label">统一社会信用代码(可选)</span><input className="input crm-mono" value={d.shipperCode} onChange={(e) => set({ shipperCode: e.target.value.toUpperCase() })} placeholder="18 位" /></label>
<label className="field"><span className="field-label">统一社会信用代码</span><input className="input crm-mono" required value={d.shipperCode} onChange={(e) => set({ shipperCode: e.target.value.toUpperCase() })} placeholder="18 位" /></label>
<div className="field cd-span3">
<span className="field-label">生产销售单位
<label className="cd-check"><input type="checkbox" checked={d.producerSame} onChange={(e) => set({ producerSame: e.target.checked, ...(e.target.checked ? {} : { producer: d.producer || d.shipper, producerCode: d.producerCode || d.shipperCode }) })} />与境内发货人相同(自动同步)</label>
</span>
{d.producerSame
? <div className="cd-sync">{pr.name || <i>填写境内发货人后自动同步</i>}{pr.code ? <span className="crm-mono"> {pr.code}</span> : null}</div>
: <div className="cd-row2"><input className="input" value={d.producer} onChange={(e) => set({ producer: e.target.value })} placeholder="生产销售单位全称" /><input className="input crm-mono" value={d.producerCode} onChange={(e) => set({ producerCode: e.target.value.toUpperCase() })} placeholder="统一社会信用代码(可选)" /></div>}
: <div className="cd-row2"><input className="input" required value={d.producer} onChange={(e) => set({ producer: e.target.value })} placeholder="生产销售单位全称" /><input className="input crm-mono" required value={d.producerCode} onChange={(e) => set({ producerCode: e.target.value.toUpperCase() })} placeholder="统一社会信用代码" /></div>}
</div>
<label className="field"><span className="field-label">合同协议号</span><input className="input crm-mono" value={d.contractNo} onChange={(e) => set({ contractNo: e.target.value })} /></label>
<label className="field"><span className="field-label">合同协议号</span><input className="input crm-mono" required value={d.contractNo} onChange={(e) => set({ contractNo: e.target.value })} /></label>
<label className="field"><span className="field-label">贸易国(地区)</span>
<input className="input" list="cd-countries" value={d.tradeCountry} onChange={(e) => set({ tradeCountry: e.target.value })} placeholder="如:白俄罗斯" />
<input className="input" required list="cd-countries" value={d.tradeCountry} onChange={(e) => set({ tradeCountry: e.target.value })} placeholder="如:白俄罗斯" />
<datalist id="cd-countries">{CD_COUNTRIES.map((x) => <option key={x} value={x} />)}</datalist>
</label>
<label className="field"><span className="field-label">成交方式</span>
<select className="input" value={d.terms} onChange={(e) => set({ terms: e.target.value })}>{CD_TERMS.map(([t, c]) => <option key={t} value={t}>{t}({c})</option>)}</select>
<select className="input" required value={d.terms} onChange={(e) => set({ terms: e.target.value })}>{CD_TERMS.map(([t, c]) => <option key={t} value={t}>{t}({c})</option>)}</select>
</label>
<label className="field"><span className="field-label">件数</span><input className="input" inputMode="numeric" value={d.packages} onChange={(e) => set({ packages: e.target.value })} /></label>
<label className="field"><span className="field-label">毛重(千克)</span><input className="input" inputMode="decimal" value={d.grossWeight} onChange={(e) => set({ grossWeight: e.target.value })} /></label>
<label className="field"><span className="field-label">净重(千克){s.kg > 0 && <small className="cd-hint">商品法定数量合计 {cdFmt(s.kg, 1)}{d.netWeight && Math.abs(cdNum(d.netWeight) - s.kg) > 0.05 ? ',和净重不一致' : ''}</small>}</span><input className="input" inputMode="decimal" value={d.netWeight} onChange={(e) => set({ netWeight: e.target.value })} /></label>
<label className="field"><span className="field-label">日期</span><input className="input" type="date" value={d.date} onChange={(e) => set({ date: e.target.value })} /></label>
<label className="field"><span className="field-label">关联客户(可选)</span>
<select className="input" value={d.customerId} onChange={(e) => pickCustomer(e.target.value)}><option value="">—</option>{customers.map((c) => <option key={c.id} value={c.id}>{c.company || c.contact}</option>)}</select>
<label className="field"><span className="field-label">件数</span><input className="input" inputMode="numeric" required value={d.packages} onChange={(e) => set({ packages: e.target.value })} /></label>
<label className="field"><span className="field-label">毛重(千克)</span><input className="input" inputMode="decimal" required value={d.grossWeight} onChange={(e) => set({ grossWeight: e.target.value })} /></label>
<label className="field"><span className="field-label">净重(千克){s.kg > 0 && <small className="cd-hint">商品法定数量合计 {cdFmt(s.kg, 1)}{d.netWeight && Math.abs(cdNum(d.netWeight) - s.kg) > 0.05 ? ',和净重不一致' : ''}</small>}</span><input className="input" inputMode="decimal" required value={d.netWeight} onChange={(e) => set({ netWeight: e.target.value })} /></label>
<label className="field"><span className="field-label">日期</span><input className="input" required type="date" value={d.date} onChange={(e) => set({ date: e.target.value })} /></label>
<label className="field"><span className="field-label">境外收货人(关联客户)</span>
<select className="input" required value={d.customerId} onChange={(e) => pickCustomer(e.target.value)}><option value="">请选择客户…</option>{customers.map((c) => <option key={c.id} value={c.id}>{c.company || c.contact}</option>)}</select>
</label>
<label className="field"><span className="field-label">从订单带入产品(可选)</span>
<select className="input" value={d.orderId || ''} onChange={(e) => fromOrder(e.target.value)}><option value="">—</option>{custOrders.map((o) => <option key={o.id} value={o.id}>{o.number || '(无编号)'} · {o.date}</option>)}</select>
@ -6425,23 +6456,23 @@ function CustomsEditor({ d, update, onBack, onDelete, onDuplicate, customers, or
</div>
<div className="cd-item-body">
<HsPicker it={it} hsList={hsList} setHsList={setHsList} defs={hsDefs} setDefs={setHsDefs} setItem={setItem} notify={notify} />
<div className="field"><span className="field-label">成交数量 / 单位</span><div className="cd-row2"><input className="input" inputMode="decimal" value={it.qty} onChange={(e) => setItem(it.id, { qty: e.target.value })} /><UnitInput value={it.unit} onChange={(v) => setItem(it.id, { unit: v })} /></div></div>
<label className="field"><span className="field-label">小计净重(千克)</span><input className="input" inputMode="decimal" value={it.kg} onChange={(e) => setItem(it.id, { kg: e.target.value })} /></label>
<div className="field"><span className="field-label">成交数量 / 单位</span><div className="cd-row2"><input className="input" required inputMode="decimal" value={it.qty} onChange={(e) => setItem(it.id, { qty: e.target.value })} /><UnitInput value={it.unit} onChange={(v) => setItem(it.id, { unit: v })} /></div></div>
<label className="field"><span className="field-label">小计净重(千克)</span><input className="input" required inputMode="decimal" value={it.kg} onChange={(e) => setItem(it.id, { kg: e.target.value })} /></label>
<div className="field"><span className="field-label">发票单位 <b className="cd-req">*</b><small className="cd-soft"> 数量不填就按成交数量</small></span>
<div className="cd-row2"><input className="input" inputMode="decimal" value={it.qty2} onChange={(e) => setItem(it.id, { qty2: e.target.value })} placeholder={it.qty || '数量'} />
<select className={`input${String(it.unit2 || '').trim() ? '' : ' cd-missing'}`} style={{ maxWidth: 92 }} value={['台', '个'].includes(it.unit2) ? it.unit2 : it.unit2 ? '_other' : ''} onChange={(e) => setItem(it.id, { unit2: e.target.value === '_other' ? (it.unit2 && !['台', '个'].includes(it.unit2) ? it.unit2 : '套') : e.target.value })}>
<select required className={`input${String(it.unit2 || '').trim() ? '' : ' cd-missing'}`} style={{ maxWidth: 92 }} value={['台', '个'].includes(it.unit2) ? it.unit2 : it.unit2 ? '_other' : ''} onChange={(e) => setItem(it.id, { unit2: e.target.value === '_other' ? (it.unit2 && !['台', '个'].includes(it.unit2) ? it.unit2 : '套') : e.target.value })}>
<option value="">请选择</option><option value="台">台</option><option value="个">个</option><option value="_other">其他…</option>
</select>
{it.unit2 && !['台', '个'].includes(it.unit2) && <input className="input" style={{ maxWidth: 70 }} value={it.unit2} onChange={(e) => setItem(it.id, { unit2: e.target.value })} />}
</div></div>
<label className="field"><span className="field-label">单价</span><input className="input" inputMode="decimal" value={it.unitPrice} onChange={(e) => setItem(it.id, { unitPrice: e.target.value })} /></label>
<label className="field"><span className="field-label">单价</span><input className="input" required inputMode="decimal" value={it.unitPrice} onChange={(e) => setItem(it.id, { unitPrice: e.target.value })} /></label>
<label className="field"><span className="field-label">币制</span>
<select className="input" value={it.currency} onChange={(e) => setItem(it.id, { currency: e.target.value })}>{CD_CURRENCIES.map((c) => <option key={c.id} value={c.id}>{c.label}({c.id})</option>)}</select>
</label>
<div className="field"><span className="field-label">总价(自动)</span><div className="cd-total">{it.qty && it.unitPrice ? `${cdFmt(cdTotal(it))} ${cdCurLabel(it.currency)}` : '—'}</div></div>
<label className="field"><span className="field-label">原产国(地区)</span><input className="input" value={it.origin} onChange={(e) => setItem(it.id, { origin: e.target.value })} /></label>
<label className="field"><span className="field-label">原产国(地区)</span><input className="input" required value={it.origin} onChange={(e) => setItem(it.id, { origin: e.target.value })} /></label>
<label className="field"><span className="field-label">最终目的国(地区)</span><input className="input" list="cd-countries" value={it.dest} onChange={(e) => setItem(it.id, { dest: e.target.value })} placeholder={d.tradeCountry || '默认同贸易国'} /></label>
<label className="field"><span className="field-label">境内货源地</span><input className="input" value={it.source} onChange={(e) => setItem(it.id, { source: e.target.value })} placeholder="如:深圳特区" /></label>
<label className="field"><span className="field-label">境内货源地</span><input className="input" required value={it.source} onChange={(e) => setItem(it.id, { source: e.target.value })} placeholder="如:深圳特区" /></label>
</div>
</div>
))}
@ -6453,6 +6484,7 @@ function CustomsEditor({ d, update, onBack, onDelete, onDuplicate, customers, or
<div className="panel-header"><div className="panel-title">预览(和下载的报关单一样,随编辑实时更新)</div></div>
<div className="panel-body" style={{ padding: 14, background: '#e9edf2' }}><CustomsPreview d={d} customers={customers} defs={hsDefs} /></div>
</div>
</div>
</>
);
}
@ -6521,11 +6553,11 @@ function HsPicker({ it, hsList, setHsList, defs, setDefs, setItem, notify }) {
{canSave && <button type="button" className="btn btn-sm" onClick={save}><Save size={12} />{entry ? '更新编码库的申报要素' : '存入编码库'}</button>}
</div>
<label className="field"><span className="field-label">商品编号(HS)</span>
<input className="input crm-mono" list="cd-hs-codes" value={it.hsCode} onChange={(e) => onHs(e.target.value)} placeholder="8507600099" />
<input className="input crm-mono" required list="cd-hs-codes" value={it.hsCode} onChange={(e) => onHs(e.target.value)} placeholder="8507600099" />
<datalist id="cd-hs-codes">{[...new Set([...hsList.map((x) => x.hsCode), ...defs.map((x) => x.hsCode)])].map((c) => <option key={c} value={c}>{hsList.filter((x) => x.hsCode === c).map((x) => x.name).join(' / ')}</option>)}</datalist>
</label>
<label className="field cd-span2"><span className="field-label">商品名称{sameHs.length > 1 && <small className="cd-soft"> 这个编码有 {sameHs.length} 个品名:{sameHs.map((x) => x.name).join('、')}</small>}</span>
<input className="input" list={`cd-hs-names-${it.id}`} value={it.name} onChange={(e) => onName(e.target.value)} placeholder="如:锂电池" />
<input className="input" required list={`cd-hs-names-${it.id}`} value={it.name} onChange={(e) => onName(e.target.value)} placeholder="如:锂电池" />
<datalist id={`cd-hs-names-${it.id}`}>{(sameHs.length ? sameHs : hsList).map((x) => <option key={x.id} value={x.name}>{x.hsCode}</option>)}</datalist>
</label>
@ -6541,13 +6573,13 @@ function HsPicker({ it, hsList, setHsList, defs, setDefs, setItem, notify }) {
<div key={i} className={`cd-elem${v ? '' : ' empty'}`}>
<span className="cd-elem-n"><i>{i + 1}</i>{el.name}</span>
{bi ? (
<select className="input" value={v} onChange={(e) => setPart(i, e.target.value)}>
<select className="input" required value={v} onChange={(e) => setPart(i, e.target.value)}>
<option value="">请选择…</option>{bi.map(([c, l]) => <option key={c} value={c}>{c} - {l}</option>)}
{v && !bi.some(([c]) => c === v) && <option value={v}>{v}</option>}
</select>
) : (
<>
<input className="input" list={`cd-el-${def.id}-${i}`} value={v} onChange={(e) => setPart(i, e.target.value)} onBlur={(e) => learn(i, e.target.value)} placeholder={(el.opts || []).length ? '选择或填写' : '填写'} />
<input className="input" required list={`cd-el-${def.id}-${i}`} value={v} onChange={(e) => setPart(i, e.target.value)} onBlur={(e) => learn(i, e.target.value)} placeholder={(el.opts || []).length ? '选择或填写' : '填写'} />
<datalist id={`cd-el-${def.id}-${i}`}>{(el.opts || []).map((o) => <option key={o} value={o} />)}</datalist>
</>
)}
@ -6561,7 +6593,7 @@ function HsPicker({ it, hsList, setHsList, defs, setDefs, setItem, notify }) {
))}
<label className="field cd-span3"><span className="field-label">规格型号(申报要素){def ? <small className="cd-soft"> 由上面各项自动拼成,也可以直接改这里</small> : null}{entry && (entry.spec || '') !== (it.spec || '') && <small className="cd-hint"> 和编码库里的不一样(只改这一单没关系;要以后都用,点上面"更新编码库")</small>}</span>
<textarea className="textarea crm-mono" rows={2} value={it.spec} onChange={(e) => setItem(it.id, { spec: e.target.value })} placeholder="1|0|稳定输出电压用|直流稳压电源|2kW|+/-10%|义达牌|无型号" />
<textarea className="textarea crm-mono" required rows={2} value={it.spec} onChange={(e) => setItem(it.id, { spec: e.target.value })} placeholder="1|0|稳定输出电压用|直流稳压电源|2kW|+/-10%|义达牌|无型号" />
</label>
</>
);